Why the while loop is getting skipped?

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;
}
    if(check%2!=0){
            while(check%2==0){

:face_with_raised_eyebrow:

5 Likes

your vector arr never cleared, so nGCD result will never be correct except the first test case :thinking:

1 Like

I think your logic is incorrect as well.
For ex:
3
16 26 28

Answer: 1

On this testcase it will get stuck in an infinite loop even if you fix the while loop.

1 Like

if was not needed, just put it for checking whether control was passing in while loop or not.

What about this one? It is again not passing control to while loop and 0 is getting printed for all 3 test cases.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	int n;
	int temp;
	int cnt=0;
	int ans= INT_MAX;
	vector<int>arr;
	
	for(int i=0; i<t; i++){
	    cin>>n;
	    
	    for(int i=0; i<n; i++){
	        cin>>temp;
	        arr.push_back(temp);
	    }
	    
	    for(int i=0; i<n; i++){
	        cnt=0;
	        while(arr[i]%2==0){
	            arr[i]= arr[i]/2;
	            cnt++;
	        }
	        ans= min(cnt, ans);
	    }
	    
	    cout<<ans<<endl;
	    
	}
	
	return 0;
}

Declare ans and arr inside the test case loop.

1 Like

One of those issues was identified for you here:

Yes, and someone proved the approach wrong, so I tried an alternate approach.

How does it matters?

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.

1 Like

@ffb_1 To see why this matters: I’ve patched your code from the OP as follows:

#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);
        }
        {
            // ssjgz added this block.
            cout << "Contents of array I've (apparently) just read in: " << endl;
            for (const auto x : arr)
            {
                cout << x << " ";
            }
            cout << endl;
        }
        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;
}

Run it with the sample test input:

[simon@simon-laptop][13:29:16]
[~/devel/hackerrank/otherpeoples]>echo "3
3
2 3 5
2
4 6
3 
4 12 24
" | ./a.out
Contents of array I've (apparently) just read in: 
2 3 5 
0
Contents of array I've (apparently) just read in: 
2 3 5 4 6 
0
Contents of array I've (apparently) just read in: 
2 3 4 5 6 4 12 24 
0

while in if condition statement is wrong.