Leetcode 2894: Divisible and Non-divisible Sums Difference
You are given two positive integers, n and m. Calculate the difference between the sum of integers in the range [1, n] that are not divisible by m (num1) and the sum of integers in the range [1, n] that are divisible by m (num2). Return the result of num1 - num2.
Problem
Approach
Steps
Complexity
Input: The input consists of two integers, n and m.
Example: n = 12, m = 4
Constraints:
• 1 <= n, m <= 1000
Output: Return the value of num1 - num2, where num1 is the sum of integers not divisible by m, and num2 is the sum of integers divisible by m.
Example: For input n = 12, m = 4, the output is 58.
Constraints:
Goal: The goal is to calculate the sum of integers that are divisible by m and those that are not, and return the difference between the two sums.
Steps:
• Loop through all integers from 1 to n.
• Check if each number is divisible by m.
• If divisible, add it to num2; if not, add it to num1.
• Calculate the difference num1 - num2.
Goal: The solution must handle numbers up to 1000 efficiently.
Steps:
• 1 <= n, m <= 1000
Assumptions:
• Both n and m are positive integers.
• Input: For input n = 12, m = 4, the output is 58.
• Explanation: The sum of numbers in the range [1, 12] that are not divisible by 4 is 54, while the sum of numbers divisible by 4 is 24. The result is 54 - 24 = 58.
Approach: The approach involves iterating through the numbers from 1 to n, categorizing them as divisible by m or not, and summing them accordingly.
Observations:
• The task involves checking divisibility for each number from 1 to n.
• This can be solved by a simple loop through the range [1, n].
Steps:
• Initialize two variables for num1 and num2.
• Loop through the range from 1 to n.
• For each number, check if it is divisible by m.
• Add to num2 if divisible, and to num1 if not.
• Return the difference num1 - num2.
Empty Inputs:
• Both n and m are guaranteed to be positive, so there are no empty inputs.
Large Inputs:
• Ensure the algorithm handles cases with large values of n and m up to 1000.
Special Values:
• Consider when m is larger than n, where no numbers are divisible by m.
Constraints:
• The algorithm should be efficient enough to handle n up to 1000.
int differenceOfSums(int n, int m) {
int res = 0;
for (int i = 1; i <= n; ++i) {
if (i % m == 0) {
res -= i;
} else {
res += i;
}
}
return res;
}
1 : Function Definition
int differenceOfSums(int n, int m) {
Define the function that calculates the difference of sums. Takes two parameters: n (upper limit) and m (divisor).
2 : Variable Initialization
int res = 0;
Initialize the result variable to zero. This will store the accumulated sum (or difference).
3 : For Loop
for (int i = 1; i <= n; ++i) {
Start a loop from 1 to n (inclusive) to iterate through all integers.
4 : Condition Check
if (i % m == 0) {
Check if the current integer i is divisible by m (using modulus operation).
5 : Subtraction
res -= i;
If i is divisible by m, subtract it from the result.
6 : Else Block
} else {
If i is not divisible by m, add it to the result.
7 : Addition
res += i;
Add the value of i to the result if it's not divisible by m.
8 : Return Statement
return res;
Return the result, which is the difference of sums after processing all integers.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) as we iterate through the range [1, n].
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1) as we only need a constant amount of space for the sums.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus