-
Notifications
You must be signed in to change notification settings - Fork 858
/
MarkAndToys.java
53 lines (52 loc) · 996 Bytes
/
MarkAndToys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
*
* Problem Statement-
* [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.sorting;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
/**
* @author Kanahaiya Gupta
*
*/
public class MarkAndToys
{
public static void main(String[] args)throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int prices[] = new int[n];
int i,j;
for (i = 0; i < n; i++)
{
prices[i] = in.nextInt();
}
j=maximumToys(prices,k);
System.out.println(j);
}
public static int maximumToys(int[] prices,int k)
{
int n=prices.length;
Arrays.sort(prices);
//main programming
int toyCount,i,sum;
sum=toyCount= 0;
for (i = 0; i < n; i++)
{
sum += prices[i];
if (sum <= k)
{
toyCount++;
}
else
{
break;
}
}
return toyCount;
}
}