Single Element in a sorted array | LeetCode 540 | C++, Java, Python | May LeetCoding Day 12

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

MayLeetCoding Challenge | Problem 12 | Single Element in a Sorted Array | 12 May,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Single Element in a Sorted Array,
Single Element in a Sorted Array c++,
Single Element in a Sorted Array Java,
Single Element in a Sorted Array python,
Single Element in a Sorted Array solution,
find single element in array of duplicates,

#Facebook #CodingInterview #LeetCode #MayLeetCodingChallenge #Google #SingleElement #Amazon #BinarySearh
Рекомендации по теме
Комментарии
Автор

Thanks for great explanation! It would be helpful to draw out two cases each (one below the other) for mid = even, and mid = odd. The two cases are:
1. LHS is OK
2. LHS not OK

charismatic
Автор

Yah Binary search!!! Was right see am learning...i also had an idea of the map technique...can i also use it then keep count as you showed us? Then return the ones with a count of 1 since there maybe multiple elements with a count==1?

williamwambua
Автор

btw amazinggg idea.... nice one sir 👍👍

amruthap
Автор

Question comes in leetcode hard with triplets

Star_Bawa
Автор

int nums) {
long int sum_repeat = 0, sum_nonrepeat = 0;

for(int x:nums)
{
sum_repeat+=x;
}

nums.erase( unique( nums.begin(), nums.end() ), nums.end() );

for(int y:nums)
{
sum_nonrepeat+=y;
}
int result = (sum_nonrepeat*2 - sum_repeat);
return (result);
}


i have used calculations. what is its time complexity ?

amruthap
Автор

Can someone make me understand why are we returning nums[l] at thje end?

abhishakebansal
Автор

i have done it in O(n) and it takes 0 ms
so, i think leetcode time calulation sucks

manishpanwar
Автор

My Solution:
class Solution {
public int singleNonDuplicate(int[] nums) {
int n = nums.length;
if(n==1){
return 1;
}
int c=1;
int prev = nums[0];
for(int i=1; i<n; i++){
if(prev == nums[i]){
c++;
}else{
if(c==1){
return nums[i-1];
}
c=1;
prev = nums[i];
}
}
return prev;
}
}


Is there something in this code which is not needed?

devalmodi
Автор

We can't use maps because of the space complexity constrain....HAHAHAHA

williamwambua
welcome to shbcf.ru