We define the valencia of a natural number as the absolute value of the substraction between the sum of the digits that are in odd positions and the sum of the digits that are in even positions (the positions are counted starting in one and from the right to the left). is balanced if its valencia is 0.
For instance 15741 is a balanced number, because the sum of the digits that are in odd positions and the sum of the digits that are in even positions is 9, therefore it has valencia 0. However, 31 is not a balanced number, because its valencia is 2.
Your task is to write a program that, given a non empty sequence of natural numbers, prints the first balanced number of the sequence. If there is not any balanced number, print the greatest valencia of the numbers in the sequence.
The input is a non empty sequence of natural numbers.
Your program must print a line with the first balanced number of the sequence. If there is not any, print the greatest valencia of the sequence. Follow the format of the instances.
Your program must implement and use the function
int valencia(int n);
that, given a natural number |n|, returns its valencia.
Author: Unknown
Translator: Carlos Molina
Generation: 2026-01-25T10:24:26.831Z
© Jutge.org, 2006–2026.
https://jutge.org