2Brute2Best
🔥0

Pair Sum in a Sorted and Rotated Array

Medium·GeeksforGeeks Pro OnlyInterview-Level
GeeksforGeeks
01

Problem Statement

You are given an array arr of distinct numbers. It was sorted in increasing order and then rotated at some unknown point: some elements from the front were moved to the back, keeping their order. For example, [6, 8, 9, 10, 11, 15] rotated becomes [11, 15, 6, 8, 9, 10].

Return true if two elements at different positions add up to target, and false otherwise.

Write it as bool pairInSortedRotated(vector<int>& arr, int target). As on GFG, assume all elements are distinct.

Input

An integer array arr (sorted, then rotated, all values distinct) and an integer target.

Output

true if some pair arr[i] + arr[j] == target with i ≠ j, otherwise false.

Example 1
Input: arr = [11, 15, 6, 8, 9, 10], target = 16
Output: true

6 + 10 = 16.

Example 2
Input: arr = [11, 15, 26, 38, 9, 10], target = 35
Output: true

26 + 9 = 35.

Example 3
Input: arr = [11, 15, 26, 38, 9, 10], target = 45
Output: false

No two different elements add up to 45.

Constraints
  • • 1 ≤ arr.size() ≤ 10^5
  • • 1 ≤ arr[i] ≤ 10^5, all distinct
  • • 1 ≤ target ≤ 2 · 10^5
  • • The array may also not be rotated at all (rotated by 0).
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“pair that adds up to target”
→ A pair sum → hash set, or two pointers if the data is sorted.
“sorted and rotated”
→ Still sorted if read in a circle → two pointers work, once we find where the smallest and largest values are.
“distinct elements”
→ The drop point (arr[i] > arr[i+1]) is unique, so finding the pivot is simple.
03

Approach Complexity Ladder

Brute Force — try every pairO(N²)O(1)

Check the sum of every pair of positions

Better — hash set of seen valuesO(N)O(N)

For each value, check if its partner (target − x) was already seen

Optimal — find the pivot, then circular two pointersO(N)O(1)

Locate the largest value, then run two pointers that wrap around with % n

Brute Force Intuition

Try every pair i < j and return true as soon as arr[i] + arr[j] == target. This ignores the sorted-rotated structure completely.

Recognition rule for this sub-pattern:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.