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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package DS; //https://leetcode.com/problems/search-insert-position/description/ //Given a sorted array and a target value, return the index if the target is found. //If not, return the index where it would be if it were inserted in order. public class InsertingElementInSortedArray { public static void main(String[] args) { // TODO Auto-generated method stub InsertingElementInSortedArray o = new InsertingElementInSortedArray(); int[] arr = { 1, 2, 5, 6, 10, 12, 14, 15, 16 }; o.doMethod(arr, 3); } void doMethod(int[] arr, int key) { int low = 0; int high = arr.length; int mid = (low + high) / 2; if (arr.length == 0) { return; } if (key < arr[mid]) { sequentialSearch(low, mid, arr, key); } if (key > arr[mid]) { sequentialSearch((mid), high, arr, key); } } void sequentialSearch(int low, int high, int[] arr, int key) { for (int i = low + 1; i < high; i++) { if (key == arr[i - 1]) { System.out.println("found at: " + i); break; } if (key == arr[i]) { System.out.println(" Found at: " + i); break; } if ((key < arr[i]) && (key > arr[i - 1])) { System.out.println(" Should be between: " + arr[i - 1] + " " + arr[i]); } } } } // Output: Should be between: 2 5 ================================= public class InsertingElementInSortedArrTREEMAP { public static void main(String[] args) { // TODO Auto-generated method stub InsertingElementInSortedArrTREEMAP o = new InsertingElementInSortedArrTREEMAP(); int[] arr = { 2, 4, 5, 6, 7, 10, 20, 30 }; o.usingTreeMap(arr, 22); } void usingTreeMap(int[] arr, int cc) { Map map = new TreeMap(); Integer count = 0; // int stream or Array.stream= java.util.Arrays.stream(int[]) // boxed= java.util.stream.IntStream.boxed() // : := Integer[] java.util.function.IntFunction.apply(int value) // STREAMS: https://www.geeksforgeeks.org/streams-arrays-java-8/ // https://www.youtube.com/watch?v=gFu3TJhog7E Integer[] what = Arrays.stream(arr).boxed().toArray(Integer[]::new); // important for (int i : what) { map.put(i, count); count++; } System.out.println(map.toString()); System.out.println("========================"); if (map.containsKey(cc)) { System.out.println(map.get(cc)); } else { map.put(cc, count); System.out.println("after adding: " + map); System.out.println(" ============================"); if (map.containsKey(cc)) { System.out.println("Element shoud be present between Vales: " + (map.get(cc) - 1) + " index and " + (map.get(cc) + 1)); } } } } // Method: 2 Logic by Santosh // Output: // {2=0, 4=1, 5=2, 6=3, 7=4, 10=5, 20=6, 30=7} ======================== // after adding: {2=0, 4=1, 5=2, 6=3, 7=4, 10=5, 20=6, 22=8, 30=7} ============================ // Element shoud be present between Vales: 7 index and 9 |
Inserting Elements In Sorted Array (2methods)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment