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
41 changes: 41 additions & 0 deletions SearchInA2DMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity : O(log(m × n))
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach:
// This 2D matrix is treated as a single 1D array because each row is sorted and
// last element of previous row is lesser than the first element of next row
// Binary search is applied on the virtual 1D array.
public class Solution
{
public bool SearchMatrix(int[][] matrix, int target)
{
int m = matrix.Length;
int n = matrix[0].Length;

int low = 0, high = m * n - 1;

while (low <= high)
{
int mid = low + (high - low) / 2;

int r = mid / n;
int c = mid % n;

if (matrix[r][c] == target)
{
return true;
}
else if (matrix[r][c] > target)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return false;
}
}
51 changes: 51 additions & 0 deletions SearchInARotatedSortedArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach:
// The array is a rotated sorted array, so at least one half is always sorted in each iteration.
// We identify the sorted half and check whether the target lies within that range to decide which side to discard.
// By reducing the search space by half each time, we achieve logarithmic time complexity using constant space.
class Solution
{
public int Search(int[] nums, int target)
{
int left = 0;
int right = nums.Length - 1;
int mid = 0;
while (left <= right)
{
mid = (left + right) / 2;
if (nums[mid] == target)
{
return mid;
}
if (nums[left] <= nums[mid])
{
if (nums[left] <= target && target < nums[mid])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
else
{
if (nums[mid] < target && target <= nums[right])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
}
return -1;
}
}


48 changes: 48 additions & 0 deletions SearchInArrayOfUnknownSize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Time Complexity :O(log n)
// 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 three sentences only
// Since the array size is unknown, we first expand the search range exponentially until the target lies within the range.
//start with low and high check if target is within the range of high if not move high twice and low in place of high
// Once a valid range is found, we apply binary search to efficiently locate the target.
// This approach ensures logarithmic time complexity while using constant extra space.


/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* class ArrayReader {
* public int Get(int index) {}
* }
*/
class Solution
{
public int Search(ArrayReader reader, int target)
{
int low = 0;
int high = 1;
while (reader.get(high) < target)
{
low = high;
high = high * 2;
}
while (low <= high)
{
int mid = low + (high - low) / 2;
if (reader.get(mid) == target)
return mid;
else if (reader.get(mid) > target)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1;
}
}