ORANDCON Editorial

bt fr X==1, why 1,3,7 didnt work??

I tried editorial solution, still I got wrong answer…All help are welcome.
Language : Kotlin

import java.util.*

fun main( args: Array) {
val input = Scanner(System.in)
val testcase = input.nextInt()

for(i in 1..testcase)
{
    var X = input.nextInt()
    var m = 13 + (1 shl 27)
    println("$X ${m} 0")
}

}

//output of above code is a1 b1 c1 (with no braces)

Can someone highlight a test case where this code would fail.

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int x;
	    cin>>x;
	    if(x==1)
	    {
	        cout<<0<<" "<<1<<" "<<5<<endl;
	        continue;
	    }
	    if((x&x-1)==0)   // checking for power of 2
	    {
	        cout<<x-2<<" "<<x<<" "<<x+1<<endl;
	        continue;
	    }
	    if(x&1)
	    {
	        cout<<1<<" "<<x-1<<" "<<x<<endl;
	    }
	    else
	    {
	        cout<<2<<" "<<x-2<<" "<<x<<endl;
	    }
	}
	return 0;
}

I think a simple and easy approach to this problem is to just use the basic property of AND and OR operations
we know any number OR ed with 0 is the number itself and any Number AND with 1 is 1

So, we take one number A as 0 , B as x ( the number ) and C as any number greater than 10^8 and less than 10^9 having all bits as 1

Here we take A as 0 , B as X ( the number given ) and C as (2^28 -1 = 268435455)
Now if we perform the given operation we get
(0 | X ) & ( X | 268435455) & ( 268435455 | 0)
= X & 268435455 & 268435455 = X

#include
#include
using namespace std;

int main() {
// your code goes here
int t;
cin >> t;
while(t–)
{
long long int x , a , b ,c;
cin >> x;
a = x;
b =0;
c = 268435455; // (2^28 - 1) it gives all one’s
cout << a <<" “<<b <<” “<<c <<”\n";
}
return 0;
}

Many ways to succeed on this problem, although it takes a little mathematical thinking to work out a general-purpose solution.

So, among all the other options, my approach was to take X, X+P and X+2*P as the A,B,C required, where P is the next power of two larger than X. This means that one or two of those higher bits are set in the intermediate results (after the OR operation) but neither survive the AND operation, bringing us back down to X.

Of course P can be any power of two larger than X, so you could choose a fixed value of 134217728 as the power of two next larger than the upper limit of X values, but I forgot to read down to the limits on X :grin: :blush: , so I’ll claim it was a more generic solution.

Since it was compact, I’ll just quote my Python solution in here rather than link:

for tx in range(int(input())):
    X = int(input())
    P2 = 2**X.bit_length()
    print(X, X+P2, X+P2*2)
1 Like

What’s Problem in this solution::
#include<bits/stdc++.h>

#include

#define int long long

#define float double

#define pb push_back

#define rep(i,ind,n) for(int i=ind;i<n;i++)

#define cases int cases;cin>>cases;while(cases–)

using namespace std;

int getBit(int n,int i){

return (( n & 1<<i ) != 0 );

}

int func(string a){

int ans=0,tmp=1;

for(int i=a.length()-1;i>=0;i--,tmp*=2){

    if(a[i]=='1')

    ans+=tmp;

}

return ans;

}

int32_t main()

{

ios_base::sync_with_stdio(0);

cin.tie(NULL);cout.tie(NULL);

string a,b,c;

cases{

    a="";b="";c="";

    int n,tmp1=0,tmp2=0;cin>>n;

    while(n!=0){

        if(getBit(n,0)==1){

            if(tmp1==0){

                a+='0';

                b+='1';

                c+='1';

                tmp1=1;

            }

            else if(tmp1==1){

                a+='1';

                b+='0';

                c+='1';

                tmp1=2;

            }

            else if(tmp1==2){

                a+='1';

                b+='1';

                c+='0';

                tmp1=3;

            }

            else{

                a+='1';                    

                b+='1';                    

                c+='1';

                tmp1=0;

            }

        }

        else{

            if(tmp2==0){

                a+='0';

                b+='0';

                c+='1';

                tmp2=1;

            }

            else if(tmp2==1){

                a+='1';

                b+='0';

                c+='0';

                tmp2=2;

            }

            else if(tmp2==2){

                a+='0';

                b+='1';

                c+='0';

                tmp2=3;

            }

            else{

                a+='0';

                b+='0';                    

                c+='0';

                tmp2=0;

            }

        }

        n=n>>1;

    }

    reverse(a.begin(),a.end());

    reverse(b.begin(),b.end());

    reverse(c.begin(),c.end());

    cout<<func(a)<<" "<<func(b)<<" "<<func(c)<<endl;

}

return 0;

}

Refer to my video for detailed explanation of Anti Palindrome and Yet another Constructive Problem

Please write it in the comment section if u have any doubts.

Please help me in finding the mistake , it is passing all sample test cases.
just tell me one test case it is failing
@joffan @abhijeet_19403 @its_me_ss @anon66310847 @jatin_2125 ``
#include <bits/stdc++.h>
using namespace std;

int main() {
int t=0;
cin>>t;
for(int e=0;e<t;e++){
int x,a,b,c;
cin>>x;
int k=log2(x);
int p=pow(2,k);
if(x==1){
cout<<“0 1 2\n”;
}
else if(p==x){
a=p;
b=a+1;
c=b+1;
cout<<a<<" “<<b<<” “<<c<<”\n";
}
else if(p!=x){
c=x;
b=p;
a=c-b;
cout<<a<<" “<<b<<” “<<c<<”\n";

    }
}

}

For X = 1

Your output is 0 1 2 , so let’s check

(0|1) &(1|2) & (2|0)

1 & 3 & 2 == 0 ( but it must be equal to 1 )

2 Likes

@its_me_ss
My code got accepted
That is the only case in which my code is failing It seems,
Thanks for your time.

2 Likes

Can someone please help why am I getting WA

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

int32_t main(){
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        if(n==1) {
            cout<<"1 0 1"<<endl;
            continue;
        }
        if(n&1){
            cout<<(n-2)<<" "<<(n-1)<<" "<<n<<endl;
            continue;
        }
        else{
            cout<<n<<" "<<(n+1)<<" "<<(n+2)<<endl;
        }
    }
}