Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

min absolute diff in an array problem: C# solution #168

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Problem: https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. Sort the input
2. Iterate the sorted input and track the absolute difference between adjacent elements
3. Print the minimum difference thus found.

Time Complexity: O(n log(n)) //actually it is O(n log(n)) + O(n)
Space Complexity: O(n) //need to store all the elements in memory for processing as sorting using quick sort is also involved.

*/
using System;
using System.Collections.Generic;
using System.Linq;

class Solution
{
static void Main(string[] args)
{
//No need to capture the size of array. We can use array's length property instead.
Console.ReadLine();
var arr_temp = Console.ReadLine().Split(' ');
var inputNumbers = Array.ConvertAll(arr_temp, int.Parse);
//contributes O(n log(n)) complexity
inputNumbers = QuickSort2(inputNumbers);
var minimumDiff = Math.Abs(inputNumbers[0] - inputNumbers[1]);
//contributes O(n) complexity
for (var i = 1; i < inputNumbers.Length - 1; i++)
minimumDiff = Math.Min(Math.Abs(inputNumbers[i] - inputNumbers[i + 1]), minimumDiff);

Console.WriteLine(minimumDiff);
}

static int[] QuickSort2(int[] arr)
{
var pivot = arr[0];
var smallerItems = new List<int>();
var equalItems = new List<int>();
var biggerItems = new List<int>();
var outputArr = new int[arr.Length];

equalItems.Add(arr[0]);

for (var i = 1; i < arr.Length; i++)
{
if (arr[i] < pivot)
smallerItems.Add(arr[i]);
else if (arr[i] > pivot)
{
biggerItems.Add(arr[i]);
}
else
equalItems.Add(arr[i]);
}

if (smallerItems.Count > 1)
smallerItems = QuickSort2(smallerItems.ToArray()).ToList();

if (biggerItems.Count > 1)
biggerItems = QuickSort2(biggerItems.ToArray()).ToList();

var j = 0;

foreach (var item in smallerItems)
outputArr[j++] = item;

foreach (var item in equalItems)
outputArr[j++] = item;

foreach (var item in biggerItems)
outputArr[j++] = item;

return outputArr;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
### Greedy
| # | Title | Solution | Time | Space | Difficulty | Points | Note
-----|---------------- |:---------------:| --------------- | --------------- |:-------------:|:--------------:| -----
| | [Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array)| <ul><li>[Java](./Algorithms/Greedy/Minimum%20Absolute%20Difference%20in%20an%20Array/Solution.java)</li> <li>[JS](./Algorithms/Greedy/Minimum%20Absolute%20Difference%20in%20an%20Array/Solution.js)</li><ul> | _O(n log(n))_ | _O(1)_ | Easy | 15| ||
| | [Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array)| <ul><li>[Java](./Algorithms/Greedy/Minimum%20Absolute%20Difference%20in%20an%20Array/Solution.java)</li> <li>[JS](./Algorithms/Greedy/Minimum%20Absolute%20Difference%20in%20an%20Array/Solution.js)</li><li>[C#](./Algorithms/Greedy/Minimum%20Absolute%20Difference%20in%20an%20Array/Solution.cs)</li><ul> | _O(n log(n))_ | _O(n)_ | Easy | 15| ||
| |[Chief Hopper](https://www.hackerrank.com/challenges/chief-hopper)| <ul><li>[Java](./Algorithms/Greedy/Chief%20Hopper/Solution.java)</li><ul> | _O(n)_ | _O(n)_| Hard | 65 | ||
| |[Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys)| <ul><li>[Java](./Algorithms/Greedy/Mark%20and%20Toys/Solution.java)</li> <li>[JS](./Algorithms/Greedy/Mark%20and%20Toys/Solution.js)</li><ul> | _O(n log(n))_ | _O(n)_| Easy | 35 | ||
| | [Marc's Cakewalk](https://www.hackerrank.com/challenges/marcs-cakewalk)| <ul><li>[Java](./Algorithms/Greedy/Marc's%20Cakewalk/Solution.java)</li><ul> | _O(n + k)_ | _O(k)_ | Easy | 15| ||
Expand Down