Why (Hacker earth - Charged up Array) one code is accepted and another is producing wrong answer

Viewed 1022

I am trying to solve the Charged Up Array problem from the online judge Hacker Earth. https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/charged-up-array-f35a5e23/
I came up with the solution and written almost the same two different codes for the problem The first one is:

#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    int T, n, i;
    long long int subset_Containg_Ai, x, sum;
    scanf("%d", &T);
    while(T--)
    {
            sum = 0;
            scanf("%d", &n);
            //subset_Containg_Ai = (long long int)pow(2, (n-1));
            //printf("Subset = %d\n", subset_Containg_Ai);
            for(i=0; i<n; i++)
            {
                scanf("%lld", &x);
                if(x>= pow(2, (n-1)))
                {
                    sum = (sum + x ) % 1000000007;
                }
            }

            printf("%lld\n", sum);
    }

    return 0;
}


This code is accepted in the judge server. But here is another version of my code

#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    int T, n, i;
    long long int subset_Containg_Ai, x, sum;
    scanf("%d", &T);
    while(T--)
    {
            sum = 0;
            scanf("%d", &n);
            subset_Containg_Ai = (long long int)pow(2, (n-1));
            //printf("Subset = %d\n", subset_Containg_Ai);
            for(i=0; i<n; i++)
            {
                scanf("%lld", &x);
                if(x>=subset_Containg_Ai)
                {
                    sum = (sum + x ) % 1000000007;
                }
            }

            printf("%lld\n", sum);
    }

    return 0;
}


In the second code, I used an extra variable "subset_Containg_Ai" and using it to compare with the variable "x" instead of calling the function pow every time. I thought it would increase the efficiency of the code. On the contrary, this second code is showing "Wrong Answer" by the judge! Can anyone tell me what is going on here?

0 Answers
Related