TopcoderArchive
Archive
Algorithm problem

CheckDigits

Problem statement, definition, constraints, and public examples.

Problem Statement

A UPC code is a 12-digit number comprised of three parts. The first six digits represent the manufacturer identification number, the next five digits represent the item number, and the last digit is known as the check digit. When a UPC code is read by a scanner, the check digit is used to determine whether or not it was scanned correctly. Here's how the check digit is calculated:

1. Sum all of the digits that are in odd positions (the first digit is considered to be in position 1).
2. Multiply this number by 3.
3. Sum all of the digits that are in even positions except for the check digit.
4. Add this number to the result of step 2.
5. Determine the smallest non-negative number that, when added to the result of step 4, would result in a multiple of 10. This number is the check digit.

For example, consider a UPC code whose first 11 digits are 73145309592. The sum of all the digits in odd positions is 7 + 1 + 5 + 0 + 5 + 2 = 20. The sum of all the digits in even positions is 3 + 4 + 3 + 9 + 9 = 28. Adding this number to 3 times the first result is 28 + 3 * 20 = 88. The smallest non-negative number that can be added to 88 to turn it into a multiple of 10 is 2. Therefore, the check digit is 2 and the complete 12-digit UPC code would be 731453095922.

Given a String[], codes, each element of which is the first 11 digits of a UPC code, determine the correct check digit for each code. Return the check digits in a int[] containing exactly the same number of elements as codes. The ith element of the return int[] should be the check digit for the ith element of codes.

Definition

Class:
CheckDigits
Method:
digits
Parameters:
String[]
Returns:
int[]
Method signature:
int[] digits(String[] codes)
(be sure your method is public)

Notes

  • codes will contain between 1 and 50 elements, inclusive.
  • Each element of codes will contain exactly 11 characters.
  • Each element of codes will contain only digits ('0'-'9').

Constraints

    Examples

    1. {"73145309592"}
      Returns: { 2 }
      This is the UPC code from the problem statement.
    2. {"73145305622", "07863536972", "73145308832"}
      Returns: { 0,  4,  0 }
    3. {"73746363102", "07777464382", "07599254702", "73145323762"}
      Returns: { 0,  5,  4,  9 }
    Back to all problems