Computer Science, asked by hanockgamer611, 1 month ago

Write a program to take n integer values in an array, also take an integer from the user as a search variable and check that the search value is present in the array or not using the binary search technique.



no Goo.gle answers and it should be in java language

Answers

Answered by samarthkrv
0

Answer:

import java.util.*;

public class Main

{

   static int search(int[] arr , int key){

       for(int i = 0; i < arr.length; i++){

           if(arr[i] == key){

               return i;

           }

       }

       return -1;

   }

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 System.out.println("Enter the Number of elements in the array-");

 int n = sc.nextInt();

 int[] arr = new int[n];

 System.out.println("Enter all " + n + " elements-");

     for(int i = 0; i < n; i++){

         arr[i] = sc.nextInt();

     }

     System.out.println("Your array is " + Arrays.toString(arr));

     System.out.print("Enter the element you want to find in the array:");

     int x = sc.nextInt();

     System.out.println(search(arr , x));

}

}

Explanation:

Similar questions