Leetcode 1952: Three Divisors

grid47
grid47
Exploring patterns and algorithms
Apr 25, 2024 4 min read

Given an integer n, return true if n has exactly three distinct positive divisors. Otherwise, return false. A divisor of n is a positive integer that divides n without leaving a remainder.
Problem
Approach
Steps
Complexity
Input: You are given an integer n. Your task is to determine if n has exactly three positive divisors.
Example: n = 9
Constraints:
• 1 <= n <= 10^4
Output: Return true if n has exactly three divisors, otherwise return false.
Example: Output: true
Constraints:
• The output should be a boolean value indicating whether n has exactly three divisors.
Goal: The goal is to check if the given integer n has exactly three distinct divisors.
Steps:
• Step 1: Find all divisors of n by iterating through integers from 1 to sqrt(n).
• Step 2: Count the number of divisors.
• Step 3: If the count of divisors is exactly 3, return true; otherwise, return false.
Goal: The integer n should be between 1 and 10^4, inclusive.
Steps:
• 1 <= n <= 10^4
Assumptions:
• The input integer n will always be a valid integer within the specified range.
Input: Input: n = 9
Explanation: The divisors of 9 are 1, 3, and 9, so it has exactly 3 divisors. Therefore, the output is true.

Input: Input: n = 10
Explanation: The divisors of 10 are 1, 2, 5, and 10, so it has 4 divisors. Therefore, the output is false.

Link to LeetCode Lab


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