quinamatics's blog

By quinamatics, history, 7 years ago, In English

Hi, what is wrong with this code. I know that test case 4: 70,3326631213 is wrong, but I can't find the optimal solution.

import java.util.Arrays; import java.util.Scanner;

public class D2417B {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    s.nextLine();
    String str = s.nextLine();
    int[] arr = new int[str.length()];
    char[] ch = str.toCharArray();
    int sum =0;
    for(int i = 0;i < ch.length; i++){
       arr[i] = Character.getNumericValue(ch[i]);
       sum += arr[i];
    }

    Arrays.sort(arr);
    int fin = 0;
    if(sum >= k)
       System.out.println(0);
    else{
       sum = k-sum;
       for(int i = 0; i < arr.length; i++){
         if(sum + arr[i] <= 9){
          fin += Math.min(sum,10-sum);
          sum =0;
          break;
         }
         else{
          sum += arr[i];
          sum -= 9;
          fin += Math.min(9-arr[i],arr[i]+1);
         }
       }
       System.out.println(fin);
    }
}

}

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +4 Vote: I do not like it

I think, you are not understanding what the output should be. The output for this problem should be the minimum number of digits that could have been modified.