Open In App

Count Possible Triangles

Last Updated : 28 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an unsorted array of positive integers, the task is to find the number of triangles that can be formed with three different array elements as three sides of triangles.

For a triangle to be possible from 3 values as sides, the sum of the two values (or sides) must always be greater than the third value (or third side). 

Examples: 

Input: arr[] = [4, 6, 3, 7]
Output: 3
Explanation: There are three triangles possible [3, 4, 6], [4, 6, 7] and [3, 6, 7]. 
Note that [3, 4, 7] is not a possible triangle.  

Input: arr[] = [10, 21, 22, 100, 101, 200, 300]
Output: 6
Explanation: There can be 6 possible triangles:
[10, 21, 22], [21, 100, 101], [22, 100, 101], [10, 100, 101], [100, 101, 200] and [101, 200, 300]

Input: arr[] = [1, 2, 3]
Output: 0
Examples: No triangles are possible.

[Naive Approach] Checking all Triplets - O(n^3) Time and O(1) Space

A simple approach is to run three nested loops that select three different values from an array. And in the innermost loop, we checks for the triangle property which specifies the sum of any two sides must be greater than the value of the third side.

C++
// C++ code to count the number of possible triangles 
// using Nested Loop
#include <iostream>
#include <vector>
using namespace std;

// Function to count all possible triangles with arr[]
// values as sides
int countTriangles(vector<int>& arr) {
    int res = 0;

    // The three loops select three different values from
    // array
    for (int i = 0; i < arr.size(); i++) {
        for (int j = i + 1; j < arr.size(); j++) {
            for (int k = j + 1; k < arr.size(); k++)

                // Sum of two sides is greater than the third
                if (arr[i] + arr[j] > arr[k] && 
                    arr[i] + arr[k] > arr[j] && 
                    arr[k] + arr[j] > arr[i])
                    res++;
        }
    }
    return res;
}

int main() {
    vector<int> arr = {4, 6, 3, 7};
    cout << countTriangles(arr);
    return 0;
}
C
// C code to count the number of possible triangles 
// using Nested Loop
#include <stdio.h>
#include <stdlib.h>

// Function to count all possible triangles with arr[] values as sides
int countTriangles(int arr[], int n) {
    int res = 0;

    // The three loops select three different values from array
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++)

                // Sum of two sides is greater than the third
                if (arr[i] + arr[j] > arr[k] && 
                    arr[i] + arr[k] > arr[j] && 
                    arr[k] + arr[j] > arr[i])
                    res++;
        }
    }
    return res;
}

int main() {
    int arr[] = {4, 6, 3, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d", countTriangles(arr, n));
    return 0;
}
Java
// Java code to count the number of possible triangles 
// using Nested Loop
import java.util.ArrayList;

class GfG {
    // Function to count all possible triangles with arr[]
    // values as sides
    static int countTriangles(int[] arr) {
        int res = 0;

        // The three loops select three different values from
        // array
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                for (int k = j + 1; k < arr.length; k++) {
                    // Sum of two sides is greater than the third
                    if (arr[i] + arr[j] > arr[k] &&
                        arr[i] + arr[k] > arr[j] &&
                        arr[k] + arr[j] > arr[i]) {
                        res++;
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {4, 6, 3, 7};
        System.out.println(countTriangles(arr));
    }
}
Python
# Python code to count the number of possible triangles 
# using Nested Loop

# Function to count all possible triangles with arr[]
# values as sides
def countTriangles(arr):
    res = 0

    # The three loops select three different values from
    # array
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):
            for k in range(j + 1, len(arr)):

                # Sum of two sides is greater than the third
                if arr[i] + arr[j] > arr[k] and \
                   arr[i] + arr[k] > arr[j] and \
                   arr[k] + arr[j] > arr[i]:
                    res += 1
    return res


if __name__ == "__main__":
    arr = [4, 6, 3, 7]
    print(countTriangles(arr))
C#
// C# code to count the number of possible triangles 
// using Nested Loop
using System;

class GfG {
  
    // Function to count all possible triangles with arr[]
    // values as sides
    static int countTriangles(int[] arr) {
        int res = 0;

        // The three loops select three different values from
        // array
        for (int i = 0; i < arr.Length; i++) {
            for (int j = i + 1; j < arr.Length; j++) {
                for (int k = j + 1; k < arr.Length; k++) {
                  
                    // Sum of two sides is greater than the third
                    if (arr[i] + arr[j] > arr[k] &&
                        arr[i] + arr[k] > arr[j] &&
                        arr[k] + arr[j] > arr[i]) {
                        res++;
                    }
                }
            }
        }
        return res;
    }

    static void Main(string[] args) {
        int[] arr = { 4, 6, 3, 7 };
        Console.WriteLine(countTriangles(arr));
    }
}
JavaScript
// JavaScript code to count the number of possible triangles 
// using Nested Loop

// Function to count all possible triangles with arr[]
// values as sides
function countTriangles(arr) {
    let res = 0;

    // The three loops select three different values from
    // array
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            for (let k = j + 1; k < arr.length; k++) {
                // Sum of two sides is greater than the third
                if (arr[i] + arr[j] > arr[k] &&
                    arr[i] + arr[k] > arr[j] &&
                    arr[k] + arr[j] > arr[i]) {
                    res++;
                }
            }
        }
    }
    return res;
}

// Driver Code
const arr = [4, 6, 3, 7];
console.log(countTriangles(arr));

Output
3

[Better Approach] Using Binary Search - O((n^2)*log n) Time and O(1) Space

The idea is to sort the array in ascending order. Then, use two nested loops: the outer loop to fix the first side, and the inner loop to fix the second side.

Next, we find the farthest index for the third side (beyond the indices of the first two sides), such that its value is less than the sum of the first two sides, using Binary Search. So a range of values for third side can be found, where it is guaranteed that its length is greater than the other individual sides but less than the sum of both sides. Add this range size to the result.

C++
// C++ code to count the number of possible triangles
// using Binary Search
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to count the number of valid triangles.
int countTriangles(vector<int> &arr) {
    int res = 0;

    // Sort the array to apply the triangle inequality efficiently
    sort(arr.begin(), arr.end());

    // Iterate through pairs of sides (arr[i], arr[j])
    for (int i = 0; i < arr.size(); i++) {
        for (int j = i + 1; j < arr.size(); j++) {
          
            // Find the first index where the sum of two sides is not valid
            int k = lower_bound(arr.begin() + j + 1, arr.end(), arr[i] + arr[j])
                - arr.begin();
            
            // Count the number of valid third sides
            int cnt = k - j - 1;
            res += cnt;
        }
    }
    return res;
}

int main() {
    vector<int> arr = {4, 6, 3, 7};
    cout << countTriangles(arr);
    return 0;
}
C
// C code to count the number of possible triangles
// using Binary Search
#include <stdio.h>
#include <stdlib.h>

// Comparison function for qsort
int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

// Lower bound function to find the first index where the sum of two sides is not valid
int lower_bound(int arr[], int n, int value, int start) {
    int left = start, right = n;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] < value)
            left = mid + 1;
        else
            right = mid;
    }
    return left;
}

// Function to count the number of valid triangles.
int countTriangles(int arr[], int n) {
    int res = 0;

    // Sort the array to apply the triangle inequality efficiently
    qsort(arr, n, sizeof(int), compare);

    // Iterate through pairs of sides (arr[i], arr[j])
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
          
            // Find the first index where the sum of two sides is not valid
            int k = lower_bound(arr, n, arr[i] + arr[j], j + 1);
            
            // Count the number of valid third sides
            int cnt = k - j - 1;
            res += cnt;
        }
    }
    return res;
}

int main() {
    int arr[] = {4, 6, 3, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", countTriangles(arr, n));
    return 0;
}
Java
// Java code to count the number of possible triangles
// using Binary Search

import java.util.Arrays;

class GfG {
    // Function to count the number of valid triangles
    static int countTriangles(int[] arr) {
        int res = 0;

        // Sort the array to apply the triangle inequality efficiently
        Arrays.sort(arr);

        // Iterate through pairs of sides (arr[i], arr[j])
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {

                // Find the first index where the sum of two sides is not valid
                int lo = j + 1, hi = arr.length;
                int target = arr[i] + arr[j];
                while (lo < hi) {
                    int mid = lo + (hi - lo) / 2;
                    if (arr[mid] < target) {
                        lo = mid + 1;
                    } else {
                        hi = mid;
                    }
                }
                // Count the number of valid third sides
                int cnt = lo - j - 1;
                res += cnt;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {4, 6, 3, 7};
        System.out.println(countTriangles(arr));
    }
}
Python
# Python code to count the number of possible triangles
# using Binary Search

from bisect import bisect_left

# Function to count the number of valid triangles
def countTriangles(arr):
    res = 0

    # Sort the array to apply the triangle inequality efficiently
    arr.sort()

    # Iterate through pairs of sides (arr[i], arr[j])
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):

            # Find the first index where the sum of two sides is not valid
            k = bisect_left(arr, arr[i] + arr[j], j + 1)

            # Count the number of valid third sides
            cnt = k - j - 1
            res += cnt

    return res

if __name__ == "__main__":
    arr = [4, 6, 3, 7]
    print(countTriangles(arr))
C#
// C# code to count the number of possible triangles
// using Binary Search

using System;
using System.Linq;

class GfG {
    // Function to count the number of valid triangles
    static int countTriangles(int[] arr) {
        int res = 0;

        // Sort the array to apply the triangle inequality efficiently
        Array.Sort(arr);

        // Iterate through pairs of sides (arr[i], arr[j])
        for (int i = 0; i < arr.Length; i++) {
            for (int j = i + 1; j < arr.Length; j++) {

                // Find the first index where the sum of two sides is not valid
                int lo = j + 1, hi = arr.Length;
                int target = arr[i] + arr[j];
                while (lo < hi) {
                    int mid = lo + (hi - lo) / 2;
                    if (arr[mid] < target) {
                        lo = mid + 1;
                    } else {
                        hi = mid;
                    }
                }

                // Count the number of valid third sides
                int cnt = lo - j - 1;
                res += cnt;
            }
        }
        return res;
    }

    static void Main(string[] args) {
        int[] arr = { 4, 6, 3, 7 };
        Console.WriteLine(countTriangles(arr));
    }
}
JavaScript
// JavaScript code to count the number of possible triangles
// using Binary Search

// Function to count the number of valid triangles
function countTriangles(arr) {
    let res = 0;

    // Sort the array to apply the triangle inequality efficiently
    arr.sort((a, b) => a - b);

    // Iterate through pairs of sides (arr[i], arr[j])
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {

            // Use binary search to find the first index 
            // where the sum of two sides is not valid
            let lo = j + 1, hi = arr.length;
            while (lo < hi) {
                let mid = Math.floor((lo + hi) / 2);
                if (arr[mid] < arr[i] + arr[j]) {
                    lo = mid + 1;
                } else {
                    hi = mid;
                }
            }
            
            // Count the number of valid third sides
            let cnt = lo - (j + 1);
            res += cnt;
        }
    }
    return res;
}

// Driver Code
const arr = [4, 6, 3, 7];
console.log(countTriangles(arr)); 

Output
3

[Expected Approach] Using Two Pointers Technique - O(n^2) Time and O(1) Space

The idea is to sort the array to simplify checking the triangle inequality. Then, for each element (treated as the largest side), use two pointers technique to find count of pairs of smaller sides that can form a triangle with it.
For this, the two pointers are initialized as: one pointer (left) starts at index 0, and the other pointer (right) is positioned just before the current largest side (arr[i]).

Now, compare the sum of arr[left] + arr[right] with the current largest side (arr[i]):

  • If the sum is greater than or equal to arr[i], a valid triangle can be formed. Count all valid pairs between left and right, then move the right pointer to the left to explore smaller side values.
  • If the sum is less than arr[i], increment the left pointer to increase the sum and check larger values.
C++
// C++ code to count the number of possible triangles
// using Two Pointers Technique

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to count the number of valid triangles.
int countTriangles(vector<int> &arr) {
    int res = 0;
    sort(arr.begin(), arr.end());

    // Iterate through the array, fixing the largest side at arr[i]
    for (int i = 2; i < arr.size(); ++i) {
      
      	// Initialize pointers for the two smaller sides
        int left = 0, right = i - 1; 

        while (left < right) {
          
            if (arr[left] + arr[right] > arr[i]) {
              
                // arr[left] + arr[right] satisfies the triangle inequality,
				// so all pairs (x, right) with (left <= x < right) are valid
                res += right - left; 
              
              	// Move the right pointer to check smaller pairs
                right--; 
            } 
          	else {
              
              	// Move the left pointer to increase the sum
                left++; 
            }
        }
    }

    return res;
}

int main() {
    vector<int> arr = {4, 6, 3, 7};
    cout << countTriangles(arr);
    return 0;
}
C
// C code to count the number of possible triangles
// using Two Pointers Technique

#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers for qsort
int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

// Function to count the number of valid triangles
int countTriangles(int arr[], int n) {
    int res = 0;
    qsort(arr, n, sizeof(int), compare);

    // Iterate through the array, fixing the largest side at arr[i]
    for (int i = 2; i < n; ++i) {
        // Initialize pointers for the two smaller sides
        int left = 0, right = i - 1;

        while (left < right) {
            if (arr[left] + arr[right] > arr[i]) {
                // arr[left] + arr[right] satisfies the triangle inequality,
                // so all pairs (x, right) with (left <= x < right) are valid
                res += right - left;

                // Move the right pointer to check smaller pairs
                right--;
            } else {
                // Move the left pointer to increase the sum
                left++;
            }
        }
    }

    return res;
}

int main() {
    int arr[] = {4, 6, 3, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", countTriangles(arr, n));
    return 0;
}
Java
// Java code to count the number of possible triangles
// using Two Pointers Technique

import java.util.Arrays;

class GfG {
    // Function to count the number of valid triangles
    static int countTriangles(int[] arr) {
        int res = 0;
        Arrays.sort(arr);

        // Iterate through the array, fixing the largest side at arr[i]
        for (int i = 2; i < arr.length; ++i) {
            // Initialize pointers for the two smaller sides
            int left = 0, right = i - 1;

            while (left < right) {
                if (arr[left] + arr[right] > arr[i]) {
                    // arr[left] + arr[right] satisfies the triangle inequality,
                    // so all pairs (x, right) with (left <= x < right) are valid
                    res += right - left;

                    // Move the right pointer to check smaller pairs
                    right--;
                } else {
                    // Move the left pointer to increase the sum
                    left++;
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        int[] arr = {4, 6, 3, 7};
        System.out.println(countTriangles(arr));
    }
}
Python
# Python code to count the number of possible triangles
# using Two Pointers Technique

def countTriangles(arr):
    res = 0
    arr.sort()

    # Iterate through the array, fixing the largest side at arr[i]
    for i in range(2, len(arr)):
      
        # Initialize pointers for the two smaller sides
        left, right = 0, i - 1

        while left < right:
            if arr[left] + arr[right] > arr[i]:
                # arr[left] + arr[right] satisfies the triangle inequality,
                # so all pairs (x, right) with (left <= x < right) are valid
                res += right - left

                # Move the right pointer to check smaller pairs
                right -= 1
                
            else:
                # Move the left pointer to increase the sum
                left += 1

    return res


if __name__ == "__main__":
    arr = [4, 6, 3, 7]
    print(countTriangles(arr))
C#
// C# code to count the number of possible triangles
// using Two Pointers Technique

using System;

class GfG {
    
    // Function to count the number of valid triangles
    static int countTriangles(int[] arr) {
        int res = 0;
        Array.Sort(arr);

        // Iterate through the array, fixing the largest side at arr[i]
        for (int i = 2; i < arr.Length; ++i) {
            // Initialize pointers for the two smaller sides
            int left = 0, right = i - 1;

            while (left < right) {
                if (arr[left] + arr[right] > arr[i]) {
                    // arr[left] + arr[right] satisfies the triangle inequality,
                    // so all pairs (x, right) with (left <= x < right) are valid
                    res += right - left;

                    // Move the right pointer to check smaller pairs
                    right--;
                } else {
                    // Move the left pointer to increase the sum
                    left++;
                }
            }
        }

        return res;
    }

    static void Main(string[] args) {
        int[] arr = {4, 6, 3, 7};
        Console.WriteLine(countTriangles(arr));
    }
}
JavaScript
// JavaScript code to count the number of possible triangles
// using Two Pointers Technique

// Function to count the number of valid triangles
function countTriangles(arr) {
    let res = 0;
    arr.sort((a, b) => a - b);

    // Iterate through the array, fixing the largest side at arr[i]
    for (let i = 2; i < arr.length; ++i) {
    
        // Initialize pointers for the two smaller sides
        let left = 0, right = i - 1;

        while (left < right) {
            if (arr[left] + arr[right] > arr[i]) {
                // arr[left] + arr[right] satisfies the triangle inequality,
                // so all pairs (x, right) with (left <= x < right) are valid
                res += right - left;

                // Move the right pointer to check smaller pairs
                right--;
            } else {
                // Move the left pointer to increase the sum
                left++;
            }
        }
    }

    return res;
}

// Driver Code
const arr = [4, 6, 3, 7];
console.log(countTriangles(arr));

Output
3



Next Article

Similar Reads