Maximum Balanced Subsequence Score
Last Updated :
06 Feb, 2024
Given a stock’s prices for the past n days in the array stockPrice. Choose a subsequence (an ordered subset of an array’s elements) of stock prices, called chosenDays, such that the chosen subsequence of stock prices is balanced. The score of the chosen subsequence is the sum of stock prices on the chosen days. Find the maximum possible score that can be obtained by choosing an optimally balanced subsequence. The subsequence of stock prices is balanced if the following condition holds, stockPrice[chosenDays[i]]-stockPrice[chosenDays[i-1]] = chosenDays[i]-chosenDays[i – 1], for i > 0.
Examples:
Input: n = 5, stockPrice = [1, 5, 3, 7, 8]
Output: 20
Explanation:The subsequence [5, 7, 8] can be chosen. Corresponding chosen days are [1, 3, 4] (considering 0-based indexing). Now,
• stockPrice[3] – stockPrice[1] = 7 – 5 = 2 and 3 – 1 = 2
• stockPrice[4] – stockPrice[3]= 8 – 7 = 1 and 4 – 3 = 1
Thus, the subsequence is balanced. Score= 5 + 7 + 8 = 20
The subsequence [1, 3] can be chosen. Corresponding chosen days are [0, 2] (considering 0-based indexing). Now,
• stockPrice[2] – stockPrice[0] = 3 – 1 = 2 and 2 – 0 = 2
Thus, the subsequence is balanced. Score= 1 + 3 = 4
20 is maximum possible. So, the answer is 20.
Input: n = 3, stockPrice = [1, 2, 3]
Output: 6
Explanation:The subsequence [1, 2, 3] can be chosen. Corresponding chosen days are [0, 1, 2] (considering 0-based indexing). Now,
• stockPrice[1] – stockPrice[0]= 2 – 1= 1 and 1 – 0 = 1
• stockPrice[2] – stockPrice[1]= 3 – 2 = 1 and 2 – 1 = 1
Thus, the subsequence is balanced. Score= 1 + 2 + 3 = 6
6 is maximum possible. So, the answer is 6.
Approach: To solve the problem follow the idea below:
Idea: The given equation can be reformulated as:
• stockPrice[chosenDays[i]] – chosenDays[i] = stockPrice[chosenDays[i-1]] – chosenDays[i-1]
This means that we can group elements together if the difference between their stock price and their index is the same.For example, in the given input 1, the elements 5, 7, and 8 can be grouped together because:
• 5 – 1 = 4
• 7 – 3 = 4
• 8 – 4 = 4
In other words, the solution works by finding the “gaps” between the stock prices. If two stock prices have the same gap, then they can be grouped together in a balanced subsequence. Now the problem is just reduced to storing the sum of elements corresponding to the gap.
Steps to solve the problem:
- Create a map mp.
- Store the difference between the stock price and the index for each element in diff.
- Store cumulative stock prices for each unique difference between stock prices and their positions.
- Iterate through the map mp to find the maximum cumulative stock price among all the balanced subsequences.
- Update the maxm variable if it finds a higher cumulative stock price.
- Return the maximum cumulative stock price stored in maxm, which is the result.
Below is the code for the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
long long
MaxBalancedSubsequenceScore( int n, vector< int >& stockPrice)
{
unordered_map< int , long long > mp;
long long maxm = 0;
for ( int i = 0; i < n; i++) {
int diff = stockPrice[i] - i;
mp += stockPrice[i];
}
for ( auto it : mp) {
if (it.second > maxm) {
maxm = it.second;
}
}
return maxm;
}
int main()
{
vector< int > stockPrice{ 1, 5, 3, 7, 8 };
cout << MaxBalancedSubsequenceScore(stockPrice.size(),
stockPrice)
<< endl;
stockPrice = { 1, 2, 3 };
cout << MaxBalancedSubsequenceScore(stockPrice.size(),
stockPrice)
<< endl;
return 0;
}
|
Java
import java.util.*;
public class MaxBalancedSubsequenceScore {
public static long getMaximumScore( int n,
int [] stockPrice)
{
Map<Integer, Long> map = new HashMap<>();
long maxm = 0 ;
for ( int i = 0 ; i < n; i++) {
int diff = stockPrice[i] - i;
if (map.containsKey(diff)) {
map.put(diff,
map.get(diff) + stockPrice[i]);
}
else {
map.put(diff, ( long )stockPrice[i]);
}
}
for (Map.Entry<Integer, Long> entry :
map.entrySet()) {
if (entry.getValue() > maxm) {
maxm = entry.getValue();
}
}
return maxm;
}
public static void main(String[] args)
{
int [] stockPrice1 = { 1 , 5 , 3 , 7 , 8 };
System.out.println(getMaximumScore(
stockPrice1.length, stockPrice1));
int [] stockPrice2 = { 1 , 2 , 3 };
System.out.println(getMaximumScore(
stockPrice2.length, stockPrice2));
}
}
|
Python3
def MaxBalancedSubsequenceScore(n, stockPrice):
mp = {}
maxm = 0
for i in range (n):
diff = stockPrice[i] - i
if diff in mp:
mp + = stockPrice[i]
else :
mp = stockPrice[i]
for key, value in mp.items():
if value > maxm:
maxm = value
return maxm
stockPrice1 = [ 1 , 5 , 3 , 7 , 8 ]
print (MaxBalancedSubsequenceScore( len (stockPrice1), stockPrice1))
stockPrice2 = [ 1 , 2 , 3 ]
print (MaxBalancedSubsequenceScore( len (stockPrice2), stockPrice2))
|
C#
using System;
using System.Collections.Generic;
class Program {
static long
MaxBalancedSubsequenceScore( int n, List< int > stockPrice)
{
Dictionary< int , long > dict
= new Dictionary< int , long >();
long maxm = 0;
for ( int i = 0; i < n; i++) {
int diff = stockPrice[i] - i;
if (dict.ContainsKey(diff))
dict += stockPrice[i];
else
dict.Add(diff, stockPrice[i]);
}
foreach ( var kvp in dict)
{
if (kvp.Value > maxm) {
maxm = kvp.Value;
}
}
return maxm;
}
static void Main()
{
List< int > stockPrice
= new List< int >{ 1, 5, 3, 7, 8 };
Console.WriteLine(MaxBalancedSubsequenceScore(
stockPrice.Count, stockPrice));
stockPrice = new List< int >{ 1, 2, 3 };
Console.WriteLine(MaxBalancedSubsequenceScore(
stockPrice.Count, stockPrice));
}
}
|
Javascript
function maxBalancedSubsequenceScore(n, stockPrice) {
let mp = {};
let maxm = 0;
for (let i = 0; i < n; i++) {
const diff = stockPrice[i] - i;
mp = (mp || 0) + stockPrice[i];
}
for (const key in mp) {
if (mp.hasOwnProperty(key) && mp[key] > maxm) {
maxm = mp[key];
}
}
return maxm;
}
const stockPrice1 = [1, 5, 3, 7, 8];
console.log(maxBalancedSubsequenceScore(stockPrice1.length, stockPrice1));
const stockPrice2 = [1, 2, 3];
console.log(maxBalancedSubsequenceScore(stockPrice2.length, stockPrice2));
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...