package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/array-transformation/
 */
public class Problem1243 {
    public List<Integer> transformArray(int[] arr) {
        while (true) {
            boolean found = false;
            for (int i = 1; i < arr.length - 2; i++) {
                if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) {
                    found = true;
                    arr[i]--;
                } else if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) {
                    found = true;
                    arr[i]++;
                }
            }
            if (!found) {
                break;
            }
        }
        List<Integer> answer = new ArrayList<>();
        for (int a : arr) {
            answer.add(a);
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1243 prob = new Problem1243();
        System.out.println(prob.transformArray(new int[]{6,2,3,4})); // [6,3,3,4]
        System.out.println(prob.transformArray(new int[]{1,6,3,4,3,5})); // [1,4,4,4,4,5]
    }
}

@@ -40,16 +40,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -290,16 +290,73 @@
  false;%0A
+            int%5B%5D copy = Arrays.copyOf(arr, arr.length);%0A
         
@@ -392,17 +392,17 @@
 ength - 
-2
+1
 ; i++) %7B
@@ -418,27 +418,28 @@
         if (
-arr
+copy
 %5Bi - 1%5D %3C ar
@@ -440,30 +440,33 @@
 %5D %3C 
-arr
+copy
 %5Bi%5D && 
-arr
+copy
 %5Bi%5D %3E 
-arr
+copy
 %5Bi +
@@ -563,19 +563,20 @@
 lse if (
-arr
+copy
 %5Bi - 1%5D 
@@ -581,30 +581,33 @@
 %5D %3E 
-arr
+copy
 %5Bi%5D && 
-arr
+copy
 %5Bi%5D %3C 
-arr
+copy
 %5Bi +
@@ -929,277 +929,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1243 prob = new Problem1243();%0A        System.out.println(prob.transformArray(new int%5B%5D%7B6,2,3,4%7D)); // %5B6,3,3,4%5D%0A        System.out.println(prob.transformArray(new int%5B%5D%7B1,6,3,4,3,5%7D)); // %5B1,4,4,4,4,5%5D%0A    %7D
 %0A%7D%0A

Solve problem 1243