Doubt of array rotation

the ques is
https://www.codechef.com/LTIME95C/problems/ARRROT

Idk why am I getting WA
https://www.codechef.com/submit/ARRROT
MY CODE:

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

int main() {
int n,q,i,k=1;

long long int count=0,sum=0;
cin>>n;
int arr[n];

for(i=0;i<n;i++)
{
cin >>arr[i];
}

cin>>q;
int out[q];
int arr2[q];
for(i=0;i<q;i++)
{
cin >>arr2[i];
}

for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
//cout<<sum<<endl<<q;

if(k<=q)
{
for(i=0;i<q;i++)
{
count=pow(2,k)*sum;
out[i]=count;
cout<<count<<endl;
k++;
}
}
return 0;
}

1 Like

Queries are dependent - which means you have to update your answer for every query.

pow(2, k) can exceed the range of all existing data types. So, you might want to implement the solution using Modular Arithmetic.

1 Like

yess thanks

why is this wrong now!!

#include
#include <math.h>

#include<bits/stdc++.h>
#define ll long long int`
using namespace std;
#define MOD 10000000007

int main() {
int n,q,i,k=1;

long long int count=0,sum=0;
cin>>n;
signed int arr[n];

for(i=0;i<n;i++)
{
cin >>arr[i];
}

cin>>q;
int out[q];
signed int arr2[q];
for(i=0;i<q;i++)
{
cin >>arr2[i];
}

for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
//cout<<sum<<endl<<q;

if(k<=q)
{
for(i=0;i<q;i++)
{
count=pow(2,k)*sum;
out[i]=count;
count=((count%MOD)+MOD%MOD);
cout<<count<<endl;
k++;
}
}
return 0;
}

The question already has answer in the previous comment.

 Try this bro:-
 I think we have to tackle cases where x(number) is -ve To calculate thre MOD you can 
  just do 
 (MOD+x)%MOD which is equivalebt to x%MOD

 example  2%5=7%5

 #include <bits/stdc++.h>

  using namespace std;

  #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

  #define vi vector<int>

  #define ll long long

  #define all(x) x.begin(),x.end()

  #define MOD 1000000007

  int main()

  {

    IOS  

    int n;

    cin>>n;

    int a[n];

    ll sum=0;

    for(int i=0;i<n;i++)

     cin>>a[i];

    for(int i=0;i<n;i++) 

     sum+=(a[i]+MOD)%MOD;

    

    int q;

    cin>>q;

    for(int i=1;i<=q;i++)

    {

        int h;

        cin>>h;

        sum=(2*sum)%MOD;

        cout<<sum<<"\n";    

    } 

    return 0;

  }