Maximum product of subsequence of size k
Last Updated :
19 Sep, 2023
Given an array A[] of n integers, the task is to find a subsequence of size k whose product is maximum among all possible k sized subsequences of the given array.
Constraints
1 <= n <= 10^5
1 <= k <= n
Examples:
Input : A[] = {1, 2, 0, 3},
k = 2
Output : 6
Explanation : Subsequence containing elements
{2, 3} gives maximum product : 2*3 = 6
Input : A[] = {1, 2, -1, -3, -6, 4},
k = 4
Output : 144
Explanation : Subsequence containing {2, -3,
-6, 4} gives maximum product : 2*(-3)*(-6)*4
= 144
Following are different cases that arise in this problem.
- CASE I: if the maximum element of A is 0 and k is odd Here if we don't include 0 in subsequence then product will be less than 0, Since the product of an odd number of negative integers gives a negative integer. Hence 0 must be included in the subsequence. Since 0 is present in subsequence, the product of subsequence is 0. Answer = 0.
- CASE II: if maximum element of A is negative and k is odd. Here the product will be less than 0,
Since the product of an odd number of negative integers gives a negative integer. So to get the maximum product, we take the product of the smallest (absolute value wise) k elements. Since absolute value wise : | A[n-1] | > | A[n-2] | ... > | A[0] |. Hence we take product of A[n-1], A[n-2], A[n-3], .... A[n-k]
Answer = A[n-1] * A[n-2] * ..... * A[n-k] - CASE III: if maximum element of A is positive and k is odd. Here in subsequence of k size if all elements are < 0 then product will be less than 0, Since the product of an odd number of negative integers gives a negative integer. Hence, at least one element must be a positive integer in the subsequence. To get max product max positive number should be present in the subsequence. Now we need to add k-1 more elements to the subsequence.
Since k is odd, k-1 becomes even. So the problem boils down to case IV. Answer = A[n-1] * Answer from CASE IV. - CASE IV: if k is even. Since k is even, we always add a pair in subsequence. So total pairs required to be added in subsequence is k/2. So for simplicity, our new k is k/2. Now since A is sorted, pair with the maximum product will always be either A[0]*A[1] OR A[n-1]*A[n-2]. In case of doubt about the previous statement think about negative numbers :)
Now,
if A[0]*A[1] > A[n-1]*A[n-2],
second max product pair will be
either A[2]*A[3] OR A[n-1]*[n-2].
else second max product pair will be
either A[0]*A[1] OR A[n-3]*[n-4].
Here is implementation of above solution
C++
// C++ code to find maximum possible product of
// sub-sequence of size k from given array of n
// integers
#include <algorithm> // for sorting
#include <iostream>
using namespace std;
// Required function
int maxProductSubarrayOfSizeK(int A[], int n, int k)
{
// sorting given input array
sort(A, A + n);
// variable to store final product of all element
// of sub-sequence of size k
int product = 1;
// CASE I
// If max element is 0 and
// k is odd then max product will be 0
if (A[n - 1] == 0 && (k & 1))
return 0;
// CASE II
// If all elements are negative and
// k is odd then max product will be
// product of rightmost-subarray of size k
if (A[n - 1] <= 0 && (k & 1)) {
for (int i = n - 1; i >= n - k; i--)
product *= A[i];
return product;
}
// else
// i is current left pointer index
int i = 0;
// j is current right pointer index
int j = n - 1;
// CASE III
// if k is odd and rightmost element in
// sorted array is positive then it
// must come in subsequence
// Multiplying A[j] with product and
// correspondingly changing j
if (k & 1) {
product *= A[j];
j--;
k--;
}
// CASE IV
// Now k is even
// Now we deal with pairs
// Each time a pair is multiplied to product
// ie.. two elements are added to subsequence each time
// Effectively k becomes half
// Hence, k >>= 1 means k /= 2
k >>= 1;
// Now finding k corresponding pairs
// to get maximum possible value of product
for (int itr = 0; itr < k; itr++) {
// product from left pointers
int left_product = A[i] * A[i + 1];
// product from right pointers
int right_product = A[j] * A[j - 1];
// Taking the max product from two choices
// Correspondingly changing the pointer's position
if (left_product > right_product) {
product *= left_product;
i += 2;
}
else {
product *= right_product;
j -= 2;
}
}
// Finally return product
return product;
}
// Driver Code to test above function
int main()
{
int A[] = { 1, 2, -1, -3, -6, 4 };
int n = sizeof(A) / sizeof(A[0]);
int k = 4;
cout << maxProductSubarrayOfSizeK(A, n, k);
return 0;
}
Java
// Java program to find maximum possible product of
// sub-sequence of size k from given array of n
// integers
import java.io.*;
import java.util.*;
class GFG {
// Function to find maximum possible product
static int maxProductSubarrayOfSizeK(int A[], int n, int k)
{
// sorting given input array
Arrays.sort(A);
// variable to store final product of all element
// of sub-sequence of size k
int product = 1;
// CASE I
// If max element is 0 and
// k is odd then max product will be 0
if (A[n - 1] == 0 && k % 2 != 0)
return 0;
// CASE II
// If all elements are negative and
// k is odd then max product will be
// product of rightmost-subarray of size k
if (A[n - 1] <= 0 && k % 2 != 0) {
for (int i = n - 1; i >= n - k; i--)
product *= A[i];
return product;
}
// else
// i is current left pointer index
int i = 0;
// j is current right pointer index
int j = n - 1;
// CASE III
// if k is odd and rightmost element in
// sorted array is positive then it
// must come in subsequence
// Multiplying A[j] with product and
// correspondingly changing j
if (k % 2 != 0) {
product *= A[j];
j--;
k--;
}
// CASE IV
// Now k is even
// Now we deal with pairs
// Each time a pair is multiplied to product
// ie.. two elements are added to subsequence each time
// Effectively k becomes half
// Hence, k >>= 1 means k /= 2
k >>= 1;
// Now finding k corresponding pairs
// to get maximum possible value of product
for (int itr = 0; itr < k; itr++) {
// product from left pointers
int left_product = A[i] * A[i + 1];
// product from right pointers
int right_product = A[j] * A[j - 1];
// Taking the max product from two choices
// Correspondingly changing the pointer's position
if (left_product > right_product) {
product *= left_product;
i += 2;
}
else {
product *= right_product;
j -= 2;
}
}
// Finally return product
return product;
}
// driver program
public static void main(String[] args)
{
int A[] = { 1, 2, -1, -3, -6, 4 };
int n = A.length;
int k = 4;
System.out.println(maxProductSubarrayOfSizeK(A, n, k));
}
}
// Contributed by Pramod Kumar
Python 3
# Python 3 code to find maximum possible
# product of sub-sequence of size k from
# given array of n integers
# Required function
def maxProductSubarrayOfSizeK(A, n, k):
# sorting given input array
A.sort()
# variable to store final product of
# all element of sub-sequence of size k
product = 1
# CASE I
# If max element is 0 and
# k is odd then max product will be 0
if (A[n - 1] == 0 and (k & 1)):
return 0
# CASE II
# If all elements are negative and
# k is odd then max product will be
# product of rightmost-subarray of size k
if (A[n - 1] <= 0 and (k & 1)) :
for i in range(n - 1, n - k + 1, -1):
product *= A[i]
return product
# else
# i is current left pointer index
i = 0
# j is current right pointer index
j = n - 1
# CASE III
# if k is odd and rightmost element in
# sorted array is positive then it
# must come in subsequence
# Multiplying A[j] with product and
# correspondingly changing j
if (k & 1):
product *= A[j]
j-= 1
k-=1
# CASE IV
# Now k is even. So, Now we deal with pairs
# Each time a pair is multiplied to product
# ie.. two elements are added to subsequence
# each time. Effectively k becomes half
# Hence, k >>= 1 means k /= 2
k >>= 1
# Now finding k corresponding pairs to get
# maximum possible value of product
for itr in range( k) :
# product from left pointers
left_product = A[i] * A[i + 1]
# product from right pointers
right_product = A[j] * A[j - 1]
# Taking the max product from two
# choices. Correspondingly changing
# the pointer's position
if (left_product > right_product) :
product *= left_product
i += 2
else :
product *= right_product
j -= 2
# Finally return product
return product
# Driver Code
if __name__ == "__main__":
A = [ 1, 2, -1, -3, -6, 4 ]
n = len(A)
k = 4
print(maxProductSubarrayOfSizeK(A, n, k))
# This code is contributed by ita_c
C#
// C# program to find maximum possible
// product of sub-sequence of size k
// from given array of n integers
using System;
class GFG {
// Function to find maximum possible product
static int maxProductSubarrayOfSizeK(int[] A, int n,
int k)
{
// sorting given input array
Array.Sort(A);
// variable to store final product of
// all element of sub-sequence of size k
int product = 1;
int i;
// CASE I
// If max element is 0 and
// k is odd then max product will be 0
if (A[n - 1] == 0 && k % 2 != 0)
return 0;
// CASE II
// If all elements are negative and
// k is odd then max product will be
// product of rightmost-subarray of size k
if (A[n - 1] <= 0 && k % 2 != 0) {
for (i = n - 1; i >= n - k; i--)
product *= A[i];
return product;
}
// else
// i is current left pointer index
i = 0;
// j is current right pointer index
int j = n - 1;
// CASE III
// if k is odd and rightmost element in
// sorted array is positive then it
// must come in subsequence
// Multiplying A[j] with product and
// correspondingly changing j
if (k % 2 != 0) {
product *= A[j];
j--;
k--;
}
// CASE IV
// Now k is even
// Now we deal with pairs
// Each time a pair is multiplied to
// product i.e.. two elements are added to
// subsequence each time Effectively k becomes half
// Hence, k >>= 1 means k /= 2
k >>= 1;
// Now finding k corresponding pairs
// to get maximum possible value of product
for (int itr = 0; itr < k; itr++) {
// product from left pointers
int left_product = A[i] * A[i + 1];
// product from right pointers
int right_product = A[j] * A[j - 1];
// Taking the max product from two choices
// Correspondingly changing the pointer's position
if (left_product > right_product) {
product *= left_product;
i += 2;
}
else {
product *= right_product;
j -= 2;
}
}
// Finally return product
return product;
}
// driver program
public static void Main()
{
int[] A = { 1, 2, -1, -3, -6, 4 };
int n = A.Length;
int k = 4;
Console.WriteLine(maxProductSubarrayOfSizeK(A, n, k));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP code to find maximum possible product of
// sub-sequence of size k from given array of n
// integers
// Required function
function maxProductSubarrayOfSizeK($A, $n, $k)
{
// sorting given input array
sort($A);
// variable to store final product of all element
// of sub-sequence of size k
$product = 1;
// CASE I
// If max element is 0 and
// k is odd then max product will be 0
if ($A[$n - 1] == 0 && ($k & 1))
return 0;
// CASE II
// If all elements are negative and
// k is odd then max product will be
// product of rightmost-subarray of size k
if ($A[$n - 1] <= 0 && ($k & 1))
{
for ($i = $n - 1; $i >= $n - $k; $i--)
$product *= $A[$i];
return $product;
}
// else
// i is current left pointer index
$i = 0;
// j is current right pointer index
$j = $n - 1;
// CASE III
// if k is odd and rightmost element in
// sorted array is positive then it
// must come in subsequence
// Multiplying A[j] with product and
// correspondingly changing j
if ($k & 1)
{
$product *= $A[$j];
$j--;
$k--;
}
// CASE IV
// Now k is even
// Now we deal with pairs
// Each time a pair is multiplied to product
// ie.. two elements are added to subsequence each time
// Effectively k becomes half
// Hence, k >>= 1 means k /= 2
$k >>= 1;
// Now finding k corresponding pairs
// to get maximum possible value of product
for ($itr = 0; $itr < $k; $itr++)
{
// product from left pointers
$left_product = $A[$i] * $A[$i + 1];
// product from right pointers
$right_product = $A[$j] * $A[$j - 1];
// Taking the max product from two choices
// Correspondingly changing the pointer's position
if ($left_product > $right_product)
{
$product *= $left_product;
$i += 2;
}
else
{
$product *= $right_product;
$j -= 2;
}
}
// Finally return product
return $product;
}
// Driver Code
$A = array(1, 2, -1, -3, -6, 4 );
$n = count($A);
$k = 4;
echo maxProductSubarrayOfSizeK($A, $n, $k);
// This code is contributed by ajit.
?>
JavaScript
<script>
// Javascript program to find maximum possible
// product of sub-sequence of size k
// from given array of n integers
// Function to find maximum possible product
function maxProductSubarrayOfSizeK(A, n, k)
{
// sorting given input array
A.sort(function(a, b){return a - b});
// variable to store final product of
// all element of sub-sequence of size k
let product = 1;
let i;
// CASE I
// If max element is 0 and
// k is odd then max product will be 0
if (A[n - 1] == 0 && k % 2 != 0)
return 0;
// CASE II
// If all elements are negative and
// k is odd then max product will be
// product of rightmost-subarray of size k
if (A[n - 1] <= 0 && k % 2 != 0) {
for (i = n - 1; i >= n - k; i--)
product *= A[i];
return product;
}
// else
// i is current left pointer index
i = 0;
// j is current right pointer index
let j = n - 1;
// CASE III
// if k is odd and rightmost element in
// sorted array is positive then it
// must come in subsequence
// Multiplying A[j] with product and
// correspondingly changing j
if (k % 2 != 0) {
product *= A[j];
j--;
k--;
}
// CASE IV
// Now k is even
// Now we deal with pairs
// Each time a pair is multiplied to
// product i.e.. two elements are added to
// subsequence each time Effectively k becomes half
// Hence, k >>= 1 means k /= 2
k >>= 1;
// Now finding k corresponding pairs
// to get maximum possible value of product
for (let itr = 0; itr < k; itr++) {
// product from left pointers
let left_product = A[i] * A[i + 1];
// product from right pointers
let right_product = A[j] * A[j - 1];
// Taking the max product from two choices
// Correspondingly changing the pointer's position
if (left_product > right_product) {
product *= left_product;
i += 2;
}
else {
product *= right_product;
j -= 2;
}
}
// Finally return product
return product;
}
let A = [ 1, 2, -1, -3, -6, 4 ];
let n = A.length;
let k = 4;
document.write(maxProductSubarrayOfSizeK(A, n, k));
</script>
Time Complexity : O(n * log n), O(n * log n) from sorting + O(k) from one traversal in array = O(n * log n)
Auxiliary Space : O(1)
Similar Reads
Maximum product of bitonic subsequence of size 3
Given an array arr[] of positive integers of size N, the task is to find the maximum product of bitonic subsequence of size 3.Bitonic Subsequence: subsequence in which elements are first in the increasing order and then decreasing order. Elements in the subsequence are follow this order arr[i] <
15 min read
Maximum product of an increasing subsequence of size 3
Given an array of distinct positive integers, the task is to find the maximum product of increasing subsequence of size 3, i.e., we need to find arr[i]*arr[j]*arr[k] such that arr[i] < arr[j] < arr[k] and i < j < k < n Examples: Input: arr[] = {10, 11, 9, 5, 6, 1, 20}Output: 2200 Expl
14 min read
Subsequence of size k with maximum possible GCD
We are given an array of positive integers and an integer k. Find the maximum possible GCD of a subsequence of size k. Examples: Input : arr[] = [2, 1, 4, 6] k = 3 Output : 2 GCD of [2, 4, 6] is 2 Input : arr[] = [1, 2, 3] k = 3 Output : 1 GCD of [1, 2, 3] is 1 Method 1 Generate all the subsequences
8 min read
Maximum sum subsequence of length K | Set 2
Given an array sequence arr[] i.e [A1, A2 â¦An] and an integer k, the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3â¦â¦â¦<=Sk. Examples: Input: arr[] = {-1, 3, 4, 2, 5}, K = 3Output: 3 4 5Explanation: Subsequence 3 4 5 with sum 12 is the s
7 min read
Largest product of a subarray of size k
Given an array consisting of n positive integers, and an integer k. Find the largest product subarray of size k, i.e., find the maximum produce of k contiguous elements in the array where k <= n.Examples :Â Input: arr[] = {1, 5, 9, 8, 2, 4, 1, 8, 1, 2} k = 6Output: 4608 The subarray is {9, 8, 2,
12 min read
Maximizing the sum of subsequence of size at most M
Given array A[] of size N, integer M and integer K. The task is to choose at most M elements from the array A[] to form a new array such that if the previous element was chosen is at index i that is A[i] and the current element at index j that is A[j] append A[j] - K * (j - i) to new array. Find the
9 min read
Maximum length subsequence possible of the form R^N K^N
Given a string containing only two characters i.e. R and K (like RRKRRKKKKK). The task is to find the maximum value of N for a subsequence possible of the form R---N times and then K---N times (i.e. of the form R^N K^N). Note: String of k should be started after the string of R i.e. first k that wou
6 min read
Maximum of all subarrays of size K using Segment Tree
Given an array arr[] and an integer K, the task is to find the maximum for each and every contiguous subarray of size K. Examples: Input: arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}, K = 3 Output: 3 3 4 5 5 5 6 Explanation: Maximum of 1, 2, 3 is 3 Maximum of 2, 3, 1 is 3 Maximum of 3, 1, 4 is 4 Maximum of 1
14 min read
Maximum Sum Subsequence of length k
Given an array sequence [A1, A2 ...An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3.........<=Sk. Examples: Input : n = 8 k = 3 A=[8 5 9 10 5 6 21 8] Output : 40 Possible Increasing subsequence of Length 3 with maximum possible s
11 min read
Maximum of all Subarrays of size k using set in C++ STL
Given an array of size N and an integer K, the task is to find the maximum for each and every contiguous sub-array of size K and print the sum of all these values in the end. Examples: Input: arr[] = {4, 10, 54, 11, 8, 7, 9}, K = 3 Output: 182 Input: arr[] = {1, 2, 3, 4, 1, 6, 7, 8, 2, 1}, K = 4 Out
3 min read