Codeforce 886A ACM ICPC Solution

###Codeforce 886A ACM ICPC Solution
Problem No:886A
Problem Link:Codeforce 886A 



Solution in C++:
Source Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char const *argv[]) {
    int a[1000],sum1,sum=0;
    int i,j,k;
    for(i=0;i<6;i++){
      std::cin >> a[i];
      sum=sum+a[i];
    }
    for(i=0;i<4;i++){
      for(j=i+1;j<5;j++){
        for(k=j+1;k<6;k++){
          sum1=a[i]+a[j]+a[k];
          if(sum1==sum-sum1){
            std::cout << "YES" << '\n';
            exit(0);
          }

        }
      }
    }
    std::cout << "NO" << '\n';
  return 0;
}


Output:
1
3
2
1
2
1
YES

Press any key to continue . . .




Explanation:
Learn to solve problem easily by this book!!!
  



In this problem it's enough to iterate through all the triples checking whether its sum equals to the sum of remaining triple or not. Answer is "YES" if equality is possible and "NO" — otherwise.
Here we just executed three loops where we calculated the sum of any three numbers then we checked whether the sum is equal to the other remaining three or not.Once we get an equal case we instantly exited the loop printing "YES" else we kept checking.Then if there's no case that doesn't follow the condition then we should print "NO".




No comments

Powered by Blogger.