Today is Sunday and Disha is out for the shopping. She has reached to the famous market of the city. She has a list of items which she wants to buy but she has a very low amount of cash today and no shop accepts any online payment method in the market. Disha has only D rupees with her and wants to buy N items (she wants to buy as many units of item as possible). She wants to shop in a way such that first she buy 1 quantity of all the items she wants to buy and then she will move to buy 2nd quantity of any item.
Answers
Answer:
Thank me later
Explanation:
import java.util.*;
public class MyClass
{ public static void main(String args[]) }
Scanner sc = new Scanner(System.in);
System.out.println("Number of items");
int num = sc.nextInt();
System.out.println("Number of Amount of Money");
int n = sc.nextInt();
String[] arr_str = new String[num];
System.out.println("Please enter value of items");
for (int i = 0; i < num; i++) {
String userInput = sc.next();
arr_str[i] = userInput;
}
System.out.println("The String array input from user is : ");
// System.out.println(Arrays.toString(arr_str));
int size = arr_str.length;
int [] arr = new int [size];
for(int i=0; i<size; i++) {
arr[i] = Integer.parseInt(arr_str[i]);
}
sc.close();
int count = 0;
int i;
int sum = 0;
Arrays.sort(arr);
while (sum < n){
for (i=0;i<num;i++){
sum = sum + arr[i];
if(sum >= n){
break;
}
count = count + 1;
}
}
System.out.println(count);
}
}
Answer:
public class Solution {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int d = scan.nextInt();
int[] prices = new int[n];
for(int i= 0 ; i < n; i++){
prices[i] = scan.nextInt();
}
int count=0;
int i=0;
while(d>0){
d=(d-prices[i]);
System.out.println(d);
if(d>0){
count++;
}
i=(i+1)%n;
}
System.out.println("Count is "+count);
}
}