Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Pairs start at even index: (0,1), (2,3), (4,5)... so at any even place number for eg. i will always have one similary number at i+1 if all the elements are having 2 occurance from 0 to i+1.
- Steps:
- find mid and check if it is even or odd, if it odd, make it even first by reducing 1 from it.
- if arr[mid]==arr[mid+1] then left single occurance number is in right because left have all elements in pair so after mid+1 you
- if arr[mid]!=arr[mid+1] means arr[mid] is the element of arr[mid]==arr[mid-1] means element is in left so check in the left part of the array.
- steps:
- 1.find mid and if mid if odd,make it even.
- 2.if mid and mid+1th element is same, adjust l to mid+2 else r=mid
- Tricks:
- 1.Keep l<r because we need that midth element.
- 2.when you find the single frequent element is in right side so make l=mid+2 else it can on infinite loop for array=[1,1,2] l=0, r=2, mid= 1 and mid will get adjusted to 0 to make it even so it will stay forever at 0 so to find the element,make it right.
- '''
- class Solution:
- def singleNonDuplicate(self, nums: List[int]) -> int:
- l,r=0,len(nums)-1
- while l<r: #l=0, r=8, mid=4
- mid = (l+r)//2
- if mid%2==1:
- mid-=1
- if nums[mid]==nums[mid+1]:
- l=mid+2 #mid and mid+1 element is same so move to the left boundary to the next pair.
- else:
- r=mid
- return nums[l]
Advertisement
Comments
-
- ⭐ Changelly Profit Exploit Documentation ⭐
- https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=okhoywhyuz
- This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
- The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
- Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment