Forming triangles using points on a square - GeeksforGeeks

10 Mar, 2022
Given a square with N points on each side of the square and none of these points co-incide with the corners of the square. The task is to calculate the total number of triangles that can be formed using these 4 * N points (N points on each side of the square) as vertices of the triangle.Examples:  
Input: N = 1 Output: 4 There is one point on each side. So we can make three rectangles by leaving one point and picking other three points out of the four. Input: N = 2 Output: 56  
 
Approach: The number of ways of choosing 3 points among 4 * N points is (4 * N)C3. However, some of them do not form a triangle. This happens when all the three chosen points are on the same side of the square. The count of these triplets is NC3 for each of the side i.e. 4 * NC3 in total. Therefore, the required count of triangles will be ((4 * N)C3) – (4 * NC3).Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count// of possible trianglesint noOfTriangles(int n){    int y = 4 * n;    return ((y * (y - 2) * (y - 1))            - (4 * n * (n - 2) * (n - 1)))           / 6;} // Driver codeint main(){    int n = 1;     cout << noOfTriangles(n);     return 0;}
// Java implementation of the above approachclass GFG{             // Function to return the count    // of possible triangles    static int noOfTriangles(int n)    {        int y = 4 * n;        return ((y * (y - 2) * (y - 1)) -                (4 * n * (n - 2) * (n - 1))) / 6;    }         // Driver code    public static void main (String[] args)    {        int n = 1;             System.out.println(noOfTriangles(n));    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach  # Function to return the count# of possible trianglesdef noOfTriangles(n):    y = 4 * n    return ((y * (y - 2) * (y - 1)) -            (4 * n * (n - 2) * (n - 1)))// 6 # Driver coden = 1 print(noOfTriangles(n)) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing System;     class GFG{             // Function to return the count    // of possible triangles    static int noOfTriangles(int n)    {        int y = 4 * n;        return ((y * (y - 2) * (y - 1)) -                (4 * n * (n - 2) * (n - 1))) / 6;    }         // Driver code    public static void Main (String[] args)    {        int n = 1;             Console.WriteLine(noOfTriangles(n));    }} // This code is contributed by 29AjayKumar
<script> // javascript implementation of the approach // Function to return the count// of possible trianglesfunction noOfTriangles(n){    var y = 4 * n;    return ((y * (y - 2) * (y - 1))            - (4 * n * (n - 2) * (n - 1)))           / 6;} // Driver codevar n = 1;document.write(noOfTriangles(n)); </script>
4
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
ankthon
29AjayKumar
rutvik_56
subhammahato348
triangle
Combinatorial
Geometric
Mathematical
Mathematical
Combinatorial
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinational Sum
Count ways to reach the nth stair using step 1, 2 or 3
Print all possible strings of length k that can be formed from a set of n characters
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
Closest Pair of Points using Divide and Conquer algorithm
How to check if a given point lies inside or outside a polygon?
Program for distance between two points on earth
How to check if two given line segments intersect?
Find if two rectangles overlap