Magical Sequence
In this class, We discuss Magical Sequence.
For Complete YouTube Video: Click Here
The reader should have some basic knowledge of coding. Click Here to take our complete course to improve your basic coding skills.
First, we understand what a magical sequence is?
Given input is a list of numbers.
Example:
[2,4,6,8,12,60,120]
From the above list, four is divisible by 2, 8 is divisible by 4, and 120 is divisible by 8.
We fond a sequence [2,4,8,120].
Similarly, we need to identify all the possible sequences.
One more possible sequence is [2,6,12,60,120]
The second sequence has more number of elements. I.e., 5.
We need to identify the sequence containing more number of elements.
We need to display the count of elements present in the maximum sequence.
Example 2:
[8,3,9,6,12,120]
The maximum sequence is [3,6,12,120].
The sequence may not start from the first element.
In the placement exams, they do not ask the questions to write code for magical sequences.
The placement questions use the base logic of the magical sequence and frame new conditions.
One should write the code independently and then check for the solution.
Logic:
We explained the logic to write the code in the video. Click Here.
Code in Python:
n = int(input(“enter the n value”))
lis=[]
lis1=[0]*n
for i in range(n):
lis.append(int(input()))
i=1
while(i<n):
j=0
while(j<i):
if((lis[i]%lis[j])==0 and lis1[j]+1 > lis1[i]):
lis1[i]=lis1[j]+1
j=j+1
i=i+1
print(max(lis1)+1)