Find all Pairs possible from the given Array
Last Updated :
13 Jul, 2024
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 print all 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
JavaScript
<script>
// Javascript implementation to find all
// Pairs possible from the given Array
// Function to print all possible
// pairs from the array
function printPairs(arr, n)
{
// Nested loop for all possible pairs
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
document.write("(" + arr[i] + ", "
+ arr[j] + ")"
+ ", ");
}
}
}
// Driver code
var arr = [ 1, 2 ];
var n = arr.length;
printPairs(arr, n);
// This code is contributed by rutvik_56.
</script>
Output(1, 1), (1, 2), (2, 1), (2, 2),
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Find all the intersecting pairs from a given array Given n pairs (S[i], F[i]) where for every i, S[i]< F[i]. Two ranges are said to intersect if and only if either of them does not fully lie inside the other one that is only one point of a pair lies between the start and end of the other pair. We have to print all the intersecting ranges for each
14 min read
Product of all the pairs from the given array Given an array arr[] of N integers, the task is to find the product of all the pairs possible from the given array such as: (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. Print the resultant answer modulus 10^9+7. Exam
11 min read
2 Sum - Find All Pairs With Given Sum Given an array arr[] and a target value, the task is to find all possible indices (i, j) of pairs (arr[i], arr[j]) whose sum is equal to target and i != j. We can return pairs in any order, but all the returned pairs should be internally sorted, that is for any pair(i, j), i should be less than j.Ex
9 min read
2 Sum - Find a pair with given sum Given an array of integers arr[] and an integer target, print a pair of two numbers such that they add up to target. You cannot use the same element twice.Examples:Input: arr[] = {2, 9, 10, 4, 15}, target = 12Output: {2, 10}Explanation: As sum of 2 and 10 is equal to 12.Input: arr[] = {3, 2, 4}, tar
15+ min read
Find Unique pair in an array with pairs of numbers Given an array where every element appears twice except a pair (two elements). Find the elements of this unique pair.Examples: Input : 6, 1, 3, 5, 1, 3, 7, 6 Output : 5 7 All elements appear twice except 5 and 7 Input : 1 3 4 1 Output : 3 4Recommended PracticeFind Unique pair in an array with pairs
8 min read