public class MyClass {
public static void find_first_and_Last_position(int arr[], int x) {
int len = arr.length;
int first = -1, last = -1;
for (int i = 0; i < len; i++) {
if (x != arr[i]) {
continue;
}
if (first == -1) {
first = i;
}
last = i;
}
if (first != -1){
System.out.println("First positions = " + first + " Last positions = " + last);
}
else
System.out.println("Not Found");
}
public static void main(String args[]) {
int arr[] = {1, 3, 7, 8, 3, 1, 9};
int n = 3;
find_first_and_Last_position(arr, n);
}
}
/*
run:
First positions = 1 Last positions = 4
*/