We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 602f98a commit 6947615Copy full SHA for 6947615
Max-GCD-Pair.py
@@ -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