How to find Missing Number In Array - Java Interview Question -4

preview_player
Показать описание
Learn How to find Missing Number In Array:

You can do this in O(n). Iterate through the array and compute the sum of all numbers. Now, sum of natural numbers from 1 to N, can be expressed as Nx(N+1)/2.

Subtract the sum of the array from Nx(N+1)/2.

That is the missing number.

=====================================================
Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion:

-~-~~-~~~-~~-~-
========================================================
Please watch: "Selenium & Automation Interview Preparation - By Naveen AutomationLabs"
-~-~~-~~~-~~-~- Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

Paid courses (Recorded) videos:
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------

✔️SOCIAL NETWORKS
--------------------------------

Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة ➡️Get Our Courses✔️
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------

✔️SOCIAL NETWORKS
--------------------------------

Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
Рекомендации по теме
Комментарии
Автор

Hi Naveen,
What if u want to print two or more missing numbers in an array when there is no declaration and if variables(user defined) are taken at runtime. Please explain this scenario?. And thanks for ur knowledge sharing on the above video.

NaveenKumar-tpli
Автор

This is brute force method. As an optimization you can also use (n*(n+1))/2 - sum of first n natural numbers, to get that one missing number. Also the array has to be sorted and contain only natural numbers.

ssandeep
Автор

Hi Naveen, thanks for the code. If we missed more than one number, we will not get output right?

RanjithKumar-mzij
Автор

Hi Naveen, what if there is two missing number in array?. How we can calculate, can you explain.

kuldeepsikka
Автор

instead of 2 arrays we can also make use of series sum formula and 1 loop:

private static int missingElements(int[] x, int no) {

int a1 = 1;
int an = no;
int expectedSum = (no * (a1 + an)) / 2;

int actualSum = 0;

for (int i = 0; i < x.length; i++) {

actualSum=actualSum+x[i];

}

return expectedSum - actualSum;
}

swordrohit
Автор

Great work Naveen!!
Instead of changing the values in for loop we can use like this
package Test;

public class Learn {

public static void main(String args[]){

int a[]={100, 101, 103, 104, 105};
int sum=0;
int sum1=0;
for(int i=0;i<a.length;i++){
sum=sum+a[i];
}
int firstValue=a[0];
int last=a.length-1;
int lastValue=a[last];
for(int
sum1=sum1+j;
}
System.out.println("the missing number is"+"-"+(sum1-sum));
}

}

nandiniarumugam
Автор

Good one!

below is the solution where it's not required to use 2 for loops:
Integer[] arr = new Integer[]{2, 3, 4, 6, 7};
Integer missingNum=null;
for(int i=0; i<arr.length; i++)
{
int a = arr[i];
int b = arr[i+1];
if(b-a!=1)
{
missingNum=a+1;
break;
}
}

shivarajun
Автор

Hi Naveen Sir
I have attended the interview today for cognizant there they asked one java program question that is


There is array a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

But in this array we don't have no. Called 11 so he said we have to confirm that this 11 is present or not
I feel its a tricky question can you give a video session on this java program

jayeshkumar
Автор

N=a.length+1
Osum=N(N+1)/2;
Missing=Osum-Sum

adityasrinivas
Автор

public class missingnumber
{

public static void main(String[] args)
{
int arr[]= {1, 2, 3, 4, 5, 6, 8, 9, 10};
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=i+1)
{
System.out.println(i+1);
return;
}
}
}

}

ravish
Автор

Great tutorials. Your logic won't work when 0 is missing in an array starting from -1 to like 10 .. below will work
public static void FindMissingNumInArray(int arr1[]){
for (int i=1;i<arr1.length;i++){
if (arr1[i]!=arr1[i-1]+1){
System.out.println("missing num is:"+ (arr1[i-1]+1));
}
}

}

vibhakhandelwal
Автор

Naveen, thanks for the videos. What if there are 2 or more missing numbers in the array how would you do it?

vaishd
Автор

Hi Naveen, Nicely break down and put together. Allow me to contribute to extend the search of missing number from any given sequence. I look forward to hear from you at your earliest convenience.

public static void main(String[] args) {
int a[] = {55, 56, 57, 59, 60}; // 58 is missing.

int sum = 0;
int sum1 = 0;

arrLen = a.length; // length of integer array.

for (int i = 0; i < arrLen; i++) {
sum += a[i];
// similar to :- sum = sum + a[i];
}
System.out.println("sum =" + sum);

for (int j = a[0]; j <= a[arrLen -1]; j++) {
sum1 += j;
// similar to :- sum1 = sum1 + j;
}
System.out.println("sum1 = " + sum1);



// Print the missing number :
System.out.println("Missing Number = " + (sum1 - sum));

}


Best Regards,
Ambarish.

ambarishlg
Автор

Well explanation Naveen thank you very much, I have question in second for loop you gave j=1, not sure why i am thinking 0 because index start from 0 right like first loop?

parshu
Автор

Thanks for sharing good questions, what if there are more than one missing numbers in the array?

baluselenium
Автор

I used arrays.binarysearch to reduce the time complexity. Works for multiple missing numbers in sorted order. int [] a = {2, 3, 4, 6, 7, 8};
int searchnumber = a[0];
for (int i = 0; i < a.length; i++) {
int found = Arrays.binarySearch(a, searchnumber);
if (found <0 ) {
number : "+searchnumber);
break;
}
searchnumber = a[i]+1;
}

nikhilbhandarkar
Автор

Hi Naveen,
Below code can check for multiple missing numbers
public static void main(String[] args) {
int [] arr = {-2, 0, 1, 2, 4, 5, 6, 8, 9, 11};
int t=arr[0]+1; // Get the first number in array

for(int i=1; i<arr.length; i++){
if(t==arr[i]){
t= arr[i]+1;
}else{
System.out.println("Missing :"+t);
t= arr[i]+1;
}
}
}

pradeepb
Автор

Hi Naveen,

Thanks for sharing. what if we asked to write a program for more than one missing numbers? do they ask such questions? if so what would be the logic.
thanks in advance

srividyan
Автор

Hi Naveen,
Wonderful explanation! Also can you start with alogirthms and Data structures in Java sessions, which will be helpful for many of us.

durgaponnapalli
Автор

Why don't you use arithmetic progression formula instead of running for loop.good explanation by the way

A
join shbcf.ru