|
| 1 | +package me.ramswaroop.arrays; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by IntelliJ IDEA. |
| 7 | + * |
| 8 | + * @author: ramswaroop |
| 9 | + * @date: 8/20/15 |
| 10 | + * @time: 10:31 AM |
| 11 | + */ |
| 12 | +public class UnsortedSubArray { |
| 13 | + |
| 14 | + /** |
| 15 | + * Finds the unsorted sub array in array {@param a} such that |
| 16 | + * sorting this sub array makes the entire array sorted. |
| 17 | + * <p/> |
| 18 | + * EXPLANATION: |
| 19 | + * 1) Find the candidate unsorted subarray |
| 20 | + * ...a) Scan from left to right and find the first element which is greater than the next element. Let s be the |
| 21 | + * index of such an element. In the above example 1, s is 3 (index of 30). |
| 22 | + * ...b) Scan from right to left and find the first element (first in right to left order) which is smaller than |
| 23 | + * the next element (next in right to left order). Let e be the index of such an element. In the above example 1, |
| 24 | + * e is 7 (index of 31). |
| 25 | + * 2) Check whether sorting the candidate unsorted subarray makes the complete array sorted or not. If not, then |
| 26 | + * include more elements in the subarray. |
| 27 | + * ...a) Find the minimum and maximum values in arr[s..e]. Let minimum and maximum values be min and max. min and |
| 28 | + * max for [30, 25, 40, 32, 31] are 25 and 40 respectively. |
| 29 | + * ...b) Find the first element (if there is any) in arr[0..s-1] which is greater than min, change s to index of |
| 30 | + * this element. There is no such element in above example 1. |
| 31 | + * ...c) Find the last element (if there is any) in arr[e+1..n-1] which is smaller than max, change e to index of |
| 32 | + * this element. In the above example 1, e is changed to 8 (index of 35) |
| 33 | + * 3) Print s and e. |
| 34 | + * |
| 35 | + * @param a |
| 36 | + * @return |
| 37 | + */ |
| 38 | + public static int[] getUnsortedSubArray(int[] a) { |
| 39 | + int start = 0, end = a.length - 1, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; |
| 40 | + int[] unsortedArray; |
| 41 | + |
| 42 | + // 1(a) |
| 43 | + for (int i = 0; i < a.length-1; i++) { |
| 44 | + if (a[i] > a[i + 1]) { |
| 45 | + start = i; |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // 1(b) |
| 51 | + for (int i = a.length - 1; i > 0; i--) { |
| 52 | + if (a[i] < a[i - 1]) { |
| 53 | + end = i; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // 2(a) - find min and max |
| 59 | + for (int i = start; i <= end; i++) { |
| 60 | + if (a[i] < min) { |
| 61 | + min = a[i]; |
| 62 | + } |
| 63 | + if (a[i] > max) { |
| 64 | + max = a[i]; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // 2(b) |
| 69 | + for (int i = 0; i < start; i++) { |
| 70 | + if (a[i] > min) { |
| 71 | + start = i; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // 2(c) |
| 77 | + for (int i = end + 1; i < a.length; i++) { |
| 78 | + if (a[i] < max) { |
| 79 | + end = i; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + unsortedArray = new int[end - start + 1]; |
| 85 | + for (int i = start, j = 0; i <= end; i++, j++) { |
| 86 | + unsortedArray[j] = a[i]; |
| 87 | + } |
| 88 | + |
| 89 | + return unsortedArray; |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String a[]) { |
| 93 | + System.out.println(Arrays.toString(getUnsortedSubArray(new int[]{10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60}))); |
| 94 | + System.out.println(Arrays.toString(getUnsortedSubArray(new int[]{0, 1, 15, 25, 6, 7, 30, 40, 50}))); |
| 95 | + System.out.println(Arrays.toString(getUnsortedSubArray(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}))); // fully sorted already |
| 96 | + } |
| 97 | +} |
0 commit comments