-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
664e561
commit 5399852
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
public class Solution { | ||
|
||
public boolean checkPrime(int x) { | ||
for (int i = 2; i <= Math.sqrt(x); i++) { | ||
if (x % i == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean primeSubOperation(int[] nums) { | ||
for (int i = 0; i < nums.length; i++) { | ||
int bound; | ||
// In case of first index, we need to find the largest prime less than nums[0]. | ||
if (i == 0) { | ||
bound = nums[0]; | ||
} else { | ||
// Otherwise, we need to find the largest prime, that makes the current element closest to the previous element. | ||
bound = nums[i] - nums[i - 1]; | ||
} | ||
|
||
// If the bound is less than or equal to 0, then the array cannot be made strictly increasing. | ||
if (bound <= 0) { | ||
return false; | ||
} | ||
|
||
// Find the largest prime less than bound. | ||
int largestPrime = 0; | ||
for (int j = bound - 1; j >= 2; j--) { | ||
if (checkPrime(j)) { | ||
largestPrime = j; | ||
break; | ||
} | ||
} | ||
|
||
// Subtract this value from nums[i]. | ||
nums[i] = nums[i] - largestPrime; | ||
} | ||
return true; | ||
} | ||
} |