Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def generate(n,MAX):
- seen = set()
- for _ in range(MAX):
- seen.add(n)
- if n%2==0 and n/2 not in seen and n/2>0:
- n = n//2
- elif n%2!=0 and n*2 not in seen and n*2<MAX:
- n = n*2
- else:
- continue
- return list(seen)
- def getMinDiff(arr, n):
- MAX,MIN = 10,10**9
- # To store the frequency of each element
- freq = [0 for i in range(MAX)]
- for i in range(n):
- # Update the frequency of current element
- for k in generate(arr[i],MAX):
- freq[k] += 1
- # If current element appears more than once then the min abs diff will be 0 i.e. |arr[i] - arr[i]|
- if (freq[k] > 1):
- return 0
- # Checking the distance between the nearest two elements in the frequency array
- for i in range(MAX):
- if (freq[i] > 0):
- i += 1
- cnt = 1
- while ((freq[i] == 0) and (i != MAX - 1)):
- cnt += 1
- i += 1
- MIN = min(cnt, MIN)
- i -= 1
- return MIN, freq
- # Driver code
- arr = [3, 6, 8]
- n = len(arr)
- print(getMinDiff(arr, n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement