CodeChef Starters 31 Division 3 (Rated) The Unchanging OR Problem Code: UNCHANGEDOR Solution




Problem Link:   https://www.codechef.com/START31C/problems/UNCHANGEDOR

 Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,x,t,cn,a,s,r;
    cin>>t;
    while(t--)
    {
        x=0;
        cin>>n;
        cn=n;
        if(n==2)
            cout<<"0"<<endl;
        else if(n==3||n==4)
            cout<<"1"<<endl;
        else
        {
            for(int i=1; i<=50; i++)
            {
                s=pow(2,i);
                x++;
                if(s>n)
                    break;
            }
            x--;
            a=cn-(pow(2,x));
            r=(pow(2,x)-x-1);
            if(a==0)
            {
                cout<<r<<endl;
            }
            else
            {
                cout<<r+a<<endl;
            }
        }
    }
}

No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.