Stone | CodeChef


can anyone tell why I getting the wrong answer?

My Code:

#include
#include
using namespace std;
int main()
{
int n,k;

cin>>n>>k;
long long int arr[n];
for(int i=0;i<n;i++)
    cin>>arr[i];

int max=*max_element(arr,arr+n);
while(k--)
{
    for(int i=0;i<n;i++)
    {
        arr[i]=max-arr[i];
    }    
    max=*max_element(arr,arr+n);
}
for(int i=0;i<n;i++)
   cout<<arr[i]<<" ";


 return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

The complexity is O(kn) I would expect it to TLE instead. You should try thinking of a faster approach.
I hope you know kn is 10^14 calculations, which the codechef judge would take 3 hours.
After a bit of observation, I noticed that

if k%2==1
  seq[i]=max(seq)-seq[i]
if k%2==0 && k!=0
  seq[i]=seq[i]-min(seq)

I’m guessing this was the needed reasoning

1 Like