Computer Science, asked by sanjanaguruprasad, 1 month ago

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.

Can you help her by calculating how many items she can buy today if she shops optimally?

Answers

Answered by dedsec995
4

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

   }

}

Answered by mindfulmaisel
0

calculating how many items she can buy today if she shops optimally

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

  }

}

Similar questions