-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from RoyalEagle73/master
code in Greedy Algorithms section
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters