304 North Cardinal St.
Dorchester Center, MA 02124

# Magical Sequence CodeChef Solution – Queslers

## Problem : Magical Sequence CodeChef Solution

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).

subsequence of AA, say [Ai1,Ai2,…,Aik][Ai1,Ai2,…,Aik], is said to be magical if the following condition is satisfied:

• For each 1≤j<k1≤j<k, (Aij,Aij+1)(Aij,Aij+1) must be among the given QQ pairs. That is, each consecutive pair of elements in this subsequence must be among the given QQ pairs.

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.

#### Input Format

• The first line of input contains a single integer TT, denoting the number of test cases.
• The first line of each test case contains an integer NN, denoting the length of the array.
• The second line of each test case contains NN space-separated integers representing the elements of the array.
• The third line of each test case contains an integer QQ, denoting the number of pairs.
• The ii-th of the next QQ lines contains two space-separated integers xixi and yiyi — the ii-th ordered pair.

#### Output Format

For each test case, print two lines.

• The first line should contain a single integer KK — the length of the longest magical subsequence of AA.
• The second line should contain KK space-separated integers in ascending order, denoting the indices of the magical subsequence you found.

In case there are multiple answers, print any of them.

#### Constraints

• 1≤T≤501≤T≤50
• 1≤N≤1051≤N≤105
• 1≤Ai≤1061≤Ai≤106
• 1≤Q≤1051≤Q≤105
• 1≤xi,yi≤1061≤xi,yi≤106
• The sum of NN over all test cases does not exceed 2⋅1052⋅105.
• The sum of QQ over all test cases does not exceed 2⋅1052⋅105.

#### Sample Input 1

``````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
``````

#### Sample Output 1

``````3
2 4 7
5
1 2 3 4 5
3
1 3 4
``````

#### Explanation

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.

## Magical Sequence CodeChef Solution in Java

``````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);
}
}``````

## Magical Sequence CodeChef Solution in C++

``````#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;
}``````
##### Magical Sequence CodeChef Solution Review:

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.

##### Conclusion:

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

Array Filling CodeChef Solution

Special Triplets CodeChef Solution