Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Structures and Algorithms

Whole Minute Dilemma Problem (C++, Java, Python)

Jatin Chanana by Jatin Chanana
January 5, 2024
Reading Time: 6 mins read
Whole Minute Dilemma Problem
Follow us on Google News   Subscribe to our newsletter

The whole-minute dilemma is a challenge where the goal is to determine the number of pairs of songs whose combined durations are perfectly divisible by 60. This article will explore this algorithmic puzzle, discuss the problem, present a brute-force solution, and then delve into a more efficient approach. We will also implement code in C++, Java, and Python.

What is a Whole-minute dilemma Problem?

In the Whole Minute Dilemma problem, we are given a list of songs each represented by its duration in seconds. The task is to find the number of pairs of songs for which the sum of their durations is divisible by 60. Our goal is to determine the count of pairs (i, j) such that i < j and (time[i] + time[j]) % 60 = 0.

For example,

time = [30,20,150,100,40]

Example of Whole Minute Dilemma Problem

Output: 3

There exist three pairs whose sum of values is divisible by 60.

We can easily solve this programming problem using a simple Brute Force approach. In this approach, we must find all the possible pairs and check if their sum is divisible by 60. We have to use a nested loop to obtain all possible pairs for this.

Steps to solve the problem using this approach:

  • Firstly we Initialize a variable count to keep track of whether any pairs are found.
  • Now we Iterate over each song, denoted as time[i].
    • Iterate over all subsequent songs, denoted as time[j], starting from j = i + 1.
    • Check if (time[i] + time[j]) % 60 == 0.
  • Now If true, we will increment the count variable.

The time complexity of this approach is O(n2), where n is the number of songs. As this approach checks all possible pairs, and for each valid pair found, increment the count variable.

Optimized Approach to Solve It

While the brute-force approach is a straightforward solution, it’s not the most efficient. Let’s explore a more optimized approach that uses the properties of modular arithmetic. We can manipulate this (time[i] + time[j]) % 60 = 0 to (time[i]%60+time[j]%60)%60 = 0 and use this to solve this problem. Let’s see how.

Steps to be followed in this approach:

  • Firstly we will create an array remainderCount to count the occurrences of remainders when each song duration is divided by 60.
    • remainder_count[i] will represent the count of songs whose duration has a remainder of i when divided by 60.
  • Pairs with Remainder 0: We can form pairs with any other song having a remainder of 0. The count is given by (remainderCount[0] * remainderCount[0]-1 )/2 where remainderCount[0] is the count of songs with remainder 0
  • Pairs with Remainder 30: For songs with a remainder of 30, the pair can be formed with any other song having a remainder of 30 The count of such pairs is given by (remainderCount[30] * remainderCount[30]-1 )/2 where remainderCount[30] is the count of songs with remainder 30
  • For other remainders, we count pairs as remainderCount[i] * remainderCount[60 – i]

C++ Code

Here is the C++ program to solve the Whole Minute Dilemma problem:

#include<bits/stdc++.h>
using namespace std;

int countPairsDivisibleBy60(vector<int>& time) {
    vector<int> remainderCount(60, 0);
    int count = 0;

    // Populate remainderCount vector
    for (int t : time) {
        remainderCount[t % 60]++;
    }

    // Count pairs with remainder 0
    count += remainderCount[0] * (remainderCount[0] - 1) / 2;

    // Count pairs with remainder 30
    count += remainderCount[30] * (remainderCount[30] - 1) / 2;

    // Count general case pairs
    for (int i = 1; i < 30; ++i) {
        count += remainderCount[i] * remainderCount[60 - i];
    }

    return count;
}

int main() {
    vector<int> time = {30, 20, 150, 100, 40};
    int result = countPairsDivisibleBy60(time);

    cout << "Number of pairs divisible by 60: " << result << endl;

    return 0;
}

Java Code

Here is the Java program as well:

import java.util.HashMap;

public class Main {
    public static int countPairsDivisibleBy60(int[] time) {
        int[] remainderCount = new int[60];
        int count = 0;

        // Populate remainderCount array
        for (int t : time) {
            remainderCount[t % 60]++;
        }

        // Count pairs with remainder 0
        count += remainderCount[0] * (remainderCount[0] - 1) / 2;

        // Count pairs with remainder 30
        count += remainderCount[30] * (remainderCount[30] - 1) / 2;

        // Count general case pairs
        for (int i = 1; i < 30; ++i) {
            count += remainderCount[i] * remainderCount[60 - i];
        }

        return count;
    }

    public static void main(String[] args) {
        int[] time = {30, 20, 150, 100, 40};
        int result = countPairsDivisibleBy60(time);

        System.out.println("Number of pairs divisible by 60: " + result);
    }
}

Python Code

Here is the Python solution for the Whole Minute Dilemma problem:

def count_pairs_divisible_by_60(time):
    remainder_count = [0] * 60
    count = 0

    # Populate remainder_count list
    for t in time:
        remainder_count[t % 60] += 1

    # Count pairs with remainder 0
    count += remainder_count[0] * (remainder_count[0] - 1) // 2

    # Count pairs with remainder 30
    count += remainder_count[30] * (remainder_count[30] - 1) // 2

    # Count general case pairs
    for i in range(1, 30):
        count += remainder_count[i] * remainder_count[60 - i]

    return count

if __name__ == "__main__":
    time = [30, 20, 150, 100, 40]
    result = count_pairs_divisible_by_60(time)

    print("Number of pairs divisible by 60:", result)

Output:

Number of pairs divisible by 60: 3

The time complexity of this approach is O(n), where n is the size of the time vector. As in this loop iterates through each element in the time vector once. The space complexity of this approach is O(1) as we are not using any extra space.

Comparing both of them, the brute force approach checks all possible pairs, and for each valid pair found, increment the count variable with O(n2) time complexity. In the optimized solution, we use a loop, which iterates through each element in the time vector once. This decreases the time complexity to O(n).

Conclusion

So, we have discussed 2 approaches, the first one is absolute brute force and the second one is an optimized approach in which we have used principles of modular arithmetic and the concept of remainders. Now you can easily crack the interview if the question is about whole minute dilemma problem. If you have some more problems, you might get expert assistance from our Python tutors online anytime anywhere.

ShareTweetShareSendSend
Jatin Chanana

Jatin Chanana

I am Jatin, a final-year undergraduate student with a keen interest in the field of software development. I possess a strong academic background and a deep passion for Data Structures, Algorithms, and Web Development.

RelatedPosts

validate ip address

Validate IP Address Problem ( C++, JAVA, Python)

April 3, 2025
Shortest Palindrome Problem

Shortest Palindrome Problem ( C++, JAVA, Python)

April 4, 2025
Zigzag Conversion Problem

Zigzag Conversion Problem (C++, Java, Python)

April 4, 2025
Next Right Pointers In Each Node

Populating Next Right Pointers In Each Node (with code)

March 27, 2024
Coin Change II

Coin Change II Problem (C++, Java, Python)

April 7, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.