304 North Cardinal St.
Dorchester Center, MA 02124

# Reduce to zero CodeChef Solution

## Reduce to zero CodeChef Solution in C++17

``````#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define Time cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
#define pb push_back
#define mp make_pair
#define line cout << endl;
#define ff first
#define ss second
#define vi vector<int>
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define printv(v)                      \
for (int i = 0; i < (v.size()); i++) \
{                                    \
cout << v[i] << " ";               \
}                                    \
line;
#define onesbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define sp(x, y) fixed << setprecision(y) << x
#define w(x) \
int x;     \
cin >> x;  \
while (x--)
#define tk(x) \
int x;      \
cin >> x;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
#define debug(x)     \
cerr << #x << " "; \
_print(x);         \
cerr << endl;
#else
#define debug(x)
#endif
template <class T>
void _print(T t)
{
cerr << t;
}

template <class T, class V>
void _print(pair<T, V> p)
{
cerr << "{";
_print(p.ff);
cerr << ",";
_print(p.ss);
cerr << "}";
}

template <class T>
void _print(vector<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

template <class T>
void _print(vector<vector<T>> v)
{
cerr << "[\n";
for (int l = 0; l < v.size(); l++)
{
{
for (int k = 0; k < v[l].size(); k++)
cerr << v[l][k] << " ";
}
cerr << "\n";
}
cerr << "]";
}

template <class T, class V>
void _print(map<T, V> v)
{
cerr << "[ ";
for (auto i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

template <class T>
void _print(set<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

const long long inf = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e6;

bool isValid(string s)
{
int len = s.size();
for (int i = 0; i < len / 2; i++)
{
if (s[i] != s[len - 1 - i])
return false;
}
return true;
}

bool isPalindrome(string S, int start, int end){

int i=start;
int j=end;

while(i<j){
if(S[i] != S[j])
return false;
i++;
j--;
}
return true;
}

void rotateMatrix(vector<vector<int>> &v, int n)
{
for (int i = 0; i < n / 2; i++)
{
for (int j = i; j < n - i - 1; j++)
{
int ptr = v[i][j];
v[i][j] = v[n - 1 - j][i];
v[n - 1 - j][i] = v[n - 1 - i][n - 1 - j];
v[n - 1 - i][n - 1 - j] = v[j][n - 1 - i];
v[j][n - 1 - i] = ptr;
}
}
}

vector<bool> is_prime(10001, 1);
vector<int> primes;

void seive() {
is_prime[0] = 0;
is_prime[1] = 0;
for (int i = 2; i < 10001; i++) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i + i; j < 10001; j += i) {
is_prime[j] = 0;
}
}
}
}

string makePalindrome(string S){

int i=0;
int j=S.length()-1;

while(i<j){

if(S[i] == S[j]){
i++;
j--;
}else{

if(isPalindrome(S, i+1, j))
return "YES";

if(isPalindrome(S, i, j-1))
return "YES";

return "NO";
}
}

return "YES";
}

int32_t main() {

int t;
cin>>t;
while(t--){
ll n,k;cin>>n>>k;

if(n==k){
cout<<n<<" ";
continue;
}

if(n==0|| k==0){
cout<<-1<<endl;
continue;
}

ll a=min(n,k);
ll b=max(n,k);
ll ans=0;

while(a<=b){
a*=2;
ans++;
}

a=a/2;
ans--;

if(a==b){
ans+=b;
cout<<ans<<endl;
}else {
ans+=b+1;
cout<<ans<<endl;
}
}
return 0;
}``````

## Reduce to zero CodeChef Solution in C++14

``````#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define Time cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
#define pb push_back
#define mp make_pair
#define line cout << endl;
#define ff first
#define ss second
#define vi vector<int>
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define printv(v)                      \
for (int i = 0; i < (v.size()); i++) \
{                                    \
cout << v[i] << " ";               \
}                                    \
line;
#define onesbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define sp(x, y) fixed << setprecision(y) << x
#define w(x) \
int x;     \
cin >> x;  \
while (x--)
#define tk(x) \
int x;      \
cin >> x;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
#define debug(x)     \
cerr << #x << " "; \
_print(x);         \
cerr << endl;
#else
#define debug(x)
#endif
template <class T>
void _print(T t)
{
cerr << t;
}

template <class T, class V>
void _print(pair<T, V> p)
{
cerr << "{";
_print(p.ff);
cerr << ",";
_print(p.ss);
cerr << "}";
}

template <class T>
void _print(vector<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

template <class T>
void _print(vector<vector<T>> v)
{
cerr << "[\n";
for (int l = 0; l < v.size(); l++)
{
{
for (int k = 0; k < v[l].size(); k++)
cerr << v[l][k] << " ";
}
cerr << "\n";
}
cerr << "]";
}

template <class T, class V>
void _print(map<T, V> v)
{
cerr << "[ ";
for (auto i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

template <class T>
void _print(set<T> v)
{
cerr << "[ ";
for (T i : v)
{
_print(i);
cerr << " ";
}
cerr << "]";
}

const long long inf = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e6;

bool isValid(string s)
{
int len = s.size();
for (int i = 0; i < len / 2; i++)
{
if (s[i] != s[len - 1 - i])
return false;
}
return true;
}

bool isPalindrome(string S, int start, int end){

int i=start;
int j=end;

while(i<j){
if(S[i] != S[j])
return false;
i++;
j--;
}
return true;
}

void rotateMatrix(vector<vector<int>> &v, int n)
{
for (int i = 0; i < n / 2; i++)
{
for (int j = i; j < n - i - 1; j++)
{
int ptr = v[i][j];
v[i][j] = v[n - 1 - j][i];
v[n - 1 - j][i] = v[n - 1 - i][n - 1 - j];
v[n - 1 - i][n - 1 - j] = v[j][n - 1 - i];
v[j][n - 1 - i] = ptr;
}
}
}

vector<bool> is_prime(10001, 1);
vector<int> primes;

void seive() {
is_prime[0] = 0;
is_prime[1] = 0;
for (int i = 2; i < 10001; i++) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i + i; j < 10001; j += i) {
is_prime[j] = 0;
}
}
}
}

string makePalindrome(string S){

int i=0;
int j=S.length()-1;

while(i<j){

if(S[i] == S[j]){
i++;
j--;
}else{

if(isPalindrome(S, i+1, j))
return "YES";

if(isPalindrome(S, i, j-1))
return "YES";

return "NO";
}
}

return "YES";
}

int32_t main() {

int t;
cin>>t;
while(t--){
ll n,k;cin>>n>>k;

if(n==k){
cout<<n<<" ";
continue;
}

if(n==0|| k==0){
cout<<-1<<endl;
continue;
}

ll a=min(n,k);
ll b=max(n,k);
ll ans=0;

while(a<=b){
a*=2;
ans++;
}

a=a/2;
ans--;

if(a==b){
ans+=b;
cout<<ans<<endl;
}else {
ans+=b+1;
cout<<ans<<endl;
}
}
return 0;
}``````

## Reduce to zero CodeChef Solution in PYTH 3

``````from math import log2, ceil

def soln(x, y):
if x == 0 and y == 0:
return 0
if x == 0 or y == 0:
return -1
if x == y:
return x
elif x > y:
return ceil(log2(x/y)) + x
else:
return ceil(log2(y/x)) + y
return -1

for _ in range(int(input())):
x, y = map(int, input().split())
print(int(soln(x, y)))``````

## Reduce to zero CodeChef Solution in C

``````#include <stdio.h>
int main(void) {
int t; long int x,y,l,i;
scanf("%d",&t);
for( ;t>0;t--)
{  scanf("%ld %ld",&x,&y);
if(x>y)
{
i=x;
x=y;
y=i;
}
if(x==y)
printf("%ld\n",x);
else if(x==0)
printf("-1\n");
else
{  i=0;
while(2*x<y)
{   x*=2;
i++;
}
l=y-x;
for( ;x!=l;x--,y--,i++);
printf("%ld\n",i+y+1);
}
}
return 0;
}``````

## Reduce to zero CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */

class io{
StringTokenizer st=new StringTokenizer("");
public io(){
return;
}

public String next(){
while(!st.hasMoreTokens()){
try{
}
catch(Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}

public int nextInt(){
return Integer.parseInt(next());
}

public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String nextLine(){
try{
}
catch(Exception e){}
return null;
}
public boolean hasNext(){
String next=null;
try{
}
catch(Exception e){
}
if(next==null)
return false;
st=new StringTokenizer(next);
return true;
}
}
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
io sc = new io();
int T = sc.nextInt();
while(T-->0){
long one = sc.nextLong();
long two = sc.nextLong();
long res = 0;

if(one ==two){
System.out.println(one);
continue;
}
if(one==0){
System.out.println("-1");
continue;
}
if(two==0){
System.out.println("-1");
continue;

}
//  boolean flag=  false;
if(one>two){
long temp = one;
one=  two;
two=temp;
}
if(one<two){
while(one<two){
one*=2;
res++;

}

res+=two;

}

System.out.println(res);

}
}
}``````

## Reduce to zero CodeChef Solution in PYPY 3

``````
def helper(x,y,count):
if x > y:
x,y = y,x
if x == 0 and y == 0:
return 0
if x == 0:
return -1
while y > x:
x = 2*x
count += 1
return count + y
for t in range(int(input())):
a,b = (map(int,input().split(' ')))
print(helper(a,b,0))``````

## Reduce to zero CodeChef Solution in PYTH

``````# cook your code here
class Zero:
def __init__(self, x, y):
self.x = x
self.y = y

def solve(self):
x = self.x
y = self.y
if x == y:
return x
if x > y:
(x, y) = (y, x)
if x == 0:
return -1
rez = 0
while 2 * x < y:
x *= 2
rez += 1
d = x - (y - x)
rez += d
y -= d
rez += y
rez += 1
return rez

def main():
T = int(raw_input())
while T > 0:
T -= 1
s = [int(a) for a in raw_input().split(' ')]
ob = Zero(s[0], s[1])
print ob.solve()

if __name__ == '__main__':
main()

``````

## Reduce to zero CodeChef Solution in C#

``````using System;

public class Test
{
static void Main(string[] args)
{
int t = 1;
while (t-- > 0) Console.WriteLine(Solve());
}

private static long Solve()
{
int si = 0;
long x = StringToLong(input[0], 0, out si);
long y = StringToLong(input[1], 0, out si);
long res = 0;

if (x < y)
{
long z = x;
x = y;
y = z;
}

if (x == 0 && y == 0) return 0;
else if (x == 0 || y == 0) return -1;

while (y * 2 <= x)
{
y *= 2;
res++;
}

if (x == y) return x + res;
return x + res + 1;
}

static long StringToLong(string s, int index, out int space_i)
// Convert string from index up to next space or end to integer.
// Return space_i = index of next space encountered, or off end of string.
{
const int c_zero = '0';
long n = s[index] - c_zero;
int len = s.Length;
while (++index < len)
{
if (s[index] < c_zero)
break;
n = n * 10 + (s[index] - c_zero);
}
space_i = index;
return n;
}
const string c_digits = "0123456789";
static string LongToString(long a)
// Convert zero or positive integer to string
{
char[] str = new char[20]; // maximum number of chars
int i = 20;
str[--i] = ' ';  // space after
do
{
str[--i] = c_digits[(int)(a % 10)];
a /= 10;
} while (a != 0);
return new string(str, i, str.Length - i);
}
}``````

## Reduce to zero CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {

var buf bytes.Buffer
for tc > 0 {
tc--

var x uint64
var y uint64

pos := readUint64(s, 0, &x) + 1
res := solve(x, y)

buf.WriteString(fmt.Sprintf("%d\n", res))
}
fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

for i := 0; i < len(s); i++ {
if s[i] == '\n' {
return s[:i]
}
}
return s
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func solve(x, y uint64) int64 {
// x => 2 * x, shifts left
// y => 2 * y, shifts right
// x-- and y--
if x > y {
x, y = y, x
}
// x < y
if x == y {
return int64(x)
}

if x == 0 {
return -1
}
var res int64
for x < y {
res++
x *= 2
}
// x * 2 > y
return res + int64(y)
}``````
##### Reduce to zero CodeChef Solution Review:

In our experience, we suggest you solve this Reduce to zero CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Reduce to zero CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Reduce to zero CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!