Given an array A[] of size N, the task is to find the length of the longest strictly increasing subset with every pair of adjacent elements satisfying the condition A[i + 1] ? 2 * A[i]. If multiple such subsets are present, then print any one of them.
Examples:
Input: A[] = {3, 1, 5, 11}
Output: 3, 5
Explanation: Among all possible subsets of the array, {3, 5} is the longest subset that satisfies the given condition.
Input: A[] = {3, 1, 7, 12}
Output: 7, 12
Explanation: Among all possible subsets of the array, {7, 12} is the longest subset that satisfies the given condition.
Naive Approach: The simplest approach to solve the problem is to sort the array in ascending order and generate all possible subsets of the given array and find the length of the longest subset that satisfies the given condition.
Time Complexity: O(N * 2N)
Auxiliary Space: O(N)
Efficient Approach: The idea is based on the observation that the longest subset will be generated only when continuous elements satisfying the given condition are taken from the sorted array.
Follow the steps below to solve the problem:
- Initialize two variables, say index and maxLen, to store the starting index and maximum length of the required subset.
- Sort the array A[] in ascending order.
- Initialize a variable, say i, for traversal and iterate while i < N and perform the following steps:
- Initialize another variable j = i, to store the endpoint of the current subset.
- Iterate while j < N – 1 and 2 * A[j] ? A[j + 1] holds true and increment the length of the current subset, say L, and increment j by 1.
- If the value of L is greater than maxLen, update maxLen to L and index to i.
- Update the value of i to j + 1.
- Print the required subset by printing the elements in the range [index, index+maxLen-1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxLenSubset( int a[], int n)
{
sort(a, a + n);
int index = 0, maxlen = -1;
int i = 0;
while (i < n) {
int j = i;
int len = 1;
while (j < n - 1) {
if (2 * a[j] >= a[j + 1]) {
len++;
}
else
break ;
j++;
}
if (maxlen < len) {
maxlen = len;
index = i;
}
j++;
i = j;
}
i = index;
while (maxlen > 0) {
cout << a[i] << " " ;
maxlen--;
i++;
}
}
int main()
{
int a[] = { 3, 1, 5, 11 };
int n = sizeof (a) / sizeof ( int );
maxLenSubset(a, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void maxLenSubset( int a[], int n)
{
Arrays.sort(a);
int index = 0 , maxlen = - 1 ;
int i = 0 ;
while (i < n)
{
int j = i;
int len = 1 ;
while (j < n - 1 )
{
if ( 2 * a[j] >= a[j + 1 ])
{
len++;
}
else
break ;
j++;
}
if (maxlen < len)
{
maxlen = len;
index = i;
}
j++;
i = j;
}
i = index;
while (maxlen > 0 )
{
System.out.print(a[i] + " " );
maxlen--;
i++;
}
}
public static void main(String[] args)
{
int a[] = { 3 , 1 , 5 , 11 };
int n = a.length;
maxLenSubset(a, n);
}
}
|
Python3
def maxLenSubset(a, n):
a.sort(reverse = False )
index = 0
maxlen = - 1
i = 0
while (i < n):
j = i
len1 = 1
while (j < n - 1 ):
if ( 2 * a[j] > = a[j + 1 ]):
len1 + = 1
else :
break
j + = 1
if (maxlen < len1):
maxlen = len1
index = i
j + = 1
i = j
i = index
while (maxlen > 0 ):
print (a[i], end = " " )
maxlen - = 1
i + = 1
if __name__ = = '__main__' :
a = [ 3 , 1 , 5 , 11 ]
n = len (a)
maxLenSubset(a, n)
|
C#
using System;
class GFG{
static void maxLenSubset( int [] a, int n)
{
Array.Sort(a);
int index = 0, maxlen = -1;
int i = 0;
while (i < n)
{
int j = i;
int len = 1;
while (j < n - 1)
{
if (2 * a[j] >= a[j + 1])
{
len++;
}
else
break ;
j++;
}
if (maxlen < len)
{
maxlen = len;
index = i;
}
j++;
i = j;
}
i = index;
while (maxlen > 0)
{
Console.Write(a[i] + " " );
maxlen--;
i++;
}
}
public static void Main( string [] args)
{
int [] a = { 3, 1, 5, 11 };
int n = a.Length;
maxLenSubset(a, n);
}
}
|
Javascript
<script>
function maxLenSubset(a, n)
{
a.sort( function (a, b) {
return a - b;
})
let index = 0, maxlen = -1;
let i = 0;
while (i < n) {
let j = i;
let len = 1;
while (j < n - 1) {
if (2 * a[j] >= a[j + 1])
{
len++;
}
else
break ;
j++;
}
if (maxlen < len) {
maxlen = len;
index = i;
}
j++;
i = j;
}
i = index;
while (maxlen > 0) {
document.write(a[i]+ " " )
maxlen--;
i++;
}
}
let a = [3, 1, 5, 11];
let n = a.length
maxLenSubset(a, n)
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)