- 3
Missing number program in java
11 Respostas
+ 8
Vijay Maurya ,
▪︎before we are going to help you, please present your attempt here.
if you haven't done a try by yourself so far, please do so. create your code in playground and post the link to it here.
▪︎please also provide a proper task description with sample of input and output.
▪︎if your question is related to a code coach exercise, please mention the tutorial name and the exercise number.
thanks!
+ 1
Missing brain post in Q&A.
+ 1
Hi, please link your code attempt so we can help you to solve it!
Simon Sauter ++🧠;
+ 1
System.out.println("Missing Number");
Done.
+ 1
Vijay Maurya
Write in same code so we can debug problem.
+ 1
I have add swap code now you can check
0
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {0,2};
sort(arr);
System.out.println(Arrays.toString(arr));
}
static int sort(int[] arr){
int i=0;
while(i < arr.length){
int correct = arr[i] - 1;
if(arr[i]< arr.length && arr[i] != arr[correct]){
swap(arr , i ,correct);
}
else{
i++;
}
}
for (int index = 0; index <arr.length ; index++) {
if(arr[index] != index){
return index;
}
}
return arr.length;
}
static void swap(int arr[] ,int first, int second){
int temp =arr[first];
arr[second] =arr[first];
arr[second] =temp;
}}
0
This is my code
0
Vijay Maurya
Where is swap method in your code and also why you are returning length of arr when you didn't assign to a variable to use that.
0
Swap method is used from other code
0
Vijay Maurya
You can use Arrays.sort() to sort an array.
https://code.sololearn.com/cOTg0k8v5b3S/?ref=app