Reduce to 1 CodeChef Solution

Problem -Reduce to 1 CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Reduce to 1 CodeChef Solution in C++17

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

// long long abs (long long x, long long y)
// {
//     long long z;
//     z = x - y;

//     if (z < 0)
//     {
//         z *= (-1);
//     }

//     return z;
// }

int main()
{
    long long t, v, m, n, x, y, z, i, j, k, a, b, d, p, q;
    long long num, val, check, count, sum, maxm, minm, mid;
    long long c0, c1;
    string s;
    cin >> t;
    v = t;
    // v = 1;

    while (v > 0)
    {
        v --;
        cin >> n;
        // cin >> s;

        // long long arr[n], ori[n], ref[n];
        // n = s.length ();

        // for (i = 0; i < n; i++)
        // {
        //     cin >> arr[i];
        // }

       if (n == 1)
       {
           cout << "0" << endl;
       } 
       else if (n % 2 == 1)
       {
           cout << "1" << endl;
       }
       else
       {
           x = n;
           while (x % 4 == 0)
           {
               x /= 4;
           }
           
           if (x % 2 == 0)
           {
               cout << "-1" << endl;
           }
           else
           {
               y = sqrt (n);
               if (n == (y * y))
               {
                   cout << "1" << endl;
               }
               else
               {
                   cout << "2" << endl;
               }

            //     j = 2;
            //     check = 0;
            //    for (i = y; i >= 2; i--)
            //    {
            //        while (j < 100)
            //        {
            //            if (pow (i, j) == n)
            //            {
            //                cout << "1" << endl;
            //                check = 1;
            //                break;
            //            }
            //            else if (pow (i, j) > n)
            //            {
            //                i --;
            //            }
            //            else
            //            {
            //                i /= 2;
            //                i ++;
            //                j ++;
            //            }

            //            if (i <= 2)
            //            {
            //                break;
            //            }
            //        }
                   
            //        if (i <= 2)
            //        {
            //            break;
            //        }

            //        if (check == 1)
            //        {
            //            break;
            //        }
            //    }

            //    if (check == 0)
            //    {
            //        cout << "2" << endl;
            //    }               
           }
       }
    }
    
    return 0;
}

Reduce to 1 CodeChef Solution in C++14

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
#define rep(i,a,b) for(ull i=a; i<b; i++)
#define repeat(n)  for(int i=0; i<n; i++) 	
#define err 1e-5
#define _z ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define pii pair<int,int>

void solve()
{
	ll n;
	cin>>n;
	if(n==1)
	{
		cout<<"0"<<endl;
		return;
	}
    else if(n%2==1)
    {
    	cout<<"1"<<endl;
    	return;
    }
    int count=0;
    while(n%2==0)
    {
    	count++;
    	n/=2;
    }
    if(count%2==1)
    {
    	cout<<"-1"<<endl;
    	return;
    }
    else
    {
    	ll x= sqrt(n);
    	if(n==x*x)
    	{
    		cout<<"1"<<endl;
    		return;
    	}
    }
    cout<<"2"<<endl;
}

int main()
{
	_z;
	int t;
	cin>>t;
	while(t--)
	{
        solve();
	}
		
}

Reduce to 1 CodeChef Solution in PYTH 3

import math
t = int(input())
for _ in range(t):
    n = int(input())
    r = math.sqrt(n)
    if n % 2 == 1:
        print(min(n-1,1))
    elif r == math.floor(r):
        print(1)
    else:
        while n >= 4 and n % 4 == 0:
            n /= 4
        if n == 1:
            print(1)
        elif n % 2 == 1:
            print(2)
        else:
            print(-1)

Reduce to 1 CodeChef Solution in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

long long int numberof2(long long int n)
{
    long long int t=0;
    while(n%2==0)
    {
        n=n/2;
        t++;
    }
    return t;
}
void loki()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int n;
        scanf("%lld",&n);
        long long int nof2=numberof2(n);
        long long int square=sqrt(n);
        long long int num=pow(square,2);
        if(n==1)
        printf("0\n");
        else if(n%2==1||num==n)
        printf("1\n");
        else
        {
            if(nof2%2==1)
            printf("-1\n");
            else
            {
                long long int p=pow(2,nof2);
                n=n/p;
                if(n==1)
                printf("1\n");
                else
                printf("2\n");
            }
        }
    }
}
int main(void) {
	loki();
	return 0;
}

Reduce to 1 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 Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		 Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        
        for (int k = 0; k < t; k++) {
            
            long n = in.nextLong();
            
            if (n == 1) {
                System.out.println(0);
                continue;
            }
            
            if (n % 2 == 1) {
                System.out.println(1);
                continue;
            }
            
            int cnt = 0;
            
            while (n % 2 == 0) {
                cnt += 1;
                n /= 2;
            }
            
            if (cnt % 2 == 0) {
                boolean flag = false;
                
                for (int i = 2; i <= cnt; i+= 2) {
                    if (cnt % i != 0) {
                        continue;
                    }
                    
                    double p = 1.0 / i;
                    if ( Math.pow((double)n, p) == (int) Math.pow((double)n, p)) {
                        flag = true;
                        break;
                    }
                    
                    
                }
                
                if (flag == true) {
                        System.out.println(1);
                    } else {
                        System.out.println(2);
                    }
            } else {
                System.out.println(-1);
            }
            
            
        }
	}
}

Reduce to 1 CodeChef Solution in PYPY 3

import math
N = int(input())
for i in range(N):
    n = int(input())
    r = math.sqrt(n)
    if n % 2 == 1:
        print(min(n-1,1))
    elif r == math.floor(r):
        print(1)
    else:
        while n >= 4 and n % 4 == 0:
            n /= 4
        if n == 1:
            print(1)
        elif n % 2 == 1:
            print(2)
        else:
            print(-1)

Reduce to 1 CodeChef Solution in PYTH

import math
t = int(input())
for i in range(t):
    n = int(input())
    if n == 1:
        print(0)
        continue
    if int(math.sqrt(n))**2 == n:
        print(1)
        continue
    if n%2==1:
        print(1)
    else:
        e = 0
        d = n
        while d%2==0:
            d //= 2
            e+=1
        if e%2==1:
            print(-1)
        elif d==1:
            print(1)
        else:
            print(2)

Reduce to 1 CodeChef Solution in C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;


public class Test
{
    public static int GetInt() => int.Parse(Console.ReadLine());
    public static long GetLong() => long.Parse(Console.ReadLine());
    
    public static int[] GetIntArray() =>  Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
    public static long[] GetLongArray() =>  Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
	
	public static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a%b);
	public static int Gcd(int[] n) => n.Aggregate((a,b) => Gcd(a,b));

	public static void Main()
	{
    var t=GetInt(); for(int i = 0; i<t; i++) { Solve(); }
  	// Solve(); 
	}
	
	public static void Solve()
	{
    long n = GetLong();
    long ncopy = n;
    if(n == 1)
    {
      Console.WriteLine(0);
      return;
    }
    int p2 = 0;
    while(n%2 == 0)
    {
      n /=2;
      p2++;
    }
    if(p2 == 0)
    {
      Console.WriteLine(1);
      return;
    }
    if(p2 % 2 != 0)
    {
      Console.WriteLine(-1);
      return;
    }
    int ans = 1;
    long q = (long)Math.Sqrt(ncopy);
    if(ncopy != q * q)
    {
        ans++;
    }
    Console.WriteLine(ans);
	}
}

Reduce to 1 CodeChef Solution in GO

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		var n uint64

		s, _ := reader.ReadBytes('\n')

		readUint64(s, 0, &n)

		res := solve(int64(n))

		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
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func solve(n int64) int64 {
	if n == 1 {
		return 0
	}
	//sqrt(n) LTE
	var count int

	for n%2 == 0 {
		n /= 2
		count++
	}

	if count == 0 {
		return 1
	}

	if count%2 == 1 {
		return -1
	}

	x := int64(math.Sqrt(float64(n)))

	if x*x == n {
		return 1
	}

	return 2
}
Reduce to 1 CodeChef Solution Review:

In our experience, we suggest you solve this Reduce to 1 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 1 CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Reduce to 1 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!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

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