# Minimum Loss

### Problem Statement :

```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.

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 n, the number of years of house data.
The second line contains n space-separated long integers that describe each price[i].```

### Solution :

```                            ```Solution in C :

In   C++  :

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

int n;
ll a;

int main() {
scanf("%d", &n);
FOR(i,n) scanf("%lld", &a[i]);
set<ll> s;
long long res = 1000000000000000000LL;
FOR(i,n) {
auto it = s.lower_bound(a[i]);
if (it != s.end()) {
res = min(res, *it-a[i]);
}
s.insert(a[i]);
}
printf("%lld\n", res);
return 0;
}

In  Java  :

import java.util.*;

public class Main{

public static void main(String[] args){
Scanner input=new Scanner(System.in);

int n=input.nextInt();

ArrayList<Long> p=new ArrayList();

for(int i=1;i<=n;i++)

long minLoss=Integer.MAX_VALUE;

ArrayList<Long> p2=(ArrayList<Long>)p.clone();

Collections.sort(p2);

for(int i=n-1;i>0;i--){
long a=p2.get(i);
long b=p2.get(i-1);
if(a-b<minLoss && p.indexOf(a)<p.indexOf(b))
minLoss=a-b;
}

System.out.println(minLoss);

}
}

In   C  :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef long long ll;

typedef struct treap {
ll v;
int p;
struct treap *l, *r;
}* Treap;

Treap newNode(ll v) {
Treap t = (Treap) malloc (sizeof (struct treap));
t->v = v;
t->p = (rand() << 15) | rand();
t->l = t->r = NULL;
return t;
}

Treap merge(Treap l, Treap r) {
if (!l)
return r;
if (!r)
return l;
if (l->p > r->p) {
l->r = merge(l->r, r);
return l;
}
r->l = merge(l, r->l);
return r;
}

void split(Treap root, Treap *l, Treap *r, ll v) {
if (!root) {
*l = *r = NULL;
return;
}
if (root->v <= v) {
split(root->r, &(root->r), r, v);
*l = root;
}
else {
split(root->l, l, &(root->l), v);
*r = root;
}
}

Treap first(Treap root) {
while (root->l)
root = root->l;
return root;
}

Treap last(Treap root) {
while (root->r)
root = root->r;
return root;
}

int main() {
srand(time(NULL));
int n;
ll mx = 0, mn = 1000000000000000000LL, p;
Treap root = NULL, t, l, r;
scanf("%d", &n);
while (n--) {
scanf("%lld", &p);
if (p < mx) {
split(root, &l, &r, p);
t = first(r);
if ((t->v - p) < mn)
mn = t->v - p;
if (!l || last(l)->v != p) {
t = newNode(p);
root = merge(l, t);
root = merge(root, r);
}
}
else if (p > mx) {
mx = p;
t = newNode(p);
root = merge(root, t);
}
}
printf("%lld", mn);
return 0;
}

In  Python3 :

n=int(input())

l = [int(item) for item in input().split()]
l2 = l.copy()
d={}
for i in range(len(l)):
d[l[i]] = i
l = sorted(l)
m = float("inf")
for i in range(len(l)-1):
if d[l[i+1]] < d[l[i]] : m = min(m,l[i + 1] - l[i])
print(m)```
```

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function