I'm given a set of integers of size N in sorted, ascending order. For simplicity, this array "arr" is as follows: [a0, a1, a2, ..., aN]. I need the array of the sum of all pairs ai and aj, with duplicates allowed: [a0 + a0, a0 + a1, a0 + a2, ..., a1 + a0, a1 + a1, ... aN + aN], size N^2. However, I need it in sorted order to binary search across it (in O(log(N^2)) time) without having to generate the entire array, which would take O(N^2 log(N^2)) time. As a binary search only needs the values of the array at certain indices, I was wondering if there was a mathematical function to determine the value of the sorted permutation sum array given a specific index (e.g. value(3) would return ak + am), allowing me to binary search across the array without generating it in full? I was thinking something like:
int value(int index) {
return arr[index/N] + arr[index%N];
}
but this doesn't take into account that the value of arr[i] + arr[k] may be greater than arr[i+1] + arr[k-5], for instance, even though arr[i+1] > arr[i]. TLDR; is there any way I could partition in less than O(N) time for this special case of array? For my own purposes, I could also accept a solution that generates the entire sorted array in less than O(N^2) time.