DSAMaster Logo
DSAMaster
BackeasyArrays

Array Amplitude

Problem Description

Given an integer array nums, calculate its amplitude. The amplitude of an array is defined as the mathematical difference between its maximum element and its minimum element. If the array contains only one element, its amplitude is 0.

Write an efficient algorithm to find this value in a single pass.

Example 1
Input
nums = [3, 8, 2, 5, 12]
Output
10

Explanation: The maximum element in the array is 12 and the minimum element is 2. The amplitude is 12 - 2 = 10.

Example 2
Input
nums = [7, 7, 7, 7]
Output
0

Explanation: The maximum element is 7 and the minimum element is 7. The amplitude is 7 - 7 = 0.

Example 3
Input
nums = [-5, 10, -3, 0, 15, -12]
Output
27

Explanation: The maximum element is 15 and the minimum element is -12. The amplitude is 15 - (-12) = 27.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
00:00
Loading...

Sign in to Solve

Access the full code editor, ThinkBuddy AI hints, and track your progress.

Sign in Free
Test Cases & Output
Enter test input here...
Click "Run Code" to execute