Length of longest subsequence such that prefix sum at every element remains greater than zero
Given an array arr[] of size N and an integer X, the task is to find the length of the longest subsequence such that the prefix sum at every element of the subsequence remains greater than zero.
Example:
Input: arr[] = {-2, -1, 1, 2, -2}, N = 5
Output: 3
Explanation: The sequence can be made of elements at index 2, 3 and 4. The prefix sum at every element stays greater than zero: 1, 3, 1
Input: arr[] = {-2, 3, 3, -7, -5, 1}, N = 6
Output: 12
Approach: The given problem can be solved using a greedy approach. The idea is to create a min-heap priority queue and traverse the array from the left to right. Add the current element arr[i] to the sum and minheap, and if the sum becomes less than zero, remove the most negative element from the minheap and subtract it from the sum. The below approach can be followed to solve the problem:
- Initialize a min-heap with priority queue data structure
- Initialize a variable sum to calculate the prefix sum of the desired subsequence
- Iterate the array and at every element arr[i] and add the value to the sum and min-heap
- If the value of sum becomes less than zero, remove the most negative element from the min-heap and subtract that value from the sum
- Return the size of the min-heap as the length of the longest subsequence
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxScore( int arr[], int N)
{
int score = 0;
priority_queue< int , vector< int >,
greater< int > >
pq;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
pq.push(arr[i]);
if (sum < 0) {
int a = pq.top();
sum -= a;
pq.pop();
}
}
return pq.size();
}
int main()
{
int arr[] = { -2, 3, 3, -7, -5, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxScore(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.PriorityQueue;
class GFG
{
static int maxScore( int arr[], int N)
{
int score = 0 ;
PriorityQueue<Integer> pq
= new PriorityQueue<Integer>();
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
pq.add(arr[i]);
if (sum < 0 ) {
int a = pq.poll();
sum -= a;
}
}
return pq.size();
}
public static void main(String[] args)
{
int arr[] = { - 2 , 3 , 3 , - 7 , - 5 , 1 };
int N = arr.length;
System.out.println(maxScore(arr, N));
}
}
|
Python3
from queue import PriorityQueue
def maxScore(arr, N):
score = 0 ;
pq = PriorityQueue();
sum = 0 ;
for i in range (N) :
sum + = arr[i];
pq.put(arr[i]);
if ( sum < 0 ):
a = pq.queue[ 0 ]
sum - = a;
pq.get()
return pq.qsize();
arr = [ - 2 , 3 , 3 , - 7 , - 5 , 1 ];
N = len (arr)
print (maxScore(arr, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int maxScore( int []arr, int N) {
int score = 0;
List< int > pq = new List< int >();
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
pq.Add(arr[i]);
pq.Sort();
if (sum < 0) {
int a = pq[0];
pq.RemoveAt(0);
sum -= a;
}
}
return pq.Count;
}
public static void Main(String[] args) {
int []arr = { -2, 3, 3, -7, -5, 1 };
int N = arr.Length;
Console.WriteLine(maxScore(arr, N));
}
}
|
Javascript
<script>
function maxScore(arr , N) {
var score = 0;
var pq = [];
var sum = 0;
for (i = 0; i < N; i++) {
sum += arr[i];
pq.push(arr[i]);
if (sum < 0) {
var a = pq.pop();
sum -= a;
}
}
return pq.length;
}
var arr = [ -2, 3, 3, -7, -5, 1 ];
var N = arr.length;
document.write(maxScore(arr, N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Last Updated :
10 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...