Future of God - Editorial

PROBLEM LINK:

Practice
Future Of God

Authors : professor_0212 , jeetjani_11
Testers : professor_0212 , jeetjani_11
Editorialist : professor_0212 , jeetjani_11

DIFFICULTY:

Cakewalk

PROBLEM:

Given three numbers a,b,c and a target number w; you have to check whether we can get w as a result by doing addition and/or subtraction operations on a,b,c. You can take a and/or b and/or c and do add or subtract and check whether we can get w as an answer at least one time.

Prerequisites:

  • knowledge of basic if-else conditions and I/O.
  • A little amount of brain to be applied while solving or understanding the editorial :wink:

Hint:

1 Point

A simple brute force logic to check all the possibilities.

99 Points

Just list down all the possibilities that we can measure and check if weight matches with at least anyone of these.

QUICK EXPLANATION:

We will be listing all the weights that we can measure with the three stones and checking that whether at least one of them matches with target weight w.
There will be 13 possible weights that we can measure which are : a, b, c, a+b, b+c, a+c, a+b+c, a+b-c, b+c-a, a+c-b, a-b, b-c, a-c.
Note that here, If any expression gives negative value then we have to consider the absolute value of it.

EXPLANATION:

As explained, we can measure 13 possible weights with these given 3 stones respectively a,b,c.
The 13 possibilities are:

  1. a
  2. b
  3. c
  4. a + b
  5. b + c
  6. a + c
  7. a + b + c
  8. abs(a - b)
  9. abs(b - c)
  10. abs(a - c)
  11. abs(a + b - c)
  12. abs(b + c - a)
  13. abs(a + c - b)
    We have to just put if conditional statements stating that anyone of these equals to w or not. If it does then print “YES”, otherwise “NO” and a new line.
    If there is any confusion or query regarding the editorial or approach feel free to ping me :+1:!!

SOLUTIONS:

Setter's Solution
#include<bits/stdc++h>
using namespace std;
#define endl “\n”
int main()
{
   int t, a1, a2 ,a3, w;
   cin >> t;
   while(t- -)
   {
   	cin >> w >> a1 >> a2 >> a3;
   	if (w == a1 || w == a2 || w == a3))
   	{
   		cout << "YES" << endl;
   	}
   	else if (w == a1 + a2 || w == a2 + a3 || w == a1 + a3 || w == a1 + a2 + a3)
   	{
   		cout << "YES" << endl;
   	}
   	else if (w == abs(a2 - a1) || w == abs(a3 - a2) || w == abs(a3 - a1))
   	{
   		cout << "YES" << endl;
   	}
   	else if (w == abs(a1 + a2 - a3) || w == abs(a3 + a2 - a1) || w == abs(a1 + a3 - a2))
   	{
   		cout << "YES" << endl;
   	}
   	else
   	{
   		cout << "NO" << endl;
   	}
   }
}

Tester's Solution
#include <iostream>
using namespace std;

string can_measure_weight(int a1, int a2, int a3, int w) 
{
   // Check if the weight to be measured is less than or equal to the sum of the three weighing stones
   if (w <= a1 + a2 + a3) 
   {
   	// Check if the weight can be formed by any combination weighing stones
   	if (w == a1 || w == a2 || w == a3 || w == a1 + a2 || w == a2 + a3 || w == a1 + a3 || w == abs(a2 - a1) || w == abs(a3 - a2) || w == abs(a3 - a1) || w == abs(a1 + a2 - a3) ||w == abs(a3 + a2 - a1) ||w == abs(a1 + a3 - a2) || w == a1 + a2 + a3)
   	{
   		return "YES";
   	}
   }
   // If the weight cannot be measured using the three weighing stones, return "NO"
   return "NO";
}

int main()
{
   int t , a1 , a2 , a3 , w ;
   cin>>t ;
   while(t--)
   {
   	cin>>w;
   	cin>>a1>>a2>>a3 ;
   	string result = can_measure_weight(a1, a2, a3, w);
   	cout << result << endl;
   }
   return 0;
}