Single Element in a Sorted Array | Amazon | Microsoft | Leetcode 540

Поділитися
Вставка
  • Опубліковано 3 січ 2025

КОМЕНТАРІ • 24

  • @SonamKumari-cm5ef
    @SonamKumari-cm5ef Місяць тому +2

    Subhanallah 😊😊 kya shaandar samjhaya aapne

  • @BiswajitRout-dl5ry
    @BiswajitRout-dl5ry 8 місяців тому +7

    most underrated channel for dsa keep up doing sir

  • @Satya-g5t
    @Satya-g5t Місяць тому +1

    nice explanation with diagrams. 😄

  • @souravjoshi2293
    @souravjoshi2293 2 роки тому +3

    Thanks a lot for making this look too easy

  • @thakuradarsh3637
    @thakuradarsh3637 Місяць тому +1

    thanks sir for providig us this types of explanantion

  • @priyanshuchaturvedi3706
    @priyanshuchaturvedi3706 7 місяців тому +1

    after watching this......problem become piece of cake, THANKYOU

  • @_Avinash_CSEB
    @_Avinash_CSEB 8 місяців тому +2

    great work , keep up doing sir.

  • @gamersgame43
    @gamersgame43 Місяць тому +2

    this is also a problem on bit manipulation taking xor sum, if the array wouldn't have been sorted

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому +2

    Class solution {
    Public int single Non Duplicate (int[]nums){
    int s=0 e=nums.length;
    While (s

  • @vivek.tiwary
    @vivek.tiwary 3 місяці тому +1

    the if else block can be simplified.
    public class Solution
    {
    public int SingleNonDuplicate(int[] nums)
    {
    /*
    #1. elements are in pair, then in ideal case
    it will start from even index & end at odd index
    #2. for single element, element != nums[prev] && element != nums[next]
    */
    int n = nums.Length;
    if (n == 1)
    return nums[0];
    if (nums[0] != nums[1])
    return nums[0];
    if (nums[^1] != nums[^2])
    return nums[^1];
    int l = 2;
    int r = n - 2;
    while (l

  • @harsh.jain22
    @harsh.jain22 Рік тому +3

    Similar solution but using indexes : Java Solution
    class Solution {
    public int singleNonDuplicate(int[] nums) {
    int n = nums.length;
    int s = 0;
    int e = n-1;

    while(s < e){
    int mid = s + (e-s)/2;
    boolean odd = false;
    boolean even = false;
    if((mid % 2) == 1){
    odd = true;
    }
    if( (mid % 2) == 0){
    even = true;
    }
    boolean isNextEqual = false;
    if(nums[mid] == nums[mid+1]){
    isNextEqual = true;
    }
    if( (odd && isNextEqual) || (even && ! isNextEqual) ){
    e = mid;
    }
    else if( (odd && ! isNextEqual) || (even && isNextEqual) ){
    s = mid+1;
    }
    }
    return nums[s];
    }
    }

  • @riaa55
    @riaa55 12 днів тому +2

    understood

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому +2

    Class solution {
    Public int singlesNon Duplicate (int[]nums){
    int s=0 e=nums.length;
    While (s

    • @Zenetz-le4mq
      @Zenetz-le4mq 11 місяців тому

      class Solution {
      public:
      int singleNonDuplicate(vector& nums) {
      int n = nums.size();
      int ans = 0;

      for(int i = 0;i

  • @doomhead9109
    @doomhead9109 Рік тому +2

    Thank you soo much

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому +2

    2nd approach correction s=mid+2

  • @mereRevyoos
    @mereRevyoos 6 місяців тому +1

    I am watching this solution after attempting Amazon SDE Online Assessment

    • @abhinav2161
      @abhinav2161 3 місяці тому

      Can you guide me for prepation for coding test

  • @utkarshnigam1573
    @utkarshnigam1573 Рік тому +1

    Hii can we do this question with map as well?

    • @abhisheknishad2282
      @abhisheknishad2282 9 місяців тому

      time complexity by using map will become O(n logn) , which will be bigger than O(logn)

  • @satyasanjay1339
    @satyasanjay1339 Рік тому +1

    My solution is also the same but there is little difference. can you please check and tell me if it does make a difference in the performance of the code?
    class Solution {
    public int singleNonDuplicate(int[] nums) {
    int start = 0;
    int end = nums.length-1;
    while(start < end){
    int mid = start + (end-start)/2;
    //Check number of elements on the right side
    boolean isEven = (end-mid)%2 == 0 ? true : false;
    if(nums[mid] == nums[mid+1]){
    if(isEven){
    start = mid+2;
    }
    else{
    end = mid-1;
    }
    }
    else{
    if(isEven){
    if(nums[mid-1] == nums[mid]){
    end = mid-2;
    }
    else{
    return nums[mid];
    }
    }
    else{
    start = mid+1;
    }
    }
    }
    return nums[start];
    }
    }