Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Problem1.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions Problem2.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
42 changes: 42 additions & 0 deletions Problem3.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}