Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sort the sides of triangle on the basis of increasing area

  • Last Updated : 24 Dec, 2021

Given an array arr[] of sides of N triangles, the task is to sort the given sides of triangles on the basis of the increasing order of area.

Examples:

Input: arr[] = {{5, 3, 7}, {15, 20, 4}, {4, 9, 6}, {8, 4, 5}}
Output: {{5, 3, 7}, {8, 4, 5}, {4, 9, 6}, {15, 20, 17}}
Explanation:
Following are the areas of triangle:

  • Area of 1st triangle (5, 3, 7) is 6.4.
  • Area of 2nd triangle (15, 20, 4) is 124.2.
  • Area of 3rd triangle (4, 9, 6) is 9.5.
  • Area of 4th triangle (8, 4, 5) is 8.1.

Therefore, ordering them increasing order of the area modifies the given array as 6.4 {5, 3, 7}, 8.1 {8, 4, 5}, 9.5 {4, 9, 6}, 124.2 {15, 20, 4}.

Input: arr[] = {{7, 24, 25}, {5, 12, 13}, {3, 4, 5}}
Output: {{3, 4, 5}, {5, 12, 13}, {7, 24, 25}}

Approach: The given can be solved by storing the sides with the area of the triangle in another array and then sort the array in increasing order of area stored and then print the sides stored in another array as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to rearrange the sides of
// triangle in increasing order of area
void rearrangeTriangle(
    vector<vector<int> > arr, int N)
{
    // Stores the area of triangles with
    // their corresponding indices
    vector<pair<float, int> > area;
 
    for (int i = 0; i < N; i++) {
 
        // Find the area
        float a = (arr[i][0]
                   + arr[i][1]
                   + arr[i][2])
                  / 2.0;
        float Area = sqrt(abs(a * (a - arr[i][0])
                              * (a - arr[i][1])
                              * (a - arr[i][2])));
 
        area.push_back({ Area, i });
    }
 
    // Sort the area vector
    sort(area.begin(), area.end());
 
    // Resultant sides
    for (int i = 0; i < area.size(); i++) {
        cout << arr[area[i].second][0]
             << " "
             << arr[area[i].second][1]
             << " "
             << arr[area[i].second][2]
             << '\n';
    }
}
 
// Driver Code
int main()
{
    vector<vector<int> > arr = {
        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }
    };
    int N = arr.size();
 
    rearrangeTriangle(arr, N);
 
    return 0;
}

Python3




# python program for the above approach
import math
 
# Function to rearrange the sides of
# triangle in increasing order of area
def rearrangeTriangle(arr, N):
 
        # Stores the area of triangles with
        # their corresponding indices
    area = []
 
    for i in range(0, N):
 
                # Find the area
        a = (arr[i][0] + arr[i][1] + arr[i][2]) / 2.0
        Area = math.sqrt(
            abs(a * (a - arr[i][0]) * (a - arr[i][1]) * (a - arr[i][2])))
 
        area.append([Area, i])
 
        # Sort the area vector
    area.sort()
 
    # Resultant sides
    for i in range(0, len(area)):
        print(arr[area[i][1]][0], end=" ")
        print(arr[area[i][1]][1], end=" ")
        print(arr[area[i][1]][2])
 
# Driver Code
if __name__ == "__main__":
 
    arr = [
        [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5]
    ]
    N = len(arr)
 
    rearrangeTriangle(arr, N)
 
    # This code is contributed by rakeshsahni

C#




// C# implementation for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  // Function to rearrange the sides of
  // triangle in increasing order of area
  static void rearrangeTriangle(
    List<List<int> > arr, int N)
  {
     
    // Stores the area of triangles with
    // their corresponding indices
    List<KeyValuePair<float, int> > area = new List<KeyValuePair<float, int> >();
 
    for (int i = 0; i < N; i++) {
 
      // Find the area
      float a = (float)(arr[i][0]
                        + arr[i][1]
                        + arr[i][2])
        / 2;
      float Area = (float)Math.Sqrt(Math.Abs(a * (a - arr[i][0])
                                             * (a - arr[i][1])
                                             * (a - arr[i][2])));
 
      area.Add(new KeyValuePair <float, int>(Area, i ));
    }
 
    // Sort the area List
    area.Sort((x, y) => x.Key.CompareTo(y.Key));
 
    // Resultant sides
    for (int i = 0; i < area.Count; i++) {
      Console.WriteLine(arr[area[i].Value][0] + " "
                        + arr[area[i].Value][1]
                        + " "
                        + arr[area[i].Value][2]);
    }
  }
 
  // Driver Code
  static public void Main ()
  {
    List<List<int> > arr = new List<List<int> >(){
      new List<int>(){ 5, 3, 7 },
      new List<int>(){ 15, 20, 4 },
      new List<int>(){ 4, 9, 6 },
      new List<int>(){ 8, 4, 5 }
    };
    int N = arr.Count;
 
    rearrangeTriangle(arr, N);
  }
}
 
// This code is contributed
// by Shubham Singh

Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to rearrange the sides of
        // triangle in increasing order of area
        function rearrangeTriangle(
            arr, N)
        {
         
            // Stores the area of triangles with
            // their corresponding indices
            let area = [];
 
            for (let i = 0; i < N; i++) {
 
                // Find the area
                let a = (arr[i][0]
                    + arr[i][1]
                    + arr[i][2])
                    / 2.0;
                let Area = Math.sqrt(Math.abs(a * (a - arr[i][0])
                    * (a - arr[i][1])
                    * (a - arr[i][2])));
 
                area.push({ first: parseInt(Area), second: parseInt(i) });
            }
 
            // Sort the area vector
            area.sort(function (a, b) {
                return a.first - b.first;
            })
 
            // Resultant sides
            for (let i = 0; i < area.length; i++) {
                document.write(arr[area[i].second][0]
                    + " "
                    + arr[area[i].second][1]
                    + " "
                    + arr[area[i].second][2]
                    + '<br>'
                )
            }
        }
 
        // Driver Code
        let arr = [
            [5, 3, 7], [15, 20, 4], [4, 9, 6], [8, 4, 5]
        ];
        let N = arr.length;
        rearrangeTriangle(arr, N);
 
       // This code is contributed by Potta Lokesh
 
    </script>
Output: 
5 3 7
8 4 5
4 9 6
15 20 4

 

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Space Optimized Approach: The above approach can also be optimized in terms of space, the idea is to use the comparator function to sort the given array in increasing order of area. Below is the comparator function that is used:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the area of sides
// of triangle stored in arr[]
float findArea(vector<int>& arr)
{
 
    // Find the semi perimeter
    float a = (arr[0]
               + arr[1]
               + arr[2])
              / 2.0;
 
    // Find area using Heron's Formula
    float Area = sqrt(abs(a * (a - arr[0])
                          * (a - arr[1])
                          * (a - arr[2])));
 
    // Return the area
    return Area;
}
 
// Comparator function to sort the given
// array of sides of triangles in
// increasing order of area
bool cmp(vector<int>& A, vector<int>& B)
{
    return findArea(A) <= findArea(B);
}
 
// Function to rearrange the sides of
// triangle in increasing order of area
void rearrangeTriangle(
    vector<vector<int> > arr, int N)
{
    // Sort the array arr[] in increasing
    // order of area
    sort(arr.begin(), arr.end(), cmp);
 
    // Resultant sides
    for (int i = 0; i < N; i++) {
        cout << arr[i][0] << " " << arr[i][1] << " "
             << arr[i][2] << '\n';
    }
}
 
// Driver Code
int main()
{
    vector<vector<int> > arr = {
        { 5, 3, 7 }, { 15, 20, 4 }, { 4, 9, 6 }, { 8, 4, 5 }
    };
    int N = arr.size();
 
    rearrangeTriangle(arr, N);
 
    return 0;
}

Python3




# Python program for the above approach
import math
 
# Function to find the area of sides
# of triangle stored in arr[]
def findArea(arr):
 
    # Find the semi perimeter
    a = (arr[0] + arr[1] + arr[2]) / 2.0
     
    # Find area using Heron's Formula
    Area = math.sqrt(abs(a * (a - arr[0]) * (a - arr[1]) * (a - arr[2])))
     
    # Return the area
    return Area
   
# Function to rearrange the sides of
# triangle in increasing order of area
def rearrangeTriangle(arr , N):
   
  # Sort the array arr[] in increasing
  # order of area
  arr.sort(key = lambda x: (findArea(x)))
   
  # Resultant sides
  for i in range(0,N):
    print(arr[i][0], arr[i][1], arr[i][2])
              
# Driver Code
if __name__ == "__main__":
 
    arr = [[5 , 3 , 7], [15 , 20 , 4], [4 , 9 , 6], [8 , 4 , 5]]
    N = len(arr)
     
    rearrangeTriangle(arr, N)
     
    # This code is contributed by bhupenderyadav18.
Output: 
5 3 7
8 4 5
4 9 6
15 20 4

 

Time Complexity: O(N*log N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!