[Solved] Toggle The Middle Contest Problem

Toggle The Middle: You are given a number N. You need to toggle the middle bit of N in binary form and print the result in decimal form.

  • If number of bits in binary form of N are odd then toggle the middle bit (Like 111 to 101).
  • If number of bits in binary form of N are even then toggle both the middle bits (Like 1111 to 1001)

Note: Toggling a bit means converting a 0 to 1 and vice versa.

Toggle The Middle Contest

Input:
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains a single number N.

Output:
For each testcase, in a new line, print the decimal form after toggling the middle bit of N.

Constraints:
1 <= T <= 100
1 <= N <= 106

Examples:
Input:

5
1
2
3
4
5
Output:
0
1
0
6
7

Examples:
Testcase3: N=3. Binary is 11. Toggle the middle bits: 00. 00 in decimal is 0
Testcase5: N=5. Binary is 101. Toggle the middle bit: 111. 111 in decimal is 7

Solution:

import java.io.*;
import java.util.*;

public class ToggleTheMiddle {
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while (st == null || !st.hasMoreElements()){
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            }catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) {
        FastReader fr = new FastReader();
        int t = fr.nextInt();
        while (t-->0){
            int n = fr.nextInt();
            int temp = n;
            n =  (int)(Math.log(n)/Math.log(2) + 1);
            if (n%2 == 0){
               int bit1 = (int)(n/2);
               int r = 1 << (bit1-1);
               temp = temp^r;
               n =  (int)(Math.log(temp)/Math.log(2) + 1);
                int b = bit1+1;
                if (n%2 == 0){
                    r = 1 << (b-1);
                }
               else {
                    r = 1 << (b-2);
                }
                System.out.println(temp^r);
            }else {
                int bit1 = (int)(n/2)+1;
                int r = 1 << (bit1-1);
                System.out.println(temp^r);
            }
        }
    }
}
def ToggleBits(n):
    a = bin(n)[2:]
    x = list(a)
    if len(a)%2 == 0:
        u = len(a)
        if x[(u//2)-1] == '1':
            x[(u//2)-1] = '0'
        elif x[(u//2)-1] == '0':
            x[(u//2)-1] = '1'
        if x[(u//2) + 1 - 1] == '1':
            x[(u//2) + 1 - 1] = '0'
        elif x[(u//2) + 1 - 1] == '0':
            x[(u//2) + 1 - 1] = '1'
    else:
        u = len(a)
        if x[((u+1)//2)-1] == '1':
            x[((u+1)//2)-1] = '0'
        elif x[((u+1)//2)-1] == '0':
            x[((u+1)//2)-1] = '1'
    y = "".join(x)
    return int(y,2)

if __name__ == '__main__':
    t = int(input())
    for c in range(t):
        n = int(input())
        print (ToggleBits(n))

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *