Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
long long n;
while(t--){
cin>>n;
int cnt3 = 0, cnt2 = 0;
int k = n;
while(k % 3 == 0 && k > 1){
k /= 3;
cnt3++;
}
while(k % 2 == 0 && k > 1){
k /= 2;
cnt2++;
}
if(k != 1){
cout<<-1<<endl;
continue;
}
if(cnt3 < cnt2){
cout<<-1<<endl;
continue;
}
int cnt6 = cnt3 - cnt2;
n *= pow(2, cnt6);
while(n>1){
n /= 6;
cnt6++;
}
cout<<cnt6<<endl;
continue;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
7
1
2
3
12
12345
15116544
387420489
1
2
3
12
12345
15116544
387420489
Output
0
-1
2
-1
-1
12
36
-1
2
-1
-1
12
36
Demonstration
Codeforcess solution 1374-B B. Multiply by 2, divide by 6 ,C++, Java, Js and Python,1374-B,Codeforcess solution