Help me in solving SPCP5 problem

My issue

please provide me the solution for this problem

My code

#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
    if (n <= 1) {
        return false;
    }

    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
    }

int nearestPrime(int n) {
    for (int i = n; i >= 2; --i) {
        if (isPrime(i)) {
            return i;
        }
    }

    // If no prime is found (unlikely for positive n), return -1 or handle it accordingly
    return -1;
  }

int main() {
	// your code goes here
	int t;
	cin>>t;
	
	while(t--){
	    int h;
	    cin>>h;
	    int rp=1,sp;
	       
	    int p=nearestPrime(h);
	    int r=h-p;
	    int d=h-p;
	    int count=1;
	    for(int rp=1;rp<=d;rp*=2){
	        r=r-rp;
	        count++;
	    }
	    if(r<0){
	        cout<<-1<<endl;
	    }
	    else 
	    cout<<count<<endl;
	    
	    
	    
	    
  }
	return 0;
}

Problem Link: Monsters Practice Coding Problem - CodeChef

https://www.codechef.com/viewsolution/1032611488
Check this
Also you should change your approach a bit

Hey @jcchakradhar I understood your code. You are just finding Nearest prime number possible for given Health only once. There lies the problem, always for The Nearest prime it is not possible to have the possible sequence for power doubling up. You have to find next nearest prime and for that prime, you have to check again the doubling sequence is possible or not.
Here’s a tip to find if the doubling sequence is possible or not.

  1. Nearest prime (say p) and the monster health (h)
  2. r = h - p
  3. log(r-1) base 2 must be integer, Then only the sequence will be possible
  4. If log(r-1)base 2 not integer. You have to check next nearest prime.Update r=h-p.You could do that using a loop.
  5. Finally if you dont ever find a prime such that sequence is possible.(Test Case).You just try without prime number i.e., log(h-1) base 2. Even if it is not also possible you need to return -1.

i think your 3rd cond should be log2(r+1) must be an integer