Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Last active May 3, 2021 13:35
Show Gist options
  • Select an option

  • Save Ram-1234/0395a6b330880cbae79666bbf47f08ba to your computer and use it in GitHub Desktop.

Select an option

Save Ram-1234/0395a6b330880cbae79666bbf47f08ba to your computer and use it in GitHub Desktop.
find index position a array element is place in sorted array without sorted order disturbance
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int size1=sc.nextInt();
int key=sc.nextInt();
int arr1[]=new int[size1];
for(int i=0;i<size1;i++){
arr1[i]=sc.nextInt();
}
int index=0;
for(int i=0;i<size1-1;i++){
if(key>=arr1[i] && key<arr1[i+1]){
index=i+1;
}
}
System.out.println(index+1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment