Submission

Status:

[PPPPPPPPPP][PPPPP]

Score: 100

User: KuyHeeTad

Problemset: ขนมของเซ้น143 (v.ยาก)

Language: c

Time: 0.191 second

Submitted On: 2024-10-03 09:17:48

#include <stdio.h>

long long int findMinDifference(long long int num, long long int i, long long int minDifference) {
    if (i >= num) {
        return minDifference; // Stop when i >= num
    }

    long long int d = ((1 + (num - i)) * (num - i) / 2) - (((2 * num) + 1 - i) * i / 2);
    
    if (d < 0) {
        d = -d; // Get the absolute value of d
    }

    if (d < minDifference) {
        minDifference = d; // Update minDifference if a smaller value is found
    } else {
        return minDifference; // Stop if d is not less than minDifference
    }

    return findMinDifference(num, i + 1, minDifference); // Recursive call
}

int main() {
    long long int num;
    scanf("%lld", &num); // Read the number from input
    
    long long int minDifference = 9000000000000000000; // Initialize to a large value
    minDifference = findMinDifference(num, 1, minDifference); // Initial function call

    printf("%lld\n", minDifference); // Print the smallest difference found
    
    return 0;
}