Number of pairs of Array where the max and min of pair is same as their indices
Last Updated :
25 Aug, 2022
Given an array A[] of N integers, the task is to calculate the total number of pairs of indices (i, j) satisfying the following conditions –
- 1 ? i < j ? N
- minimum(A[i], A[j]) = i
- maximum(A[i], A[j]) = j
Note: 1-based indexing is considered.
Examples:
Input: N = 4, A[] = {1, 3, 2, 4}
Output: 2
Explanation: First pair of indices is (1, 4),
As minimum(A[1], A[4]) = minimum(1, 4) = 1 and
maximum(A[1], A[4]) = maximum(1, 4) = 4.
Similarly, second pair is (3, 2).
Input: N = 10, A[] = {5, 8, 2, 2, 1, 6, 7, 2, 9, 10}
Output: 8
Approach: The problem can be solved based on the following idea:
The conditions given in the problem would be satisfied, if one of these two conditions holds :
- 1st Type: A[i] = i and A[j] = j
- 2nd Type: A[i] = j and A[j] = i
Say there are K such indices where A[i] = i. So, number of pairs satisfying the first condition is K * (K – 1) / 2.
The number of pairs satisfying the second condition can be simply counted by traversing through the array.
i.e., checking if A[i] ? i and A[A[i]] = i.
Follow the steps mentioned below to implement the idea:
- Traverse through the array and find the position where the value and the position are same (say K).
- Find the pairs of the first type mentioned above using the provided formula.
- Now traverse again using and find the second type of pair following the mentioned method.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int N, int A[])
{
int count = 0;
int answer = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == (i + 1)) {
count++;
}
}
answer += count * (count - 1) / 2;
for ( int i = 0; i < N; i++) {
if (A[i] > (i + 1) && A[A[i] - 1] == (i + 1)) {
answer++;
}
}
return answer;
}
int main()
{
int N = 4;
int A[] = { 1, 3, 2, 4 };
int answer = countPairs(N, A);
cout << answer << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPairs( int N, int A[])
{
int count = 0 ;
int answer = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] == (i + 1 )) {
count++;
}
}
answer += count * (count - 1 ) / 2 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] > (i + 1 ) && A[A[i] - 1 ] == (i + 1 )) {
answer++;
}
}
return answer;
}
public static void main (String[] args)
{
int N = 4 ;
int A[] = { 1 , 3 , 2 , 4 };
int answer = countPairs(N, A);
System.out.println(answer);
}
}
|
Python3
def countpairs(n,a) - > int :
count = 0
answer = 0
for i in range ( 0 ,n):
if (a[i] = = i + 1 ):
count + = 1
answer + = ((count) * (count - 1 )) / / 2
for i in range ( 0 ,n):
if (a[i] > (i + 1 ) and a[a[i] - 1 ] = = (i + 1 )):
answer + = 1
return answer
if __name__ = = '__main__' :
n = 4
a = [ 1 , 3 , 2 , 4 ]
ans = countpairs(n,a)
print (ans)
|
C#
using System;
class GFG
{
static int countPairs( int N, int [] A)
{
int count = 0;
int answer = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == (i + 1)) {
count++;
}
}
answer += count * (count - 1) / 2;
for ( int i = 0; i < N; i++) {
if (A[i] > (i + 1) && A[A[i] - 1] == (i + 1)) {
answer++;
}
}
return answer;
}
public static void Main()
{
int N = 4;
int [] A = { 1, 3, 2, 4 };
int answer = countPairs(N, A);
Console.Write(answer);
}
}
|
Javascript
<script>
function countPairs( N,A)
{
let count = 0;
let answer = 0;
for (let i = 0; i < N; i++) {
if (A[i] == (i + 1)) {
count++;
}
}
answer += count * (count - 1) / 2;
for ( let i = 0; i < N; i++) {
if (A[i] > (i + 1) && A[A[i] - 1] == (i + 1)) {
answer++;
}
}
return answer;
}
let N = 4;
let A = [ 1, 3, 2, 4 ];
let answer = countPairs(N, A);
document.write(answer);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...