Untitled
unknown
java
5 months ago
965 B
4
Indexable
public class ArrayTransformation { public static int solution(int[] A) { int N = A.length; if (N == 0) return 0; // If A is empty, no moves are needed. int moves = A[0]; // First element indicates the moves needed to reach it from 0. // Calculate the number of additional moves needed from differences between consecutive elements for (int i = 1; i < N; i++) { int diff = A[i] - A[i - 1]; if (diff > 0) { // Only positive differences need extra moves moves += diff; } } return moves; } public static void main(String[] args) { // Test cases System.out.println(solution(new int[] {2, 1, 3})); // Should return 4 System.out.println(solution(new int[] {2, 2, 0, 1})); // Should return 3 System.out.println(solution(new int[] {5, 4, 2, 4, 1})); // Should return 7 } }
Editor is loading...
Leave a Comment