Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1305 

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 ≤ M ≤ 20, the number of sticks. M integers follow; each gives the length of a stick — an integer between 1 and 10,000. Output For each case, output a line containing ‘yes’ if is is possible to form a square; otherwise output ‘no’.

Sample Input 3 4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5

Sample Output yes no yes

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        int res = INT_MAX;
        // find any square surface
        for(int l=1;l < =sqrt(n);l++)
        for(int h=l;h < =sqrt(n);h++)
        // check if exist width for such n,l,h
        if((n%h) == 0 && ((n/h)%l) == 0){
            int w = (n/h)/l;
            res = min(res, 2*l*w+2*h*w+2*l*h);
        }
        cout << res << endl;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3,
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Output

x
+
cmd
yes
no
y
Advertisements

Demonstration


UVA Online Judge solution - 10365-Blocks - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10364-Square - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10369-Arctic Network - UVA Online Judge solution in C,C++,java