Skip to content

Commit 6b51c15

Browse files
committed
Corrected the invalid answer: Have implemented the program to find whether the array can be made strictly increasing using the values from another array.
1 parent 370a649 commit 6b51c15

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

.idea/workspace.xml

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/raj/MakeArrayStrictlyIncreasing.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,50 @@ Given two integer arrays arr1 and arr2, return the minimum number of operations
2929
*/
3030
package com.raj;
3131

32+
import java.util.Arrays;
33+
3234
public class MakeArrayStrictlyIncreasing {
3335
public static void main(String[] args) {
3436
// Initialization.
3537
int[] arr1 = {1, 5, 3, 6, 7};
3638
int[] arr2 = {4, 3, 1};
39+
boolean isInitialValueChanged = false;
3740
int ans = 0;
3841

3942
// Logic.
40-
for (int i = 0; i < arr1.length - 1; i++) {
43+
// Sort the option array.
44+
Arrays.sort(arr2);
45+
for (int i = 1; i < arr1.length - 1; i++) {
4146
if (arr1[i] >= arr1[i + 1]) {
42-
boolean valueFound = false;
43-
if (i == 0) {
44-
for (int j : arr2) {
45-
if (arr1[i + 1] > j) {
46-
valueFound = true;
47+
boolean isValueFounded = false;
48+
if (!isInitialValueChanged) {
49+
for (int j = 0; j < arr2.length; j++) {
50+
if (arr1[i - 1] < arr2[j]) {
51+
arr1[i] = arr2[j];
52+
isValueFounded = true;
53+
isInitialValueChanged = true;
54+
i--;
4755
ans++;
4856
break;
4957
}
5058
}
51-
} else {
52-
for (int j : arr2) {
53-
if (arr1[i - 1] <= j && arr1[i + 1] > j) {
54-
valueFound = true;
59+
}
60+
if (!isValueFounded) {
61+
for (int j = 0; j < arr2.length; j++) {
62+
if (arr1[i] < arr2[j]) {
63+
arr1[i + 1] = arr2[j];
64+
isValueFounded = true;
65+
isInitialValueChanged = false;
5566
ans++;
5667
break;
5768
}
5869
}
5970
}
60-
if (!valueFound) {
71+
if (!isValueFounded) {
6172
ans = -1;
62-
}
63-
}
64-
}
65-
66-
if (ans == 1 && arr1[arr1.length - 2] >= arr1[arr1.length - 1]) {
67-
boolean valueFound = false;
68-
for (int j : arr2) {
69-
if (arr1[arr1.length - 2] < j) {
70-
valueFound = true;
71-
ans++;
7273
break;
7374
}
7475
}
75-
if (!valueFound) {
76-
ans = -1;
77-
}
7876
}
7977

8078
// Display the result.

0 commit comments

Comments
 (0)