Skip to content

Commit

Permalink
Merge pull request #92 from RoyalEagle73/master
Browse files Browse the repository at this point in the history
code in Greedy Algorithms section
  • Loading branch information
maze-runnar authored Dec 31, 2019
2 parents 2e8597a + 57c9853 commit f3cb8a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions Greedy Algorithm/MaxMin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Complete the maxMin function below.
static int maxMin(int k, int[] arr) {
Arrays.sort(arr);
int min = 1000000000;
int i = 0;
for(i=0; (i+k-1)<arr.length; i++){
if(arr[i+k-1] - arr[i] < min)
min = arr[i+k-1] - arr[i];
}
System.out.println(i);
return min;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int k = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int[] arr = new int[n];

for (int i = 0; i < n; i++) {
int arrItem = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
arr[i] = arrItem;
}

int result = maxMin(k, arr);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ in C++, Java, Python and Javascript.
| |[Luck Balance](https://www.hackerrank.com/challenges/luck-balance/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/luck_balance.cpp)||[Done](/Greedy%20Algorithm/LuckBalance.py)||
||[Minimum Absolute Difference in an Array](https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/minimum_absolute_difference_in_an_array.cpp)||[Done](/Greedy%20Algorithm/MinimumDifference.py)||
||[Greedy Florist](https://www.hackerrank.com/challenges/greedy-florist/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/greedy_florist.cpp)||[Done](/Greedy%20Algorithm/GreedyFlorist.py)||
||[Max Min](https://www.hackerrank.com/challenges/angry-children/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/max_min.cpp)||[Done](/Greedy%20Algorithm/MaxMin.py)||
||[Max Min](https://www.hackerrank.com/challenges/angry-children/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/max_min.cpp)|[Done](/Greedy%20Algorithm/MaxMin.java)|[Done](/Greedy%20Algorithm/MaxMin.py)||
||[Reverse Shuffle Merge](https://www.hackerrank.com/challenges/reverse-shuffle-merge/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=greedy-algorithms)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Greedy%20Algorithm/reverse_shuffle_merge.cpp)||[Done](/Greedy%20Algorithm/ReverseShuffleMerge.py)||
|Search| || |||
||[Minimum Time Required](https://www.hackerrank.com/challenges/minimum-time-required/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Seach/minimum_time_required.cpp)||[Done](/Search/MinimumTime.py)||
Expand Down

0 comments on commit f3cb8a5

Please sign in to comment.