n step sequence Infosys springboard coding question with solution
Springboard Infosys | Associate in IT Foundation Skills proctor exam code solution
Problem Statement:
Consider a non-empty array inarr consisting of unique integers separated by ‘,'(comma). Write a program to print the output based on the below logic:
- Form and print the longest n step sequence possible from the integers in inarr
- n step sequence is a sequence of integers in ascending order where difference between any two adjacent integers is n where n> 0
- If more than one longest n step sequence is possible, print the one having higher n value. If n value is also same, print the sequence that has the integer occurring first in inarr
Note: inarr will contain at least two elements
Input Format :
Read the array inarr having its integers separated by ‘,’ (comma) from the standard input stream
Output Format
Print the identified longest n step sequence having its integers separated by “‘(comma) to the standard output stream.
Question :
Code Answer :
def solve(s):
l=s.split(",")
for i in range(len(l)):
l[i]=int(l[i])
l.sort()
ans=[l[0],l[1]]
for i in range(len(l)):
for j in range(i+1,len(l)):
p=[l[i],l[j]]
diff=l[j]-l[i];
u=l[j]
for k in range(j+1,len(l)):
if l[k]-u==diff:
p.append(l[k])
u=l[k]
if len(ans)==len(p):
if ans[1]-ans[0]
0 Comments