Difference in pow and loop results in Test case Failure of Question Powered Parameters

Question Link : Powered Parameters Practice Coding Problem - CodeChef

My Code :
include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
long long ans = 0;
int x = min(30,n);
for(int i=0;i<n;i++){
if(arr[i] == 1){
ans+=n;
continue;
}
for(int j = 1;j<x;j++){
if(pow(arr[i],j) <= arr[j-1])
ans++;
}
}
cout<<ans<<endl;
}
}

Code which passing all TC’s

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

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
long long ans = 0;
int x = min(30,n);
for(int i=0;i<n;i++){
if(arr[i] == 1){
ans+=n;
continue;
}
for(int j = 0;j<x;j++){
if(pow(arr[i],j+1) <= arr[j])
ans++;
}
}
cout<<ans<<endl;
}
}

Changing only pow function and loop resulting in Test cases Passing. I cannot see what Testcase is Failing. So I cannot deduce what is wring in First code. Please help.

It is because of <x as in this case ,if n=26 you won’t be checking for the element at the 25th place as you are running a loop only till 25.
arr[j-1]=arr[25-1]=arr[24].
That is why the first code fails.

If you just change <x to <=x the code will get accepted.

1 Like

Thanks, I got it.