Single /Multiple Missing Number In Array

Single /Multiple Missing Number In Array

Code Upskill

3 года назад

15,047 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@dakshgupta305
@dakshgupta305 - 06.09.2023 20:43

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

}
}

Ответить
@techieadi4377
@techieadi4377 - 22.08.2023 12:57

Very very nice explain

Ответить
@user-wg9ov5hn9b
@user-wg9ov5hn9b - 25.08.2022 10:00

//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

Ответить
@saadshirgaonkar9576
@saadshirgaonkar9576 - 16.08.2022 10:02

thanks bro

Ответить
@rohitmiryala8543
@rohitmiryala8543 - 09.11.2021 19:03

out of bond Excepton !!

Ответить
@sagargudi47
@sagargudi47 - 29.10.2021 16:42

if i give some other input it shows ArrayOutOfBondException. strange !!!!

Ответить
@naturalbeauty5858
@naturalbeauty5858 - 09.05.2021 07:19

Nice

Ответить