diff options
author | Jack Sangdahl <[email protected]> | 2025-05-26 18:31:00 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-26 18:31:00 -0600 |
commit | 74934eb304f3205b3eb61f4e970fc70e9b6383e2 (patch) | |
tree | 9ce1cdcabedd745f1d0ae446e8f984d02bfdcb93 /div_non_div_sum_diff.c | |
parent | 0f8510ead88cab82038d1ab8cba3997a508370eb (diff) | |
download | leetcode-74934eb304f3205b3eb61f4e970fc70e9b6383e2.tar.gz leetcode-74934eb304f3205b3eb61f4e970fc70e9b6383e2.zip |
2894. divisible and non-divisible sums difference
Diffstat (limited to 'div_non_div_sum_diff.c')
-rw-r--r-- | div_non_div_sum_diff.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/div_non_div_sum_diff.c b/div_non_div_sum_diff.c new file mode 100644 index 0000000..90ecdc6 --- /dev/null +++ b/div_non_div_sum_diff.c @@ -0,0 +1,19 @@ +// 2894. Divisible and Non-divisible Sums Difference +#include "leetcode.h" + +/* + * you are given a positive integers 'n' and 'm'. define two integers as + * follows: 'num1': the sum of all integers in the range '[1, n]' that are not + * divisible by 'm'. 'num2': the sum of all integers in the range '[1, n]' that + * are divisible by 'm'. return the integer 'num1 - num2' + */ + +int differenceOfSums(int n, int m) { + return (1 + n) * n / 2 - (1 + n / m) * (n / m) * m; +} + +int main() { + printf("%d\n", differenceOfSums(10, 3)); // expect: 19 + printf("%d\n", differenceOfSums(5, 6)); // expect: 15 + printf("%d\n", differenceOfSums(5, 1)); // expect: -15 +} |