Reduce to 1 Solution || Codechef Starters - 36

      

Reduce to 1 Solution 

Codechef Starters - 36
Problem Code : RED1

Solution :

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    ll t = 1;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        if (n % 2 == 1)
        {
            if (n != 1)
                cout << 1 << "\n";
            else
                cout << 0 << "\n";
        }
        else
        {
            map<ll, ll> mp;
            ll temp = n;
            while (temp % 2 == 0)
            {
                mp[2]++;
                temp /= 2;
            }
            bool ok = true;
            ll ans = 0;
            if (mp[2] % 2 == 1)
            {
                ok = false;
            }
            else
            {
                if (sqrt(temp) == floor(sqrt(temp)))
                {
                    ans = 1;
                }
                else
                {
                    ans = 2;
                }
            }
            cout << (ok ? ans : -1) << "\n";
        }
    }
}

Comments