Help with calculating Big O

I need some help with how to calculate Big O for this algorithm.

Say for example you have two functions, X and Y whose complexities are O(n). And say there’s a 3rd function Z with the following code:

void Z(int n){
   int i,p=1;
   for(i=1;i<=n;i++){
      X(n);
      if(i==p){
         Y(n);
         p*=2;
      }
   }
}

Can you help me figure out what the worst-case complexity (Big O) for Z would be in this case?
Also what would the Big O be for Z if X was O(1) and Y was O(n²)?

I’m stumped trying to figure out how the if-statement would affect the calculation in both cases. Any help would be really appreciated. Thanks in advance.

So what are your thoughts?

I thought about it and for the 1st case, I figured it’d be O(n²) since you have the for-loop which runs n times and X with O(n) complexity which is n*n, and since Y gets executed less than n times it’s negligible.
For the 2nd case I’d say O(n³).
But I’m not sure if those are the correct answers, I have a strong feeling they’re not and would love if someone could come and say what’s the correct answer and why.