Skip to main content

TCS

TCS OA-18 2024

Problem Description

We are given a list of numbers. We need to return the maximum difference between the smallest and largest number.

Note: Smallest number should be before largest number.

Input Format

  • The first line contains an integer N, representing the array's length.
  • The second line contains N space-separated integers.

Output Format

An integer representing the maximum difference between the smallest number that appears before the largest number in the list.

Constraints

  • 2 ≤ arr.size() ≤ 100
  • -10³ ≤ arr[i] ≤ 10³

Example

Sample Input:

7
[2, 3, 10, 6, 4, 8, 1]

Sample Output:

8

Explanation:

The maximum difference is obtained by taking nums[2] (10) and nums[0] (2), giving 10 - 2 = 8.