Комментарии:
This code fails for several test cases
.
.
correct code ------>
import java.util.*;
import java.lang.*;
class prob{
public static void main(String[] args) {
int[] arr = {1,1,2,3,5,5,7,9,9,9};
int M=Integer.MIN_VALUE;
for(int i:arr){
if(i>M){
M=i;
}
}
int[] temp=new int[M];
for (int j : arr) {
temp[j-1] = 1;
}
for(int j=0;j<temp.length;j++){
if(temp[j]==0){
System.out.println(j+1);
}
}
}
}
Very very nice explain
Ответить//Program to find missing number from the array//
import java.io.*;
public class Missnum
{
public static void main(String[] args)
{
int a[]=new int[100];
int i,lim,result=0,sum=0,mnum=0;
try
{
DataInputStream R=new DataInputStream(System.in);
System.out.println("Enter a limit:-");
lim=Integer.parseInt(R.readLine());
System.out.println("Enter some numbers:-");
for(i=0;i<lim;i++)
{
a[i]=Integer.parseInt(R.readLine());
}
for(i=0;i<lim;i++)
{
result=a[i]*(a[i]+1)/2; /*if a[i] is (1,2,3,5) then here the last value 5 is taken then
5*(5+1)/2 answer is 15 and store it in result*/
sum=sum+a[i]; /*then find sum of all user entering numbers like 1+2+3+5 and store
answer in sum*/
mnum=result-sum; /*then finally find missing number by subtracting the value of result that is 15
with the value in sum that is 11 and we got 4 and store it in mnum that is
missing number*/
}
System.out.println("Here is the missing number:-"+mnum); //And here show the final result to user//
}
catch (Exception e)
{
}
}
}
Output
Enter a limit:-
4
Enter some numbers:-
1
2
3
5
Here is the missing number:-4
thanks bro
Ответитьout of bond Excepton !!
Ответитьif i give some other input it shows ArrayOutOfBondException. strange !!!!
ОтветитьNice
Ответить