Oracle | Onsite | Determine the Largest Prime in a Given Range

Question (I edited to make it more Leetcode like)

Given an 2 dimensional array items with a length of Q in each element there are 2 integers li and ri
Return an array with a length of Q and each element containing largest prime in the range li​ and ri inclusive. If there is no prime numbers in a given range, the element must be -1 instead.

Example 1:

  • Input: items = [[5,5], [6,8], [20,25], [13,17], [50,55]]
  • Output: [5, 7, 23, 17, 53]

Explanation:

  • Between 5 and 5, the only number is 5, which is a prime.
  • Between 6 and 8, the largest prime number is 7.
  • Between 20 and 25, the largest prime number is 23.
  • Between 13 and 17, the largest prime number is 17.
  • Between 50 and 55, the largest prime number is 53.

Example 2:

  • Input: items = [[4,10], [10,15], [17,19], [1,1], [21,23]]
  • Output: [7, 13, 19, -1, 23]

Explanation:

  • Between 4 and 10, the largest prime number is 7.
  • Between 10 and 15, the largest prime number is 13.
  • Between 17 and 19, the largest prime number is 19.
  • Between 1 and 1, there is no prime number, so it returns -1.
  • Between 21 and 23, the largest prime number is 23.

Example 3:

  • Input: items = [[3,7], [11,13], [15,18], [29,31], [62,66], [25,28]]
  • Output: [7, 13, 17, 31, -1, -1]

Explanation:

  • Between 3 and 7, the largest prime number is 7.
  • Between 11 and 13, the largest prime number is 13.
  • Between 15 and 18, the largest prime number is 17.
  • Between 29 and 31, the largest prime number is 31.
  • Between 62 and 66, there is no prime number, so it returns -1.
  • Between 25 and 28, there is no prime number, so it returns -1.
Comments (0)