Given a sorted array, can we build a sorted array of the sums of all pairs in On2?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The problem of generating a sorted array of the sums of all pairs from a given sorted array is intriguing both from a theoretical and practical standpoint. In computer science, understanding the complexity and feasibility of such operations can have significant implications on performance and optimization.
Understanding the Problem
Given a sorted array `A` of size `n`, the task is to compute a new sorted array `S` that consists of the sums of all possible pairs `(A[i], A[j])` where `0 ≤ i < j < n`. The challenge is to achieve this in time complexity.
Approach to the Solution
Naive Method
A straightforward approach to solve this problem would be to:
- Initialize an empty list `S`.
- Iterate over all pairs `(i, j)` with `i < j` and add the sum `A[i] + A[j]` to `S`.
- Sort the list `S`.
While easy to implement, this method does not perform in time since sorting the sums after computing them could increase the time complexity to , especially when using typical comparison-based sorting algorithms.
Optimized Approach
To ensure we stick to , leveraging the properties of the sorted array `A` is crucial. Note the following observations:
- Pair Sums and Sortedness: Since the array `A` is sorted, the smallest sum is `A[0] + A[1]` and the largest is `A[n-2] + A[n-1]`. Any mid-range sum will be naturally bounded by these extremes.
- Two-Pointer Technique: Given that `A` is sorted, using the two-pointer technique can help in efficiently generating sums within possible bounds:
- Start with one pointer `i` at the beginning of the array and another pointer `j` at the next index (`i + 1`).
- Calculate sums `A[i] + A[j]`, adjust pointers based on desired conditions, and move through possible combinations.
This technique ensures that each sum is generated in time, and iterating over all pairs happens within the bounds naturally.
Here is a pseudo-code snippet of the above approach:
Related reading
- Given a sorted integer array, how may Binary Search trees can be formed from it?
- Given a string, find two identical subsequences with consecutive indexes C
- Given a string of a million numbers, return all repeating 3 digit numbers
- Given a word, convert it into a palindrome with minimum addition of letters to it
- Given a tensor flow model graph, how to find the input node and output node names
- Given an array, can I find in On the longest range, whose endpoints are the greatest values in the range?
- Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate?
- Given an unsorted Array find maximum value of Aj - Ai where ji..in On time

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.