Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

Given an array **arr[]** of distinct integers of size **N** and a sum value **X**, the task is to find count of triplets with the sum smaller than the given sum value **X**.

**Example 1:**

**Input: **N = 6, X = 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, X = 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** X** as parameters and returns the count of triplets.

**Expected Time Complexity:** O(N^{2}).

**Expected Auxiliary Space:** O(1).

**Constraints:**

3 ≤ N ≤ 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Count triplets with sum smaller than X

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.