Skip to main content

JP Morgan

JP Morgan OA 2023 - Identify Maximum Integer in a String


You are given a string str of length n. Your task is to find and print the maximum integer present in the string str. An integer is derived from consecutive digit characters in the string.

Note:

  • Print -1 if no integer is found.
  • Print -2 if the string is empty.

Input Format:

The input consists of two lines:

  1. The first line contains an integer n representing the length of the string.
  2. The second line contains the string str.

Output Format:

Print the maximum integer present in the string str. Print -1 if no integer is found. 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