Skip to main content

JP Morgan

JP Morgan OA-3 2023

Problem Description

You are provided with a string str of length n. Your task is to find and print the largest integer found within the string. An integer is formed by consecutive digit characters in the string.

Note:

  • If no integer is present in the string, print -1.
  • If the string is empty, print -2.

Input Format:

  • The first line contains an integer n, representing the length of the string.
  • The second line contains the string str.

Output Format:

  • Print the largest integer found in the string.
  • If no integer is present, print -1.
  • If the string is empty, print -2.

Constraints:

  • 1 <= n <= 100,000

Example:

Input:

39
33 milestone is either 12 or 75 km away

Output:

75

Explanation:

The string has 3 integers: 33, 12, and 75. Since 75 is the maximum, the output is 75.

Sample Input:

29
I have 8 500g packets of rice

Sample Output:

8