We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This must be an easy problem actually.
all you have to do is sort the array, and then check the difference of adjacent pairs, if its less than ur last min value, update it only if the index of those pairs are in same way in original array.
''' Improved version using Hash.'''n=int(input().strip())numbers=list(map(int,input().strip().split()))myDict={}foriinrange(n):myDict[numbers[i]]=iminCost=10**10nums=sorted(myDict)foriinrange(1,n):if(nums[i]-nums[i-1]<minCost)and(myDict[nums[i]]<myDict[nums[i-1]]):minCost=nums[i]-nums[i-1]print(minCost)
Minimum Loss
You are viewing a single comment's thread. Return to all comments →
This must be an easy problem actually. all you have to do is sort the array, and then check the difference of adjacent pairs, if its less than ur last min value, update it only if the index of those pairs are in same way in original array.