LeetCode 2652: Sum Multiples Solution

Master LeetCode problem 2652 (Sum Multiples), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2652. Sum Multiples

Easy

Problem Explanation

Explanation

To solve this problem, we iterate through all numbers from 1 to n and check if the number is divisible by 3, 5, or 7. If the number is divisible by any of these, we add it to the sum. Finally, we return the sum as the result.

  • We iterate through numbers from 1 to n
  • For each number, we check if it is divisible by 3, 5, or 7
  • If it is divisible by any of these, we add it to the sum
  • Return the final sum as the result

Time Complexity: O(n) Space Complexity: O(1)

Solution Code

class Solution {
    public int sumMultiples(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) {
                sum += i;
            }
        }
        return sum;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2652 (Sum Multiples)?

This page provides optimized solutions for LeetCode problem 2652 (Sum Multiples) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2652 (Sum Multiples)?

The time complexity for LeetCode 2652 (Sum Multiples) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2652 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2652 in Java, C++, or Python.

Back to LeetCode Solutions