Leetcode 2652: Sum Multiples

grid47
grid47
Exploring patterns and algorithms
Feb 15, 2024 4 min read

Given a positive integer ’n’, find the sum of all integers between 1 and ’n’ (inclusive) that are divisible by 3, 5, or 7.
Problem
Approach
Steps
Complexity
Input: The input consists of a single integer 'n' where 1 <= n <= 1000.
Example: Input: n = 12
Constraints:
• 1 <= n <= 1000
Output: Return the sum of all numbers in the range [1, n] that are divisible by 3, 5, or 7.
Example: Output: 45
Constraints:
• The sum will always be an integer.
Goal: The goal is to sum all numbers divisible by 3, 5, or 7 from 1 to n.
Steps:
• Step 1: Iterate over all integers from 1 to n.
• Step 2: For each number, check if it is divisible by 3, 5, or 7.
• Step 3: If divisible, add it to the sum.
• Step 4: Return the total sum.
Goal: The solution should efficiently handle the input size up to n = 1000.
Steps:
• 1 <= n <= 1000
Assumptions:
• The input 'n' is a valid positive integer.
Input: Input: n = 7
Explanation: The numbers divisible by 3, 5, or 7 in the range [1, 7] are 3, 5, 6, and 7. The sum is 3 + 5 + 6 + 7 = 21.

Input: Input: n = 10
Explanation: The numbers divisible by 3, 5, or 7 in the range [1, 10] are 3, 5, 6, 7, 9, and 10. The sum is 3 + 5 + 6 + 7 + 9 + 10 = 40.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus