Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given an array A=[A1,A2,…,AN]A=[A1,A2,…,AN] containing NN elements. You are also given QQ ordered pairs of integers, where the ii-th pair is denoted (xi,yi)(xi,yi).
A subsequence of AA, say [Ai1,Ai2,…,Aik][Ai1,Ai2,…,Aik], is said to be magical if the following condition is satisfied:
Find the length of the longest magical subsequence of AA, and print the indices of one such longest subsequence.
If there are multiple longest magical subsequences, you may print the indices of any of them.
For each test case, print two lines.
In case there are multiple answers, print any of them.
3
7
1 3 2 8 3 7 6
5
3 7
3 3
3 8
8 7
8 6
5
1 2 1 2 1
5
2 1
3 1
1 1
3 2
1 2
4
1 2 1 4
5
1 3
1 1
1 4
2 4
4 4
3
2 4 7
5
1 2 3 4 5
3
1 3 4
Test case 11: The length of the longest magical subsequence is 33. One such subsequence is [3,8,6][3,8,6], given by indices [2,4,7][2,4,7]. The adjacent pairs are (3,8)(3,8) and (8,6)(8,6), both of which are among the given pairs.
Test case 22: The length of the longest magical subsequence is 55 — take the entire array. Adjacent elements are (1,2)(1,2) and (2,1)(2,1), both of which are in the given pairs.
Test case 33: The length of the longest magical subsequence is 33. One such subsequence of length 33 is [1,1,4][1,1,4], with indices [1,3,4][1,3,4]. Adjacent elements are (1,1)(1,1) and (1,4)(1,4), which are among the given pairs.
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n; i++){
arr[i] = sc.nextInt();
}
int[] dp = new int[n];
Arrays.fill(dp,1);
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(arr[i]%arr[j] == 0 && dp[j]+1 > dp[i]){
// System.out.println("Coming inside");
dp[i] = dp[j]+1;
}
}
}
int max = 1;
for(int i=0;i<n;i++){
if(max< dp[i]){
max = dp[i];
}
}
System.out.println(max);
}
}
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v(n);
for(int i = 0; i < n; ++i){
int a;
cin>>a;
v[i] = a;
}
int ans = 0;
if(n == 0){
cout<<ans<<endl;
return 0;
}
ans = 1;
vector<int> dp(n,1);
for(int i = 1; i < n; ++i){
for(int j = 0; j < i; ++j){
if(v[i] % v[j] == 0 && dp[i] < dp[j] + 1){
dp[i] = dp[j] + 1;
}
}
ans = max(ans,dp[i]);
}
cout<<ans<<endl;
return 0;
}
In our experience, we suggest you solve this Magical Sequence CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Magical Sequence Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Magical Sequence CodeChef Solution.
I hope this Magical Sequence CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More CodeChef Solutions >>
Olympics Ranking CodeChef Solution
Problem Difficulties CodeChef Solution
Chef and Bulb Invention CodeChef Solution