Logic of the problem

K = 3

K = 3

0 1 1 1 1 0 0 1 1

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

K = 3

 1 1 0 0 1 1

LENGTH = 9

K = 3

  0 1 1

LENGTH = 9

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

Segments = length / K

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

Segments = 9/3

= 3

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

But how to check if a string can be converted such that consecutive segments are mirror images of each other?

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

Let's look at the example once..!!

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

No. of 0's must be equal to the consecutive segment.

No. of 1's must be equal to the consecutive segment.

K = 3

0 1 1 1 1 0 0 1 1

LENGTH = 9

No. of 0's must be equal IN ALL the segment.

No. of 1's must be equal IN ALL the segment.

BIGGER

PICTURE

K = 3

0 1 1 1 1 0 0 1 0

LENGTH = 9

No. of 0's must be equal IN ALL the segment.

No. of 1's must be equal IN ALL the segment.

BIGGER

PICTURE

K = 3

0 1 1 1 1 0 0 1 0

LENGTH = 9

No. of 0's must be equal IN ALL the segment.

No. of 1's must be equal IN ALL the segment.

BIGGER

PICTURE

Are we actually able to divide no. of 0's and 1's equally in all the segments ..!!

Are we actually able to divide no. of 0's and 1's equally in all the segments ..!!

0 1 1 1 1 0 0 1 1

Are we actually able to divide no. of 0's and 1's equally in all the segments ..!!

0 1 1 1 1 0 0 1 1

if((count_of_0%total_segments == 0) && (count_of_1%total_segments == 0))
POSSIBLE
else
IMPOSSIBLE

Logic for lexicographically smallest string

We have only two numbers i.e., 0 and 1

Logic for lexicographically smallest string

We have only two numbers i.e., 0 and 1

 0 0 1 1

1 1 0 0

1 1 0 0

0 0 1 1

Logic for lexicographically smallest string

We have only two numbers i.e., 0 and 1

 0 0 1 1

1 1 0 0

1 1 0 0

0 0 1 1

lexicographically small

Logic for lexicographically smallest string

 0 0 1 1

1 1 0 0

1 1 0 0

0 0 1 1

For all even segment print all the 0's first then all the 1's and for all odd segment print all the 1's first then all the 0's

CODE

#include <bits/stdc++.h> 
using namespace std;
int main()
{
	int t;
	cin >> t;
    while(t--)
    {
    	int n,k;
    	cin >> n >> k;
    	string s;
    	cin >> s;
    	int count_0 = 0;
    	int count_1 = 0;
    	for(int  i = 0; i<s.length(); i++)
    	{
    		if(s[i] == '0')
    			count_0++;
    		else
    			count_1++;
    	}
    	int number_of_seg = n/k;
    	if((count_0%number_of_seg == 0) && (count_1%number_of_seg == 0))
    	{
            for(int  i = 0; i<number_of_seg; i++)
            {
            	if(i%2==0)
            	{
            		for(int j = 0; j<count_0/number_of_seg; j++)
            			cout << '0';
            		for(int j= 0;j<count_1/number_of_seg; j++)
            			cout <<'1';
            	}
            	else
            	{
            		for(int j= 0;j<count_1/number_of_seg; j++)
            			cout <<'1';
            		for(int j = 0; j<count_0/number_of_seg; j++)
            			cout << '0';
            	}
            }
            cout << endl;
    	}
    	else
    	{
    		cout << "IMPOSSIBLE" << endl;
    	}
    }


return 0;
}

deck

By Chirayu Jain