SRM 480 DIV2 Easy(250) - Cryptography

この問題は読んだ瞬間に一番小さい数を+1した場合は最大になることが直感的に分かった。
したがって、実装もそれに沿った形で行った。
これぐらいの難易度であれば解けそうな雰囲気。簡単そうな問題を選んでくれているのだろうけれど・・・。

Problem Statement

TopCoder Security Agency (TSA, established today) has just invented a new encryption system! This encryption system takes as its input a list of numbers to encrypt.

You work at TSA and your task is to implement a very important part of the encryption process. You are allowed to pick one number in the input list and increment its value by 1. This should be done in such way that the product of all numbers in the list after this change becomes as large as possible.

Given the list of numbers as int numbers, return the maximum product you can obtain. It is guaranteed that the return value will not exceed 262.

Definition

  • Class: Cryptography
  • Method: encrypt
  • Parameters: int
  • Returns: long
  • Method signature: long encrypt(int[] numbers) (be sure your method is public)

Limits

  • Time limit (s): 2.000
  • Memory limit (MB): 64

Constraints

  • numbers will contain between 2 and 50 elements, inclusive.
  • Each element of numbers will be between 1 and 1000, inclusive.
  • The return value will not exceed 2^62.

Solution

import java.util.Arrays;

public class Cryptography {

    public long encrypt(int[] numbers) {
        Arrays.sort(numbers);
        numbers[0] += 1;
        long product = 1;
        for (int i = 0; i < numbers.length; i++) {
            product *= numbers[i];
        }
        return product;
    }

}