Open In App

Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A[i] ≥ A[i + 1]

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the length of the
// longest subset satisfying given conditions
void maxLenSubset(int a[], int n)
{
    // Sort the array in ascending order
    sort(a, a + n);
 
    // Stores the starting index and maximum
    // length of the required subset
    int index = 0, maxlen = -1;
 
    // Pointer to traverse the array
    int i = 0;
 
    // Iterate while i < n
    while (i < n) {
 
        // Stores end point
        // of current subset
        int j = i;
 
        // Store the length of
        // the current subset
        int len = 1;
 
        // Continue adding elements to the current
        // subset till the condition satisfies
        while (j < n - 1) {
 
            if (2 * a[j] >= a[j + 1]) {
 
                // Increment length of
                // the current subset
                len++;
            }
            else
                break;
 
            // Increment the pointer j
            j++;
        }
 
        // If length of the current subset
        // exceeds overall maximum length
        if (maxlen < len) {
 
            // Update maxlen
            maxlen = len;
 
            // Update index
            index = i;
        }
 
        // Increment j
        j++;
 
        // Update i
        i = j;
    }
 
    // Store the starting index of
    // the required subset in i
    i = index;
 
    // Print the required subset
    while (maxlen > 0) {
 
        // Print the array element
        cout << a[i] << " ";
 
        // Decrement maxlen
        maxlen--;
 
        // Increment i
        i++;
    }
}
 
// Driver Code
int main()
{
    // Given array
    int a[] = { 3, 1, 5, 11 };
 
    // Store the size of the array
    int n = sizeof(a) / sizeof(int);
 
    maxLenSubset(a, n);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the length of the
// longest subset satisfying given conditions
static void maxLenSubset(int a[], int n)
{
     
    // Sort the array in ascending order
    Arrays.sort(a);
 
    // Stores the starting index and maximum
    // length of the required subset
    int index = 0, maxlen = -1;
 
    // Pointer to traverse the array
    int i = 0;
 
    // Iterate while i < n
    while (i < n)
    {
         
        // Stores end point
        // of current subset
        int j = i;
 
        // Store the length of
        // the current subset
        int len = 1;
 
        // Continue adding elements to the current
        // subset till the condition satisfies
        while (j < n - 1)
        {
            if (2 * a[j] >= a[j + 1])
            {
                 
                // Increment length of
                // the current subset
                len++;
            }
            else
                break;
 
            // Increment the pointer j
            j++;
        }
 
        // If length of the current subset
        // exceeds overall maximum length
        if (maxlen < len)
        {
             
            // Update maxlen
            maxlen = len;
 
            // Update index
            index = i;
        }
 
        // Increment j
        j++;
 
        // Update i
        i = j;
    }
 
    // Store the starting index of
    // the required subset in i
    i = index;
 
    // Print the required subset
    while (maxlen > 0)
    {
         
        // Print the array element
        System.out.print(a[i] + " ");
 
        // Decrement maxlen
        maxlen--;
 
        // Increment i
        i++;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int a[] = { 3, 1, 5, 11 };
 
    // Store the size of the array
    int n = a.length;
 
    maxLenSubset(a, n);
}
}
 
// This code is contributed by Kingash


Python3




# Python3 program for the above approach
 
# Function to find the length of the
# longest subset satisfying given conditions
def maxLenSubset(a, n):
     
    # Sort the array in ascending order
    a.sort(reverse = False)
 
    # Stores the starting index and maximum
    # length of the required subset
    index = 0
    maxlen = -1
 
    # Pointer to traverse the array
    i = 0
 
    # Iterate while i < n
    while (i < n):
         
        # Stores end point
        # of current subset
        j = i
 
        # Store the length of
        # the current subset
        len1 = 1
 
        # Continue adding elements to the current
        # subset till the condition satisfies
        while (j < n - 1):
            if (2 * a[j] >= a[j + 1]):
                 
                # Increment length of
                # the current subset
                len1 += 1
            else:
                break
 
            # Increment the pointer j
            j += 1
 
        # If length of the current subset
        # exceeds overall maximum length
        if (maxlen < len1):
             
            # Update maxlen
            maxlen = len1
 
            # Update index
            index = i
 
        # Increment j
        j += 1
 
        # Update i
        i = j
 
    # Store the starting index of
    # the required subset in i
    i = index
 
    # Print the required subset
    while (maxlen > 0):
         
        # Print the array element
        print(a[i], end = " ")
 
        # Decrement maxlen
        maxlen -= 1
 
        # Increment i
        i += 1
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    a =  [3, 1, 5, 11]
 
    # Store the size of the array
    n = len(a)
 
    maxLenSubset(a, n)
 
# This code is contributed by SURENDRA_GANGWAR


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the length of the
// longest subset satisfying given conditions
static void maxLenSubset(int[] a, int n)
{
     
    // Sort the array in ascending order
    Array.Sort(a);
 
    // Stores the starting index and maximum
    // length of the required subset
    int index = 0, maxlen = -1;
 
    // Pointer to traverse the array
    int i = 0;
 
    // Iterate while i < n
    while (i < n)
    {
         
        // Stores end point
        // of current subset
        int j = i;
 
        // Store the length of
        // the current subset
        int len = 1;
 
        // Continue adding elements to the current
        // subset till the condition satisfies
        while (j < n - 1)
        {
            if (2 * a[j] >= a[j + 1])
            {
                 
                // Increment length of
                // the current subset
                len++;
            }
            else
                break;
 
            // Increment the pointer j
            j++;
        }
 
        // If length of the current subset
        // exceeds overall maximum length
        if (maxlen < len)
        {
             
            // Update maxlen
            maxlen = len;
 
            // Update index
            index = i;
        }
 
        // Increment j
        j++;
 
        // Update i
        i = j;
    }
 
    // Store the starting index of
    // the required subset in i
    i = index;
 
    // Print the required subset
    while (maxlen > 0)
    {
         
        // Print the array element
        Console.Write(a[i] + " ");
 
        // Decrement maxlen
        maxlen--;
 
        // Increment i
        i++;
    }
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given array
    int[] a = { 3, 1, 5, 11 };
 
    // Store the size of the array
    int n = a.Length;
 
    maxLenSubset(a, n);
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
 
        // Javascript program for
        // the above approach
 
        // Function to find the
        // length of the
        // longest subset satisfying
        // given conditions
        function maxLenSubset(a, n)
        {
            // Sort the array in ascending order
            a.sort( function(a, b) {
                return a - b;
            })
             
            // Stores the starting
            // index and maximum
            // length of the required subset
            let index = 0, maxlen = -1;
 
            // Pointer to traverse the array
            let i = 0;
 
            // Iterate while i < n
            while (i < n) {
 
                // Stores end point
                // of current subset
                let j = i;
 
                // Store the length of
                // the current subset
                let len = 1;
 
                // Continue adding elements
                // to the current
                // subset till the
                // condition satisfies
                while (j < n - 1) {
 
                    if (2 * a[j] >= a[j + 1])
                    {
 
                        // Increment length of
                        // the current subset
                        len++;
                    }
                    else
                        break;
 
                    // Increment the pointer j
                    j++;
                }
 
                // If length of the current subset
                // exceeds overall maximum length
                if (maxlen < len) {
 
                    // Update maxlen
                    maxlen = len;
 
                    // Update index
                    index = i;
                }
 
                // Increment j
                j++;
 
                // Update i
                i = j;
            }
 
            // Store the starting index of
            // the required subset in i
            i = index;
 
            // Print the required subset
            while (maxlen > 0) {
 
                // Print the array element
                document.write(a[i]+" ")
                // Decrement maxlen
                maxlen--;
 
                // Increment i
                i++;
            }
        }
 
        // Driver Code
 
        // Given array
        let a = [3, 1, 5, 11];
         
        // Store the size of the array
        let n = a.length
 
        maxLenSubset(a, n)
 
        // This code is contributed by Hritik
         
    </script>


Output: 

3 5

 

Time Complexity: O(N * log(N))
Auxiliary Space: O(1)

 



Last Updated : 23 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads