This is the Odd-GCD problem but I don’t know why the command is not going in last 3 lines while loop. Someon please help and also check if logic is correct.
Thanks
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b){
if(a==0){
return b;
}
if(b==0){
return a;
}
return GCD(b, a%b);
}
int nGCD(vector<int>arr, int n){
int temp= arr[0];
for(int i=1; i<n; i++){
temp= GCD(temp, arr[i]);
}
return temp;
}
int main() {
// your code goes here
int t;
cin>>t;
int n;
int in;
vector<int>arr;
for(int i=0; i<t; i++){
cin>>n;
for(int i=0; i<n; i++){
cin>>in;
arr.push_back(in);
}
sort(arr.begin(), arr.end());
int check= nGCD(arr, n);
int ans=0;
if(check%2!=0){
while(check%2==0){
check= GCD(arr[0]/2, check);
ans= ans+1;
}
cout<<ans<<endl;
} else{
cout<<ans<<endl;
}
ans=0;
}
return 0;
}
Your program is run against multiple test cases. You should reset the variables for every test case. Declaring variables inside the test case loop will reset the variables automatically for every test case.