Question

In: Computer Science

Java language please Problem There are N houses for sale. The i-th house costs Ai dollars...

Java language please

Problem

There are N houses for sale. The i-th house costs Ai dollars to buy. You have a budget of B dollars to spend.

What is the maximum number of houses you can buy?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing the two integers N and B. The second line contains N integers. The i-th integer is Ai, the cost of the i-th house.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of houses you can buy.

Limits

Time limit: 15 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ B ≤ 105.
1 ≤ Ai ≤ 1000, for all i.

Test set 1

1 ≤ N ≤ 100.

Test set 2

1 ≤ N ≤ 105.

Sample


Input

Output
3
4 100
20 90 40 90
4 50
30 30 10 10
3 300
999 999 999

  
Case #1: 2
Case #2: 3
Case #3: 0

  

In Sample Case #1, you have a budget of 100 dollars. You can buy the 1st and 3rd houses for 20 + 40 = 60 dollars.
In Sample Case #2, you have a budget of 50 dollars. You can buy the 1st, 3rd and 4th houses for 30 + 10 + 10 = 50 dollars.
In Sample Case #3, you have a budget of 300 dollars. You cannot buy any houses (so the answer is 0).

this is what I have so far

import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testCases = in.nextInt();
for (int testCase = 1; testCase <= testCases; testCase++) {
int numberOfHouses = in.nextInt();
int budget = in.nextInt();
int[] costOfHouses = new int[numberOfHouses];
for (int index = 0; index < numberOfHouses; index++) {
costOfHouses[index] = in.nextInt();
}

int ans = buyMaximumHouses(costOfHouses, budget);
System.out.println("Case #" + testCase + ": " + ans);
}
}
static int buyMaximumHouses(int[] costOfHouses, int budget) {
// TODO: implement this method to return maximum houses that can
// be bought with the given budget.
}
}

Solutions

Expert Solution

import java.util.Collections;
import java.util.ArrayList;
import java.util.Scanner;
class Solution {
    private static long N;
    private static long A;
    private static long B;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
           N = sc.nextInt();
        for (long i = 0; i < N; i++) {
            A = sc.nextInt();
            B = sc.nextInt();
            ArrayList<Integer> houses = new ArrayList<Integer>();
            for (long j = 0; j < A; j++) {
            houses.add(sc.nextInt());
            }
            long no_of_houses = findTheHouses(houses,B);
            System.out.println("Case #"+ (i+1) +": "+no_of_houses);

        }

        sc.close();
    }

    private static long findTheHouses(ArrayList<Integer> houses, long max_cost) {
        Collections.sort(houses);
        long count_budget =0;
        int houses_count=0;
        do{
                count_budget = count_budget +houses.get(houses_count);
            if(count_budget<=max_cost)
                houses_count++;
                else
                break;
        }while(houses.size()>houses_count);
        
    return houses_count;    
    }
}

I hope it helps.


Related Solutions

The n th Triangle Problem Write a code for finding the n th triangle number of...
The n th Triangle Problem Write a code for finding the n th triangle number of triangle sequences: 1, 3, 6, 10, ..., n. That is, your code should accept an integer number, which indicates the triangle levels, and returns how many dots we need to form a triangle with respect to the given level. For example, consider the Fig 1. For n = 3 (can be also written as T3), your code should returns 6. Provide a single program...
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n...
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n natural numbers: H(n) = 1+ 1/2 + 1/3 +1/4 +... +1/n Write a recursive method and an accompanying main method to compute the n^th Harmonic number. I have tried but get a blank and would really apreciate a fully explained code.
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java...
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java or Python? When would you use Java and when would you use Python?
please, solve this problem.: implementing with java language create a bank account management system according to...
please, solve this problem.: implementing with java language create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and methods: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a method that returns the balance of a specific account. abstract deposit(amount): abstract method to be implemented in both Checking and SavingAccount classes. abstract withdraw(amount): abstract method to be implemented in both Checking and SavingAccount classes....
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median mA of An is a value in An such that half the elements in An are less than m (and so, the other half are greater than or equal m). In fact, the median element is said to have a middle rank. (a) Develop an algorithm that uses Sorting to return mA given An. (6%) (b) Now assume that one is given another list...
STATEMENT OF PROBLEM A developer is building houses in a new neighborhood. She offers 3 house...
STATEMENT OF PROBLEM A developer is building houses in a new neighborhood. She offers 3 house plans: “Montgomery”, “Kettering”, and “Saxon”   Although all the windows are the same, each house plan requires a different number of windows: the Montgomery takes 20 windows, the Kettering 15, and the Saxon 12.   She wants you to design a programming solution that will allow her to enter the number of houses she plans to build of each model and have the program display the...
I t is N queens problem please complete it //*************************************************************** // D.S. Malik // // This...
I t is N queens problem please complete it //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8); //constructor //Postcondition: noOfSolutions = 0; noOfQueens = queens; // queensInRow is a pointer to the array // that store the n-tuple. // If no value is specified for the parameter queens, // the default value, which is 8, is assigned to it. bool canPlaceQueen(int k,...
java. please don't use complicated language I can follow up. Write a for loop that prints...
java. please don't use complicated language I can follow up. Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis. 1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...] Write this code once with for loop, once with while loop,...
The agency problem costs firms and investors billions of dollars per year. Please answer the following:...
The agency problem costs firms and investors billions of dollars per year. Please answer the following: Describe the agency problem and provide an example. What steps can a corporation take to align management with shareholder’s interest? Can the debt structure of a corporation play a role in preventing an agency problem? If yes, please explain. How? Could there be drawbacks in using debt to curtail the agency problem? If yes, please explain.
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT