We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Search
  4. Minimum Loss

Minimum Loss

Problem
Submissions
Leaderboard
Discussions
Editorial

Lauren has a chart of distinct projected prices for a house over the next several years. She must buy the house in one year and sell it in another, and she must do so at a loss. She wants to minimize her financial loss.

Example

Her minimum loss is incurred by purchasing in year at and reselling in year at . Return .

Function Description

Complete the minimumLoss function in the editor below.

minimumLoss has the following parameter(s):

  • int price[n]: home prices at each year

Returns

  • int: the minimum loss possible

Input Format

The first line contains an integer , the number of years of house data.
The second line contains space-separated long integers that describe each .

Constraints

  • All the prices are distinct.
  • A valid answer exists.

Subtasks

  • for of the maximum score.

Sample Input 0

3
5 10 3

Sample Output 0

2

Explanation 0

Lauren buys the house in year at and sells it in year at for a minimal loss of .

Sample Input 1

5
20 7 8 2 5

Sample Output 1

2

Explanation 1

Lauren buys the house in year at and sells it in year at for a minimal loss of .

Author

nabila_ahmed

Difficulty

Medium

Max Score

35

Submitted By

29711

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature