Given an array, the task is to find sum of maximum sum alternating subsequence starting with first element. Here alternating sequence means first decreasing, then increasing, then decreasing, … For example 10, 5, 14, 3 is an alternating sequence. 
Note that the reverse type of sequence (increasing – decreasing – increasing -…) is not considered alternating here.
Examples: 
 
Input :  arr[] = {4, 3, 8, 5, 3, 8}  
Output :  28
Explanation:
The alternating subsequence (starting with first element) 
that has above maximum sum is {4, 3, 8, 5, 8}
Input : arr[] = {4, 8, 2, 5, 6, 8} 
Output :  14
The alternating subsequence (starting with first element) 
that has above maximum sum is {4, 2, 8}
This problem is similar to Longest Increasing Subsequence (LIS) problem. and can be solved using Dynamic Programming.
 
Create two empty array that store result of maximum
sum  of alternate sub-sequence
inc[] : inc[i] stores results of maximum sum alternating
        subsequence ending with arr[i] such that arr[i]
        is greater than previous element of the subsequence 
dec[] : dec[i] stores results of maximum sum alternating
        subsequence ending with arr[i] such that arr[i]
        is less than previous element of the subsequence 
Include first element of 'arr' in both inc[] and dec[] 
inc[0] = dec[0] = arr[0]
// Maintain a flag i.e. it will makes the greater
// elements count only if the first decreasing element
// is counted.
flag  = 0 
Traversal two loops
  i goes from 1 to  n-1 
    j goes 0 to i-1
      IF arr[j] > arr[i]
        dec[i] = max(dec[i], inc[j] + arr[i])
    
        // Denotes first decreasing is found
        flag = 1 
  
      ELSE IF arr[j] < arr[i] && flag == 1 
        inc[i] = max(inc[i], dec[j]+arr[i]);
     
Final Last Find maximum value inc[] and dec[] .
Below is implementation of above idea.
 Note:- For the case where the first element of the array is the smallest element in the array. The output will be the first element only. This is an edge case that need to be checked. Taking a variable and initializing it with the first value of the array and then comparing it with other values will find the min. Check if the min is equal to arr[0]. If it is true then arr[0] is to be returned, because there is no decreasing step available to find an alternating subsequence.
C++
| // C++ program to find sum of maximum // sum alternating sequence starting with // first element. #include <bits/stdc++.h> usingnamespacestd;  // Return sum of maximum sum alternating // sequence starting with arr[0] and is first // decreasing. intmaxAlternateSum(intarr[], intn) {     if(n == 1)         returnarr[0];     // handling the edge case     intmin = arr[0];     for(inti = 1; i < n; i++) {         if(min > arr[i])             min = arr[i];     }     if(min == arr[0]) {         returnarr[0];     }     // create two empty array that store result of     // maximum sum of alternate sub-sequence      // stores sum of decreasing and increasing     // sub-sequence     intdec[n];     memset(dec, 0, sizeof(dec));      // store sum of increasing and decreasing sub-sequence     intinc[n];     memset(inc, 0, sizeof(inc));      // As per question, first element must be part     // of solution.     dec[0] = inc[0] = arr[0];      intflag = 0;      // Traverse remaining elements of array     for(inti = 1; i < n; i++) {         for(intj = 0; j < i; j++) {             // IF current sub-sequence is decreasing the             // update dec[j] if needed. dec[i] by current             // inc[j] + arr[i]             if(arr[j] > arr[i]) {                 dec[i] = max(dec[i], inc[j] + arr[i]);                  // Revert the flag , if first decreasing                 // is found                 flag = 1;             }              // If next element is greater but flag should be             // 1 i.e. this element should be counted after             // the first decreasing element gets counted             elseif(arr[j] < arr[i] && flag == 1)                  // If current sub-sequence is increasing                 // then update inc[i]                 inc[i] = max(inc[i], dec[j] + arr[i]);         }     }      // find maximum sum in b/w inc[] and dec[]     intresult = INT_MIN;     for(inti = 0; i < n; i++) {         if(result < inc[i])             result = inc[i];         if(result < dec[i])             result = dec[i];     }      // return maximum sum alternate sub-sequence     returnresult; }  // Driver program intmain() {     intarr[] = { 8, 2, 3, 5, 7, 9, 10 };     intn = sizeof(arr) / sizeof(arr[0]);     cout << "Maximum sum = "<< maxAlternateSum(arr, n)          << endl;     return0; } | 
Java
| // Java program to find sum of maximum // sum alternating sequence starting with // first element.  publicclassGFG {     // Return sum of maximum sum alternating     // sequence starting with arr[0] and is first     // decreasing.     staticintmaxAlternateSum(intarr[], intn)     {         if(n == 1)             returnarr[0];         // handling the edge case         intmin = arr[0];         for(inti = 1; i < n; i++) {             if(min > arr[i])                 min = arr[i];         }         if(min == arr[0]) {             returnarr[0];         }         // create two empty array that store result of         // maximum sum of alternate sub-sequence          // stores sum of decreasing and increasing         // sub-sequence         intdec[] = newint[n];          // store sum of increasing and decreasing         // sub-sequence         intinc[] = newint[n];          // As per question, first element must be part         // of solution.         dec[0] = inc[0] = arr[0];          intflag = 0;          // Traverse remaining elements of array         for(inti = 1; i < n; i++) {             for(intj = 0; j < i; j++) {                 // IF current sub-sequence is decreasing the                 // update dec[j] if needed. dec[i] by                 // current inc[j] + arr[i]                 if(arr[j] > arr[i]) {                     dec[i]                         = Math.max(dec[i], inc[j] + arr[i]);                      // Revert the flag , if first decreasing                     // is found                     flag = 1;                 }                  // If next element is greater but flag                 // should be 1 i.e. this element should be                 // counted after the first decreasing                 // element gets counted                 elseif(arr[j] < arr[i] && flag == 1)                      // If current sub-sequence is increasing                     // then update inc[i]                     inc[i]                         = Math.max(inc[i], dec[j] + arr[i]);             }         }          // find maximum sum in b/w inc[] and dec[]         intresult = Integer.MIN_VALUE;         for(inti = 0; i < n; i++) {             if(result < inc[i])                 result = inc[i];             if(result < dec[i])                 result = dec[i];         }          // return maximum sum alternate sub-sequence         returnresult;     }      // Driver Method     publicstaticvoidmain(String[] args)     {         intarr[] = { 8, 2, 3, 5, 7, 9, 10};         System.out.println(             "Maximum sum = "            + maxAlternateSum(arr, arr.length));     } } | 
Python3
| # Python3 program to find sum of maximum # sum alternating sequence starting with # first element.  # Return sum of maximum sum alternating # sequence starting with arr[0] and is # first decreasing.   defmaxAlternateSum(arr, n):      if(n ==1):         returnarr[0]     min=arr[0]     fori inrange(1, n):         if(min> arr[i]):             min=arr[i]     if(arr[0] ==min):         returnarr[0]     # Create two empty array that     # store result of maximum sum     # of alternate sub-sequence      # Stores sum of decreasing and     # increasing sub-sequence     dec =[0fori inrange(n +1)]      # store sum of increasing and     # decreasing sub-sequence     inc =[0fori inrange(n +1)]      # As per question, first element     # must be part of solution.     dec[0] =inc[0] =arr[0]      flag =0     # Traverse remaining elements of array     fori inrange(1, n):          forj inrange(i):              # IF current sub-sequence is decreasing the             # update dec[j] if needed. dec[i] by current             # inc[j] + arr[i]             if(arr[j] > arr[i]):                  dec[i] =max(dec[i], inc[j] +arr[i])                  # Revert the flag, if first                 # decreasing is found                 flag =1             # If next element is greater but flag should be 1             # i.e. this element should be counted after the             # first decreasing element gets counted             elseif(arr[j] < arr[i] andflag ==1):                  # If current sub-sequence is                 # increasing then update inc[i]                 inc[i] =max(inc[i], dec[j] +arr[i])      # Find maximum sum in b/w inc[] and dec[]     result =-2147483648    fori inrange(n):          if(result < inc[i]):             result =inc[i]         if(result < dec[i]):             result =dec[i]      # Return maximum sum     # alternate sub-sequence     returnresult   # Driver program arr =[8, 2, 3, 5, 7, 9, 10] n =len(arr) print("Maximum sum = ",       maxAlternateSum(arr, n))  # This code is contributed by Anant Agarwal.  | 
C#
| // C# program to find sum of maximum // sum alternating sequence starting with // first element. usingSystem; classGFG {      // Return sum of maximum     // sum alternating     // sequence starting with     // arr[0] and is first     // decreasing.     staticintmaxAlternateSum(int[] arr, intn)     {         if(n == 1)             returnarr[0];         // handling the edge case         intmin = arr[0];         for(inti = 1; i < n; i++) {             if(min > arr[i])                 min = arr[i];         }         if(min == arr[0]) {             returnarr[0];         }         // create two empty array that         // store result of maximum sum         // of alternate sub-sequence         // stores sum of decreasing         // and increasing sub-sequence         int[] dec = newint[n];          // store sum of increasing and         // decreasing sub-sequence         int[] inc = newint[n];          // As per question, first         // element must be part         // of solution.         dec[0] = inc[0] = arr[0];          intflag = 0;          // Traverse remaining elements of array         for(inti = 1; i < n; i++) {             for(intj = 0; j < i; j++) {                  // IF current sub-sequence                 // is decreasing the                 // update dec[j] if needed.                 // dec[i] by current                 // inc[j] + arr[i]                 if(arr[j] > arr[i]) {                     dec[i]                         = Math.Max(dec[i], inc[j] + arr[i]);                      // Revert the flag , if                     // first decreasing                     // is found                     flag = 1;                 }                  // If next element is greater                 // but flag should be 1                 // i.e. this element should                 // be counted after the                 // first decreasing element                 // gets counted                 elseif(arr[j] < arr[i] && flag == 1)                      // If current sub-sequence                     // is increasing then update                     // inc[i]                     inc[i]                         = Math.Max(inc[i], dec[j] + arr[i]);             }         }          // find maximum sum in b/w         // inc[] and dec[]         intresult = int.MinValue;         for(inti = 0; i < n; i++) {             if(result < inc[i])                 result = inc[i];             if(result < dec[i])                 result = dec[i];         }          // return maximum sum         // alternate sub-sequence         returnresult;     }      // Driver Method     publicstaticvoidMain()     {         int[] arr = { 8, 2, 3, 5, 7, 9, 10 };         Console.Write("Maximum sum = "                      + maxAlternateSum(arr, arr.Length));     } }  // This code is contributed by Nitin Mittal. | 
PHP
| <?php // PHP program to find sum of maximum  // sum alternating sequence starting  // with first element.   // Return sum of maximum sum alternating  // sequence starting with arr[0] and is  // first decreasing.  functionmaxAlternateSum($arr, $n)  {      if($n== 1)          return$arr[0];      $min= $arr[0];      for($i= 1; $i< $n; $i++)      { $min= max($min,$arr[$i]);}   if($arr[0]==$min)     return$arr[0];     // create two empty array that store result      // of maximum sum of alternate sub-sequence       // stores sum of decreasing and       // increasing sub-sequence      $dec= array_fill(0, $n, 0);      // store sum of increasing and      // decreasing sub-sequence      $inc= array_fill(0, $n, 0);       // As per question, first element      // must be part of solution.      $dec[0] = $inc[0] = $arr[0];       $flag= 0;       // Traverse remaining elements of array      for($i= 1; $i< $n; $i++)      {          for($j= 0; $j< $i; $j++)          {              // IF current sub-sequence is decreasing              // the update dec[j] if needed. dec[i]               // by current inc[j] + arr[i]              if($arr[$j] > $arr[$i])              {                  $dec[$i] = max($dec[$i], $inc[$j] +                                           $arr[$i]);                   // Revert the flag , if first                  // decreasing is found                  $flag= 1;              }               // If next element is greater but flag              // should be 1 i.e. this element should              // be counted after the first decreasing             // element gets counted              elseif($arr[$j] < $arr[$i] && $flag== 1)                   // If current sub-sequence is increasing                  // then update inc[i]                  $inc[$i] = max($inc[$i], $dec[$j] +                                           $arr[$i]);          }      }       // find maximum sum in b/w inc[] and dec[]      $result= -(PHP_INT_MAX - 1);      for($i= 0 ; $i< $n; $i++)      {          if($result< $inc[$i])              $result= $inc[$i];          if($result< $dec[$i])              $result= $dec[$i];      }       // return maximum sum alternate sub-sequence      return$result;  }   // Driver Code  $arr= array(8, 2, 3, 5, 7, 9, 10);  $n= sizeof($arr);  echo"Maximum sum = ",       maxAlternateSum($arr, $n);  // This code is contributed by Ryuga ?>  | 
Javascript
| <script>      // JavaScript program to find sum of maximum     // sum alternating sequence starting with     // first element.          // Return sum of maximum sum alternating     // sequence starting with arr[0] and is first     // decreasing.     functionmaxAlternateSum(arr, n)     {         if(n == 1)             returnarr[0];                      //handling the edge case         int min = arr[0];         for(int i=1; i<n; i++){         if(min>arr[i])           min = arr[i];         }         if(min==arr[0]){          returnarr[0];          }                  // create two empty array that store result of        // maximum sum of alternate sub-sequence                 // stores sum of decreasing and increasing         // sub-sequence         let dec = newArray(n);         dec.fill(0);                            // store sum of increasing and decreasing sub-sequence         let inc = newArray(n);         inc.fill(0);                 // As per question, first element must be part         // of solution.         dec[0] = inc[0] = arr[0];                 let flag = 0 ;                 // Traverse remaining elements of array         for(let i=1; i<n; i++)         {             for(let j=0; j<i; j++)             {                 // IF current sub-sequence is decreasing the                 // update dec[j] if needed. dec[i] by current                 // inc[j] + arr[i]                 if(arr[j] > arr[i])                 {                     dec[i] = Math.max(dec[i], inc[j]+arr[i]);                             // Revert the flag , if first decreasing                     // is found                     flag = 1;                 }                         // If next element is greater but flag should be 1                 // i.e. this element should be counted after the                 // first decreasing element gets counted                 elseif(arr[j] < arr[i] && flag == 1)                             // If current sub-sequence is increasing                     // then update inc[i]                     inc[i] = Math.max(inc[i], dec[j]+arr[i]);             }         }                 // find maximum sum in b/w inc[] and dec[]         let result = Number.MIN_VALUE;         for(let i = 0 ; i < n; i++)         {             if(result < inc[i])                 result = inc[i];             if(result < dec[i])                 result = dec[i];         }                 // return maximum sum alternate sub-sequence         returnresult;     }          let arr = [8, 2, 3, 5, 7, 9, 10];     document.write("Maximum sum = "+                        maxAlternateSum(arr , arr.length));  </script> | 
Maximum sum = 25
Time Complexity : O(n2) 
Auxiliary Space : O(n)
This article is contributed by Sahil Chhabra. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

 
                                    







