Skip to content

Instantly share code, notes, and snippets.

@bhavjot
Created March 10, 2017 05:37
Show Gist options
  • Save bhavjot/9250907690946783992108aac36d880b to your computer and use it in GitHub Desktop.
Save bhavjot/9250907690946783992108aac36d880b to your computer and use it in GitHub Desktop.
Minimum Loss - Binary Search - some test cases still fail - not a solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int n = Convert.ToInt32(Console.ReadLine());
string[] arr_temp = Console.ReadLine().Split(' ');
long[] arr = Array.ConvertAll(arr_temp,Int64.Parse);
long min = Int64.MaxValue;
List<Int64> sortedPriceList = new List<Int64>();
for (int i=n-1;i>=0;i--)
{
Int64 currentPrice = arr[i];
int index = sortedPriceList.BinarySearch(currentPrice);
if(index<0){
int position = ~index;
if(position >0)
{
min = Math.Min(min, currentPrice-sortedPriceList[position-1]);
}
sortedPriceList.Insert(position,currentPrice);
}
}
Console.Write(min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment