-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram29.java
More file actions
118 lines (82 loc) · 3.36 KB
/
program29.java
File metadata and controls
118 lines (82 loc) · 3.36 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
112
113
114
115
116
117
118
/*
2601. Prime Subtraction Operation
Medium
299
29
Companies
You are given a 0-indexed integer array nums of length n.
You can perform the following operation as many times as you want:
Pick an index i that you haven’t picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i].
Return true if you can make nums a strictly increasing array using the above operation and false otherwise.
A strictly increasing array is an array whose each element is strictly greater than its preceding element.
Example 1:
Input: nums = [4,9,6,10]
Output: true
Explanation: In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
After the second operation, nums is sorted in strictly increasing order, so the answer is true.
Example 2:
Input: nums = [6,8,11,12]
Output: true
Explanation: Initially nums is sorted in strictly increasing order, so we don't need to make any operations.
Example 3:
Input: nums = [5,8,3]
Output: false
Explanation: It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.
*/
package LeetCode;
public class program29 {
static boolean primeSubOperation(int[] nums) {
boolean flag = false;
flag = issoted(nums);
// System.out.println(flag);
if (flag) {
return flag;
}
else {
for (int i = 0; i < nums.length; i++) {
for (int j = nums[i] - 1; j >= 1; j--) {
int cnt = 0;
for (int k = 1; k <= j; k++) {
if (j % k == 0) {
cnt++;
}
if (cnt > 2) {
break;
}
}
if (cnt == 2) {
nums[i] = nums[i] - j;
System.out.println(nums[i]);
flag = issoted(nums);
// System.out.println(flag);
if (flag) {
return flag;
}
break;
}
}
}
}
return false;
}
static boolean issoted(int nums[]) {
boolean flag = true;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] >= nums[j]) {
return false;
}
}
}
return flag;
}
public static void main(String[] args) {
int nums[] = {15,20,17,7,16};
// int a[] = primeSubOperation(nums);
// for(int i=0;i<a.length;i++){
// System.out.print(a[i]+" ");
// }
System.out.println(primeSubOperation(nums));
}
}