Maximum length Subsequence with alternating sign and maximum Sum
Last Updated :
28 Feb, 2022
Given an array arr[] of size n having both positive and negative integer excluding zero. The task is to find the subsequence with an alternating sign having maximum size and maximum sum that is, in a subsequence sign of each adjacent element is opposite for example if the first one is positive then the second one has to be negative followed by another positive integer and so on.
Examples:
Input: arr[] = {2, 3, 7, -6, -4}
Output: 7 -4
Explanation:
Possible subsequences are [2, -6] [2, -4] [3, -6] [3, -4] [7, -6] [7, -4].
Out of these [7, -4] has the maximum sum.
Input: arr[] = {-4, 9, 4, 11, -5, -17, 9, -3, -5, 2}
Output: -4 11 -5 9 -3 2
Approach:
The main idea to solve the above problem is to find the maximum element from the segments of the array which consists of the same sign which means we have to pick the maximum element among continuous positive and continuous negative elements. As we want maximum size we will only take one element from each segment and also to maximize the sum, we need to take the maximum element of each segment.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void findSubsequence( int arr[], int n)
{
int sign[n] = { 0 };
for ( int i = 0; i < n; i++) {
if (arr[i] > 0)
sign[i] = 1;
else
sign[i] = -1;
}
int k = 0;
int result[n] = { 0 };
for ( int i = 0; i < n; i++) {
int cur = arr[i];
int j = i;
while (j < n && sign[i] == sign[j]) {
cur = max(cur, arr[j]);
++j;
}
result[k++] = cur;
i = j - 1;
}
for ( int i = 0; i < k; i++)
cout << result[i] << " " ;
cout << "\n" ;
}
int main()
{
int arr[] = { -4, 9, 4, 11, -5, -17, 9, -3, -5, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
findSubsequence(arr, n);
return 0;
}
|
Java
class GFG{
static void findSubsequence( int arr[], int n)
{
int sign[] = new int [n];
for ( int i = 0 ; i < n; i++) {
if (arr[i] > 0 )
sign[i] = 1 ;
else
sign[i] = - 1 ;
}
int k = 0 ;
int result[] = new int [n];
for ( int i = 0 ; i < n; i++) {
int cur = arr[i];
int j = i;
while (j < n && sign[i] == sign[j]) {
cur = Math.max(cur, arr[j]);
++j;
}
result[k++] = cur;
i = j - 1 ;
}
for ( int i = 0 ; i < k; i++)
System.out.print(result[i]+ " " );
System.out.print( "\n" );
}
public static void main(String[] args)
{
int arr[] = { - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 };
int n = arr.length;
findSubsequence(arr, n);
}
}
|
Python3
def findSubsequence(arr, n):
sign = [ 0 ] * n
for i in range (n):
if (arr[i] > 0 ):
sign[i] = 1
else :
sign[i] = - 1
k = 0
result = [ 0 ] * n
i = 0
while i < n:
cur = arr[i]
j = i
while (j < n and sign[i] = = sign[j]):
cur = max (cur, arr[j])
j + = 1
result[k] = cur
k + = 1
i = j - 1
i + = 1
for i in range (k):
print (result[i],end = " " )
if __name__ = = '__main__' :
arr = [ - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 ]
n = len (arr)
findSubsequence(arr, n)
|
C#
using System;
public class GFG{
static void findSubsequence( int []arr, int n)
{
int []sign = new int [n];
for ( int i = 0; i < n; i++) {
if (arr[i] > 0)
sign[i] = 1;
else
sign[i] = -1;
}
int k = 0;
int []result = new int [n];
for ( int i = 0; i < n; i++) {
int cur = arr[i];
int j = i;
while (j < n && sign[i] == sign[j]) {
cur = Math.Max(cur, arr[j]);
++j;
}
result[k++] = cur;
i = j - 1;
}
for ( int i = 0; i < k; i++)
Console.Write(result[i]+ " " );
Console.Write( "\n" );
}
public static void Main(String[] args)
{
int []arr = { -4, 9, 4, 11, -5, -17, 9, -3, -5, 2 };
int n = arr.Length;
findSubsequence(arr, n);
}
}
|
Javascript
<script>
function findSubsequence(arr, n)
{
let sign = Array.from({length: n}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
if (arr[i] > 0)
sign[i] = 1;
else
sign[i] = -1;
}
let k = 0;
let result = Array.from({length: n}, (_, i) => 0);
for (let i = 0; i < n; i++) {
let cur = arr[i];
let j = i;
while (j < n && sign[i] == sign[j]) {
cur = Math.max(cur, arr[j]);
++j;
}
result[k++] = cur;
i = j - 1;
}
for (let i = 0; i < k; i++)
document.write(result[i]+ " " );
document.write( "<br/>" );
}
let arr = [ -4, 9, 4, 11, -5, -17, 9, -3, -5, 2 ];
let n = arr.length;
findSubsequence(arr, n);
</script>
|
Time Complexity :O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...