Editorial for Pricing Dilemma
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Python
N = int(input())
nums = list(map(int, input().split()))
nums.sort()
print(nums[-1] * nums[-2] - nums[0] * nums[1])
Java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int[] nums = new int[N];
for (int i = 0; i < N; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums);
System.out.println(nums[N - 1] * nums[N - 2] - nums[0] * nums[1]);
}
}
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N; cin >> N;
vector<int> nums(N);
for (int i = 0; i < N; i++) {
cin >> nums[i];
}
sort(nums.begin(), nums.end());
cout << nums[N - 1] * nums[N - 2] - nums[0] * nums[1] << '\n';
}
Comments