Two vs Ten | CodeChef Solution

Hello coders, today we are going to solve Two vs Ten CodeChef Solution whose Problem Code is TWOVSTEN.

Two vs Ten

Task

Chef Two and Chef Ten are playing a game with a number X. In one turn, they can multiply X by 2. The goal of the game is to make X divisible by 10.

Help the Chefs find the smallest number of turns necessary to win the game (it may be possible to win in zero turns) or determine that it is impossible.

Input Format

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains a single integer denoting the initial value of X.

Output Format

For each test case, print a single line containing one integer — the minimum required number of turns or −1 if there is no way to win the game.

Constraints

  • 1 ≤ T ≤ 1000
  • 0 ≤ X ≤ 10^9

Subtasks

Subtask #1 (100 points): original constraints

Example Input

3
10
25
1

Example Output

0
1
-1

Solution – Two vs Ten | CodeChef Solution

C++

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main(){
        int t, x;
        cin >> t;
        while (t--){
                cin >> x;
                if (x % 10 == 0)
                        printf("%d\n", 0);
                else if (x % 5 == 0)
                        printf("%d\n", 1);
                else

                        printf("%d\n", -1);
        }
        return 0;
}

Python

# cook your dish here
t=int(input())
for i in range (t):
    x=int(input())
    if x%10==0:
        print('0')
    elif x%10==5:
        print(1)
    else:
        print('-1')

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 Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- != 0) {
			int count = 0;
			String s = sc.next();

            int b = Integer.parseInt(s.substring(s.length()-1));
            if(b == 0 || b== 5) {
            	
            	while(b%10 != 0) {
            		b = b*2;
            		count++;
            	}
            	System.out.println(count);
            }else {
            	System.out.println("-1");
            }
		}
		
	}

}

Disclaimer: The above Problem (Two vs Ten) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.