Skip to content

Commit

Permalink
Solutions to Sorting section
Browse files Browse the repository at this point in the history
  • Loading branch information
sknsht committed Oct 11, 2018
1 parent cf7f2e8 commit 3d4159e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Interview Preparation Kit/Sorting/Mark and Toys/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.*;
import java.util.*;

public class Solution {

static int maximumToys(int[] prices, int k) {
Arrays.sort(prices);

int numToys = 0;
for (int price : prices) {
if (price <= k) {
numToys++;
k = k - price;
} else {
break;
}
}
return numToys;
}

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")));

String[] nk = scanner.nextLine().split(" ");

int n = Integer.parseInt(nk[0]);
int k = Integer.parseInt(nk[1]);

int[] prices = new int[n];
String[] pricesItems = scanner.nextLine().split(" ");

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

for (int i = 0; i < n; i++) {
int pricesItem = Integer.parseInt(pricesItems[i]);
prices[i] = pricesItem;
}

int result = maximumToys(prices, k);

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

bufferedWriter.close();
scanner.close();
}
}

0 comments on commit 3d4159e

Please sign in to comment.