SPOJ : SUMFOUR - 4 values whose sum is 0
My Idea about this problem is given below :
At first i stored 4 columns in four arrays namely A[ ] , B[ ] , C[ ] , D[ ] .
Then i stored summations of all combinations of A[ ] and B[ ] array ( A*B SET ) in an Array namely mp[ ] .
Again i stored summations of all combinations of C[ ] and D[ ] array ( C*D SET ) in an Array namely mp2[ ] .
Then u can traverse any one of these two arrays ( mp[ ] , mp2[ ] ) and for each of the value in
traversing array( suppose mp[ ]) , find out how many elements of other array(because of repeatation in the array mp2[ ] ) coincide , Be careful , you have to remember that repeatation can happen in your traversing ( mp[ ] )array also.
Code is given below:
#include<bits/stdc++.h>
At first i stored 4 columns in four arrays namely A[ ] , B[ ] , C[ ] , D[ ] .
Then i stored summations of all combinations of A[ ] and B[ ] array ( A*B SET ) in an Array namely mp[ ] .
Again i stored summations of all combinations of C[ ] and D[ ] array ( C*D SET ) in an Array namely mp2[ ] .
Then u can traverse any one of these two arrays ( mp[ ] , mp2[ ] ) and for each of the value in
traversing array( suppose mp[ ]) , find out how many elements of other array(because of repeatation in the array mp2[ ] ) coincide , Be careful , you have to remember that repeatation can happen in your traversing ( mp[ ] )array also.
Code is given below:
Comments
Post a Comment