Skip to content

Commit 6947615

Browse files
committed
Redcube
1 parent 602f98a commit 6947615

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Max-GCD-Pair.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''We are given an array of positive integers. Find the pair in array with maximum GCD.'''
2+
def maxGCDPair(arr):
3+
high = max(arr)
4+
n = len(arr)
5+
count = [0]*(high+1)
6+
7+
for i in range(n):
8+
count[arr[i]] = 1
9+
10+
counter = 0
11+
#Iterating from max to 1
12+
#GCD is always between MAX and 1
13+
for i in range(high,0,-1):
14+
j = i
15+
#Iterating from curr potential GCD till it is less than max
16+
while j <= high:
17+
if count[j] == 1:
18+
counter += 1
19+
#Incrementing potential GCD by itself 2i,3i....
20+
j += i
21+
if counter == 2:
22+
return i
23+
24+
arr = [1,2,12,3,6]
25+
print maxGCDPair(arr)

0 commit comments

Comments
 (0)