![]() |
VOOZH | about |
Given an array arr[] of N integers, the task is to find all the pairs possible from the given array.
Note:
Examples:
Input: arr[] = {1, 2}
Output: (1, 1), (1, 2), (2, 1), (2, 2).
Input: arr[] = {1, 2, 3}
Output: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)
Approach:
In order to find all the possible pairs from the array, we need to traverse the array and select the first element of the pair. Then we need to pair this element with all the elements in the array from index 0 to N-1.
Below is the step by step approach:
Below is the implementation of the above approach:
(1, 1), (1, 2), (2, 1), (2, 2),
Time Complexity: O(N2)
Auxiliary Space: O(1)