Advertisement
saurav_kalsoor

Minimum Moves - JAVA

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