C, can run test case but not accept


  • 0
    C

    This is my code, it is not accepted.
    When I submit , it was rejected for wrong answer when the test case input is 10.
    However I can get the ans correct in custom case. Is there any fatal mistake in it?

    if (n <= 2) return 0;
    if (n == 3) return 1;
    
    
    bool* notPrime = (bool*)malloc(sizeof(bool) * n);
    int sqr = sqrt(n);
    int count = 1;
    
    for (int j = 2; j <= sqr ; j++) {
    	if (notPrime[j] == 0) {
    		for (int k = 2; j * k <= n; k++) {
    			if (notPrime[j * k] == 0) {
    				if (j * k == n) break;
    				notPrime[j * k] = 1;
    				count++;
    			}
    		}
    	}
    }
    count = n-count-1;
    free(notPrime);
    
    return count;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.