给定大小为N的数组A [] ,任务是找出每对相邻元素满足条件A [i + 1]≤2 * A [i]的最长严格增长子集的长度。如果存在多个这样的子集,则打印其中任何一个。
例子:
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.
天真的方法:解决问题的最简单方法是按升序对数组进行排序,并生成给定数组的所有可能子集,并找到满足给定条件的最长子集的长度。
时间复杂度: O(N * 2 N )
辅助空间: O(N)
高效方法:该想法基于以下观察:只有从排序数组中获取满足给定条件的连续元素时,才会生成最长子集。
请按照以下步骤解决问题:
- 初始化两个变量,例如index和maxLen ,以存储所需子集的起始索引和最大长度。
- 以升序对数组A []进行排序。
- 初始化变量i进行遍历,并在i
进行迭代,并执行以下步骤: - 初始化另一个变量j = i ,以存储当前子集的端点。
- 在j
和2 * 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)