Pair Sum

 

You have been given an integer array/list(ARR) and a number X. Find and return the total number of pairs in the array/list which sum to X.

Sample Input 1:
9
1 3 6 2 5 4 3 2 4
7
Sample Output 1:
7
public class Solution {  

    public static int pairSum(int arr[], int x) {
    
        int l=arr.length;
        int s=0;
        int c=0;
        for(int i=0;i<l;i++)
        {
            for(int j=i+1;j<l;j++)
            {
                
                s=arr[i]+arr[j];
                if(s==x)
                    c++;
            }
        }
        return c;
    }
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern