Day 19 - Java

Problem Statement:

Lord Shiva is pleased by sincere devotion and meditation. In order to please Lord Shiva, devotees must meditate for a specific duration and collect a certain number of Rudraksha beads and ebony beads. You are given the duration of meditation sessions in minutes. Each 60 minutes of meditation earns one Rudraksha bead, and each 15 minutes of meditation earns one ebony bead. Determine whether Lord Shiva would be pleased based on the following criteria:

The total duration of meditation sessions should be at least 1440 minutes (24 hours).

At least 108 rudraksha beads should be collected.

At least 72 ebony beads should be collected.

Write a program to calculate the total meditation time, the counts of rudraksha and ebony beads collected, and determine if Lord Shiva is pleased.

Input Format:

The first line contains an integer, n, denoting the number of meditation sessions.

The next line contains n space-separated integers, arr[0] arr[1] ... arr[n-1], denoting the duration of each meditation session in minutes.

Output Format:

Print the total meditation time in hours.

Print the count of rudraksha beads collected.

Print the count of ebony beads collected.

Print "Lord Shiva is pleased!" if Lord Shiva is pleased based on the given criteria; otherwise, print "Meditate more and collect more beads!".

Sample Input:

3

90 120 150

Sample Output:

6 hours

5 rudraksha

24 ebony

Meditate more and collect more beads!

Test Cases:

Input:

4

60 60 60 60

Output:

4 hours

4 rudraksha

16 ebony

Meditate more and collect more beads!

Input:

2

480 480

Output:

16 hours

16 rudraksha

64 ebony

Lord Shiva is pleased!

Test Case 3:

Input:

5

45 30 90 150 120

Output:

7 hours

5 rudraksha

29 ebony

Meditate more and collect more beads!


SOLUTION:

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

class Program{

    static int totalMeditationTime(int[] min) {

        int total = 0;

        for (int i : min) {

            total += i;

        }

        return total;

    }

    

    static Map<String, Integer> totalBeadsCount(int[] min) {

        Map<String, Integer> beadsCount = new HashMap<>();

        for (int i : min) {

            int rudrakshaCount = i / 60; 

            int ebonyCount = i / 15; 

          beadsCount.put("rudraksha", beadsCount. getOrDefault ("rudraksha", 0) + rudrakshaCount);

            beadsCount.put("ebony", beadsCount. getOrDefault ("ebony", 0) + ebonyCount);

        }

        return beadsCount;

    }


    static String isShivaPleased(int min, Map<String, Integer> beadsCount) {

        int totalRudrakshaCount = beadsCount. getOrDefault ("rudraksha", 0);

        int totalEbonyCount = beadsCount.getOrDefault("ebony", 0);

        if (min >= 1440 && totalRudrakshaCount >= 108 && totalEbonyCount >= 72) {

            return "Lord Shiva is pleased!";

        } else {

            return "Meditate more and collect more beads!";

        }

    }


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {

            arr[i] = sc.nextInt();

        }

        int min = totalMeditationTime(arr);

        Map<String, Integer> beadsCount = totalBeadsCount(arr);

        System.out.println(min/60 + " hours");

       System.out.println(beadsCount. getOrDefault("rudraksha", 0) + " rudraksha");

       System.out.println(beadsCount. getOrDefault("ebony", 0) + " ebony");

        String shivaPleased = isShivaPleased(min, beadsCount);

        System.out.println(shivaPleased);

    }

}


Insights:

  • The program calculates the total duration of meditation sessions in minutes. It calculates the total counts of rudraksha and ebony beads based on the duration of each meditation session.
  • The program prints the total meditation time in hours, the count of rudraksha beads, and the count of ebony beads.
  • It checks whether Lord Shiva is pleased based on the total meditation time and the counts of rudraksha and ebony beads.
  • The program encapsulates related functionality into separate methods (totalMeditationTime, totalBeadsCount, isShivaPleased), enhancing code readability and reusability.
  • The map is populated dynamically within the totalBeadsCount method as the program iterates through the meditation session durations. For each session, the counts of rudraksha and ebony beads are calculated and added to the map accordingly.\
  • The getOrDefault method is used to retrieve the count of a specific type of bead from the map. If the specified key (bead type) exists in the map, the corresponding value (bead count) is returned. If the key does not exist, the method returns a default value of 0.
  • The use of getOrDefault ensures that the program does not throw a NullPointerException if a bead type is not present in the map. Instead, it gracefully handles such cases by returning a default count of 0 for the missing bead type.
Let the sacred rhythms of Lord Shiva's meditation awaken the coder within, paving a path of enlightenment through the realms of code.

Enjoy Coding! :) 🧘


Comments