Help me in solving CHEFIZZA problem

My issue

can someone explain the approach??
can’t find any useful help online…

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	}

}

Problem Link: Chef loves Pizza Chef loves halfs Practice Coding Problem

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

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	// your code goes here
	Scanner sc=new Scanner(System.in);
	 int t=sc.nextInt();
    	while(t-->0){
	    long  x=sc.nextLong();
	    if(x == 0 || x==2 || x==4){   //they will divide equally for small cases
	    System.out.println(0);
	    }
	    else{
	        long  temp = 2;     /*i f it is the power of 2 then it divides equally,so no smaller pieces, otherwise we have to print 2*(x-temp(which is less than x)) because (x-temp) tells the 
number of smaller pieces and there are 2 larger pieces at a time and we will multiply with 2.
*/
	        while(temp*2 <= x){
	            temp *= 2;
	        }
	        System.out.println(2*(x-temp));
	    }
	    
	}

}
}

1 Like

check (vii) in Question figure for clarity for 14 as input

thankyou @dhanush3015 , the comments were quite useful .

1 Like

@dheva_10 You are Welcome. I am being helpful to you.