VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-all-pairs-possible-from-the-given-array/

⇱ Find all Pairs possible from the given Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find all Pairs possible from the given Array

Last Updated : 12 Jul, 2025

Given an array arr[] of N integers, the task is to find all the pairs possible from the given array. 
Note:

  1. (arr[i], arr[i]) is also considered as a valid pair.
  2. (arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs.


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: 

  • Traverse the array and select an element in each traversal.
  • For each element selected, traverse the array with help of another loop and form the pair of this element with each element in the array from the second loop.
  • The array in the second loop will get executed from its first element to its last element, i.e. from index 0 to N-1.
  • Print each pair formed.


Below is the implementation of the above approach:


Output
(1, 1), (1, 2), (2, 1), (2, 2), 

Time Complexity: O(N2)

Auxiliary Space: O(1)

Comment
Article Tags: