Given four arrays of 3 numbers each which represents sides and angles of two triangles. The task is to check if the two triangles are Congruent or not. Also print the theorem by which they are congruent. Note: All sides and angles given as input are for valid triangles. Examples:
Input : side1 = [3, 4, 5] angle1 = [90, 60, 30]
side2 = [4, 3, 5] angle2 = [60, 30, 90]
Output: Triangles are congruent by SSS SAS ASA AAS HL.
Input : side1 = [3, 5, 6] angle1 = [80, 50, 50]
side2 = [1, 1, 1] angle2 = [60, 60, 60]
Output: Triangles are not congruent
Congruent triangles are two or more triangles that have all corresponding sides that are equal or a pair of sides and between angle are equal or a pair of angle and side between are equal or a pair of angle and other side are equal or hypotenuse and one side are equal. The congruency of triangles can be proved by the following theorems:
- Side-Side-Side (SSS) Congruency criteria: If all the sides of a triangle are equal to the sides of another triangle then the triangles are said to be congruent by the property of Side-Side-Side (SSS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and CA=C’A’ then, triangles are congruent.
- Side-Angle-Side (SAS) Congruent criteria: If two sides of the two triangles are equal and the angle between them is same in both triangle then the triangles are said to be congruent by the property of Side-Angle-Side (SAS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and
= triangles are congruent. - Angle-Side-Angle (ASA) Congruent criteria :If two angles of the two triangles are equal and the length of side between them is same in both triangle then the triangles are said to be congruent by the property of Angle-Side-Angle (ASA).In above triangle ABC and A’B’C’ if,
= and = and BC=B’C’ then, triangles are congruent. - Angle-Angle-Side (AAS) Congruent criteria :If two angles of the two triangles are equal and the length of other side is same in both triangle then the triangles are said to be congruent by the property of Angle-Angle-Side (AAS). In above triangle ABC and A’B’C’ if,
= and = and CA=C’A’ then, triangles are congruent. - Hypotenuse-Leg (HL) Congruent criteria : If the hypotenuse of the two triangles are equal and the length of any other one side is same in both triangle then the triangles are said to be congruent by the property of Hypotenuse-Leg (HL).
Below is the implementation of the above theorems.
C++
#include<bits/stdc++.h>
using namespace std;
int cong_sas(vector< int > s1,vector< int > s2,vector< int > a1,vector< int > a2){
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
sort(a1.begin(), a1.end());
sort(a2.begin(), a2.end());
if (s1[0] == s2[0] && s1[1] == s2[1]){
if (a1[2] == a2[2]){
return 1;
}
}
if ( s1[1] == s2[1] && s1[2] == s2[2]){
if ( a1[0] == a2[0]){
return 1;
}
}
if ( s1[2] == s2[2] && s1[0] == s2[0]){
if ( a1[1] == a2[1]){
return 1;
}
}
return 0;
}
int cong_asa(vector< int > s1,vector< int > s2,vector< int > a1,vector< int > a2){
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
sort(a1.begin(), a1.end());
sort(a2.begin(), a2.end());
if (a1[0] == a2[0] && a1[1] == a2[1]){
if (s1[2] == s2[2]){
return 1;
}
}
if ( a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[0] == s2[0]){
return 1;
}
}
if ( a1[2] == a2[2] && a1[0] == a2[0]){
if (s1[1] == s2[1]){
return 1;
}
}
return 0;
}
int cong_aas(vector< int > s1, vector< int > s2, vector< int > a1, vector< int > a2){
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
sort(a1.begin(), a1.end());
sort(a2.begin(), a2.end());
if (a1[0] == a2[0] && a1[1] == a2[1]){
if (s1[0] == s2[0] || s1[1] == s2[1]){
return 1;
}
}
if (a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[1] == s2[1] || s1[2] == s2[2]){
return 1;
}
}
if (a1[2] == a2[2] && a1[0] == a2[0]){
if ( s1[0] == s2[0] || s1[2] == s2[2]){
return 1;
}
}
return 0;
}
int cong_hl(vector< int > s1, vector< int > s2){
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (s1[2] == s2[2]){
if ( s1[1] == s2[1] || s1[0] == s2[0]){
return 1;
}
}
return 0;
}
int cong_sss(vector< int > s1,vector< int > s2){
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){
return 1;
}
return 0;
}
int main()
{
vector< int > s1 = {3, 4, 5};
vector< int > s2 = {4, 3, 5};
vector< int > a1 = {90, 60, 30};
vector< int > a2 = {60, 30, 90};
int sss = cong_sss(s1, s2);
int sas = cong_sas(s1, s2, a1, a2);
int asa = cong_asa(s1, s2, a1, a2);
int aas = cong_aas(s1, s2, a1, a2);
int hl = cong_hl(s1, s2);
if (sss || sas || asa || aas || hl){
cout << "Triangles are congruent by " << endl;
if (sss) cout << "SSS " << endl;
if (sas) cout << "SAS " << endl;
if (asa) cout << "ASA " << endl;
if (aas) cout << "AAS " << endl;
if (hl) cout << "HL " << endl;
}
else cout << "Triangles are not congruent" << endl;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int cong_sas( int [] s1, int [] s2, int [] a1, int [] a2){
Arrays.sort(s1);
Arrays.sort(s2);
Arrays.sort(a1);
Arrays.sort(a2);
if (s1[ 0 ] == s2[ 0 ] && s1[ 1 ] == s2[ 1 ]){
if (a1[ 2 ] == a2[ 2 ]){
return 1 ;
}
}
if ( s1[ 1 ] == s2[ 1 ] && s1[ 2 ] == s2[ 2 ]){
if ( a1[ 0 ] == a2[ 0 ]){
return 1 ;
}
}
if ( s1[ 2 ] == s2[ 2 ] && s1[ 0 ] == s2[ 0 ]){
if ( a1[ 1 ] == a2[ 1 ]){
return 1 ;
}
}
return 0 ;
}
static int cong_asa( int [] s1, int [] s2, int [] a1, int [] a2){
Arrays.sort(s1);
Arrays.sort(s2);
Arrays.sort(a1);
Arrays.sort(a2);
if (a1[ 0 ] == a2[ 0 ] && a1[ 1 ] == a2[ 1 ]){
if (s1[ 2 ] == s2[ 2 ]){
return 1 ;
}
}
if ( a1[ 1 ] == a2[ 1 ] && a1[ 2 ] == a2[ 2 ]){
if (s1[ 0 ] == s2[ 0 ]){
return 1 ;
}
}
if ( a1[ 2 ] == a2[ 2 ] && a1[ 0 ] == a2[ 0 ]){
if (s1[ 1 ] == s2[ 1 ]){
return 1 ;
}
}
return 0 ;
}
static int cong_aas( int [] s1, int [] s2, int [] a1, int [] a2){
Arrays.sort(s1);
Arrays.sort(s2);
Arrays.sort(a1);
Arrays.sort(a2);
if (a1[ 0 ] == a2[ 0 ] && a1[ 1 ] == a2[ 1 ]){
if (s1[ 0 ] == s2[ 0 ] || s1[ 1 ] == s2[ 1 ]){
return 1 ;
}
}
if (a1[ 1 ] == a2[ 1 ] && a1[ 2 ] == a2[ 2 ]){
if (s1[ 1 ] == s2[ 1 ] || s1[ 2 ] == s2[ 2 ]){
return 1 ;
}
}
if (a1[ 2 ] == a2[ 2 ] && a1[ 0 ] == a2[ 0 ]){
if ( s1[ 0 ] == s2[ 0 ] || s1[ 2 ] == s2[ 2 ]){
return 1 ;
}
}
return 0 ;
}
static int cong_hl( int [] s1, int [] s2){
Arrays.sort(s1);
Arrays.sort(s2);
if (s1[ 2 ] == s2[ 2 ]){
if ( s1[ 1 ] == s2[ 1 ] || s1[ 0 ] == s2[ 0 ]){
return 1 ;
}
}
return 0 ;
}
static int cong_sss( int [] s1, int [] s2){
Arrays.sort(s1);
Arrays.sort(s2);
if (s1[ 0 ] == s2[ 0 ] && s1[ 1 ] == s2[ 1 ] && s1[ 2 ] == s2[ 2 ]){
return 1 ;
}
return 0 ;
}
public static void main(String[] args) {
int [] s1 = { 3 , 4 , 5 };
int [] s2 = { 4 , 3 , 5 };
int [] a1 = { 90 , 60 , 30 };
int [] a2 = { 60 , 30 , 90 };
int sss = cong_sss(s1, s2);
int sas = cong_sas(s1, s2, a1, a2);
int asa = cong_asa(s1, s2, a1, a2);
int aas = cong_aas(s1, s2, a1, a2);
int hl = cong_hl(s1, s2);
if (sss == 1 || sas == 1 || asa == 1 || aas == 1 || hl == 1 ){
System.out.println( "Triangles are congruent by " );
if (sss == 1 ) System.out.println( "SSS " );
if (sas == 1 ) System.out.println( "SAS " );
if (asa == 1 ) System.out.println( "ASA " );
if (aas == 1 ) System.out.println( "AAS " );
if (hl == 1 ) System.out.println( "HL " );
}
else System.out.println( "Triangles are not congruent" );
}
}
|
Python
def cong_sas(s1, s2, a1, a2):
s1 = [ float (i) for i in s1]
s2 = [ float (i) for i in s2]
a1 = [ float (i) for i in a1]
a2 = [ float (i) for i in a2]
s1.sort()
s2.sort()
a1.sort()
a2.sort()
if s1[ 0 ] = = s2[ 0 ] and s1[ 1 ] = = s2[ 1 ]:
if a1[ 2 ] = = a2[ 2 ]:
return 1
if s1[ 1 ] = = s2[ 1 ] and s1[ 2 ] = = s2[ 2 ]:
if a1[ 0 ] = = a2[ 0 ]:
return 1
if s1[ 2 ] = = s2[ 2 ] and s1[ 0 ] = = s2[ 0 ]:
if a1[ 1 ] = = a2[ 1 ]:
return 1
return 0
def cong_asa(s1, s2, a1, a2):
s1 = [ float (i) for i in s1]
s2 = [ float (i) for i in s2]
a1 = [ float (i) for i in a1]
a2 = [ float (i) for i in a2]
s1.sort()
s2.sort()
a1.sort()
a2.sort()
if a1[ 0 ] = = a2[ 0 ] and a1[ 1 ] = = a2[ 1 ]:
if s1[ 2 ] = = s2[ 2 ]:
return 1
if a1[ 1 ] = = a2[ 1 ] and a1[ 2 ] = = a2[ 2 ]:
if s1[ 0 ] = = s2[ 0 ]:
return 1
if a1[ 2 ] = = a2[ 2 ] and a1[ 0 ] = = a2[ 0 ]:
if s1[ 1 ] = = s2[ 1 ]:
return 1
return 0
def cong_aas(s1, s2, a1, a2):
s1 = [ float (i) for i in s1]
s2 = [ float (i) for i in s2]
a1 = [ float (i) for i in a1]
a2 = [ float (i) for i in a2]
s1.sort()
s2.sort()
a1.sort()
a2.sort()
if a1[ 0 ] = = a2[ 0 ] and a1[ 1 ] = = a2[ 1 ]:
if s1[ 0 ] = = s2[ 0 ] or s1[ 1 ] = = s2[ 1 ]:
return 1
if a1[ 1 ] = = a2[ 1 ] and a1[ 2 ] = = a2[ 2 ]:
if s1[ 1 ] = = s2[ 1 ] or s1[ 2 ] = = s2[ 2 ]:
return 1
if a1[ 2 ] = = a2[ 2 ] and a1[ 0 ] = = a2[ 0 ]:
if s1[ 0 ] = = s2[ 0 ] or s1[ 2 ] = = s2[ 2 ]:
return 1
return 0
def cong_hl(s1, s2):
s1 = [ float (i) for i in s1]
s2 = [ float (i) for i in s2]
s1.sort()
s2.sort()
if s1[ 2 ] = = s2[ 2 ]:
if s1[ 1 ] = = s2[ 1 ] or s1[ 0 ] = = s2[ 0 ]:
return 1
return 0
def cong_sss(s1, s2):
s1 = [ float (i) for i in s1]
s2 = [ float (i) for i in s2]
s1.sort()
s2.sort()
if (s1[ 0 ] = = s2[ 0 ] and s1[ 1 ] = = s2[ 1 ] and s1[ 2 ] = = s2[ 2 ]):
return 1
return 0
s1 = [ 3 , 4 , 5 ]
s2 = [ 4 , 3 , 5 ]
a1 = [ 90 , 60 , 30 ]
a2 = [ 60 , 30 , 90 ]
sss = cong_sss(s1, s2)
sas = cong_sas(s1, s2, a1, a2)
asa = cong_asa(s1, s2, a1, a2)
aas = cong_aas(s1, s2, a1, a2)
hl = cong_hl(s1, s2, )
if sss or sas or asa or aas or hl :
print "Triangles are congruent by",
if sss: print "SSS",
if sas: print "SAS",
if asa: print "ASA",
if aas: print "AAS",
if hl: print "HL",
else : print "Triangles are not congruent"
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int cong_sas(List< int > s1,List< int > s2,List< int > a1,List< int > a2){
s1.Sort();
s2.Sort();
a1.Sort();
a2.Sort();
if (s1[0] == s2[0] && s1[1] == s2[1]){
if (a1[2] == a2[2]){
return 1;
}
}
if ( s1[1] == s2[1] && s1[2] == s2[2]){
if ( a1[0] == a2[0]){
return 1;
}
}
if ( s1[2] == s2[2] && s1[0] == s2[0]){
if ( a1[1] == a2[1]){
return 1;
}
}
return 0;
}
static int cong_asa(List< int > s1,List< int > s2,List< int > a1,List< int > a2){
s1.Sort();
s2.Sort();
a1.Sort();
a2.Sort();
if (a1[0] == a2[0] && a1[1] == a2[1])
{
if (s1[2] == s2[2]){
return 1;
}
}
if ( a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[0] == s2[0]){
return 1;
}
}
if ( a1[2] == a2[2] && a1[0] == a2[0]){
if (s1[1] == s2[1]){
return 1;
}
}
return 0;
}
static int cong_aas(List< int > s1, List< int > s2, List< int > a1, List< int > a2){
s1.Sort();
s2.Sort();
a1.Sort();
a2.Sort();
if (a1[0] == a2[0] && a1[1] == a2[1])
{
if (s1[0] == s2[0] || s1[1] == s2[1]){
return 1;
}
}
if (a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[1] == s2[1] || s1[2] == s2[2]){
return 1;
}
}
if (a1[2] == a2[2] && a1[0] == a2[0]){
if ( s1[0] == s2[0] || s1[2] == s2[2]){
return 1;
}
}
return 0;
}
static int cong_hl(List< int > s1, List< int > s2){
s1.Sort();
s2.Sort();
if (s1[2] == s2[2]){
if ( s1[1] == s2[1] || s1[0] == s2[0]){
return 1;
}
}
return 0;
}
static int cong_sss(List< int > s1,List< int > s2){
s1.Sort();
s2.Sort();
if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){
return 1;
}
return 0;
}
static void Main( string [] args)
{
List< int > s1 = new List< int >(){3, 4, 5};
List< int > s2 = new List< int >(){4, 3, 5};
List< int > a1 = new List< int >(){90, 60, 30};
List< int > a2 = new List< int >(){60, 30, 90};
int sss = cong_sss(s1, s2);
int sas = cong_sas(s1, s2, a1, a2);
int asa = cong_asa(s1, s2, a1, a2);
int aas = cong_aas(s1, s2, a1, a2);
int hl = cong_hl(s1, s2);
if (sss==1 || sas==1 || asa==1 || aas==1 || hl==1){
Console.WriteLine( "Triangles are congruent by " );
if (sss==1)
Console.WriteLine( "SSS " );
if (sas==1)
Console.WriteLine( "SAS " );
if (asa==1)
Console.WriteLine( "ASA " );
if (aas==1)
Console.WriteLine( "AAS " );
if (hl==1)
Console.WriteLine( "HL " );
}
else
Console.WriteLine( "Triangles are not congruent" );
}
}
|
Javascript
<script>
function cong_sas(s1, s2, a1, a2){
s1.sort();
s2.sort();
a1.sort();
a2.sort();
if (s1[0] == s2[0] && s1[1] == s2[1]){
if (a1[2] == a2[2]){
return 1;
}
}
if ( s1[1] == s2[1] && s1[2] == s2[2]){
if ( a1[0] == a2[0]){
return 1;
}
}
if ( s1[2] == s2[2] && s1[0] == s2[0]){
if ( a1[1] == a2[1]){
return 1;
}
}
return 0;
}
function cong_asa(s1, s2, a1, a2){
s1.sort();
s2.sort();
a1.sort();
a2.sort();
if (a1[0] == a2[0] && a1[1] == a2[1]){
if (s1[2] == s2[2]){
return 1;
}
}
if ( a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[0] == s2[0]){
return 1;
}
}
if ( a1[2] == a2[2] && a1[0] == a2[0]){
if (s1[1] == s2[1]){
return 1;
}
}
return 0;
}
function cong_aas(s1, s2, a1, a2){
s1.sort();
s2.sort();
a1.sort();
a2.sort();
if (a1[0] == a2[0] && a1[1] == a2[1]){
if (s1[0] == s2[0] || s1[1] == s2[1]){
return 1;
}
}
if (a1[1] == a2[1] && a1[2] == a2[2]){
if (s1[1] == s2[1] || s1[2] == s2[2]){
return 1;
}
}
if (a1[2] == a2[2] && a1[0] == a2[0]){
if ( s1[0] == s2[0] || s1[2] == s2[2]){
return 1;
}
}
return 0;
}
function cong_hl(s1, s2){
s1.sort();
s2.sort();
if (s1[2] == s2[2]){
if ( s1[1] == s2[1] || s1[0] == s2[0]){
return 1;
}
}
return 0;
}
function cong_sss(s1, s2){
s1.sort();
s2.sort();
if (s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){
return 1;
}
return 0;
}
s1 = [3, 4, 5];
s2 = [4, 3, 5];
a1 = [90, 60, 30];
a2 = [60, 30, 90];
sss = cong_sss(s1, s2);
sas = cong_sas(s1, s2, a1, a2);
asa = cong_asa(s1, s2, a1, a2);
aas = cong_aas(s1, s2, a1, a2);
hl = cong_hl(s1, s2, );
if (sss || sas || asa || aas || hl){
document.write( "Triangles are congruent by " );
if (sss) document.write( "SSS " );
if (sas) document.write( "SAS " );
if (asa) document.write( "ASA " );
if (aas) document.write( "AAS " );
if (hl) document.write( "HL " );
}
else document.write( "Triangles are not congruent" );
</script>
|
Output:Triangles are congruent by SSS SAS ASA AAS HL
Time Complexity: O(1) As the arrays have only 3 elements, so the total time taken can be treated as constant. Auxiliary Space: O(1)
|