Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Minimum swaps
You are given the following:
An Integer N
A binary string S of length N×N
N is always even and string S has an equal number of 0's and 1's. You are allowed to swap any two elements S[i] and S[j] (0≤i ≠j<N×N) of string S.
Task
Determine the minimum number of swaps required to be done on string S such that the following conditions hold true for the entire string.
If (i+1) mod N-(i) mod N=1, then (S[i+1]+S[i])=1 for (0≤i<N×N-1).
If i+N<N×N, then (S[i+N]+S[i])=1.
Note
Assuming 0 based indexing.
Example
Assumptions
N=2
String S="1010"
Approach
Swapping indices 0 and 1, we get string S="0110",
String S="0110" satisfies all the conditions, Hence answer is 1.
Function description
Complete the solve function provided in the editor.
This function takes the following 2 parameters and returns the minimum number of swaps required.
N: represents the integer N
S: Represents the binary string S of length N×N
[![enter image description here][1]][1]


[1]: https://i.stack.imgur.com/FUOJc.png
Sample Input:1,2,0011
Sample output:1
Explanation
The first represents the number of test cases T=1
For test case 1
Given
N=2
String S="0011"
Approach
Swapping indices 0 and 1 we get string S="1001",
String S="1001" satisfies all the conditions, Hence the answer is 1.

What I have tried:

it was very difficult to understand question can any one exaplain how to solve this
Posted
Updated 28-Apr-23 8:39am
v2

Quote:
it was very difficult to understand question can any one exaplain how to solve this
The assignment is set to test what you have learned in your class. If you do not understand the question then you should discuss with your teacher.
 
Share this answer
 
Comments
Meghana N.V 4-May-23 0:55am    
this question from hacker earth
Richard MacCutchan 4-May-23 3:04am    
Well if you don't understand it why did you choose it in the first place?
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Meghana N.V 4-May-23 1:03am    
I have tried to solve I'm stuck in understanding problem like for inputs like N=2, S=1010
to satisfy the second condition they have swapped indices 0 and 1 so out put is 0110 i.e for i=0 and N=2.
now I have incremented i=1, s[i]=1 and s[i+1]=1(here i satisfy this condition i + 1 % N - (i) % N == 1 ) where 1st condition is not satisfying ryt then why didn't swap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900