Loading Now

findLongestConsecutiveRange

You are given 4 positive intergers a, b, c, d. You use these 4 numbers exactly 1 time combined with 4 basic operations : +, -, *, / and parentheses () to create new numbers.

Note: Divide is only used if it is divisible ( I.e : 6/2 is valid while 6/4 is not).

For example, with 4 positive integers 7,2,5,1 you can form expressions like: (7-2)*5–1 or (7+1)*(5-2)and the result of these expression is 24 (of course, there are many others expression can be generated from these 4 numbers)

Write a program that determines the longest consecutive integer sequence that can be generated from the given 4 positive integers combined with operations.

Note: - is only used as an operator and not used to denote negative numbers. For example (-7)+5+2+1 is invalid.


Example: 

  • With input numbers = [7,2,6,1] the output should be findLongestConsecutiveRange(numbers) = "-18 to 26"
    You can see all generated numbers in this url https://pastebin.com/fsR5ySxD

Input/Output:

  • [execution time limit] 0.5s (Cpp), 3s (Java, C#), 4s (JavaScript, Python)

  • [input] array.integer numbers

     numbers.length = 4 
     1 ≤ numbers[i] ≤ 100 

  • [output] string

    Return the answer in form :  "a to b" 
    where a is the starting number of the longest sequence of number, b is the ending number of that sequence
    If there are more than one longest sequence, choose the one that has the largest starting number

Post Comment

Contact