SAVEICECREAM Savings and Ice Cream

Author : Kavya Gupta

Tester : Sidharth Sethi

Editorialist : Kavya Gupta

Savings and Ice-Cream

Alice is a good girl who loves to save money whenever she gets some. Today, Her Father is very happy and wants to give her a treat of any 1 ice cream she wants. He sends her to a nearby shop for choosing any ice cream she wants from there and let him know the price of it before purchasing it, so he can hand over the money to her. The shop is having N ice creams with different prices.

Alice likes saving for her own, so she will go and choose an ice cream with a high cost for telling to her father and will purchase the ice cream with a lesser price and will save the remaining money for her savings. Tell the maximum amount she can save if she can choose from those N icecreams only.

Input Format:

  • In the first line, an integer T is given, the number of test cases.
  • In each test case, an integer N is given, where N is the number of ice creams in the shop.
  • In the next line, N integers are given representing the cost of i-th ice cream.

Output Format:

An integer containing the amount of money Alice can save.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100
  • 1 ≤ Price of ice cream ≤ 100

Sample Input

2
5
1 7 3 6 2
1
6

Sample Output

6
0

Explanation:

In the first case, she can choose ice cream with a price of 7 for telling to her father and choose ice cream with a price of 1 coin and can save 6 coins. In the second test case, she can only choose the ice cream for 6 coins, so she can’t save any money.

Solution

The approach here is to find the difference between the max and min element in the array provided.

#include <iostream>
using namespace std;
void RadheRadhe()
{
    int n;
    cin >> n;
    int mn = 100, mx = 0;
    while (n--)
    {
        int x;
        cin >> x;
        mn = min(mn, x);
        mx = max(mx, x);
    }
    cout << mx - mn << "\n";
}
int main()
{
    int t;
    cin >> t;
    while (t--)
        RadheRadhe();
}