imashutosh51

Single Element in a Sorted Array

Feb 10th, 2026 (edited)
68
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. '''
  2. 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.
  3.  
  4. Steps:
  5. find mid and check if it is even or odd, if it odd, make it even first by reducing 1 from it.
  6. 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
  7. 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.
  8.  
  9. steps:
  10. 1.find mid and if mid if odd,make it even.
  11. 2.if mid and mid+1th element is same, adjust l to mid+2 else r=mid
  12.  
  13. Tricks:
  14. 1.Keep l<r because we need that midth element.
  15. 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.
  16. '''
  17. class Solution:
  18.     def singleNonDuplicate(self, nums: List[int]) -> int:
  19.         l,r=0,len(nums)-1
  20.         while l<r: #l=0, r=8, mid=4
  21.             mid = (l+r)//2
  22.             if mid%2==1:
  23.                 mid-=1
  24.             if nums[mid]==nums[mid+1]:
  25.                 l=mid+2 #mid and mid+1 element is same so move to the left boundary to the next pair.
  26.             else:
  27.                 r=mid
  28.         return nums[l]
  29.  
  30.  
  31.  
  32.        
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Nyxwexda
    11 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=okhoywhyuz
    4.  
    5. 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.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment