给定一个由N 个整数组成的数组arr[] ,任务是找到给定数组中最长的子序列,使得对于子序列(arr[i], arr[j])中的所有对,其中i != j arr[i] 除以 arr[j] ,反之亦然。如果不存在这样的子序列,则打印 -1。
例子:
Input: arr[] = {2, 4, 6, 1, 3, 11}
Output: 3
Longest valid sub-sequences are {1, 2, 6} and {1, 3, 6}.
Input: arr[] = {21, 22, 6, 4, 13, 7, 332}
Output: 2
方法:这个问题是最长递增子序列问题的一个简单变体。改变的是基本条件和通过对给定数组进行排序来减少计算次数的技巧。
- 首先对给定的数组进行排序,以便我们只需要检查arr[i] > arr[j] for i > j 的值。
- 然后我们使用两个循环向前移动,外循环从1运行到N ,内循环从0运行到i 。
- 现在在内部循环中,我们必须找到arr[j]的数量,其中j是从0到i – 1划分元素arr[i] 。
- 并且递推关系将是dp[i] = max(dp[i], 1 + dp[j]) 。
- 我们将更新名为res的变量中的最大dp[i]值,这将是最终答案。
下面是上述方法的实现:
C++
CPP
// C++ implementation of the approach
#include
using namespace std;
// Function to return the length of the
// longest required sub-sequence
int find(int n, int a[])
{
// Sort the array
sort(a, a + n);
// To store the resultant length
int res = 1;
int dp[n];
// If array contains only one element
// then it divides itself
dp[0] = 1;
for (int i = 1; i < n; i++) {
// Every element divides itself
dp[i] = 1;
// Count for all numbers which
// are lesser than a[i]
for (int j = 0; j < i; j++) {
if (a[i] % a[j] == 0) {
// If a[i] % a[j] then update the maximum
// subsequence length,
// dp[i] = max(dp[i], 1 + dp[j])
// where j is in the range [0, i-1]
dp[i] = std::max(dp[i], 1 + dp[j]);
}
}
res = std::max(res, dp[i]);
}
// If array contains only one element
// then i = j which doesn't satisfy the condition
return (res == 1) ? -1 : res;
}
// Driver code
int main()
{
int a[] = { 2, 4, 6, 1, 3, 11 };
int n = sizeof(a) / sizeof(int);
cout << find(n, a);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
import java.io.*;
class GFG
{
// Function to return the length of the
// longest required sub-sequence
static int find(int n, int a[])
{
// Sort the array
Arrays.sort(a);
// To store the resultant length
int res = 1;
int dp[] = new int[n];
// If array contains only one element
// then it divides itself
dp[0] = 1;
for (int i = 1; i < n; i++)
{
// Every element divides itself
dp[i] = 1;
// Count for all numbers which
// are lesser than a[i]
for (int j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
// If a[i] % a[j] then update the maximum
// subsequence length,
// dp[i] = Math.max(dp[i], 1 + dp[j])
// where j is in the range [0, i-1]
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
res = Math.max(res, dp[i]);
}
// If array contains only one element
// then i = j which doesn't satisfy the condition
return (res == 1) ? -1 : res;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 2, 4, 6, 1, 3, 11 };
int n = a.length;
System.out.println (find(n, a));
}
}
// This code is contributed by jit_t
Python3
# Python3 implementation of the approach
# Function to return the length of the
# longest required sub-sequence
def find(n, a) :
# Sort the array
a.sort();
# To store the resultant length
res = 1;
dp = [0]*n;
# If array contains only one element
# then it divides itself
dp[0] = 1;
for i in range(1, n) :
# Every element divides itself
dp[i] = 1;
# Count for all numbers which
# are lesser than a[i]
for j in range(i) :
if (a[i] % a[j] == 0) :
# If a[i] % a[j] then update the maximum
# subsequence length,
# dp[i] = max(dp[i], 1 + dp[j])
# where j is in the range [0, i-1]
dp[i] = max(dp[i], 1 + dp[j]);
res = max(res, dp[i]);
# If array contains only one element
# then i = j which doesn't satisfy the condition
if (res == 1):
return -1
else :
return res;
# Driver code
if __name__ == "__main__" :
a = [ 2, 4, 6, 1, 3, 11 ];
n = len(a);
print(find(n, a));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the length of the
// longest required sub-sequence
static int find(int n, int []a)
{
// Sort the array
Array.Sort(a);
// To store the resultant length
int res = 1;
int []dp = new int[n];
// If array contains only one element
// then it divides itself
dp[0] = 1;
for (int i = 1; i < n; i++)
{
// Every element divides itself
dp[i] = 1;
// Count for all numbers which
// are lesser than a[i]
for (int j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
// If a[i] % a[j] then update the maximum
// subsequence length,
// dp[i] = Math.max(dp[i], 1 + dp[j])
// where j is in the range [0, i-1]
dp[i] = Math.Max(dp[i], 1 + dp[j]);
}
}
res = Math.Max(res, dp[i]);
}
// If array contains only one element
// then i = j which doesn't satisfy the condition
return (res == 1) ? -1 : res;
}
// Driver code
public static void Main ()
{
int []a = { 2, 4, 6, 1, 3, 11 };
int n = a.Length;
Console.WriteLine(find(n, a));
}
}
// This code is contributed by anuj_67..
Java
// Java implementation of the approach
import java.util.Arrays;
import java.io.*;
class GFG
{
// Function to return the length of the
// longest required sub-sequence
static int find(int n, int a[])
{
// Sort the array
Arrays.sort(a);
// To store the resultant length
int res = 1;
int dp[] = new int[n];
// If array contains only one element
// then it divides itself
dp[0] = 1;
for (int i = 1; i < n; i++)
{
// Every element divides itself
dp[i] = 1;
// Count for all numbers which
// are lesser than a[i]
for (int j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
// If a[i] % a[j] then update the maximum
// subsequence length,
// dp[i] = Math.max(dp[i], 1 + dp[j])
// where j is in the range [0, i-1]
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
res = Math.max(res, dp[i]);
}
// If array contains only one element
// then i = j which doesn't satisfy the condition
return (res == 1) ? -1 : res;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 2, 4, 6, 1, 3, 11 };
int n = a.length;
System.out.println (find(n, a));
}
}
// This code is contributed by jit_t
蟒蛇3
# Python3 implementation of the approach
# Function to return the length of the
# longest required sub-sequence
def find(n, a) :
# Sort the array
a.sort();
# To store the resultant length
res = 1;
dp = [0]*n;
# If array contains only one element
# then it divides itself
dp[0] = 1;
for i in range(1, n) :
# Every element divides itself
dp[i] = 1;
# Count for all numbers which
# are lesser than a[i]
for j in range(i) :
if (a[i] % a[j] == 0) :
# If a[i] % a[j] then update the maximum
# subsequence length,
# dp[i] = max(dp[i], 1 + dp[j])
# where j is in the range [0, i-1]
dp[i] = max(dp[i], 1 + dp[j]);
res = max(res, dp[i]);
# If array contains only one element
# then i = j which doesn't satisfy the condition
if (res == 1):
return -1
else :
return res;
# Driver code
if __name__ == "__main__" :
a = [ 2, 4, 6, 1, 3, 11 ];
n = len(a);
print(find(n, a));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the length of the
// longest required sub-sequence
static int find(int n, int []a)
{
// Sort the array
Array.Sort(a);
// To store the resultant length
int res = 1;
int []dp = new int[n];
// If array contains only one element
// then it divides itself
dp[0] = 1;
for (int i = 1; i < n; i++)
{
// Every element divides itself
dp[i] = 1;
// Count for all numbers which
// are lesser than a[i]
for (int j = 0; j < i; j++)
{
if (a[i] % a[j] == 0)
{
// If a[i] % a[j] then update the maximum
// subsequence length,
// dp[i] = Math.max(dp[i], 1 + dp[j])
// where j is in the range [0, i-1]
dp[i] = Math.Max(dp[i], 1 + dp[j]);
}
}
res = Math.Max(res, dp[i]);
}
// If array contains only one element
// then i = j which doesn't satisfy the condition
return (res == 1) ? -1 : res;
}
// Driver code
public static void Main ()
{
int []a = { 2, 4, 6, 1, 3, 11 };
int n = a.Length;
Console.WriteLine(find(n, a));
}
}
// This code is contributed by anuj_67..
输出:
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。