Maximizing Product-Weighted Subsequence Length
Last Updated :
28 Dec, 2023
Given an array arr[] of size N, you have to find the length of the longest subsequence such that the product of the elements in the subsequence divided by the factorial of its length is maximized.
Examples:
Input: N = 5, arr[] = {1, 2, 4, 5, 2}
Output: 2
Explanation: We can choose 4 and 5 giving an answer of (4*5)/(2!) = 10, which is the maximum possible.
Input: N = 3, arr[] = {1, 3, 2}
Output: 2
Explanation: We can have a subsequence with elements 3 and 2 to obtain the maximum possible answer of 3.
Approach: This can be solved with the following idea;
The main idea is to sort the array in ascending order and then iteratively(from largest to smallest elements) construct a subsequence and check if the size of the subsequence is greater than ith element which means if we include this element in subsequence then overall result will be reduced hence break the loop here and return the length of subsequence.
Below are the steps involved:
- Sort the input array arr in ascending order.
- Initialize two variables: ind to keep track of the length of subsequence while traversing the sorted array, and ans to store the length of the longest subsequence.
- Start iterating through the sorted array from the end (largest elements).
- Check if the current element at index i is greater than or equal to the value of ind. If it is, it means that this element can be part of the subsequence. Increment ans to indicate the inclusion of this element in the subsequence.
- If the current element is not greater than or equal to ind, break out of the loop as there is no need to continue checking further. We’ve already found the longest subsequence based on the problem’s criteria.
- Return the value of ans as the length of the longest subsequence.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int longestSubsequenceLength( int n, vector< int >& arr)
{
sort(arr.begin(), arr.end());
int ans = 0;
int index = 1;
for ( int i = n - 1; i >= 0; i--) {
if (index <= arr[i]) {
index++;
ans++;
}
else {
break ;
}
}
return ans;
}
int main()
{
int n = 4;
vector< int > arr = { 2, 4, 5, 1 };
cout << longestSubsequenceLength(n, arr);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LongestSubsequence {
static int longestSubsequenceLength( int n, List<Integer> arr) {
Collections.sort(arr);
int ans = 0 ;
int index = 1 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
if (index <= arr.get(i)) {
index++;
ans++;
} else {
break ;
}
}
return ans;
}
public static void main(String[] args) {
int n = 4 ;
List<Integer> arr = Arrays.asList( 2 , 4 , 5 , 1 );
System.out.println(longestSubsequenceLength(n, arr));
}
}
|
Python3
def longest_subsequence_length(n, arr):
arr.sort()
ans = 0
index = 1
for i in range (n - 1 , - 1 , - 1 ):
if index < = arr[i]:
index + = 1
ans + = 1
else :
break
return ans
n = 4
arr = [ 2 , 4 , 5 , 1 ]
print (longest_subsequence_length(n, arr))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class LongestSubsequence {
static int LongestSubsequenceLength( int n,
List< int > arr)
{
arr.Sort();
int ans = 0;
int index = 1;
for ( int i = n - 1; i >= 0; i--) {
if (index <= arr[i]) {
index++;
ans++;
}
else {
break ;
}
}
return ans;
}
public static void Main( string [] args)
{
int n = 4;
List< int > arr = new List< int >{ 2, 4, 5, 1 };
Console.WriteLine(LongestSubsequenceLength(n, arr));
}
}
|
Javascript
function longestSubsequenceLength(n, arr) {
arr.sort((a, b) => a - b);
let ans = 0;
let index = 1;
for (let i = n - 1; i >= 0; i--) {
if (index <= arr[i]) {
index++;
ans++;
} else {
break ;
}
}
return ans;
}
let n = 4;
let arr = [2, 4, 5, 1];
console.log(longestSubsequenceLength(n, arr));
|
Time Complexity: O(n*log n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...