Problem: 2057C - Trip to the Olympiad
Language: Java 8

Previous submission (verdict: Wrong answer on test 2):
import java.util.*;

public class C{
    public static boolean powerOf2(int n){
        if(n==1) return false;
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while(t>0){
            t--;

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            for(int i=30; i>=0; i--){
                int rbit = (((1<<i)&r)>>i);
                if(rbit==1){
                    start = i;
                    break;
                }
            }

            if(powerOf2(r)){
                mid = 0;
                smallerThanr = true;
                start--;
            }
            else if(powerOf2(l+1)){
                mid += 1<<start;
                greaterThanl = true;
                if((((1<<start)&l)>>1) == 0) greaterThanl = true;
                start--;
            }
            else{
                start = 30;
            }

            for(int i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 && rbit == 1 ){
                    if(!greaterThanl){
                        mBit = 1;
                    }
                    else{
                        smallerThanr = true;
                    } // else 0
                }
                else if(lbit == 0 && rbit == 0){
                    if(!smallerThanr){
                        mBit = 0;
                    }
                    else{
                        mBit = 1;
                        greaterThanl = true;
                    } //else 0
                }
                else if(lbit == 0 && rbit == 1){
                    if(!smallerThanr){
                        mBit = 0;
                        smallerThanr = true;
                    }
                    else if(!greaterThanl){
                        mBit = 1;
                        greaterThanl = true;
                    }
                }
                else{ // lbit == 1 && rbit == 0
                    if(!greaterThanl){
                        mBit = 1;
                    }
                }

                mid += (mBit<<i);
            }

            System.out.println(l + " " + mid + " " + r);
        }
    }
}

Submission (verdict: Wrong answer on test 2):
import java.util.*;

public class C{
    public static boolean powerOf2(int n){
        if(n==1) return false;
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while(t>0){
            t--;

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            int i = start;
            while(i>=0){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                if(lbit != rbit) break;
                mid += (lbit<<i);
                i--;
            }
            // System.out.println(mid);

            start = i;
            for(i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 || rbit==1) mBit=0;
                else mBit = 1;
                mid += (mBit<<i);
            }
            // System.out.println(mid);

            
            i = start;
            while(i>=0 && mid <= l){
                int mBit = (((1<<i)&mid)>>i);
                if(mBit==0 && (mid + (1<<i) < r)){
                    if(l < mid+(1<<i)) mid += (1<<i);
                }
                i--;
            }
            System.out.println(l + " " + mid + " " + r);
        }
    }
}