Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int factorial(int n);
int factorial(int n) {
  if (n == 1) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

bool is_prime(ll n) {
  // Assumes that n is a positive natural number
  // We know 1 is not a prime number
  if (n == 1) {
    return false;
  }

  ll i = 2;
  // This will loop from 2 to int(sqrt(x))
  while (i * i <= n) {
    // Check if i divides x without leaving a remainder
    if (n % i == 0) {
      // This means that n has a factor in between 2 and sqrt(n)
      // So it is not a prime number
      return false;
    }
    i += 1;
  }
  // If we did not find any factor in the above loop,
  // then n is a prime number
  return true;
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
typedef unsigned long long ull;

int check(int x, vector<int> a, int n) {
  int county = 0;
  map<int, int> mapa;
  for (int i = 0; i < n; i++) {
    mapa.insert(pair<int, int>(a[i], 0));
  }
  for (int i = 0; i < n; i++) {
    mapa[a[i]]++;
  }
  // cout<<mapa[1]<<mapa[2]<<mapa[3]<<mapa[4]<<mapa[5]<<endl;
  for (int i = 0; i < a.size(); i++) {
    mapa[a[i]]--;
    if (mapa[abs(a[i] - x)] > 0) {
      county++;

      mapa[abs(a[i] - x)]--;
    }
  }
  return county;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long x, k, d;
  cin >> x >> k >> d;
  long long cha = x;
  if (cha > 0) {
    ll z;
    if (x / d <= k) {
      ll u = x / d;
      z = k - (x / d);
      x = x - u * d;

    } else {
      z = 0;
      x = x - k * d;
    }

    /*while(z>0)
    {
        if(county%2==0)
        {
            x=x-d;
            county++;
        }
        else
        {
            x=x+d;
            county++;
        }


        z--;
    }*/
    if (z > 0) {
      if (z % 2 != 0) {
        x = x - d;
      }
    }

  } else if (cha == 0) {
    if (k % 2 != 0) {
      x = x - d;
    }

  } else if (cha < 0) {
    ll z;
    if (abs(x) / d <= k) {
      ll u = abs(x) / d;
      z = k - (abs(x) / d);
      x = x + u * d;
    } else {
      x = x + k * d;
      z = 0;
    }
    ll county = 0;

    /* while(z>0)
     {
         if(county%2==0)
         {
             x=x+d;
             county++;
         }
         else
         {
             x=x-d;
             county++;
         }


         z--;
     }*/
    if (z > 0) {
      if (z % 2 != 0) {
        x = x + d;
      }
    }
  }
  cout << abs(x) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int factorial(int n);
int factorial(int n) {
  if (n == 1) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

bool is_prime(ll n) {
  // Assumes that n is a positive natural number
  // We know 1 is not a prime number
  if (n == 1) {
    return false;
  }

  ll i = 2;
  // This will loop from 2 to int(sqrt(x))
  while (i * i <= n) {
    // Check if i divides x without leaving a remainder
    if (n % i == 0) {
      // This means that n has a factor in between 2 and sqrt(n)
      // So it is not a prime number
      return false;
    }
    i += 1;
  }
  // If we did not find any factor in the above loop,
  // then n is a prime number
  return true;
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
typedef unsigned long long ull;

int check(int x, vector<int> a, int n) {
  int county = 0;
  map<int, int> mapa;
  for (int i = 0; i < n; i++) {
    mapa.insert(pair<int, int>(a[i], 0));
  }
  for (int i = 0; i < n; i++) {
    mapa[a[i]]++;
  }
  // cout<<mapa[1]<<mapa[2]<<mapa[3]<<mapa[4]<<mapa[5]<<endl;
  for (int i = 0; i < a.size(); i++) {
    mapa[a[i]]--;
    if (mapa[abs(a[i] - x)] > 0) {
      county++;

      mapa[abs(a[i] - x)]--;
    }
  }
  return county;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long x, k, d;
  cin >> x >> k >> d;
  long long cha = x;
  if (cha >= 0) {
    while (k > 0) {
      if (abs(x) >= d) {
        x = x - d;
      } else if (abs(x) < d) {
        break;
      }

      k--;
    }
    ll county = 0;
    while (k > 0) {
      if (county % 2 == 0) {
        x = x - d;
        county++;
      } else {
        x = x + d;
        county++;
      }

      k--;
    }

  } else if (cha < 0) {
    while (k > 0) {
      if (abs(x) >= d) {
        x = x + d;
      } else if (abs(x) < d) {
        break;
      }

      k--;
    }
    ll county = 0;
    while (k > 0) {
      if (county % 2 == 0) {
        x = x + d;
        county++;
      } else {
        x = x - d;
        county++;
      }

      k--;
    }
  }
  cout << abs(x) << endl;
}