Skip to content

Commit 6231bf1

Browse files
Add files via upload
1 parent 7cd3da9 commit 6231bf1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

PairSumInRotatedSortedArray.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class PairSumInRotatedSortedArray {
2+
public boolean findPairWithSum(int[] nums, int target) {
3+
int n = nums.length;
4+
if (n < 2) {
5+
return false;
6+
}
7+
8+
int left = 0;
9+
int right = n - 1;
10+
11+
while (left < right) {
12+
int sum = nums[left] + nums[right];
13+
14+
if (sum == target) {
15+
return true;
16+
} else if (sum < target) {
17+
left = (left + 1) % n;
18+
} else {
19+
right = (right - 1 + n) % n;
20+
}
21+
22+
if (left == right) {
23+
left++;
24+
right--;
25+
}
26+
}
27+
28+
return false;
29+
}
30+
31+
public static void main(String[] args) {
32+
PairSumInRotatedSortedArray solution = new PairSumInRotatedSortedArray();
33+
int[] nums = {4, 5, 6, 7, 0, 1, 2};
34+
int target = 9;
35+
36+
boolean foundPair = solution.findPairWithSum(nums, target);
37+
System.out.println("Pair with sum " + target + " found: " + foundPair);
38+
}
39+
}

0 commit comments

Comments
 (0)