pls explain what is wrong with m code ?
Failed test cases after compilation
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] x = new int[n]; for( int i = 0; i < n ; i++){ x[i] = sc.nextInt(); } int c = Search(m,x); if ( c!= -1) System.out.println(x[c]); } public static int Search( int M , int[] arr ){ int l = 0, h = arr[arr.length -1], m = (arr.length)/2; while( l<=h ){ if(arr[m] == M){ return(m); } else if (arr[m] < M){ l = m + 1; m = (l + h)/2; } else{ h = m - 1; m = (l + h)/2; } } return -1; } }
Hi ,
First line of code in Search() function has problem.
Int l=0, h= are.length-1, m= (l+h)/2
Thanks
how to generate the link for the code on coding blocks
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
still 2 test cases are failing
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] x = new int[n];
for( int i = 0; i < n ; i++){
x[i] = sc.nextInt();
}
int m = sc.nextInt();
System.out.println(Search(m,x));
}
public static int Search( int M , int[] arr ){
int l = 0, h = arr.length -1, m = (arr.length)/2;
while( l<=h ){
if(arr[m] == M){
return(m);
}
else if (arr[m] < M){
l = m + 1;
m = (l + h)/2;
}
else{
h = m - 1;
m = (l + h)/2;
}
}
return -1;
}
}
you have to array input first then the value of m
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
i have given the link of my vode…can you pls explain why my code is failing
2 test cases are failing
@himanshuep32
INSTEAD OF THIS
int n = sc.nextInt();
int m = sc.nextInt();
int[] x = new int[n];
for( int i = 0; i < n ; i++){
x[i] = sc.nextInt();
}
System.out.println(Search(m,x));
DO THIS
int n = sc.nextInt();
int[] x = new int[n];
for( int i = 0; i < n ; i++){
x[i] = sc.nextInt();
}
int m = sc.nextInt();
System.out.println(Search(m,x));