You are given an array of integers and queries. In each query, you are given an integer .
Your task is to find the minimum index greater than such that:
- Sum of digits of is greater than the sum of digits of
- <
If there is no answer, then print -1
Example:
Input: size=5, arr[]={ 62, 70, 28, 62, 92 }
Output: 2
Approach
Java
public class MinimumIndexes {static int sumDigits(int in) {char[] arr = String.valueOf(in).toCharArray();int out = 0;for (char a : arr) {out += a - '0';}return out;}public static void main(String[] args) throws Exception {int size = 5;int[] arr = { 62, 70, 28, 62, 92 };int[] sumArr = new int[size];for (int i = 0; i < size; i++) {sumArr[i] = sumDigits(arr[i]);}int index = 1;int out = -1;for (int i = index - 1; i < size; i++) {if (arr[i] > arr[index - 1] && sumArr[i] < sumArr[index - 1]) {out = i + 1;break;}}System.out.println(out);}}
No comments:
Post a Comment