Skip to content

Commit f9232f3

Browse files
Prime Factors sum in java medium good
There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step: Copy All: You can copy all the characters present on the screen (a partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen. Example 1: Input: n = 3 Output: 3 Explanation: Initially, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Example 2: Input: n = 1 Output: 0 Constraints: 1 <= n <= 1000 Seen this question in a real interview before? 1/5 Yes No Accepted 248.9K Submissions 418.6K Acceptance Rate 59.5% Topics Companies Hint 1 How many characters may be there in the clipboard at the last step if n = 3? n = 7? n = 10? n = 24?
1 parent 1a087cc commit f9232f3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2-keys-keyboard.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int minSteps(int n) {
3+
if(n == 1) return 0;
4+
return sumOfPrimeFactors(n);
5+
}
6+
public static int sumOfPrimeFactors(int number) {
7+
int sum = 0;
8+
while (number % 2 == 0) {
9+
sum += 2;
10+
number /= 2;
11+
}
12+
for (int i = 3; i <= Math.sqrt(number); i += 2) {
13+
while (number % i == 0) {
14+
sum += i;
15+
number /= i;
16+
}
17+
}
18+
if (number > 2) {
19+
sum += number;
20+
}
21+
return sum;
22+
}
23+
}
24+
// Prime Factorization:
25+
// First, the program checks for the smallest prime factor, which is 2, and divides the number until it can't be divided further.
26+
// Then, it checks for other odd factors, starting from 3.
27+
// If the remaining number after division is greater than 2, it is a prime factor itself and is added to the sum.

0 commit comments

Comments
 (0)