Skip to content

Latest commit

 

History

History
43 lines (39 loc) · 1.7 KB

Is_Lucky.md

File metadata and controls

43 lines (39 loc) · 1.7 KB

🔷 Is Lucky 🔷

Challenge description

Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.

Given a ticket number n, determine if it's lucky or not.

Example

  • For n = 1230, the output should be
    solution(n) = true;
  • For n = 239017, the output should be
    solution(n) = false.

Input/Output

  • [execution time limit] 3 seconds (java)

  • [memory limit] 1 GB

  • [input] integer n

    A ticket number represented as a positive integer with an even number of digits.

    Guaranteed constraints:
    10 ≤ n < 106.

  • [output] boolean

    true if n is a lucky ticket number, false otherwise.

[Java] Syntax Tips

## Solutions:
  • JS solution

    function solution(n) {
    let sum1 = 0
    let sum2 = 0
    const nString = n.toString()
    for(let i = 0, j = nString.length - 1; i < nString.length / 2; i++, j--){
    sum1 += nString.charAt(i)
    sum2 += nString.charAt(j)
    }
    return sum1 === sum2;
    }
    JS Execution

  • Java solution

    static boolean solution(int n){
    var nString = String.valueOf(n);
    int sum1 = 0, sum2 = 0;
    for (int i = 0, j = nString.length() - 1; i < nString.length() / 2; i++, j--) {
    sum1 += Integer.parseInt(nString.charAt(i) + "");
    sum2 += Character.getNumericValue(nString.charAt(j));
    }
    return sum1 == sum2;
    }
    Java Execution