给定一个由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
方法:这个问题是最长的子序列问题的简单变化。变化的是基本条件和通过对给定数组进行排序来减少计算数量的技巧。
- 首先对给定的数组排序,以便我们仅需要检查i> j的arr [i]> arr [j]的值。
- 然后我们使用两个循环前进,外部循环从1到N ,内部循环从0到i 。
- 现在在内部循环中,我们必须找到arr [j]的数量,其中j是从0到i – 1的整数,它除以元素arr [i] 。
- 递归关系将是dp [i] = max(dp [i],1 + dp [j]) 。
- 我们将在名为res的变量中更新max dp [i]值,这将是最终答案。
下面是上述方法的实现:
C++
// 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 elemnet divides itself
dp[i] = 1;
// Count for all numbers which
// are leser 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 elemnet divides itself
dp[i] = 1;
// Count for all numbers which
// are leser 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 elemnet divides itself
dp[i] = 1;
# Count for all numbers which
# are leser 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 elemnet divides itself
dp[i] = 1;
// Count for all numbers which
// are leser 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