Number Difference

Irvi Aini ยท May 28, 2025

๐ŸŽฎ Number Difference Game ๐ŸŽฎ

An interactive programming challenge that visualizes the difference between sums of numbers divisible and not divisible by a given value.

๐ŸŽฎ Interactive Challenge ๐ŸŽฎ

Play the Game

๐Ÿง‘โ€๐Ÿ’ป Code Implementation

class Solution {
public:
    int differenceOfSums(int n, int m) {
        std::vector<int> numbers(n);
        std::iota(numbers.begin(), numbers.end(), 1);
        
        return std::transform_reduce(numbers.begin(), numbers.end(), 0, 
            std::plus<int>{},
            [m](int num) { return num % m == 0 ? -num : num; });
    }
};

๐ŸŽฏ How it works:

Goal: Calculate the difference between the sum of numbers NOT divisible by m and the sum of numbers divisible by m.

Algorithm: For each number from 1 to n, if it's divisible by m, subtract it from our answer. Otherwise, add it to our answer.

Math Formula: result = ฮฃ(non-divisible) - ฮฃ(divisible)

Twitter, Facebook