Pages

Find the maximum number of chocolates

Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern.

An unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue.

After her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue.
Note that she can paint tiles in any order she wants.

Given the required information, find the maximum number of chocolates Joty can get.

import java.util.Scanner;

public class Chocolates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter n : ");
int n = sc.nextInt();
System.out.println("Enter a :");
int a = sc.nextInt();
System.out.println("Enter b:");
int b = sc.nextInt();
System.out.println("Enter p :");
int p = sc.nextInt();
System.out.println("Enter a :");
int q = sc.nextInt();     
    System.out.print("Max Chocolates :" +maxChocolates(n, a, b, p, q));
}


public static int maxChocolates(int n, int a, int b, int p, int q){
       int count = 0;
       for(int i=1; i<=n; i++){
           int maxChocs = 0;
         
           if(i % a == 0){
               maxChocs = p;
           }
           if(i % b == 0 && q > maxChocs){
               maxChocs = q;
           }
           count = count + maxChocs;
       }
        return count;
   }
}

Output 1:


Enter n : 5
Enter a : 2
Enter b : 3
Enter p : 12
Enter q : 15
Max Chocolates : 39



Output 2:


Enter n : 20
Enter a : 2
Enter b : 3
Enter p : 3
Enter q : 5
Max Chocolates : 51




No comments:

Post a Comment