Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
import java.util.*;
 
class GFG {
    
    // Return the number of pairs with equal
    // values.
    static int countPairs(int arr[], int n)
    {
        int ans = 0;
     
        // for each index i and j
        for (int i = 0; i < n; i++)
            for (int j = i+1; j < n; j++)
     
                // finding the index with same
                // value but different index.
                if (arr[i] == arr[j])
                    ans++;
        return ans;
    }
     
    //driver code
    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??
Posted
Updated 21-Jun-18 21:16pm
v3
Comments
OriginalGriff 22-Jun-18 1:25am    
And?
What have you tried to fix it?
Where are you stuck?
What help do you need?
Richard MacCutchan 22-Jun-18 3:19am    
How do you define a pair from that array? There are many combinations that can be seen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900