📌  相关文章
📜  每对相邻元素满足条件 2 * A[i] ≥ A[i + 1] 的最长严格递增子集的长度

📅  最后修改于: 2021-09-06 06:33:47             🧑  作者: Mango

给定大小为N的数组A[] ,任务是找到每对相邻元素满足条件A[i + 1] ≤ 2 * A[i]的最长严格递增子集的长度。如果存在多个这样的子集,则打印它们中的任何一个。

例子:

朴素的方法:解决问题的最简单的方法是将数组按升序排序并生成给定数组的所有可能子集,并找到满足给定条件的最长子集的长度。

时间复杂度: O(N * 2 N )
辅助空间: O(N)

高效的方法:这个想法是基于这样的观察:只有当满足给定条件的连续元素从排序数组中取出时,才会生成最长的子集。

请按照以下步骤解决问题:

  • 初始化两个变量,比如indexmaxLen ,以存储所需子集的起始索引和最大长度。
  • 按升序对数组A[]进行排序。
  • 初始化一个变量,比如i ,用于遍历和迭代,而i < N并执行以下步骤:
    • 初始化另一个变量j = i ,以存储当前子集的端点。
    • j < N – 12 * A[j] ≥ A[j + 1]成立时迭代并增加当前子集的长度,比如L ,并将j增加1
    • 如果L的值大于maxLen ,则将maxLen更新为L并将索引更新为i
    • i的值更新为j + 1
  • 通过打印范围[index, index+maxLen-1] 中的元素来打印所需的子集。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
3 5

时间复杂度: O(N * log(N))
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live