Given an array arr[] of N integers, the task is to find all the pairs possible from the given array.
Note:
- (arr[i], arr[i]) is also considered as a valid pair.
- (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:
C++
// C++ implementation to find all // Pairs possible from the given Array #include <bits/stdc++.h> using namespace std; // Function to print all possible // pairs from the array void printPairs(int arr[], int n) { // Nested loop for all possible pairs for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << "(" << arr[i] << ", " << arr[j] << ")" << ", "; } } } // Driver code int main() { int arr[] = { 1, 2 }; int n = sizeof(arr) / sizeof(arr[0]); printPairs(arr, n); return 0; } |
Java
// Java implementation to find all // Pairs possible from the given Array class GFG{ // Function to print all possible // pairs from the array static void printPairs(int arr[], int n) { // Nested loop for all possible pairs for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print("(" + arr[i]+ ", " + arr[j]+ ")" + ", "); } } } // Driver code public static void main(String[] args) { int arr[] = { 1, 2 }; int n = arr.length; printPairs(arr, n); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation to find all # Pairs possible from the given Array # Function to prall possible # pairs from the array def printPairs(arr, n): # Nested loop for all possible pairs for i in range(n): for j in range(n): print("(",arr[i],",",arr[j],")",end=", ") # Driver code arr=[1, 2] n = len(arr) printPairs(arr, n) # This code is contributed by mohit kumar 29 |
C#
// C# implementation to find all // Pairs possible from the given Array using System; class GFG{ // Function to print all possible // pairs from the array static void printPairs(int []arr, int n) { // Nested loop for all possible pairs for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write("(" + arr[i]+ ", " + arr[j]+ ")" + ", "); } } } // Driver code public static void Main(string[] args) { int []arr = { 1, 2 }; int n = arr.Length; printPairs(arr, n); } } // This code is contributed by AnkitRai01 |
(1, 1), (1, 2), (2, 1), (2, 2),
Time Complexity: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the sum of all possible pairs in an array of N elements
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count of all possible pairs of array elements with same parity
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Find the maximum cost of an array of pairs choosing at most K pairs
- Sum of division of the possible pairs for the given Array
- All possible co-prime distinct element pairs within a range [L, R]
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Maximize GCD of all possible pairs from 1 to N
- Count of all possible pairs having sum of LCM and GCD equal to N
- Minimum LCM and GCD possible among all possible sub-arrays
- Find all the intersecting pairs from a given array
- Find the minimum value of the given expression over all pairs of the array
- Find the sum of maximum difference possible from all subset of a given array.
- Find if it is possible to make all elements of an array equal by the given operations
- Mean of array generated by products of all pairs of the given array
- Possible pairs forming a Pythagorean Triple with a given value
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

