Least offer Program in java

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:

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);
    }
}
Next:
  1. Ascending and descending order
  2. Mail Domain
  3. Count repeating words

Leave a Comment

Scroll to Top