Triplet Sum

 

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


Sample Input 1:
7
1 2 3 4 5 6 7 
12
Sample Output 1:
5

public class Solution {
    
    public static int findTriplet(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++)
            {
                for(int k=j+1;k<l;k++)
                {
                    s=arr[i]+arr[j]+arr[k];
                    if(s==x)
                    {
                        c++;
                        

                    }
                }
            }
           
        }
         return c;
    }

}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern