From f93d94677c9ca724d5b80cf625b59a775523f212 Mon Sep 17 00:00:00 2001 From: Anirudh Venkateshwaran Date: Sun, 3 May 2026 18:34:41 -0700 Subject: [PATCH] Solved Problem 1 - Find first and last position of element in sorted array, Problem 2 - Find minimum in rotated sorted array and Problem 3 - Find peak element in array --- Problem1.cs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem2.cs | 51 +++++++++++++++++++++++++++++++ Problem3.cs | 42 ++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 Problem1.cs create mode 100644 Problem2.cs create mode 100644 Problem3.cs diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..c2033125 --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,87 @@ +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +/* +I have created two helper methods - one performs binary search to find the index of first occurrence of the element. If element is not +found, it returns {-1,-1} as the result, else it stores the index of first occurrence of element as the first element of the result +array. The right binary search is performed if the element exists in the array and optimizes on the search space by only searching +for the element from the index of the first occurrence in the array to the last element +*/ + +public class Solution +{ + public int[] SearchRange(int[] nums, int target) + { + int[] result = new int[] { -1, -1 }; + + result[0] = LeftBinarySearch(nums, target); + + if (result[0] == -1) + { + return result; + } + + result[1] = RightBinarySearch(nums, target, result[0]); + + return result; + } + + public int LeftBinarySearch(int[] nums, int target) + { + int low = 0, high = nums.Length - 1; + + int result = -1; + + while (low <= high) + { + int mid = low + (high - low) / 2; + + if (nums[mid] == target) + { + result = mid; + high = mid - 1; + } + + else if (nums[mid] < target) + { + low = mid + 1; + } + + else + { + high = mid - 1; + } + } + + return result; + } + + public int RightBinarySearch(int[] nums, int target, int low) + { + int high = nums.Length - 1; + int result = low; + + while (low <= high) + { + int mid = low + (high - low) / 2; + + if (nums[mid] == target) + { + result = mid; + low = mid + 1; + } + + else if (nums[mid] > target) + { + high = mid - 1; + } + } + + return result; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..149da581 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,51 @@ +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +/* +In a rotated sorted array, one half of the array is sorted and the other is not. + +Base condition - if array/subarray is sorted, return first element as that's the smallest element + +If mid element is the first element or it's smaller than it's previous element and if the mid element is the last element or it's smaller +than the next element, return that element as it's the smallest element in the array. + +Else if first half of the array is sorted, search through the other half for the minima or vice-versa. +*/ + +public class Solution { + public int FindMin(int[] nums) { + int low = 0, high = nums.Length - 1; + + while(low <= high) + { + if(nums[low] <= nums[high]) + { + return nums[low]; + } + + int mid = low + (high - low)/2; + + if((mid == 0 || nums[mid] < nums[mid-1]) && (mid == nums.Length - 1 || nums[mid] < nums[mid+1])) + { + return nums[mid]; + } + + else if(nums[low] <= nums[mid]) + { + low = mid + 1; + } + + else + { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/Problem3.cs b/Problem3.cs new file mode 100644 index 00000000..0f562eab --- /dev/null +++ b/Problem3.cs @@ -0,0 +1,42 @@ +// Time Complexity : O(logn) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach + +/* +Base condition - If mid element is the first element or it's greater than it's previous element and if the mid element is the last element or it's greater +than the next element, return that element as it's the smallest element in the array. + +Else if element at mid + 1 index is greater, proceed to search through that part of the array or check through the first part +of the array. +*/ + +public class Solution { + public int FindPeakElement(int[] nums) { + int low = 0, high = nums.Length - 1; + while(low <= high) + { + int mid = low + (high - low)/2; + + if((mid==0 || nums[mid] > nums[mid - 1]) && (mid==nums.Length - 1 || nums[mid] > nums[mid +1])) + { + return mid; + } + + else if(nums[mid + 1] > nums[mid]) + { + low = mid + 1; + } + + else + { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file