-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr04.java
61 lines (52 loc) · 1.36 KB
/
arr04.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
//question
//given an sorted Array. find the starting and ending
//position of a given target value.
//if target is not found then return [-1, -1]
// i/p=> arr[]= {5,7,7,8,8,10}
// given target = 8;
// output =[3,4]
package Array;
public class arr04 {
//programme for findinig first occurance of target element
public static int firstOccuranceIndex(int arr[] , int target) {
int si = 0;
while(si < arr.length) {
if(arr[si] == target) {
return si;
}
si++;
}
return -1;
}
//programme for finding last occurance of target element
public static int lastOccuranceIndex(int arr[] , int target) {
int ei = arr.length-1;
while(ei >= 0) {
if(arr[ei] == target) {
return ei;
}
ei--;
}
return -1;
}
//search finding range and fill the array and return
public static int[] searchRangeArr(int arr[] , int target) {
int ansArr[] = new int[2];
ansArr[0] = firstOccuranceIndex(arr, target);
ansArr[1] = lastOccuranceIndex(arr, target);
return ansArr;
}
//function for print Arr
public static void printArr(int arr[]) {
System.out.println("your required array =>");
for(int i=0; i< arr.length; i++) {
System.out.print(arr[i] +" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {5,7,7,8,8,10};
int ansArr[] = searchRangeArr(arr, 8);
printArr(ansArr);
}
}