Length of the longest subsequence with negative sum of all prefixes
Last Updated :
29 Jun, 2021
Given an array arr[] consisting of N integers, the task is to find the length of the longest subsequence such that the prefix sum at each index of the subsequence is negative.
Examples:
Input: arr[] = {-1, -3, 3, -5, 8, 2}
Output: 5
Explanation: Longest subsequence satisfying the condition is {-1, -3, 3, -5, 2}.
Input: arr[] = {2, -5, 2, -1, 5, 1, -9, 10}
Output: 6
Explanation: Longest subsequence satisfying the condition is {-1, -3, 3, -5, 2}.
Approach: The problem can be solved by using a Priority Queue. Follow the steps below to solve the problem:
- Initialize a priority queue, say pq, and a variable, say S as 0, to store the elements of the subsequence formed from elements up to an index i and to store the sum of the elements in the priority queue.
- Iterate over the range [0, N – 1] using the variable i and perform the following steps:
- Finally, after completing the above steps, print pq.size() as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLengthSubsequence( int arr[], int N)
{
priority_queue< int > pq;
int S = 0;
for ( int i = 0; i < N; i++) {
S += arr[i];
pq.push(arr[i]);
while (S > 0) {
S -= pq.top();
pq.pop();
}
}
return pq.size();
}
int main()
{
int arr[6] = { -1, -3, 3, -5, 8, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxLengthSubsequence(arr, N);
return 0;
}
|
Java
import java.util.Collections;
import java.util.PriorityQueue;
public class GFG
{
static int maxLengthSubsequence( int arr[], int N)
{
PriorityQueue<Integer> pq = new PriorityQueue<>(
Collections.reverseOrder());
int S = 0 ;
for ( int i = 0 ; i < N; i++)
{
S += arr[i];
pq.add(arr[i]);
while (S > 0 )
{
S -= pq.peek();
pq.remove();
}
}
return pq.size();
}
public static void main(String[] args)
{
int arr[] = { - 1 , - 3 , 3 , - 5 , 8 , 2 };
int N = arr.length;
System.out.println(maxLengthSubsequence(arr, N));
}
}
|
Python3
def maxLengthSubsequence(arr, N):
pq = []
S = 0
for i in range (N):
S + = arr[i]
pq.append(arr[i])
pq.sort(reverse = False )
while (S > 0 ):
S = S - max (pq)
pq = pq[ 1 :]
return len (pq)
if __name__ = = '__main__' :
arr = [ - 1 , - 3 , 3 , - 5 , 8 , 2 ]
N = len (arr)
print (maxLengthSubsequence(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxLengthSubsequence( int []arr, int N)
{
List< int > pq = new List< int >();
int S = 0;
for ( int i = 0; i < N; i++)
{
S += arr[i];
pq.Add(arr[i]);
pq.Sort();
while (S > 0) {
pq.Sort();
S -= pq[pq.Count-1];
pq.RemoveAt(0);
}
}
return pq.Count;
}
public static void Main()
{
int []arr = { -1, -3, 3, -5, 8, 2 };
int N = arr.Length;
Console.Write(maxLengthSubsequence(arr, N));
}
}
|
Javascript
<script>
function maxLengthSubsequence(arr, N) {
let pq = new Array();
let S = 0;
for (let i = 0; i < N; i++) {
S += arr[i];
pq.push(arr[i]);
pq.sort((a, b) => a - b);
while (S > 0) {
pq.sort((a, b) => a - b);
S -= pq[pq.length - 1];
pq.shift();
}
}
return pq.length;
}
let arr = [-1, -3, 3, -5, 8, 2];
let N = arr.length;
document.write(maxLengthSubsequence(arr, N));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...