Algorithm
Problem link- https://www.spoj.com/problems/EC_CONB/
EC_CONB - Even Numbers
Marina is a college girl who likes to play with all kinds of numbers. One day she was very bored and decided to play around with even numbers.
She writes N numbers on the board (odd and even numbers) and then, she modifies only even numbers (for some reason she likes the odd numbers) and reversed its binary representation (from the left to the right ) and replaces each even number. But soon she gets bored and ask you help in order to automate the conversion process.
Input
In the first line contains the value of N. This integer is followed by N lines, every one with a positive integer ai (1 <= ai <= 10^7).
Output
The output will contain N lines, the numbers that are on the blackboard after the conversion process.
Example
Input: 5 10 8 3 5 2 Output: 5 1 3 5 1
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#include <climits>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define MOD (ll)10000007
#define pb push_back
#define EPS 1e-9
#define FOR(i, n) for(int i = 0;i < n; i++)
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
int t;
int main(){
cin>>t;
int arr[1000];
while(t--){
int n;
cin>>n;
if(n&1){ //odd
cout<<n<<endl;
}else{
int count = 0;
while(n > 0){
arr[count] = n%2;
n/=2;
count++;
}
count-=1;
int i = 0;
ll num = 0;
while(count >= 0){
if(arr[count]==1)
num+=(ll)exp(2, i);
count--;
i++;
}
cout<<num<<endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
10
8
3
5
2
Output
1
3
5
1
Demonstration
SPOJ Solution-Even Numbers-Solution in C, C++, Java, Python