-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementInSortedArray.java
63 lines (53 loc) · 1.94 KB
/
ElementInSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.datastructure;
import java.util.Scanner;
public class ElementInSortedArray {
static int CountMinimumElementIndex(int array[], int target) {
int start = 0;
int n = array.length;
int end = n - 1;
//Iterative binary search
while(start<=end)
{
int mid = (start+end)/2;
//cout<<nums[mid]<<"\n";
if(array[mid]==target)
return mid;
else if(array[mid]>=array[start]) //check in the left side
{
if(target<=array[mid] && target>=array[start])
end = mid-1;
else
start = mid+1;
}
else
{
if(target>=array[mid] && target<=array[end]) //check in the right side
start = mid+1;
else
end = mid-1;
}
}
return -1;
}
public static void main(String[] args) {
// int arr[] = {15, 18, 2, 3, 6, 12};
Scanner s = new Scanner(System.in); //new instance and calling the input func
System.out.println("Enter the length of the array:");
int length = s.nextInt(); //defining size and getting the input
int[] array = new int[length]; // defining array of length provided
System.out.println("Enter the elements of the array:");
for (int i = 0; i < length; i++) {
array[i] = s.nextInt();
}
// int target = s.nextInt();
int n = array.length;
System.out.println("Enter the number to search:");
int target = s.nextInt();
int index = CountMinimumElementIndex(array, target);
if (index != -1) {
System.out.println("Element found at index " + index);
} else {
System.out.println("Element not found in the array");
}
}
}