public class MyClass {
public static void findPreviousSmaller(int arr[]) {
int size = arr.length;
for (int i = 0; i < size; i++) {
int not_found = 1; // if not found write -1
int previous_smaller = arr[i];
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < previous_smaller) {
previous_smaller = arr[j];
not_found = 0;
}
}
System.out.print(((not_found == 1) ? -1 : previous_smaller) + " ");
}
}
public static void main(String args[]) {
int arr[] = { 2, 6, 3, 7, 8, 1, 9, 0, 13, 19, 18 };
// 2:-1 // 6:2 // 3:2 // 7:2 // 8:2 // 1:-1 // 9:1 // 0:-1 // 13:0 // 19:0 // 18:0
findPreviousSmaller(arr);
}
}
/*
run:
-1 2 2 2 2 -1 1 -1 0 0 0
*/