Skip to content

Commit 7caad0a

Browse files
committed
CodeChef: Easy Problems
1 parent a71e49e commit 7caad0a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that
2+
# GCD of all integers in that subarray is 1.
3+
#
4+
# Formally,
5+
# For a subarray Ai,Ai+1...Aj where 1 ≤ i < j ≤ N to be valid: GCD(Ai,Ai+1...Aj) should be 1. You have to
6+
# print the size of the largest valid subarray.
7+
#
8+
#
9+
# If no valid subarray exists, output -1.
10+
#
11+
# Note:A single element is not considered as a subarray according to the definition of this problem.
12+
#
13+
# Input
14+
# First line contains T, the number of testcases. Each testcase consists of N in one line followed by N
15+
# integers in the next line.
16+
#
17+
# Output
18+
# For each testcase, print the required answer in one line.
19+
#
20+
# Constraints
21+
# 1 ≤ T ≤ 10
22+
# 2 ≤ N ≤ 105
23+
# 1 ≤ Ai ≤ 105
24+
#
25+
# Example
26+
# Input:
27+
# 2
28+
# 2
29+
# 7 2
30+
# 3
31+
# 2 2 4
32+
#
33+
# Output:
34+
# 2
35+
# -1
36+
# Explanation
37+
# Example case 1.GCD(2,7)=1. So the subarray [A1,A2] is valid.
38+
#
39+
# Example case 2.No subarray satisfies.
40+
#
41+
42+
def gcd(firstNumber, secondNumber):
43+
firstNumber = int(firstNumber)
44+
secondNumber = int(secondNumber)
45+
if firstNumber < secondNumber:
46+
smaller = firstNumber
47+
else:
48+
smaller = secondNumber
49+
50+
for i in range(1, smaller + 1):
51+
if (firstNumber % i == 0) and (secondNumber % i == 0):
52+
gcd = i
53+
54+
return gcd
55+
56+
for _ in range(int(input())):
57+
count = int(input())
58+
array = input().split()
59+
currentGCD = array[0]
60+
flag = 0
61+
size = 0
62+
for i in array:
63+
currentGCD = gcd(i, currentGCD)
64+
if currentGCD == 1:
65+
flag = 1
66+
print(count)
67+
break
68+
69+
if flag == 0:
70+
print(-1)

0 commit comments

Comments
 (0)