<p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</p>

<ul>
	<li>For example, the alternating sum of <code>[4,2,5,3]</code> is <code>(4 + 5) - (2 + 3) = 4</code>.</li>
</ul>

<p>Given an array <code>nums</code>, return <em>the <strong>maximum alternating sum</strong> of any subsequence of </em><code>nums</code><em> (after <strong>reindexing</strong> the elements of the subsequence)</em>.</p>

<ul>
</ul>

<p>A <strong>subsequence</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements&#39; relative order. For example, <code>[2,7,4]</code> is a subsequence of <code>[4,<u>2</u>,3,<u>7</u>,2,1,<u>4</u>]</code> (the underlined elements), while <code>[2,4,2]</code> is not.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [<u>4</u>,<u>2</u>,<u>5</u>,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,6,7,<u>8</u>]
<strong>Output:</strong> 8
<strong>Explanation:</strong> It is optimal to choose the subsequence [8] with alternating sum 8.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [<u>6</u>,2,<u>1</u>,2,4,<u>5</u>]
<strong>Output:</strong> 10
<strong>Explanation:</strong> It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>
class Solution:
    def maxAlternatingSum(self, nums: List[int]) -> int:

        #lets try recursion
        # you might get consfused with these approach as you have to think about subsequence
        # so same index will be even or odd you have to understant don't use i as from the
        # original array
        def recursion(i, flag):
            # flag = True, even
            # flag = False, odd

            if i == len(nums):
                return 0
                

            #skip
            # flag remain the same
            t2 = recursion(i+1,  flag)

            # take 
            if flag:
                # take
                flag = False
                t1 = nums[i] + recursion(i+1, flag)
            else:
                # odd
                # take
                flag = True
                t1 = -1*nums[i] + recursion(i+1, flag)
            return max(t1, t2)

        def memoization(i, flag, memo):
            # flag = True, even 1
            # flag = False, odd 0


            if i == len(nums):
                return 0
            # print(memo[i][flag])    
            if memo[i][flag]!=-1:
               return memo[i][flag] 
                

            #skip
            # flag remain the same
            t2 = memoization(i+1,  flag, memo)

            # take 
            if flag:
                # take
                # flag = False
                t1 = nums[i] + memoization(i+1, False, memo)
            else:
                # odd
                # take
                # flag = True : not to update the value of flag like this cause you will
                # then miss the flag for which the current function call was made
                t1 = -1*nums[i] + memoization(i+1, True, memo)

            memo[i][flag] = max(t1, t2)
            # print(i, flag, memo[i][flag])

            return memo[i][flag]

        # return recursion(0, True)
        memo = [[-1]*2 for i in range(len(nums))]
        return memoization(0, True, memo)

            

            

1399