Find a subsequence which upon reversing gives the maximum sum subarray
Last Updated :
20 Mar, 2023
Given an array arr of integers of size N, the task is to find a subsequence in which upon reversing the order, the maximum sum subarray can be obtained.
Examples:
Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output: [-2 -3 1 5]
Explanation : After selecting subsequence -2 -3 1 5 and reverse it elements, modified array will be {5, 1, 4, -1, -2, -3, -2, -3} and thus the maximum contagious sum i.e. 5 + 1 + 4 = 10
Input: arr[] = {2, -6, -12, 7, -13, 9, -14}
Output: [-6 -12 7 9]
Explanation: After selecting the above subsequence modified array will be {2, 9, 7, -12, -13, -6, -14} and thus the maximum contagious sum i.e. is 2 + 9 + 7 = 18
Approach: The idea is simple we have to modify the array such that all positive elements comes together, so we have to find the subsequence such that all positive elements come together when we reverse the subsequence.
- Let suppose there are ” p ” non- negative elements in the array. Divide the array into two parts: first p elements and the remaining elements .
- let ” px ” be non-negative elements in first part of array. so the negative elements in the first part will be:
(size of first part of array – number of non-negative elements) = p – px
- Also number of non-negative elements in second part of array is
(total non-negative elements – non-negative elements in first part of array) = p – px
- So we have to select negative elements p- px elements from first part and p-px non-negative elements from the second part of array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findSubsequce( int arr[], int n)
{
int p = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
vector< int > res;
for ( int i = 0; i < p; i++) {
if (arr[i] < 0)
res.push_back(arr[i]);
}
for ( int i = p; i < n; i++) {
if (arr[i] >= 0)
res.push_back(arr[i]);
}
return res;
}
int main()
{
int arr[] = { -2, -3, 4, -1,
-2, 1, 5, -3 };
int n = sizeof (arr) / sizeof (arr[0]);
vector< int > res = findSubsequce(arr, n);
for ( int i = 0; i < res.size(); i++) {
cout << res[i] << " " ;
}
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList<Integer>
findSubsequence( int arr[], int n)
{
int p = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] >= 0 )
p++;
}
ArrayList<Integer> res
= new ArrayList<Integer>();
for ( int i = 0 ; i < p; i++) {
if (arr[i] < 0 )
res.add(arr[i]);
}
for ( int i = p; i < n; i++) {
if (arr[i] >= 0 )
res.add(arr[i]);
}
return res;
}
public static void main(String[] args)
{
int arr[] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 };
int n = arr.length;
ArrayList<Integer> res = findSubsequence(arr, n);
for ( int i = 0 ; i < res.size(); i++) {
System.out.print(res.get(i) + " " );
}
}
}
|
Python3
def findSubsequce(arr, n):
p = 0
for i in range (n):
if (arr[i] > = 0 ):
p + = 1
res = []
for i in range (p):
if (arr[i] < 0 ):
res.append(arr[i])
for i in range (p, n):
if (arr[i] > = 0 ):
res.append(arr[i])
return res
if __name__ = = "__main__" :
arr = [ - 2 , - 3 , 4 , - 1 ,
- 2 , 1 , 5 , - 3 ]
n = len (arr)
res = findSubsequce(arr, n)
for i in range ( len (res)):
print (res[i], end = " " )
|
C#
using System;
using System.Collections;
public class GFG{
public static ArrayList
findSubsequence( int [] arr, int n)
{
int p = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
var res = new ArrayList();
for ( int i = 0; i < p; i++) {
if (arr[i] < 0)
res.Add(arr[i]);
}
for ( int i = p; i < n; i++) {
if (arr[i] >= 0)
res.Add(arr[i]);
}
return res;
}
static public void Main (){
int [] arr = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = arr.Length;
ArrayList res = findSubsequence(arr, n);
for ( int i = 0; i < res.Count; i++) {
Console.Write(res[i] + " " );
}
}
}
|
Javascript
<script>
function findSubsequce(arr, n)
{
let p = 0;
for (let i = 0; i < n; i++) {
if (arr[i] >= 0)
p++;
}
let res =[];
for (let i = 0; i < p; i++) {
if (arr[i] < 0)
res.push(arr[i]);
}
for (let i = p; i < n; i++) {
if (arr[i] >= 0)
res.push(arr[i]);
}
return res;
}
let arr = [-2, -3, 4, -1,
-2, 1, 5, -3]
let n = arr.length;
let res = findSubsequce(arr, n);
for (let i = 0; i < res.length; i++) {
document.write(res[i]+ " " )
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...