# [HELP] I am not able to make logic on this question

There is a set of 3 numbers a,b,c and a number x.
All you have to do is to check whether it is possible to make a Right Angled Triangle by adding the
value x to any two of the numbers in the given triplet. (After adding value x assume triplet as the
sides of the triangle).
You have to print “YES” if it is possible or else print “NO” without the quotation marks.
It is necessary to add the number x.

INPUT FORMAT
First line contains a number T denoting total number of test cases.
Each test case contains four space separated integers a,b,c,x .
OUTPUT FORMAT
In the output you have to print YES or NO according to the condition discussed above.
CONSTRAINTS
1<=T<10
-10^17 < a,b,c,x < 10^17

Example
1
3 1 6 7
output-YES

1 Like

What is the name of this one? In making sure I understand the problem, I added 7 to both 3 and 1, getting the numbers 10, 8, 6, which is a scalar multiple of the well known 3-4-5 right triangle. So I see how it’s a YES. I am thinking about how to make an efficient algorithm to figure this out, though.

Since there are only `3 numbers` there will be `3 combinations` and you add x one by one and check if its satisfies `Right Triangle property`.

3 Likes

yes best approach to check that would be using pythagoras theorem …

1 Like

But the constraints are 10^17 it would overflow

1 Like

It was given in my college coding test named Check the Triplet on hackerearth

Best would be to use python in this case.

Can anyone help with cpp?

This might help you. The code is bit clumsy, please bear with it
@hyphencore700

``````#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MAX_LIMIT 10000000
#define remove_zero(str) str.erase(0, str.find_first_not_of('0'));
#define all(x) x.begin(),x.end()
#define br break
#define pb push_back
using ld = long double;
#define endl "\n"
using namespace std;
string multiply(string num1, string num2)
{
ll n1 = num1.size();
ll n2 = num2.size();
if (n1 == 0 || n2 == 0)
return "0";

vector<ll> result(n1 + n2, 0);

ll i_n1 = 0;
ll i_n2 = 0;

for (ll i = n1 - 1; i >= 0; i--) {
ll carry = 0;
ll n1 = num1[i] - '0';

i_n2 = 0;

for (ll j = n2 - 1; j >= 0; j--) {

ll n2 = num2[j] - '0';

ll sum = n1 * n2 + result[i_n1 + i_n2] + carry;

carry = sum / 10;

result[i_n1 + i_n2] = sum % 10;

i_n2++;
}

if (carry > 0)
result[i_n1 + i_n2] += carry;

i_n1++;
}

ll i = result.size() - 1;
while (i >= 0 && result[i] == 0)
i--;

if (i == -1)
return "0";

string s = "";
while (i >= 0)
s += std::to_string(result[i--]);

return s;
}
string findSum(string str1, string str2)
{
if (str1.length() > str2.length())
swap(str1, str2);

string str = "";

ll n1 = str1.length(), n2 = str2.length();
ll diff = n2 - n1;

ll carry = 0;

for (ll i=n1-1; i>=0; i--)
{
ll sum = ((str1[i]-'0') +
(str2[i+diff]-'0') +
carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}

for (ll i=n2-n1-1; i>=0; i--)
{
ll sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
if (carry)
str.push_back(carry+'0');

reverse(str.begin(), str.end());

return str;
}
bool isSmaller(string str1, string str2)
{
int n1 = str1.length(), n2 = str2.length();

if (n1 < n2)
return true;
if (n2 < n1)
return false;

for (int i=0; i<n1; i++)
if (str1[i] < str2[i])
return true;
else if (str1[i] > str2[i])
return false;

return false;
}
string findDiff(string str1, string str2)
{
if (isSmaller(str1, str2))
swap(str1, str2);

string str = "";
ll n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());

ll carry = 0;
for (ll i=0; i<n2; i++)
{

ll sub = ((str1[i]-'0')-(str2[i]-'0')-carry);
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;

str.push_back(sub + '0');
}
for (ll i=n2; i<n1; i++)
{
ll sub = ((str1[i]-'0') - carry);
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;

str.push_back(sub + '0');
}
reverse(str.begin(), str.end());

return remove_zero(str);
}

void solve(){
//input
string a,b,c,x;
cin>>a>>b>>c>>x;
string s1=findSum(a,x),s2=findSum(b,x),s3=findSum(c,x);
string str1=multiply(s1,s1),str2=multiply(s2,s2),str3=multiply(s3,s3);
string f=findSum(str1,str2),f1=findSum(str2,str3),f3=findSum(str1,str3);
string ans=multiply(a,a),ans1=multiply(b,b),ans2=multiply(c,c);
if(f==ans2 or str1==findDiff(ans2,str2) or str2==findDiff(ans2,str1)) {
cout<<"Yes"<<endl;
}
else if(f1==ans or str2==findDiff(ans,str3) or str3==findDiff(ans,str2)){
cout<<"Yes"<<endl;
}
else if(f3==ans1 or str1==findDiff(ans1,str3) or str3==findDiff(ans1,str1)){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
int32_t main()
{
fio;
ll t=1;
cin>>t;
while(t--){
solve();
}
}``````
2 Likes

You can use
Take any two sides three times and check that
Max(a,b,c)<sum of other two if yes then check
a/(c-b)==(b+c)/a. If yes then triangle is right angled . In this way it will not overflow with constraints