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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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 (192.65 KiB) Viewed 65 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 (193.48 KiB) Viewed 65 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 (240.38 KiB) Viewed 65 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 (100.21 KiB) Viewed 65 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 "00011101000" 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.
do so. 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. S We can swap first two characters, S becomes 0101. and the value of S will b 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. Represents the given string. K: Represents the Integer. Input format Note: This is the input format that you must use to provide curto shown
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 00111001 0 Sample output
1 #include<bits/sed++.h> using namespace std; 7 9 10 11 2 3 4 5 6 7 18 19 28 21 12 13 14 15 16 17 20 int solve (string S, long long K) { } int main() { } ios::sync_with_stdio(0); cin.tie(0); string S; cin>>S; long long K; cin >> K; int out; = solve(S, K); out cout << out_;
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply