In this HackerRank Minimum Loss problem solution, 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.
Problem solution in Python.
n = int(input()) arr = list(map(int,input().split())) di = {arr[i]:i for i in range(n)} arr = sorted(arr) m=10000000 for i in range(1,n): if di[arr[i]]<di[arr[i-1]]: m=min(m,arr[i]-arr[i-1]) print(m)
Problem solution in Java.
import java.io.IOException; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { new Solution().run(); } private void run() throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] prices = new long[n]; for (int i = 0; i < n; i++) { prices[i] = in.nextLong(); } long minimalLoss = fasterGetMinimalLoss(n, prices); System.out.println(minimalLoss); } private long fasterGetMinimalLoss(int n, long[] prices) { TreeSet<Long> pos = new TreeSet<>(); long minimalLoss = Long.MAX_VALUE; for (int i = 0; i < n; i++) { long endPrice = prices[i]; Long biggerPrevious = pos.ceiling(endPrice); if (biggerPrevious != null) { long loss = biggerPrevious - endPrice; if (loss > 0 && loss < minimalLoss) { minimalLoss = loss; } } pos.add(endPrice); } return minimalLoss; } private long dummyGetMinimalLoss(int n, long[] prices) { long minimalLoss = Long.MAX_VALUE; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long loss = prices[i] - prices[j]; if (loss > 0 && loss < minimalLoss) { minimalLoss = loss; } } } return minimalLoss; } }
Problem solution in C++.
#include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<pair<long long, int> > a(n); for(int i = 0; i < n; i++){ cin >> a[i].first; a[i].second = i; } sort(a.begin(), a.end()); long long ans = (long long)1e18; for(int i = 1; i < n; i++) { if(a[i].second < a[i - 1].second) { if(ans > a[i].first - a[i - 1].first) ans = a[i].first - a[i - 1].first; } } cout << ans; return 0; }
Problem solution in C.
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> typedef struct { long int val; int pos; } node_t; int cmpfunc(const void * a, const void * b) { long int diff = ((node_t*)b)->val - ((node_t*)a)->val; if(diff < 0) return -1; else if(diff > 0) return 1; return 0; } int minimumLoss(int price_size, long int* price) { node_t* pricenodes = malloc(sizeof(node_t)*price_size); for(int i=0; i<price_size; ++i) { pricenodes[i].val = price[i]; pricenodes[i].pos = i; } // Reverse sort qsort(pricenodes, price_size, sizeof(*pricenodes), cmpfunc); long int curmin = -1; for(int i=1; i<price_size; ++i) if(pricenodes[i].pos > pricenodes[i-1].pos) if(-1 == curmin || curmin > pricenodes[i-1].val - pricenodes[i].val) curmin = pricenodes[i-1].val - pricenodes[i].val; return curmin; } int main() { int n; scanf("%i", &n); long int *price = malloc(sizeof(long int) * n); for (int price_i = 0; price_i < n; price_i++) { scanf("%li",&price[price_i]); } int result = minimumLoss(n, price); printf("%dn", result); return 0; }