Count of ways to split N into Triplets forming a Triangle Last Updated : 11 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to find the number of ways to split N into ordered triplets which can together form a triangle. Examples: Input: N = 15 Output: Total number of triangles possible are 28 Input: N = 9 Output: Total number of triangles possible is 10 Approach: The following observation needs to be made in order to solve the problem: If N is split into 3 integers a, b and c, then the following conditions need to be satisfied for a, b and c to form a triangle: a + b > c a + c > b b + c > a Therefore, iterate over the range [1, N] using nested loops to generate triplets, and for each triplet check if it forms a triangle or not. Below is the implementation of the above approach: C++ // C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return the // required number of ways int Numberofways(int n) { int count = 0; for (int a = 1; a < n; a++) { for (int b = 1; b < n; b++) { int c = n - (a + b); // Check if a, b and c can // form a triangle if (a + b > c && a + c > b && b + c > a) { count++; } } } // Return number of ways return count; } // Driver Code int main() { int n = 15; cout << Numberofways(n) << endl; return 0; } Java // Java Program to implement // the above approach import java.io.*; class GFG { // Function to return the // required number of ways static int Numberofways(int n) { int count = 0; for (int a = 1; a < n; a++) { for (int b = 0; b < n; b++) { int c = n - (a + b); // Check if a, b, c can // form a triangle if (a + b > c && a + c > b && b + c > a) { count++; } } } return count; } // Driver Code public static void main(String[] args) { int n = 15; System.out.println(Numberofways(n)); } } Python3 # Python Program to implement # the above approach # Function to return the # required number of ways def Numberofways(n): count = 0 for a in range(1, n): for b in range(1, n): c = n - (a + b) # Check if a, b, c can form a triangle if(a < b + c and b < a + c and c < a + b): count += 1 return count # Driver code n = 15 print(Numberofways(n)) C# // C# Program to implement // the above approach using System; class GFG { // Function to return the // required number of ways static int Numberofways(int n) { int count = 0; for (int a = 1; a < n; a++) { for (int b = 1; b < n; b++) { int c = n - (a + b); // Check if a, b, c can form // a triangle or not if (a + b > c && a + c > b && b + c > a) { count++; } } } // Return number of ways return count; } // Driver Code static public void Main() { int n = 15; Console.WriteLine(Numberofways(n)); } } JavaScript <script> // Javascript Program to implement // the above approach // Function to return the // required number of ways function Numberofways(n) { var count = 0; for (var a = 1; a < n; a++) { for (var b = 1; b < n; b++) { var c = n - (a + b); // Check if a, b and c can // form a triangle if (a + b > c && a + c > b && b + c > a) { count++; } } } // Return number of ways return count; } // Driver Code var n = 15; document.write( Numberofways(n)); // This code is contributed by noob2000. </script> Output: 28 Time Complexity: O(N2) Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Count of ways to split N into Triplets forming a Triangle R rubaimandal3 Follow Improve Article Tags : Misc Greedy Searching Mathematical Computer Science Fundamentals DSA triangle +3 More Practice Tags : GreedyMathematicalMiscSearching Similar Reads Count Non-Path Triplets in a Tree Given a tree with n vertices numbered from 1 to n. The ith edge connects vertex ai to vertex bi, the task is to find the number of tuples of integers (i, j, k) such that: i < j < k and 1 <= i, j, k <= n. The given tree does not contain a simple path that includes all three vertices i, j, 12 min read Count of triplets till N whose product is at most N Given a positive integer N, the task is to find the number of triplets (A, B, C) from the first N Natural Numbers such that A * B * C ⤠N. Examples: Input: N = 2Output: 4Explanation:Following are the triplets that satisfy the given criteria: ( 1, 1, 1 ) => 1 * 1 * 1 = 1 ⤠2.( 1, 1, 2 ) => 1 * 9 min read Count of ordered triplets(x, y, z) for a given set of input Given three integers N, M and P. The task is to count the number of possible ordered triplets of form (x, y, z) where 1 ⤠x ⤠N, 1 ⤠y ⤠M and 1 ⤠z ⤠P Since this count can be very large, return the answer modulo 109 + 7. Examples: Input: N = 3, M = 3, P = 3Output: 6Explanation: The possible triple 4 min read Count ways to form Triplet of consecutive integers having the given numbers Given an integer N (N ? 3), two distinct numbers X and Y, the task is to find the maximum number of possible ways by which a third drawn number (the third number lies in range [1, N]) can make a triplet of consecutive with given two numbers. Also, print the triplets. Examples: Input: N = 3, X = 2, Y 12 min read Count of triangles with total n points with m collinear There are 'n' points in a plane, out of which 'm' points are co-linear. Find the number of triangles formed by the points as vertices ?Examples : Input : n = 5, m = 4 Output : 6 Out of five points, four points are collinear, we can make 6 triangles. We can choose any 2 points from 4 collinear points 6 min read Like