Advertisement
saurav_kalsoor

Min Moves - JAVA

Aug 13th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Problem {
  7.  
  8.     public static void main(String[] args){
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         int[] array = new int[n];
  12.  
  13.         for(int i=0; i<n; i++){
  14.             array[i] = sc.nextInt();
  15.         }
  16.         System.out.println(minMoves(array, n));
  17.  
  18.     }
  19.  
  20.     public static int minMoves(int[] array, int n){
  21.         int moves = 0;
  22.  
  23.         for(int i=1 ; i < n; i++){
  24.             if(array[i] < array[i-1]){
  25.                 moves += (array[i-1] - array[i]);
  26.                 array[i] = array[i-1];
  27.             }
  28.         }
  29.  
  30.         return moves;
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement