Weights Codechef Solution

Hello Programmers In this post, you will know how to solve the Weights Codechef Solution. The Problem Code is WGHTS.

Weights Codechef Solution
Weights Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

Chef is playing with weights. He has an object weighing W units. He also has three weights each of X, Y and Z units respectively. Help him determine whether he can measure the exact weight of the object with one or more of these weights.

If it is possible to measure the weight of object with one or more of these weights, print YES, otherwise print NO.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of single line containing a four positive integers ,W,X,Y, and Z.

Output Format

For each test case, output on a new line YES if it is possible to measure the weight of object with one or more of these weights, otherwise print NO.

You may print each character of the string in either uppercase or lowercase (for example, the strings yesYESYes, and yeS will all be treated as identical).

Constraints

Weights Codechef Solution

Sample 1

Input

4
5 2 1 6
7 9 7 2
20 8 10 12
20 10 11 12

NO
YES
YES
NO

LinkedIn Skill Assessment Answers

Coursera Quiz Answers

Explanation:

Test Case 1: It is not possible to measure 5 units using any combination of given weights.

Test Case 2: Chef can use the second weight of 7 units to measure the object exactly.

Test Case 3: Chef can use combination of first and third weights to measure 8+12=20 units.

Test Case 4: Chef cannot measure 20 units of weight using any combination of given weights

Weights Codechef Solution in CPP

#include <iostream>
using namespace std;
int main() {
	int T;
	cin>>T;
	cout<<endl;
	for(int i=0;i<T;i++)
	{
	    int W,X,Y,Z;
	    cin>>W>>X>>Y>>Z;
	    if(W==X || W==Y || W==Z)
	    {
	        cout<<"\nYES";
	    }
	    else if( (X+Y)==W || (X+Z)==W || (Y+Z)==W )
	    {
	        cout<<"\nYES";
	    }
	    else if(W == (X+Y+Z))
	    {
	        cout<<"\nYES";
	    }
	    else
	    {
	        cout<<"\nNO";
	    }
	}
}
       

Weights Codechef Solution in JAVA

/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int x=sc.nextInt();
		for(int i=0;i<x;i++){
		    int a=sc.nextInt();
		    int b=sc.nextInt();
		    int c=sc.nextInt();
		    int d=sc.nextInt();
		    if(b+c==a || c+d==a || b+d==a || b+c+d==a){
		        System.out.println("YES");
		    }else if (b==a || c==a || d==a){
		        System.out.println("YES");
		    }else{
		        System.out.println("NO");
		    }
		}
	}
}

Weights Codechef Solution in Python

# cook your dish here
for _ in range(int(input())):
    W, X, Y, Z = map(int, input().split(' '))
    weights = [X, Y, Z, X+Y, X+Z, Y+Z, X+Y+Z]
    if W in weights:
        print('YES')
    else:
        print('NO')
    

Disclaimer: The above Problem (Weights) is generated by CodeChef but the solution is provided by  BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Greater Average Codechef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *