dimanche 6 juin 2021

method that takes a 1d array and key as input and prints the position of the largest number lesser than key. (array can have duplicates) [closed]

class solution{
public static void Met(int[] a,int key)
{
int n=A.length;
int mid=0,top=0;
while(top<=n)
{
mid=(top+n)/2;
if(key<A[0])
{
System.out.print(-1);
return;
}
if(A[0]>=key)
{
System.out.print(-1);
return;
}
else
if(key<A[mid])
if(mid==0)
System.out.println(-1);
else 
n=mid-1;
}
else
{
if(mid==n-1)
{
break;
}
else if(key>=A[mid])
{
top=mid+1;
}
}}}

question : Write a method Met that takes as a parameter a 1D array and also another integer, the key.you have to find the position of the largest number that is lesser than the key,in the array.

the array is sorted but can have duplicates. this is the code I used but I am facing issues with hidden test cases...

Aucun commentaire:

Enregistrer un commentaire