LeetCode 1952: Three Divisors Solution

Master LeetCode problem 1952 (Three Divisors), 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.

1952. Three Divisors

Problem Explanation

Explanation

To solve this problem, we need to check if the given integer n has exactly three positive divisors. We can determine the number of divisors by iterating from 1 to the square root of n and counting the divisors. If the count reaches 3, we return true. The time complexity of this approach is O(sqrt(n)).

Solution Code

class Solution {
    public boolean isThree(int n) {
        int count = 0;
        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                count++;
                if (n / i != i) {
                    count++;
                }
            }
            if (count > 3) {
                return false;
            }
        }
        return count == 3;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1952 (Three Divisors)?

This page provides optimized solutions for LeetCode problem 1952 (Three Divisors) 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 1952 (Three Divisors)?

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

Can I run code for LeetCode 1952 on DevExCode?

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

Back to LeetCode Solutions