+ 2

Find the missing number in an integer array of 1 to 10. For eg :- I/p - 1,2,4,5,6,8,9,10 o/p- 3 and 7

Use array in java

24th Nov 2020, 1:41 PM
Ritika Singh
Ritika Singh - avatar
3 Respostas
+ 4
Your attempt first.. Pls try and share you attempt....
24th Nov 2020, 1:45 PM
Jayakrishna 🇮🇳
+ 2
I tried for one missing element , that too an error is occurring import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the limit"); int n=sc.nextInt(); int a[]=new int[n]; int sum=0; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); System.out.print(a[i]+","); sum=sum+a[i]; } System.out.println(); int totalsum=(n*(n+1))/2; int missing=totalsum-sum; System.out.println(missing); } }
24th Nov 2020, 2:55 PM
Ritika Singh
Ritika Singh - avatar
+ 2
I don't find any error in Program. But You are taking array size, then elements. Finding sum of array elements and subtracting it from (sum of n sequence number).. This approach i don't find 'how it find missing elements'? According to your question, in a simple way you can do like : you need find 1 to 10 numbers so no need to take input of array size, declare array of size 10. And next just take inputs, if input is in range 1 to 10 then set array[input-1] = 1 and next with a loop you can find missing elements where array[i] ==0. Output i+1. You can also do with taking inputs and next with 2 loop, checking if array contains 1 to 10 are not.. There are alternatives but the those may take more loops or comparisons... Hoping it helps.. Reply if you have any difficulty in getting it solved..
24th Nov 2020, 9:57 PM
Jayakrishna 🇮🇳