# Techgig code gladiators cards combination

## Combination of cards

A group of friends went for a vacation. One Night, they went to a casino and enjoyed a lot. A person from the group started gambling there and found his game hard to crack as it was very difficult and logical. That game is played with set of n number of cards. Number on cards are mentioned from 1 to n. One side of card is white and other is black colour.

On each chance he can perform any of the following 4 actions:

1) All the cards flip to opposite side, those that are white side are turned towards black side and those that are black are turned towards white side.

2) Flips the side of all the odd numbered cards.

3) Flips the side of all the even numbered cards.

4) Flips the side of the cards whose number is of the form 3K+1 (with K>=0), i.e., 1, 4, 7…

There is a counter that records total number of chances played. When the game starts, all the cards turn towards white side and counter is set to zero.

He needs to determine the final combinations of all the N cards that are consistent with the given information without any repetition of the card combinations.

Input Format

You will be given a function with four arguments. Argument 1: is the integer value which is total number of cards available.

Argument 2: is the integer value which is total number of chances.

Argument 3: Third input will be the string containing comma separated card numbers which are turned towards white side in the final combination. This string consist -1, if there is no restriction of white side.

Argument 4: Forth input will be the string containing comma separated card numbers, which are turned towards black side in the final combination. This string consist -1, if there is no restriction of black side.

Output Format

It will be a string containing all final combinations separated by #, where all white cards are denoted by 1 and all black cards are denoted by 0 in each combination and Combination of cards should be in numerical ascending order in the output string.

For example, if we have two final combinations ‘0110110110’ and ‘0101010101’ then the correct output will be “0101010101#0110110110” not “0110110110#0101010101”.

Constraints

The total number of cards are constrained by 10 <= N <= 100

The total number of chances are constrained by 1 <= C <= 10000

The number of cards you are informed to be on white side (W), in the final combination, is constrained by 0 <= W <= 2 .

The number of cards you are informed to be on black side (B), in the final combination,, is constrained by 0 <= B<= 2 .

There is at least one possible final combination for each input test case.

Sample Test Case 1

Input
10
1
-1
7

Output

0000000000#0101010101#0110110110

Explanation

In this case, there are three possible final combinations:

• Either all cards are turned towards black side
• All the cards 1, 4, 7, 10 are turned towards black side, and cards 2, 3, 5, 6, 8, 9 are turned towards white side
• All the cards 1, 3, 5, 7, 9 are turned towards black side, and cards 2, 4, 6, 8, 10 are turned towards white side

Sample Test Case 2

Input

40
3
16
2,25

Output

0011100011100011100011100011100011100011

## Solution in C++

#include <iostream>
#include <cstring>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

char flip(char ch) {
if(ch == ‘0’) return ‘1’;
return ‘0’;
}

string E(string s) {
for(int i = 0; i < s.length(); i++) {
if(i % 2 == 1) {
s[i] = flip(s[i]);
}
}
return s;
}

string O(string s) {
for(int i = 0; i < s.length(); i++) {
if(i % 2 == 0) {
s[i] = flip(s[i]);
}
}
return s;
}

string A(string s) {
for(int i = 0; i < s.length(); i++) {
s[i] = flip(s[i]);
}
return s;
}

string _3(string s) {
for(int i = 0; i < s.length(); i++) {
if(i % 3 == 0) {
s[i] = flip(s[i]);
}
}
return s;
}

vector<int> strToInt(string str) {
vector<int> vect;
stringstream ss(str);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == ‘,’)
ss.ignore();
}
return vect;
}

void printHash(vector<string> v) {
sort(v.begin(), v.end());

for(int i = 0; i < v.size(); i++) {
if(i != v.size() – 1) {
cout << v[i] << “#”;
}
else {
cout << v[i] << endl;
}
}
}

void checkWB(vector<string> s) {
cout << “hello” << endl;
}

int main() {
int N, C;
cin >> N;
cin >> C;
vector<string> s(8);

string str(N, ‘1’);
s = O(str); //O
s = E(str); //E
s = _3(str); //3
s = A(str); //A (all ‘0’)
s = E(s); //3E
s = A(s); //3A
s = O(s); //3O
s = str; //(all ‘1’)

string W, B;
cin >> W >> B;

vector<int> vW = strToInt(W);
vector<int> vB = strToInt(B);

if(vW == -1 && vB == -1) {
if(C == 1) {
vector<string> _s(s.begin(), s.begin() + 4);
printHash(_s);
}
else if(C == 2) {
vector<string> ss(s.begin(), s.begin() + 2);
for(int i = 3; i < 8; i++) {
ss.push_back(s[i]);
}
printHash(ss);
}
else {
printHash(s);
}
} else {
if(C == 1) {
s.erase(s.end() – 4, s.end()); //remove (all ‘1’ case from vector)
}
else if(C == 2) {
s.erase(s.begin() + 2);
}

if(vW != -1) {
vector<string> newS;
for(int i = 0; i < vW.size(); i++) {
for(int j = 0; j < s.size(); j++) {
if(s[j][vW[i] – 1] == ‘1’) {
newS.push_back(s[j]);
}
}
s.resize(0);
s = newS;
newS.resize(0);
}
}

if(vB != -1) {
vector<string> newS;
for(int i = 0; i < vB.size(); i++) {
for(int j = 0; j < s.size(); j++) {
if(s[j][vB[i] – 1] == ‘0’) {
newS.push_back(s[j]);
}
}
s.resize(0);
s = newS;
newS.resize(0);
}
}
printHash(s);
}

return 0;
}