Page 1 of 1

Good string? Given a binary string (consisting of O's and 1's only) and an integer K. We have to make the value of the s

Posted: Tue Jul 12, 2022 8:05 am
by answerhappygod
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 1
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 1 (209.07 KiB) Viewed 80 times
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 2
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 2 (201.55 KiB) Viewed 80 times
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 3
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 3 (205.99 KiB) Viewed 80 times
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 4
Good String Given A Binary String Consisting Of O S And 1 S Only And An Integer K We Have To Make The Value Of The S 4 (116.37 KiB) Viewed 80 times
Good string? Given a binary string (consisting of O's and 1's only) and an integer K. We have to make the value of the string K by a minimum number of operations. Steps to calculate the value of the string: Cut the string into the minimum number of disjoint substrings such that every substring has the same character('0' or '1'). The substring value is the number of characters present in it. The value of the string is the xor of all the values of the disjoint substrings. Example: string is "0001110111000" its value would be (3^3^1^3^3) = 1. The value of this string is 1. We will cut the string like "000", "111", "0", "111", "000" and substring values would be [3,3,1,3,3].) Note: operator denotes XOR. In one operation we can only swap consecutive values. So given a binary string, we have to make the value of the string K in the minimum number of operations. Print the minimum number of operations to do so. If it is impossible to do so, print -1.
If it is impossible to do so, print -1. Example Consider S= 1001, K = 0. Currently, the value of S is 1 2 1 that is 2. We can swap first two characters, S becomes 0101. and the value of S will be 0. So, the answer will be 1. Function description Complete the solve function provided in the editor. This function takes the following 2 parameters and returns the minimum number of operations to make the value of the string equal to K. S S: Represents the given string. •K: Represents the integer. Input format Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
Input format Note: This is the input format that you must use to provide custom input (available above the Compile and Test button). • There are two lines in the input, the first line has the string S. The second line has an integer K • Output format • Print the minimum number of operations to make the value of string equal to K, or -1 if it's impossible. Constraints 1< Number of zeroes in string ≤ 45 1< Number of ones in the string 45 1≤K<1012 Code snippets (also called starter code/boilerplate code) This question has code snippets for C, CPP, Java, and Python. Sample input D 00111001 0 Sample output
4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include<bits/stdc++.h> using namespace std; int solve (string S, long long K) { // write your code here } int main() { } ios::sync_with_stdio(0); cin.tie(0); string S; cin>>S; long long K; cin >> K; int out_; out_ = solve(S, K); cout << out_;