From b8c2c484fb02414b47d007927bc1898efc749af7 Mon Sep 17 00:00:00 2001 From: ousmane Date: Wed, 14 Jun 2023 20:00:31 +0800 Subject: [PATCH] Solve bitonic array --- .../java-interview-questions/Main.class | Bin 678 -> 618 bytes src/Main.java | 4 +- src/intermediate/Searching.java | 49 ++++++++++++------ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/out/production/java-interview-questions/Main.class b/out/production/java-interview-questions/Main.class index 00b4ec43a4513bfb2fe4679cd9fc4bbd41db9800..a4037e01ce8f711d6a084f3a82578c01fba63490 100644 GIT binary patch delta 124 zcmZ3+`if;jq7Y9(YGSr;eraAwVrCvA1FJ^##=1sExoid&24)5Z21WKgT#?Ky{*f%K z{*kO~{*i1f{yy51{g`B}MHsjlBpG-Z6c~6JWPwT*AQ~8y7?goL5um6FgDL|fPz?uz R8iP6m6OgR|lwo4f1OPvK5()qS delta 209 zcmaFGvW#^?qO4eIPHJvyUWtF2XI@%nUS>(EV^L9JB_jiyMzrU~;zq{$ISece%nS?+ zK?2-+xFVU^{3BU7{3BTf*!?5f1UUU8*#)@#BRK@3{38X}1X6$`yC6dokm3+z=mAok zf(#3Q6qg{wDt{ks0j?bk?6#X2cxARR2yXFfed2I5TiFcroxW1Tye4 t$O1(a7??GHR1kv_gEEkIX5eQ~VNhjY1S;ZSP-9RBYG-5ssbFHz1OQi-Beehk diff --git a/src/Main.java b/src/Main.java index 780cac3..e4ece89 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,8 +7,8 @@ public class Main { public static void main(String[] args) { - int[] arr = {3, 5, 7, 9, 10, 90, 100, 130, 140, 160, 170}; - int result = Searching.elementOfInfiniteArray(arr, 10); + int[] arr = {1, 2, 3, 1}; + int result = Searching.peakMountain(arr); System.out.println(result); } diff --git a/src/intermediate/Searching.java b/src/intermediate/Searching.java index 60adc87..173690f 100644 --- a/src/intermediate/Searching.java +++ b/src/intermediate/Searching.java @@ -445,16 +445,14 @@ public static int[] searchInRange(int[] nums, int target) { private static int firstAndLastPosition1(int[] nums, int target, boolean firstOccurence) { int start = 0, end = nums.length - 1; int ans = -1; - while (start <= end){ + while (start <= end) { int mid = start + (end - start) / 2; if (nums[mid] > target) { end = mid - 1; - } - else if (nums[mid] < target) { + } else if (nums[mid] < target) { start = mid + 1; - } - else { + } else { ans = mid; if (firstOccurence) end = mid - 1; // go to left to check if there is still remaining @@ -469,14 +467,14 @@ else if (nums[mid] < target) { // Find position of an element in a sorted array of infinite numbers // Double the size of the array until the target position if found // [3, 4, 5, 6, 7, 8] target = 6 - public static int elementOfInfiniteArray(int[] nums, int target){ + public static int elementOfInfiniteArray(int[] nums, int target) { // first find the range // then start with a box of size 2 int start = 0; int end = 1; // condition for the target to lie in the range - while (target > nums[end]){ + while (target > nums[end]) { int temp = end + 1; // Our new start position number 2 end = end + (end - start + 1) * 2; @@ -486,9 +484,9 @@ public static int elementOfInfiniteArray(int[] nums, int target){ return binarySearch(nums, target, start, end); } - private static int binarySearch(int[] nums, int target, int start, int end){ + private static int binarySearch(int[] nums, int target, int start, int end) { - while (start <= end){ + while (start <= end) { int middle = start + (end - start) / 2; if (nums[middle] == target) @@ -502,28 +500,28 @@ else if (nums[middle] > target) } // https://leetcode.com/problems/intersection-of-two-arrays/submissions/ - public static int[] intersectionOfArrays(int[]nums1, int[]nums2){ - Set common=new HashSet<>(); + public static int[] intersectionOfArrays(int[] nums1, int[] nums2) { + Set common = new HashSet<>(); Arrays.sort(nums2); - for (Integer num: nums1){ + for (Integer num : nums1) { if (isFound(nums2, num)) common.add(num); } int j = 0; - int[] ans=new int[common.size()]; - for (Integer i: common){ + int[] ans = new int[common.size()]; + for (Integer i : common) { ans[j++] = i; } return ans; } // This check if the target element is found in the array - private static boolean isFound(int[]nums, int target){ + private static boolean isFound(int[] nums, int target) { int start = 0, end = nums.length - 1; - while (start <= end){ - int mid=start + (end - start) / 2; + while (start <= end) { + int mid = start + (end - start) / 2; if (nums[mid] == target) return true; @@ -534,4 +532,21 @@ else if (nums[mid] > target) } return false; } + + // https://leetcode.com/problems/find-peak-element/ + public static int peakMountain(int[] nums) { + int start = 0, end = nums.length - 1; + + while (start < end){ + int middle = start + (end - start) / 2; + + if (nums[middle] > nums[middle+1]){ + end = middle; // Decreasing order + } + else{ + start = middle + 1; // Increasing order + } + } + return start; + } }