![]() |
VOOZH | about |
Given a binary string of size N and an integer K, the task is to perform K operations upon the string and print the final string:
Examples:
Input: str = "1011", K = 2
Output: 0010
After the first step, string will be reversed and becomes "1101".
After the second step, the string will be complemented and becomes "0010".Input: str = "1001", K = 4
Output: 1001
After all operation the string will remain same.
Naive Approach:
Traverse for all K steps and if the current step is odd then perform the reverse operation, otherwise complement the string.
Efficient Approach: Upon observing the given operation pattern:
Below is the implementation of the above approach:
11001
Time Complexity: O(N)
Auxiliary Space: O(1)