Pair Sum in a Sorted and Rotated Array
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.
An integer array arr (sorted, then rotated, all values distinct) and an integer target.
true if some pair arr[i] + arr[j] == target with i ≠ j, otherwise false.
6 + 10 = 16.
26 + 9 = 35.
No two different elements add up to 45.
- • 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).
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check the sum of every pair of positions
For each value, check if its partner (target − x) was already seen
Locate the largest value, then run two pointers that wrap around with % n
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.