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

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 73

  • @shivarajun3787
    @shivarajun3787 ปีที่แล้ว +3

    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

  • @ravish851
    @ravish851 9 หลายเดือนก่อน +2

    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

  • @NaveenKumar-tp4li
    @NaveenKumar-tp4li 4 ปีที่แล้ว +5

    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.

  • @nandiniarumugam9772
    @nandiniarumugam9772 7 ปีที่แล้ว +4

    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

    • @naveenautomationlabs
      @naveenautomationlabs  7 ปีที่แล้ว +4

      yes u can do that. I have taken this example just to explain the concept. U can remove the hard coded values in the second loop.

    • @love_soni
      @love_soni 6 ปีที่แล้ว

      What if the list is not sorted?

    • @parvathygangadharan7144
      @parvathygangadharan7144 5 ปีที่แล้ว +2

      But does this work if boundary values are missing?

    • @bharathkoneru4008
      @bharathkoneru4008 4 ปีที่แล้ว

      This code doesn't work for the boundary values!

  • @pradeepb6914
    @pradeepb6914 6 ปีที่แล้ว +3

    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

    • @praveengarigipati8775
      @praveengarigipati8775 6 ปีที่แล้ว +1

      Hi Pradeep, simple we can write down program like below
      //Program to identify missing multiple numbers
      int[] nums = {1,2,4,5,7,9,10};
      for(int i=0;i

    • @sumajachaganti
      @sumajachaganti 6 ปีที่แล้ว

      Hi,But if two or more numbers are missing one after the other,all the missing numbers are not getting printed like eg: int[] nums = {-3,0,1,2,4,5,7,9,10} . here,-1 is not getting printed.

  • @swordrohit07
    @swordrohit07 3 ปีที่แล้ว +1

    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;
    }

  • @ssandeep79
    @ssandeep79 6 ปีที่แล้ว +6

    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.

    • @sivanesansomanathapuramdas8086
      @sivanesansomanathapuramdas8086 6 ปีที่แล้ว

      i ,agree with you @ssandeep79 your method is correct ! :)

    • @shraddhaohal7704
      @shraddhaohal7704 2 ปีที่แล้ว

      Read the comments only to see if someone already gave the formula for sum of n natural numbers. Was not disappointed 😁

  • @vaishd
    @vaishd 8 วันที่ผ่านมา

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

  • @soudiptadutta6886
    @soudiptadutta6886 6 ปีที่แล้ว +2

    Use XOR Operation. Here is the code:
    int[] a = {100,101,103,104,105,102,107};
    int res = a.length ;
    for(int i = 0 ;i

  • @RanjithKumar-mz8ij
    @RanjithKumar-mz8ij 5 ปีที่แล้ว +3

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

  • @baluselenium3516
    @baluselenium3516 4 ปีที่แล้ว +3

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

  • @vibhakhandelwal2881
    @vibhakhandelwal2881 6 ปีที่แล้ว +6

    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

    • @raghuveermh6869
      @raghuveermh6869 5 ปีที่แล้ว

      Hi, I have tried it is working

    • @ijaz8608
      @ijaz8608 5 ปีที่แล้ว

      for negative number u cant use this method
      try this
      private static void printMissingNumber(int[] numbers, int count) {
      int missingCount = count - numbers.length;
      BitSet bitSet = new BitSet(count);

      for (int number : numbers) {
      bitSet.set(number - 1);
      }

      System.out.printf("Missing numbers in integer array %s, with total number %d is %n",
      Arrays.toString(numbers), count);
      int lastMissingIndex = 0;
      for (int i = 0; i < missingCount; i++) {
      lastMissingIndex = bitSet.nextClearBit(lastMissingIndex);
      System.out.println(++lastMissingIndex);
      }

      }

    • @mustapharaimilawal8053
      @mustapharaimilawal8053 5 ปีที่แล้ว

      Hello, your code works but can you please give some explanation of your logic, i can see it in the code but please explain.Thanks.

    • @abhifast555
      @abhifast555 5 ปีที่แล้ว +1

      Your code fails when the numbers are not sorted

    • @jeyhunaliyev1765
      @jeyhunaliyev1765 3 ปีที่แล้ว

      @@abhifast555
      Arrays.sort(arr1);
      will solve the problem
      but if there negative numbers
      wont work
      only with positive numbers

  • @kuldeepsikka8006
    @kuldeepsikka8006 5 ปีที่แล้ว +4

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

  • @ambarishlg
    @ambarishlg 5 ปีที่แล้ว +4

    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

    • @ramya92111
      @ramya92111 4 ปีที่แล้ว

      this doesn't work for boundary values

  • @JagdishOdedra
    @JagdishOdedra 5 ปีที่แล้ว +2

    Hi Naveen, Thanks for wonderful tutorial.I have a question
    How to find missing number from unsorted array?

    • @ijaz8608
      @ijaz8608 5 ปีที่แล้ว +2

      you can use this same method for an unsorted array as well
      because here we are only calculating the difference between sums

  • @9675A
    @9675A 5 ปีที่แล้ว +1

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

  • @sushkund4198
    @sushkund4198 5 ปีที่แล้ว

    Hi Naveen,
    In this video i got to know how to find one missing number. If there are multiple numbers are missing what logic we need to follow?

    • @thespicyspicey
      @thespicyspicey 3 ปีที่แล้ว

      Set the starting index as x = 1.
      Check whether the previous number array[x-1] is less than of the current number array[x] by 1.
      If it's not lesser by 1.. then subtract the value at array[x] by array[x-1]. You will get the a number.
      If the number is 2, then the missing number must be array[x]-1 or array[x-1]+1. In other words.. it's 1 step lesser than the number on the right or 1 step greater than the number on the left.
      Suppose if there are multiple numbers i.e the difference gives more than 2.
      1,2,6,7
      6-2 = 4
      Consider 4 as n.
      Which means there are 3 (n-1 -> 4-1) elements missing between 2 and 6.
      Those three elements are 3,4,5.

  • @parshu3238
    @parshu3238 4 ปีที่แล้ว +1

    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?

    • @parshu3238
      @parshu3238 4 ปีที่แล้ว

      @@vinaykumarsharma9493 thanks lot Vinaykumar got it it's just number count

    • @parshu3238
      @parshu3238 4 ปีที่แล้ว

      @@vinaykumarsharma9493 hmm right thanks Vinay :)

  • @nikhilbhandarkar3322
    @nikhilbhandarkar3322 4 ปีที่แล้ว

    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

  • @srividyan7979
    @srividyan7979 7 ปีที่แล้ว +2

    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

  • @durgaponnapalli2971
    @durgaponnapalli2971 5 ปีที่แล้ว

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

  • @maheshglobal6349
    @maheshglobal6349 5 ปีที่แล้ว +1

    Please include Anagram Program in interview programs

  • @anupamachaudhary3990
    @anupamachaudhary3990 6 หลายเดือนก่อน

    Very helpful

  • @drmangrum
    @drmangrum 5 ปีที่แล้ว +1

    Assumes values are assumed sorted and hard to read:
    public static void main(String [] args) {
    int a[] = {1, 2, 3, 4, 6, 7};
    Collection values = new HashSet();
    for (int i : a) {
    values.add(i);
    }
    int max = Arrays.stream(a).max().getAsInt();
    int min = Arrays.stream(a).min().getAsInt();
    for (int i = min; i < max; i++) {
    if (!values.contains(i)) {
    System.out.println(i + " is missing.");
    }
    }
    }

  • @kuldeepsikka8006
    @kuldeepsikka8006 5 ปีที่แล้ว

    Hi Naveen,

  • @deepakmikkilineni9329
    @deepakmikkilineni9329 6 ปีที่แล้ว +1

    if i want to write code for two or three missing letters then whats was the logic

  • @abhijeets6593
    @abhijeets6593 4 ปีที่แล้ว +1

    how to find if more than 1 number is missing ?

  • @varaprasadchennuboyina1171
    @varaprasadchennuboyina1171 5 ปีที่แล้ว

    Please add trailing of white space. . .

  • @nitingupta82
    @nitingupta82 5 ปีที่แล้ว

    Thank you very much!!

  • @Ewwww167
    @Ewwww167 หลายเดือนก่อน

    what if the 2 or 3 numbers will be miss at same time means...?

  • @RebazCh
    @RebazCh 6 ปีที่แล้ว

    Thanks You , I Understand 😍

  • @meeneshrajput2128
    @meeneshrajput2128 8 หลายเดือนก่อน

    How to find different missing number in java please write this program sir

  • @AshishDubeyash4you
    @AshishDubeyash4you ปีที่แล้ว

    Easiest way & this will print more than one missing numbers as well.
    package arrays;
    import java.util.HashSet;
    import java.util.Set;
    public class findMissingNumber {
    public static void main(String[] args) {
    int [] abc = {1,2,4,7};
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    Set seen = new HashSet();
    for(int num:abc)
    {
    seen.add(num);
    min = Math.min(min,num);
    max = Math.max(max,num);
    }
    for(int i=min;i

  • @deepakkhatri1995
    @deepakkhatri1995 5 ปีที่แล้ว +1

    two number is missing thn what will we do ? like 3 and 5

    • @MultiPrasad99
      @MultiPrasad99 5 ปีที่แล้ว

      See this
      sc = new Scanner(System.in);
      String input []= new String[5];
      int inputNum []= new int[5];
      int smallestNum=0;

      System.out.println("Enter the 5 numbers:");
      for(int i=0; i

  • @md.irshad8933
    @md.irshad8933 5 ปีที่แล้ว

    Thank you boss

  • @syedalam4729
    @syedalam4729 5 ปีที่แล้ว

    why did you start second loop j

  • @lakshmich3208
    @lakshmich3208 7 ปีที่แล้ว

    Hi Naveen ji, if two or more numbers are missing in an array..how can we find out those missing numbers..

    • @kamalpandey3132
      @kamalpandey3132 6 ปีที่แล้ว +1

      Hi Lakshmi, did you get any luck on this? Plz share if so

  • @rajasekharkoperla6031
    @rajasekharkoperla6031 7 ปีที่แล้ว +5

    how can u find two numbers is missing?

    • @lokeshsharma-gd1xo
      @lokeshsharma-gd1xo 6 ปีที่แล้ว

      I think this Solution is not generic, Let say if user forget to add 2 no instead of single no, then this solution is failed, Plz fix this video a{1,2,3,5,7} ex 4 and 6 were missing

    • @nikhiltillu6201
      @nikhiltillu6201 6 ปีที่แล้ว

      int arr[] = {1,2,3,4,5,6,8,10,11,12,13,15};

      int len = arr.length;
      int j = 1;

      for(int i = 0; i< len; i++)
      {
      if(arr[i] != j)
      {

      System.out.println(j);
      j++;
      }
      j++;
      }

    • @ijaz8608
      @ijaz8608 5 ปีที่แล้ว

      use this method
      private static void printMissingNumber(int[] numbers, int count) {
      int missingCount = count - numbers.length;
      BitSet bitSet = new BitSet(count);

      for (int number : numbers) {
      bitSet.set(number - 1);
      }

      System.out.printf("Missing numbers in integer array %s, with total number %d is %n",
      Arrays.toString(numbers), count);
      int lastMissingIndex = 0;
      for (int i = 0; i < missingCount; i++) {
      lastMissingIndex = bitSet.nextClearBit(lastMissingIndex);
      System.out.println(++lastMissingIndex);
      }

      }

  • @robinkedia
    @robinkedia 7 ปีที่แล้ว

    This will only work if numbers are from 1 to x. Not like 20, 21, 22, 23, 24, 25.

    • @LeonardoDiasdeOliveira
      @LeonardoDiasdeOliveira 6 ปีที่แล้ว +1

      This code works with your example:
      In this case the missing number is 23
      public static void main(String[] args) {
      final int[] n = {20, 21, 22, 24, 25};
      int sum1 = Arrays.stream(n).sum();
      int sum2 = IntStream.rangeClosed(n[0], n[n.length - 1]).sum();
      System.out.println(sum2 - sum1);
      }

    • @kshitijshrivastava9881
      @kshitijshrivastava9881 6 ปีที่แล้ว

      It works. You have to manipulate the loops:
      int a[] = {22,23,25,26,27,28,29,30};

      //find sum of array elements

      int sum=0;
      for(int i=0;i

  • @hmdraza
    @hmdraza 4 ปีที่แล้ว

    What if 0 is missing?

  • @oscaribanez3622
    @oscaribanez3622 5 ปีที่แล้ว

    Doesnt work with zeros

  • @shankarpk89
    @shankarpk89 5 ปีที่แล้ว

    /* MULTIPLE MISSING NUMBERS AND DUPLICATES IN THE ARRAY */
    package javaInterview;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    public class FindMissingNumbers {
    static int i = 0;
    static ArrayList al = new ArrayList();
    public static void main(String[] args) {
    Integer[] numArr = { 100, 100, 90, 101, 90, 91, 92, 98, 92 };
    al = FindMissingNumbers.findMissingNumbers(numArr);
    System.out.println("MISSING NUMBERS ARE");
    System.out.println("--------------------");
    for (Integer i : al) {
    System.out.println(i);
    }
    }
    public static ArrayList findMissingNumbers(Integer arr[]) {
    Set sObj = new HashSet(Arrays.asList(arr));
    List lObj = new ArrayList(sObj);
    Collections.sort(lObj);
    Integer iSmallest = lObj.get(0);
    Integer iLargest = lObj.get(lObj.size() - 1);
    for (int i = iSmallest + 1; i

  • @sanketh768
    @sanketh768 3 ปีที่แล้ว +1

    What if multiple numbers are missing in the sequence?

  • @robinkedia
    @robinkedia 7 ปีที่แล้ว

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] numbers = new int[] { 211, 212, 213, 214, 215, 217, 218, 219};
    findMissingNumber(numbers);
    }
    //Write a java program to find missing number in an array?
    public static void findMissingNumber(int[] numbers) {
    int arrayTotal = 0;
    int missingTotal = 0;
    int min = 0;
    int max = 0;
    for(int i=0;i numbers[i]) {
    min = numbers[i];
    }
    if(max < numbers[i]) {
    max = numbers[i];
    }
    arrayTotal = arrayTotal + numbers[i];
    }
    for(int i=min;i