-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily147.cpp
More file actions
112 lines (95 loc) · 3.08 KB
/
Copy pathdaily147.cpp
File metadata and controls
112 lines (95 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Solution 1 - FAIL
class Solution {
public:
bool primeSubOperation(vector<int>& nums) {
/*
start from right
if number to the left is greater, reduce until smaller
if reductions cause a negative number, return false
return true
*/
for (int i = nums.size() - 1; i > 0; --i) {
std::cout << "curr: " << nums[i] << ", left: " << nums[i - 1] << std::endl;
auto n = nums[i - 1];
int prev = 0;
int res = nums[i - 1];
while (res >= nums[i]) {
std::cout << n << std::endl;
if (n == prev)
return false;
prev = n;
n = nearestLowerPrime(n);
res = nums[i - 1] - n;
}
std::cout << n << std::endl;
std::cout << "exited\n";
nums[i - 1] = res;
}
return true;
}
private:
// Helper function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) return false;
if (num == 2 || num == 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
// Function to find the nearest prime less than n
int nearestLowerPrime(int n) {
// Start from n - 1 and check downwards
for (int i = n - 1; i > 1; --i) {
if (isPrime(i)) return i;
}
return -1; // In case there's no prime (edge case for very small values of n)
}
};
// Solution 2
class Solution {
public:
bool primeSubOperation(vector<int>& nums) {
int maxElement = getMaxElement(nums);
// Create Sieve of Eratosthenes array to identify prime numbers
vector<bool> sieve(maxElement + 1, true);
sieve[1] = false;
for (int i = 2; i <= sqrt(maxElement + 1); i++) {
if (sieve[i]) {
for (int j = i * i; j <= maxElement; j += i) {
sieve[j] = false;
}
}
}
// Check if array can be made strictly increasing by subtracting prime numbers
int currValue = 1;
int i = 0;
while (i < nums.size()) {
int difference = nums[i] - currValue;
// Return false if current number is already smaller than required value
if (difference < 0) {
return false;
}
// Move to next number if difference is prime or zero
if (sieve[difference] == true || difference == 0) {
i++;
currValue++;
} else {
currValue++;
}
}
return true;
}
private:
// Helper method to find maximum element in array
int getMaxElement(vector<int>& nums) {
int max = -1;
for (int num : nums) {
if (num > max) {
max = num;
}
}
return max;
}
};