Count triplets with sum smaller than X | Practice | GeeksforGeeks

Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.

Example 1:

Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output:  2
Explanation: Below are triplets with 
sum less than 2 (-2, 0, 1) and (-2, 0, 3). 

 
Example 2:

Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with 
sum less than 12 (1, 3, 4), (1, 3, 5), 
(1, 3, 7) and (1, 4, 5).


Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N  and integer sum as parameters and returns the count of triplets.

Expected Time Complexity: O(N2).
Expected Auxiliary Space: O(1).
0
yuvrajranabtcse205 days ago
C++ ans in n^2 timecomplexiblity
 
int ans=0,right,lastdrop=n-1,ques;    sort(arr,arr+n);    for(int i=0;i<n-2;i++){        lastdrop=n-1;                //this loop will have o(n) time complexibilty...therefore total=n^2        for(int j=i+1;j<=lastdrop;j++){        right=lastdrop;        while(j<right){            ques=arr[i]+arr[j]+arr[right];            if(ques>=sum){                right-=1;                lastdrop=right;            }            else if(ques<sum){                ans+=(right-j);                lastdrop=right;                break;            }        }  } }    return ans;}   
0
gaurav119087902 weeks ago
class Solution{   long countTriplets(long ar[], int n,int sum)   {       long count=0;       long res=0;       Arrays.sort(ar);       for(int i=n-1;i>=0;i--){           res=isPair(ar,0, i-1, sum-ar[i]);           if(res>0){               count+=res;           }           res=0;       }       return count;   }   static long isPair(long ar[], int l, int r, long sum){       long res=0;       while(l<r){           if(ar[l]+ar[r]<sum){               res++;               l++;               r--;           }           else if(ar[l]+ar[r]<=sum){               r--;           }                  }       return res;   }}I am getting TLE. Why ?
 
0
anushkay009212 weeks ago
 long countTriplets(long arr[], int n,int sum)
   {
       int count = 0 ;
       Arrays.sort(arr);
       for(int i = 0 ; i < n ; i++)
       {
           if(arr[i] < sum)
           {
               int j  =  i+1 ; int k = n-1;
               while(j < k)
               {
                  if(sum > arr[i] + arr[j] + arr[k])
                  {
                      count = count + (k - j);
                      j++;
                  }
                  else k--;
               }
           }
       }
       return count;
   }
0
aasif23643 weeks ago
 {    sort(arr , arr+n);     long long cnt=0;    for(int i=0 ; i<n-2 ; i++){        int j=i+1;        int k=n-1;        while(j < k){            long long s = arr[i] + arr[j] + arr[k];                  if(s < sum){                cnt+=(k-j);                              j++;            }            else{                k--;            }        }    }        return cnt;}
+2
kunal7073 weeks ago
class Solution
{
    long countTriplets(long arr[], int n,int sum)
    {
        Arrays.sort(arr); 
        
        int count=0;
        for(int i=0;i<arr.length;i++){
            
            int start=i+1;
            int end=n-1;
            
            
            while(start<end){
                
                if(arr[i]+arr[start]+arr[end]>=sum){
                        end--;
                    
                }
                else{
                    count+=end-start;
                    start++;
                }
                
            }
            
        }

         return count;
    }
}
+1
dev1711 month ago
Simple C++ O(n^2), two pointers
	#define ll long long 
	long long countTriplets(long long arr[], int n, long long sum)
	{
            ll ans=0;
            sort(arr,arr+n);
            for(int i=0;i<n-2;i++){
                ll sum1=sum-arr[i];
                int s=i+1,e=n-1;
                while(s<e){
                    ll sum2=arr[s]+arr[e];
                    if(sum2<sum1){
                        ans+=e-s;
                        s++;
                    }else{
                        e--;
                    }
                    
                    
                }
            }
            return ans;
	}
0
zindgi barbad ho gya2 months ago
Solution is getting accepted even if (i < j < k) condition mentioned in question does not met. 
Editorial also does not mention about the condition and the editorial solution is directly applicable here.
0
zindgi barbad ho gya
This comment was deleted.
+1
dipanshusharma93132 months ago
// java solution
class Solution{   long countTriplets(long arr[], int n,int sum)   {       Arrays.sort(arr);       long t = 0;       for(int i=0;i<n;i++){          int a = i+1;          int b = n-1;           while(a<b){               if(arr[i]+arr[a]+arr[b]<sum){                   t = t+b-a;                   a++;               }               else{                   b--;               }           }       }       return t;   }}
+1
abhijain52 months ago
long long countTriplets(long long arr[], int n, long long sum)
	{
	    sort(arr,arr+n);
	    long long count=0;
	    for(long long i=0;i<=n-3;i++)
	    {
	        for(long long low=i+1,high=n-1;low<high;)
	        {
	            long long s=arr[i]+arr[low]+arr[high];
	            if(s<sum)
	            {
	             count+=(high-low);
	             low++;
	            }
	            else
	               high--;
	        }
	    }
	    return count;
	}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.