Find the number of duplicates index pairs (
p,q
) in an array such that
p<q
.
You can write the program in Java. P and Q refers to the index.
Input: 1 3 -4 1 -2 3 1 6 8 1 -4 2 1 60 90
What I have tried:
import java.util.*;
class GFG {
static int countPairs(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (arr[i] == arr[j])
ans++;
return ans;
}
public static void main (String[] args)
{
int arr[] = { 1, 3, -4, 1, -2, 3, 1, 6, 8, 1, -4, 2, 1, 60, 90};
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
I tried this but it doent work as The Question is asking me??