code exceeding time limit despite having admissible time complexity theoretically.

Правка en2, от VaishCodes, 2025-04-12 23:12:26

problem statement: https://codeforces.me/problemset/problem/1977/B language of my code: C my submission: https://codeforces.me/contest/1977/submission/315178770

my code:

include <stdio.h>

include <stdlib.h>

int bin(int arr[], int x) { int i; for (i = 0; x != 0; i++) { arr[i] = x % 2; x = x / 2; } return i; } int main() { int t; long long int x;

scanf("%d", &t);
for (int j = 0; j < t; j++)
{
    int arr[35];
    scanf("%lld", &x);
    int n;
    int flag = 0;
    n = bin(arr, x);
    for (int i = 1; i < n; i++)
    {

        if (arr[i] * arr[i - 1] != 0 && flag == 0)
        {
            flag++;
            arr[i - 1] = -1;
            if (i == n - 1)
            {
                arr[i] = 0;
                arr[i + 1] = 1;
                n++;break;``

            }
        }
        else if (flag != 0 && arr[i] == arr[i - 1])
        {
            arr[i - 1] = 0;
            if (i == n - 1)
            {
                arr[i] = 0;
                arr[i + 1] = 1;
                n++;break;

            }
        }
        else if (arr[i] != arr[i - 1] && flag != 0)
        {
            flag = 0;
            arr[i] = 1;
            arr[i - 1] = 0;
        }
    }

    printf("%d\n", n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

}

there is no nesting in any loop, and any loop runs atmost 32 times, why is my code exceeding time limit of 1 second??

Теги 1100, gnu c11, time limit exceeded

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский VaishCodes 2025-04-12 23:26:07 0 (published)
en3 Английский VaishCodes 2025-04-12 23:14:56 1474
en2 Английский VaishCodes 2025-04-12 23:12:26 18 (saved to drafts)
en1 Английский VaishCodes 2025-04-12 20:59:00 1795 Initial revision (published)