6/recent/ticker-posts

HackerRank Problem Solving (Basic) Solution 1 : JAVA

Unexpected Demand Hackerrank Solution Java


A widget manufacturer is facing unexpectedly high demand for its new product,. They would like to satisfy as many customers as possible. Given a number of widgets available and a list of customer orders, what is the maximum number of orders the manufacturer can fulfill in full?

 Function Description

Complete the function filledOrders in the editor below. The function must return a single integer denoting the maximum possible number of fulfilled orders.

 

filledOrders has the following parameter(s):

    order :  an array of integers listing the orders

    k : an integer denoting widgets available for shipment

 

Constraints

1 ≤ n ≤  2 x 105

1 ≤  order[i] ≤  109

1 ≤ k ≤ 109

Input Format For Custom Testing

The first line contains an integer, n, denoting the number of orders.
Each line i of the n subsequent lines contains an integer order[i].

Next line contains a single integer k denoting the widgets available.

 

Sample Case 0

Sample Input For Custom Testing

2

10

30

40

Sample Output

2

Explanation

order = [10,30] with 40 widgets available. Both orders can be fulfilled.

 

Sample Case 1

Sample Input For Custom Testing

3

5

4

6

3

Sample Output

0

Explanation

order = [5,4,6] with 3 widgets available

None of the orders can be fulfilled.


Java Code: 

class Result {

        public static int filledOrders(List<Integer> order, int k) {

            int len=order.size();

            int count=0;

            Collections.sort(order);

            for(int ord:order)

            {

                    if(ord<=k)

                    {

                          count++;

                          k=k-ord;

                    }

             }

                return count;

    }

}

Suggested Books:

   

Post a Comment

0 Comments