Skip to content

Commit 98dc3ba

Browse files
Merge pull request #257 from Adi-3108/Adi
Add Search in Rotated Sorted Array implementation in C++
2 parents c8ef650 + e84b245 commit 98dc3ba

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Algorithm: Search in Rotated Sorted Array
3+
* Language: C++
4+
* Description:
5+
* Searches for an integer 'target' in a sorted array 'nums' that has been
6+
* left-rotated at an unknown pivot point 'k'. The algorithm must achieve
7+
* O(log n) runtime complexity, which requires a modified Binary Search.
8+
* * Problem Constraints:
9+
* - nums is sorted ascendingly with distinct values.
10+
* - The array structure is [nums[k], ..., nums[n-1], nums[0], ..., nums[k-1]].
11+
* - Example: [4,5,6,7,0,1,2] (rotated at k=4 from [0,1,2,4,5,6,7]).
12+
* * Time Complexity: O(log n) // Achieved via single-pass Binary Search
13+
* Space Complexity: O(1) // In-place search using only a few pointers
14+
* Author: Adi-3108
15+
16+
*/
17+
18+
#include <iostream>
19+
#include <vector>
20+
#include <algorithm>
21+
22+
/**
23+
* @brief Searches for a target value in a possibly rotated sorted array using O(log n) time.
24+
* * The core idea is that in a rotated sorted array, at least one half (left or right)
25+
* defined by the 'mid' point MUST be sorted. We identify the sorted half and check
26+
* if the 'target' falls within that sorted range. If it does, we move our search
27+
* boundary (low/high) into that sorted half. Otherwise, the target must be in the
28+
* unsorted/rotated half, and we move the search there.
29+
* * @param nums The rotated sorted array of unique integers.
30+
* @param target The integer value to search for.
31+
* @return int The index of the target if found, or -1 otherwise.
32+
*/
33+
int searchRotatedSortedArray(const std::vector<int>& nums, int target) {
34+
// Input validation (Constraint: nums.length >= 1, but safe to check)
35+
if (nums.empty()) {
36+
return -1;
37+
}
38+
39+
int low = 0;
40+
int high = nums.size() - 1;
41+
42+
while (low <= high) {
43+
int mid = low + (high - low) / 2;
44+
45+
if (nums[mid] == target) {
46+
return mid;
47+
}
48+
49+
// --- Core Binary Search Logic ---
50+
51+
// 1. Check if the left half is sorted (comparison between the extremes)
52+
if (nums[low] <= nums[mid]) {
53+
// Left half [low...mid] is sorted.
54+
55+
// Check if the target is within the sorted left half
56+
if (target >= nums[low] && target < nums[mid]) {
57+
// Target is in the sorted left half; search there.
58+
high = mid - 1;
59+
} else {
60+
// Target must be in the rotated right half; search there.
61+
low = mid + 1;
62+
}
63+
}
64+
65+
// 2. Otherwise, the right half must be sorted (from nums[mid] to nums[high])
66+
else {
67+
// Right half [mid...high] is sorted.
68+
69+
// Check if the target is within the sorted right half
70+
if (target > nums[mid] && target <= nums[high]) {
71+
// Target is in the sorted right half; search there.
72+
low = mid + 1;
73+
} else {
74+
// Target must be in the rotated left half; search there.
75+
high = mid - 1;
76+
}
77+
}
78+
}
79+
80+
// Target not found after exhausting the array.
81+
return -1;
82+
}
83+
84+
// Test Function
85+
void testSearchRotatedSortedArray() {
86+
std::cout << "--- Testing Search in Rotated Sorted Array ---\n";
87+
88+
// Example 1: Input: [4,5,6,7,0,1,2], target = 0, Output: 4
89+
std::vector<int> nums1 = {4, 5, 6, 7, 0, 1, 2};
90+
int target1 = 0;
91+
int expected1 = 4;
92+
int result1 = searchRotatedSortedArray(nums1, target1);
93+
std::cout << "Test 1: Input: [4,5,6,7,0,1,2], Target: 0\n";
94+
std::cout << "Expected: " << expected1 << ", Actual: " << result1 << " | "
95+
<< (result1 == expected1 ? "PASS" : "FAIL") << std::endl;
96+
97+
// Example 2: Input: [4,5,6,7,0,1,2], target = 3, Output: -1
98+
std::vector<int> nums2 = {4, 5, 6, 7, 0, 1, 2};
99+
int target2 = 3;
100+
int expected2 = -1;
101+
int result2 = searchRotatedSortedArray(nums2, target2);
102+
std::cout << "Test 2: Input: [4,5,6,7,0,1,2], Target: 3\n";
103+
std::cout << "Expected: " << expected2 << ", Actual: " << result2 << " | "
104+
<< (result2 == expected2 ? "PASS" : "FAIL") << std::endl;
105+
106+
// Example 3: Input: [1], target = 0, Output: -1
107+
std::vector<int> nums3 = {1};
108+
int target3 = 0;
109+
int expected3 = -1;
110+
int result3 = searchRotatedSortedArray(nums3, target3);
111+
std::cout << "Test 3: Input: [1], Target: 0\n";
112+
std::cout << "Expected: " << expected3 << ", Actual: " << result3 << " | "
113+
<< (result3 == expected3 ? "PASS" : "FAIL") << std::endl;
114+
115+
// Edge Case: No rotation (Target found)
116+
std::vector<int> nums4 = {1, 2, 3, 4, 5};
117+
int target4 = 4;
118+
int expected4 = 3;
119+
int result4 = searchRotatedSortedArray(nums4, target4);
120+
std::cout << "Test 4: Input: [1,2,3,4,5], Target: 4\n";
121+
std::cout << "Expected: " << expected4 << ", Actual: " << result4 << " | "
122+
<< (result4 == expected4 ? "PASS" : "FAIL") << std::endl;
123+
}
124+
125+
int main() {
126+
testSearchRotatedSortedArray();
127+
return 0;
128+
}

0 commit comments

Comments
 (0)