Team 7 faces a horrible foe. He can only be defeated with a special quadruple combination attack of strength (\$1 <=S <= 10^9\$). Naruto, Sasuke, Sakura and Kakashi must attack simultaneously to perform the combo. Each of them can choose from \$N\$ (\$1 <= N <=1000\$) attacks, having strengths \$si\$ each (\$0 <= i < N\$, \$1 <= si <=10^9\$). The strengths of individual attacks add up to form the strength of the combo.
Is there a valid combination that they can use? Note that the same attacks are available to all of them.
You are required to write a function which takes input as follows:
An integer \$N\$ as number of attacks, an integer vector
s[]
as the strengths of \$N\$ attacks and an integer S as the required strength of the combo. Set the output variable to the number of distinct valid combos.Two combinations are different if they differ in strength of at least one attack used.
Input: 1 {1} 4
Output: 1 ===> {1,1,1,1}Input: 2 {1,2} 5
Output: 1 ===> {1,1,1,2}
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
const int noOfPalyers = 4;
int validCombo(int input1,int input2[],int input3)
{
std::cout<< "-----------------------------------------------------------------------------"<<"\n";
int count = 0;
std::vector<int> vec;
int size =input1*noOfPalyers;
for(int i = 0; i < input1; i++)
{
for(int j = 0; j < noOfPalyers;j++)
{
vec.push_back(input2[i]);
}
}
std::vector< std::vector< std::pair<int, int> > > vecHash;
for(int i =0; i < (size*size); i++)
{
vecHash.push_back(std::vector< std::pair<int, int> > ());
}
for(int i =0; i < size; i++)
{
for(int j =1; j < size; j++)
{
int key = vec[i] + vec[j];
if(vec[i]<= vec[j])
vecHash[key].push_back(std::make_pair(vec[i], vec[j]));
else
vecHash[key].push_back(std::make_pair(vec[j], vec[i]));
}
}
std::set< std::vector<int> > solution;
for(int i = 0; i < input3; i++)
{
if(vecHash[input3 - i].size() > 0)
{
for(int k = 0; k < vecHash[i].size() ; k++)
{
std::pair<int, int> fPair = vecHash[i][k];
for(int m =0; m < vecHash[input3 - i].size(); m++)
{
std::pair<int, int> sPair = vecHash[input3 - i][m];
std::vector<int> vec;
vec.push_back(fPair.first);
vec.push_back(fPair.second);
vec.push_back(sPair.first);
vec.push_back(sPair.second);
std::sort(vec.begin(), vec.end());
solution.insert(vec);
}
}
}
}
std::set< std::vector< int> >::iterator it;
for (it = solution.begin(); it != solution.end(); it++)
{
std::cout<< "\n";
for(int i = 0; i < (*it).size(); i++)
std::cout << (*it)[i] << " , ";
std::cout<< "\n";
}
count = solution.size();
std::cout << "solution size = " << solutionSet.size();
std::cout<< "\n";
std::cout<< "\n";
std::cout<< "-----------------------------------------------------------------------------"<<"\n";
return count;
}
int main()
{
int i = 3;
int arr[] = {1,2,3};
int j = 7;
int arr1[] ={1};
std::cout <<"o/p == " << validCombo(i, arr, j)<< "\n";
std::cout <<"o/p == " << validCombo(1, arr1, 4);
getchar();
return 0;
}