Java program of Least offer

Question:-

Maya buys “N” no of products from a shop. The shop offers a different percentage of discount for each item. She wants to know the item that has the minimum discount offer, so that she can avoid buying that and save money.

[Input Format: The first input refers to the no of items; the second input is the item name, price and discount percentage separated by comma(,)]

Sample Input 1:

4

mobile,10000,20

shoe,5000,10

watch,6000,15

laptop,35000,5

Sample Output 1:

shoe

Explanation: the discount on mobile is 2000, the discount on shoe is 500, the discount on watch is 900 and the discount on laptop is 1750. So the discount on shoe is the minimum.

Note: More than one product can have the minimum discount , display those items separated by coma(,)

Code:-

Main.java

import java.util.*;
public class Main 
{
    public static void main (String[] args) {
        
        Scanner sc =new Scanner(System.in);
        
        int no_input=sc.nextInt();
        sc.nextLine();
        String input[]=new String[no_input];
        String input1[][]=new String[no_input][3];
        
        for(int i=0;i<no_input;i++)
        {
            input[i]=sc.nextLine();
            input1[i]=input[i].split(",");
        }
        
        int discount[]=new int[no_input];
        
        for(int i=0;i<no_input;i++)
        {
            discount[i]=(Integer.parseInt(input1[i][2])*Integer.parseInt(input1[i][1]))/100;
        }
        
        int min_dis=32767;//highest value that can be stored in Integer
        String output=new String();
        
        for(int i=0;i<input.length;i++)
        {
            if(min_dis>discount[i])
            {
                min_dis=discount[i];
            }
        }
        
        for(int i=0;i<input.length;i++)
        {
            if(discount[i]==min_dis)
            {
                output=output+input1[i][0]+" ";
            }
        }
        
        System.out.println(output);
    }
}

Recommended:-

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x