Split N
Kulyash is given an integer N. His task is to break N into some number of (integer) powers of 2
TO achieve this, he can perform the following operation several times (possibly, zero):
Choose an integer ???? X which he already has, and break X into 2 integer parts Y and
Z such that X=Y+Z.
Find the minimum number of operations required by Kulyash to accomplish his task.
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int c=0;
for(int i=0;i<n;i++){
int k=pow(2,i);
for(int j=i;j<n;j++){
if(k==n){c=0;}
else if(k+pow(2,j)==n){
c+=1;
}
}
}
cout<<c<<"n";
}
// your code goes here
}
I tried by implementing as shown above, the test cases are passing but one test case is not passing due to Time Limit Exceeded. How to reduce the time limit for my code.
Sanjeet Chukka AP21110011402 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3