Good Reductions

4.7

3 votes
, Data Structures, Math, Basics of Implementation, Basic Programming, Basic Math
Problem

You are given a large number S containing N digits.If sum of digits in the number is odd than the number is considered good.You can do the following operation any number of times to make the given number good:
Operation -
Divide the number by 10.

Task :

Return the length of maximum possible good number from the given number, if a good number is not achievable then return 1.

Assumption :

S=1221

Approach :

The sum of digits in S is 6 if we divide S by 10 , 1 will be removed and sum of digits will become 5 and length of S becomes 3 so the answer is 3

Function description:

Complete the function solve provided in the editor. This function takes the following two parameters and returns the required answer:

N represents the number of digits in the given number.

S represents the given number.

Input format :

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

The first line contains T denoting the number of test cases. also specifies the number of times you have to run the solve function on a different set of inputs.

For each test case, the first line contains the integer N and second line contains the number S.

Output format :

For each test case, print the length of good number obtained by using minimum number of operations.

Constraints:

1T10

1N105  

 

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Sample Input
3
4
1221
5
82428
3
111
Sample Output
3
-1
3
Time Limit: 1.5
Memory Limit: 256
Source Limit:
Explanation

The first line contains the number of test cases.

The first test case is explained in the above example in the question.

In the second test case, the number can't become a good number so answer is 1.

Editor Image

?