# Determine winner of game by converting two consecutive 1s to 0s in C++

## Introduction to the Problem:

The problem description revolves round a very simple game using bits in sequence, for which players could change their move as turn by turn comes. The in-game objective is to convert two consecutive 1s into zero, which will be reinforced by the provided moves. After that, the player who has a successful combination of moves will have a chance to win. The player whose pioneer will be able to substitute all pairs of consecutive 1s by 0s will come out winner.

## Problem Overview:

Initial State: The game derives from a series of binary digits (0s-1s) depicting the initial position.

Player Moves: Players in turn make their moves. A move implies the search of for two occupied ones at a time which then gives way to zeroes.

Winning Condition: The gamble comes to an end, and the player collects the money only when no two consecutive ones are discovered. If Every Pair is Changed into 0s, it goes to the winner Player 1. The last people will be alive if the binary digits are still some 1s after that.

## Implementation in C++:

The mechanism for string manipulation can be used for easier implementation in C++ language. We are using 'determineWinner()' function as an input, which is iterated by constant pairs of 0s till it converts to 1s. After that, we bring our made sequence to see who won.

### Program:

Output:

```Enter the sequence of 0s and 1s: 110101000110
Iteration 1:
Found pair of consecutive 1s at index 0 and 1. Converting them to 0s.
Found pair of consecutive 1s at index 9 and 10. Converting them to 0s.
Updated sequence: 000101000000
Iteration 2:
Updated sequence: 000101000000
Player 2 wins!
```

Explanation:

The code includes the execution direction (using iostream) and string manipulation functions (string).

• Function determineWinner():

The computer determines the winning outcome of the game by taking the initial 0s and 1s to evaluate the output. It goes from item to item in the sequence, comparing pairs of consecutive ones. If that's the case, it will do them zero instead. This procedure is carried on until no pairs of 1s can be placed in a consecutive sequence. Lastly, the winner is decided based on the element of remaining blind spots.

• Main Function:

The main() function is responsible for directing the user to input the initial sequence of the binaries. The function determineWinner() is called when the following initial sequence is provided. After the game (i.e. who wins), the display is achieved.

The comments within the code lean on every step of the procedure, such as initializing variables, looping through the sequence, and checking for consecutive pairs of 1s to complete the task successfully. Gives about the discussion also gives a background about iteration counting and how the function/sequence changes in each iteration.

• Output:

In the middle of the function execution of determineWinner(), a host of different message dots to the console are printed sequentially to show how the game is going. These are namely: the current version, the position indices of the series of ones discovered in each iteration, and the sequence changed after the iteration.

The code basically gives the player a more structured technique of winning the game by having the computer substitute pairs of consecutive 1s with a single 0s. It comprises feedback and verbose data results that will help user to debug and get used to the grammar.

### Complexity Analysis:

Time Complexity:

The time complexity of the code depends mostly on the length of the input sequence.

Iteration through Sequence:

The algorithm repeats the tests on the input sequence till there is no more combination found that is only composed of 1s two times consecutively. In the unfavourable situation where the input sequence consists only of 1s, the code will iterate more than once. It occurs with an asymptotic growth of O(n) at each iteration of the sequence, where n is its length.

Finding consecutive pairs of 1s:

While being executed, the code iterates the sequence to find consecutive pairs of 1s within each iteration. This algorithm's inner loop complexity is O(n), where n is the sequence length.

Finally, the code complexity is O(n²), and n is the length of the input sequence. It is using nested loops that constantly loop through the sequence elements.

Space Complexity:

The code space complexity depends on the code's input sequence and the variables used to process them.

Input Sequence:

Input sequence string occupies O(n) space, where n is the sequence length.

The code has a boolean variable called foundPair that determines if consecutive pairs of 1s are encountered. It also employs an integer variable called iterations to keep track of the number of iterations.

These variables occupy O(1) constant space.

Function Call Stack:

On the other hand, this algorithm does not include recursive calls and no large function call stack usage, so the space complexity due to function calls is insignificant.

The code space complexity is O(n), where n is the input sequence length. It simply arises out of the input sequence itself. Added by other parameters is the constant factor and does not have a great influence on the overall space complexity.

## Approach-2: Stack Approach

The "Stack Approach" is an approach to find out the victory in the match by converting number of occurrences of '1' into '0' in line of binary digits. This method implements a stack data structure to maintain the indices of recorded '1's. During the sequence exploration, each '1' encountered is pushed onto the stacking list. When repeated '1's are found, their indices are popped from the stack, and the characters in the sequence are replaced with '0's.

Such processing goes on until no more pairs of '1's adjacent to each other are produced. The Stack Strategy allows a more precise game rules scanning, which is critically important for complex games. This approach that separates scanning and updating steps itself simplifies logic and improves code modularity. The time complexity is determined by the number of elements and doublets of '1's, while the size of the stack (in relation to the number of '1's) sets the space complexity.

### Program:

Output:

```Enter the sequence of 0s and 1s: 1101001001
Final sequence: Converting pair of consecutive 1s at indices 0 and 1 to 0s.
Converting pair of consecutive 1s at indices 3 and 6 to 0s.
Player 2 wins!
Total iterations: 2
0000000001
```

Explanation:

• Function determineWinner():

This function takes as an argument the string sequence that expresses the initial condition of game. It initializes a stack (std::stack<int> indexStack) for keeping track of the positions of the '1's. It repeats the sequence, moving the indices of the '1's to the stack.

If two '1's are found in a row, it removes the previously added indices from the stack, changes the linked elements to '0's and increments the iteration counter. In an end, it checks if there are any '1's left to tell who the winner is: Player 1 if there are no '1's, otherwise Player 2. Eventually, it returns the transformed sequence.

• Main Function:

The user must input the initial sequence of 0s and 1s in the main () function.

Finally, the determineWinner() function is called with the sequence as an input variable, and the final sequence is printed to the console. The code efficiently utilizes a stack data structure to keep the indices of the '1s' encountered and get the '1s pairs consecutively.

It displays the informative console output, reflecting the process of substituting consecutive pairs of '1's to '0's and the winning party. The function determineWinner() is responsible for the game logic and main() for the user interaction.

### Complexity Analysis:

Time Complexity:

Iteration through Sequence:

The code loops as a single for loop in function determineWinner() that is responsible to deal with input sequence only once in the entire program.

It is common for the duration of each loop to result in O(n) time, where n signifies the length of the original sequence.

Stack Operations:

The loop carries out the operating task of removing and placing items into the stack using the pushing and popping mechanism.

Stack operations have in average a complexity of O(1), providing those and other operations (e.g. deque) are constant time.

But, the number of stack operation can go to max O(n), which is the number of input digits, based on the occurrences of "1" in the number.

Finding Consecutive Pairs:

According to the stack-based mechanism, consecutive '1' pairs of the sequence must be retrieved. These methods of checks have a time complexity that is O(1) because they are basically just comparisons and accessing the elements using the index number.

Considering these factors, the code time complexity will be O(n), which here is the length of the input sequence.

### Space Complexity:

Stack Space:

The space complexity of stack depends on number of elements in the stack which is least resistant to failure as worst case can be O(n) where n is the length of the input sequence.

Other Variables:

In the case of the code, the iteration variable (an integer) plays the role of recording the iteration. Therefore, the overall space complexity of the code is O(n), and n is the input sequence.