Комментарии:
If the array doesn't contain any missing than
Ответитьmast vide thi, sare methods samajh aa gye. Thanq.
Ответитьsir very good explain thankyou sir ji
ОтветитьSOLUTION : Here is the solution of all these method in javascript
1) Find Missing Number By Hashing :
const findMissingNumberByHashing = (array: number[]) => {
const hash: Record<string, number> = {};
for (let i = 0; i < array.length; i++) {
hash[array[i]] = 1;
}
for (let i = 1; i <= array.length + 1; i++) {
if (!hash[i]) return i;
}
};
findMissingNumberByHashing([1, 3, 4, 5, 6]);
2) Find Missing Number by Gauss's Method for Summing :
const findMissingNumber = (array: number[]) => {
// (Gauss's Method for Summing)
const n = array.length + 1; // 7 (adding 1 because one number is missing in the array)
const sum = (n * (n + 1)) / 2; // 28
const arraySum = array.reduce((acc, curr) => acc + curr, 0); // 21
return sum - arraySum; // 28 - 21 = 7
};
findMissingNumber([1, 2, 3, 4, 5, 6])
3) Find Missing Number By XOR :
const findMissingNumberByXOR = (array: number[]) => {
let x1 = array[0]; // 1
let x2 = 1; // 1
for (let i = 1; i < array.length; i++) { // in this loop we are calculating the XOR of all the elements of the array
x1 = x1 ^ array[i]; // 1 ^ 3 ^ 4 ^ 5 ^ 6 = 3
}
for (let i = 2; i <= array.length + 1; i++) { // in this loop we are calculating the XOR of all the numbers from 1 to n + 1
x2 = x2 ^ i; // 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 1
}
return x1 ^ x2; // 3 ^ 1 = 2
}
findMissingNumberByXOR([1, 3, 4, 5, 6]);
i found out better approach T.C->O(LOGN) (IF ARRAY IS SORTED) WE CAN USE BINARY SEARCH
int s = 0;
int e = N - 1;
while (s <= e) {
int mid = s + (e - s) / 2;
// If the element at index mid is not in its correct position
// i.e., arr[mid] != mid + 1
if (arr[mid] != mid + 1) {
// If arr[mid] is greater than mid + 1 or arr[mid] is negative,
// the missing number is on the left side
if (arr[mid] > mid + 1 || arr[mid] <= 0) {
e = mid - 1;
}
// If arr[mid] is less than mid + 1, the missing number is on the right side
else {
s = mid + 1;
}
}
// If the element is in its correct position, search on the right side
else {
s = mid + 1;
}
}
return s + 1; // The missing number is s + 1
New 2 Method
1.Match the array position no OR
2.find Even or Odd no
can you pls give its source code.. it would be really helpful
Ответитьcode tera baap sikayege kya
Ответитьgreat
ОтветитьThank you sir , Just one question that how can I build my mind to think for the solution in such various approaches
ОтветитьWe can Also Use Cyle Sort
ОтветитьXORing all the elements & then XORing the numbers till n.
And then XORing both tof the obtained results.
This brings us the left out element as XOR of a no with itself is zero(like in binary).
Good video
Ответитьthat was just amazing, before this video i was doing like sorting all the element in the array and finding all the element whether if it is at correct index for not
ОтветитьYrr 7 8 9 11 12 ke liye work nahi karega :)
ОтветитьAs well as please explain code for the problem.
ОтветитьLovely
ОтветитьMethode 2 won't work if 0 present in array
ОтветитьCode for XOR:
//N value=5:
int n;
cin>>n;
//arr=[1,2,3,4]
int arr[n-1],int ans=0;
for(int i=0;i<n-1;i++){
cin>>arr[i];
ans^=arr[i]^(i+1);
}
cout<<ans^n;
I think this one is more efficient, in very few cases you have to iterate over whole array. If array is in order.
for(int i=0;i<arr.length;i++){
if(arr[i] != i+1){
S.o.p.l(i+1);
break;
}
brute better optimal -------> awesome.
ОтветитьSir, kindly explain what is overflow for this case.
ОтветитьThere are lot of numbers in a list
All of them are whole numbers below 100
They are in not any order
Just random whole numbers below 100
We have to find list of numbers which are missing in the series?
Anyone solve it ?
From where n=5 comes ??
Ответитьnote that xor of any number repeats every four digits. so you ca use mod to predetermine the xor of Len(nums). then to find the missing number find the xor of the nums values ONLY. finally return the xor of Len(nums).^ xor of the nums values
Ответитьnice explanation sir
Ответитьuse binary search, time complexity will be logN
ОтветитьGOOD BLOG
ОтветитьSir can you explain the code for XOR method ??
ОтветитьYour voice is same as apna college channel...
Ответитьwill help if the array is in order
function missingNum(arr) {
for(i=arr.length-1;i>=0;i--) {
if(arr[i] - arr[i-1] == 2) {
return arr[i]
}
}
return -1;
}
Can you please share the code of the third approach?
ОтветитьXOR one is really cool . I tried with -negative value and array is not sorted still working perfect . Thank a lot sir ji
ОтветитьGreat video ! 🙌 I understand the XOR operation by this video.
ОтветитьSir, what about the time it takes to make the second array(the complete one)?
ОтветитьSort the array then
For(int i=0;i<n;i++)
if(arr[i]!=i+1)
printf("%d",i+1);
Your explaining level ....😍😍😍😍
Ответитьin second method the n=4 not 5
Ответитьnice solution!
ОтветитьMethod 2 sum formula not work for all
As if 1, 2,5 now answer must be 3,4 but using tis formula we get 7 wrong
How to find more then one missing element???
Ответитьfind missing number in arere
ОтветитьWrite a Swift program to check if one of the first 4 elements in a given array of integers is a 7. The length of the array may be less than 4. PLS MAKE VIDEO ON IT
ОтветитьThanks! for the video
ОтветитьFor i=0 to n-1
if(arr[i]!=i+1)
return i+1;
Can’t we just: 1. initialise counter to array’s zero-th element. 2. Iterate through array increment this counter, wherever the counter and array element does not match - is the answer.
ОтветитьNumber of terms toh aapne 4 le rkhi hai likha 5 hai
ОтветитьIf I subtract numbers instead of xor-ing , then will it be as efficient as the xor solution?
i.e for the array 1 2 3 5
(1-1) + (2-2) + (3-3) + (4-5) + (5) = 4
Thanks
Ответить