Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Searching Algorithms
  • MCQs on Searching Algorithms
  • Tutorial on Searching Algorithms
  • Linear Search
  • Binary Search
  • Ternary Search
  • Jump Search
  • Sentinel Linear Search
  • Interpolation Search
  • Exponential Search
  • Fibonacci Search
  • Ubiquitous Binary Search
  • Linear Search Vs Binary Search
  • Interpolation Search Vs Binary Search
  • Binary Search Vs Ternary Search
  • Sentinel Linear Search Vs Linear Search
Open In App
Next Article:
Count of ways to split N into Triplets forming a Triangle
Next article icon

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)


Next Article
Count of ways to split N into Triplets forming a Triangle

R

rubaimandal3
Improve
Article Tags :
  • Misc
  • Greedy
  • Searching
  • Mathematical
  • Computer Science Fundamentals
  • DSA
  • triangle
Practice Tags :
  • Greedy
  • Mathematical
  • Misc
  • Searching

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences