getting WA in RRSTONE Problem ,,,,Here is my code

#include
using namespace std;

long long int n,k;
long long int a[1000000];
int main()
{
    cin>>n;
    cin>>k;
    for(long int i=0;i<n;i++)
    cin>>a[i];
    while(k--)
    {
        unsigned int max=-2000000;
        for(long int i=0;i<n;i++)
        {
            if(max<a[i])
            max=a[i];
        }
        for(long int i=0;i<n;i++)
        {
            a[i]=max-a[i];
        }

    }
    for(long int i=0;i<n;i++)
    {
        cout<<a[i]<<" ";

    }


}

Take care of the value of max as
A[i] can be as low as -1000000000
Use

max = INT_MIN ;

and include header file “limits.h”

As for the even-odd part,let us take an example.

we have 5 -1 7 0

after 1st round: 2 8 0 7

after 2nd round: 6 0 8 1

after 3rd round: 2 8 0 7

after 4th round: 6 0 8 1

and so on…

So,if you see,ther will always be repetition in the answer.
By your approach you will get TLE if you iterate k times. So just see if k is odd or even

Hope it helps :slight_smile:

1 Like

Problems -

  1. You have assigned initial max = -2000000 (= -2*10^6), while it is clearly mentioned that Ai does not exceed 2 * 10^9 by it's absolute value. Consider an array which has all elements Ai (-2 * 10^9 <= Ai < -2 * 10^6). Your max will give an incorrect value.

  2. You have a while loop that runs k times, and two for loops inside it which themselves loop n times. In total you loop k * (2 * n) times, which is an O(k*n) complexity. According to the time limit, even if your solution is correct, it will not pass the time constraints. There exists a more optimal solution to this problem and I think you can get the hint from @asif_mak’s answer.

can u pls explain