<p>You are given an integer array <code>nums</code>. The <strong>absolute sum</strong> of a subarray <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> is <code>abs(nums<sub>l</sub> + nums<sub>l+1</sub> + ... + nums<sub>r-1</sub> + nums<sub>r</sub>)</code>.</p>

<p>Return <em>the <strong>maximum</strong> absolute sum of any <strong>(possibly empty)</strong> subarray of </em><code>nums</code>.</p>

<p>Note that <code>abs(x)</code> is defined as follows:</p>

<ul>
	<li>If <code>x</code> is a negative integer, then <code>abs(x) = -x</code>.</li>
	<li>If <code>x</code> is a non-negative integer, then <code>abs(x) = x</code>.</li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [1,-3,2,3,-4]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,-5,1,-4,3,-2]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.
</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>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int maxAbsoluteSum(vector<int>& nums) {
        int ans=0;
        int n=nums.size();
        vector<int >v;
        int sum=0;int idx=0;
        while(idx<n){
        if(nums[idx]>=0) {
            while(idx<n&&nums[idx]>=0){sum+=nums[idx];idx++;}
            v.push_back(sum);sum=0;
            }
        else 
           { while(idx<n&&nums[idx]<=0){
                sum+=nums[idx];idx++;
            }
            v.push_back(sum);sum=0;}
        }
        for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
        int maxi=0;
        int temp=0;if(v[0]<0) temp++;
        for(int i=temp;i<v.size()-1;i=i+2){
            maxi+=v[i];ans=max(ans,abs(maxi));
            if(maxi>=abs(v[i+1])){
                maxi+=v[i+1];
                ans=max(ans,maxi);
            }
            else {
                maxi=0;
            }
        }
        if(maxi>=0&&v[v.size()-1]>0){
            maxi+=v[v.size()-1];ans=max(ans,maxi);
        }
        temp=0;if(v[0]>=0) temp++;maxi=0;
        for(int i=temp;i<v.size()-1;i=i+2){
            maxi+=v[i];ans=max(ans,abs(maxi));
            if(abs(maxi)>=(v[i+1])){
                maxi+=v[i+1];
                ans=max(ans,abs(maxi));
            }
            else maxi=0;
        }
        if(maxi<=0&&v[v.size()-1]<0){
            maxi+=v[v.size()-1];ans=max(ans,abs(maxi));
        }
        
        return ans;
        

        
    }
};
45900