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

#include <bits/stdc++.h>
#define ll long long
// #define inf 1000000000000000000
using namespace std;
ll mod = 1000000000000000001;
bool check(string a) {
  if (a.size() > 19) {
    // cout<<'t';
    return true;
  } else if (a.size() < 19)
    return false;
  else {
    for (int i = 0; i < a.size(); i++) {
      if (i == 0) {
        if (a[i] > '1') {
          // cout<<'y';
          return true;
        }
      } else if (a[i] > '0') {
        // cout<<'n';
        return true;
      }
    }
  }
  return false;
}
string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}
string numtostr(ll n) {
  string ans = "";
  while (n > 0) {
    ll rem = n % 10;
    ans += (char)(rem + '0');
    n /= 10;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  int n;
  cin >> n;
  ll arr[n];
  string mul = "1";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  for (int i = 0; i < n; i++) {
    string tmp = numtostr(arr[i]);
    // cout<<tmp<<endl;
    mul = multiply(mul, tmp);
    if (check(mul) == true) {
      // cout<<mul<<endl;
      cout << -1 << endl;
      return 0;
    }
  }
  // cout<<mul<<" "<<mul2<<endl;
  cout << mul << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
// #define inf 1000000000000000000
using namespace std;
ll mod = 1e18;
bool check(string a) {
  if (a.size() > 19) {
    // cout<<'t';
    return true;
  } else if (a.size() < 19)
    return false;
  else {
    for (int i = 0; i < a.size(); i++) {
      if (i == 0) {
        if (a[i] > '1') {
          // cout<<'y';
          return true;
        }
      } else if (a[i] > '0') {
        // cout<<'n';
        return true;
      }
    }
  }
  return false;
}
string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

int main() {
  int n;
  cin >> n;
  string arr[n];
  string mul = "1";
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    /*if(check(mul,arr[i])==true) {
        cout<<-1<<endl;
        return 0;
    }
    else */
    mul = multiply(mul, arr[i]);
    // cout<<arr[i]<<" "<<mul<<endl;
  }
  if (check(mul) == true)
    cout << -1 << endl;
  else
    cout << mul << endl;
  return 0;
}