Infosys 6 July 2024 Coding Question
Today (6 Jul 2024) Infosys SP Asked Coding Question
def max_beauty_subsequence(N, A):
MOD = 10**9 + 7
def gcd(x, y):
while y:
x, y = y, x % y
return x
max_beauty = 0
for i in range(N):
for j in range(i + 1, N):
subseq = A[i:j+1]
subseq_len = len(subseq)
if subseq_len > 1:
current_gcd = abs(subseq[0])
for num in subseq[1:]:
current_gcd = gcd(current_gcd, abs(num))
if current_gcd > 1:
break
if current_gcd > 1:
beauty = sum((subseq[k] – subseq[k-1])**2 for k in range(1, subseq_len))
max_beauty = max(max_beauty, beauty % MOD)
return max_beauty
# Example usage:
N = 5
A = [1, 2, 1, 2, 1]
print(max_beauty_subsequence(N, A)) # Output: 1
Click Below to Download
Infosys SP 6 Jul 2024 asked Coding Question -Hirin 6ba87e4d774d47da840c622e5a435dac