{"id":982,"date":"2023-12-14T17:00:00","date_gmt":"2023-12-14T17:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=982"},"modified":"2024-01-05T17:51:00","modified_gmt":"2024-01-05T17:51:00","slug":"whole-minute-dilemma","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/whole-minute-dilemma\/","title":{"rendered":"Whole Minute Dilemma Problem (C++, Java, Python)"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Whole-minute dilemma Problem?<\/strong><\/h2>\n\n\n\n<p><strong>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 &lt; j and (time[i] + time[j]) % 60 = 0.<\/strong><\/p>\n\n\n\n<p>For example,<\/p>\n\n\n\n<p>time = [30,20,150,100,40]<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/XbAslCMvPwcgLBCwXpGkTo2bCVdRXikirTYH7rRR6zt_KA3L1Fh46UBZhueO8Vop-NzaBI33HsyhOIzev5fNRPvvqQcaMEYilT4rjWxUflwUONu8YoawUU_dmO7kCSUCZJ8rGTJD21ZxzFkXm5ImBsA\" alt=\"Example of Whole Minute Dilemma Problem\"\/><\/figure>\n<\/div>\n\n\n<p><strong>Output:<\/strong> 3<\/p>\n\n\n\n<p>There exist three pairs whose sum of values is divisible by 60.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Steps to solve the problem using this approach:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly we Initialize a variable count to keep track of whether any pairs are found.<\/li>\n\n\n\n<li>Now we Iterate over each song, denoted as time[i].\n<ul class=\"wp-block-list\">\n<li>Iterate over all subsequent songs, denoted as time[j], starting from j = i + 1.<\/li>\n\n\n\n<li>Check if (time[i] + time[j]) % 60 == 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Now If true, we will increment the count variable.<\/li>\n<\/ul>\n\n\n\n<p>The time complexity of this approach is O(n<sup>2<\/sup>), where n is the number of songs. As this approach checks all possible pairs, and for each valid pair found, increment the count variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Optimized Approach to Solve It<\/strong><\/h2>\n\n\n\n<p>While the brute-force approach is a straightforward solution, it&#8217;s not the most efficient. Let&#8217;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\u2019s see how.<\/p>\n\n\n\n<p>Steps to be followed in this approach:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly we will create an array remainderCount to count the occurrences of remainders when each song duration is divided by 60.\n<ul class=\"wp-block-list\">\n<li>remainder_count[i] will represent the count of songs whose duration has a remainder of i when divided by 60.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Pairs with Remainder 0:<\/strong> 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<br><\/li>\n\n\n\n<li><strong>Pairs with Remainder 30:<\/strong> 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<br><\/li>\n\n\n\n<li>For other remainders, we count pairs as remainderCount[i] * remainderCount[60 &#8211; i]<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Code<\/strong><\/h3>\n\n\n\n<p>Here is the C++ program to solve the Whole Minute Dilemma problem:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;C++&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include&lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nint countPairsDivisibleBy60(vector&lt;int&gt;&amp; time) {\n    vector&lt;int&gt; remainderCount(60, 0);\n    int count = 0;\n\n    \/\/ Populate remainderCount vector\n    for (int t : time) {\n        remainderCount[t % 60]++;\n    }\n\n    \/\/ Count pairs with remainder 0\n    count += remainderCount[0] * (remainderCount[0] - 1) \/ 2;\n\n    \/\/ Count pairs with remainder 30\n    count += remainderCount[30] * (remainderCount[30] - 1) \/ 2;\n\n    \/\/ Count general case pairs\n    for (int i = 1; i &lt; 30; ++i) {\n        count += remainderCount[i] * remainderCount[60 - i];\n    }\n\n    return count;\n}\n\nint main() {\n    vector&lt;int&gt; time = {30, 20, 150, 100, 40};\n    int result = countPairsDivisibleBy60(time);\n\n    cout &lt;&lt; &quot;Number of pairs divisible by 60: &quot; &lt;&lt; result &lt;&lt; endl;\n\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java Code<\/strong><\/h3>\n\n\n\n<p>Here is the Java program as well:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Java&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.HashMap;\n\npublic class Main {\n    public static int countPairsDivisibleBy60(int[] time) {\n        int[] remainderCount = new int[60];\n        int count = 0;\n\n        \/\/ Populate remainderCount array\n        for (int t : time) {\n            remainderCount[t % 60]++;\n        }\n\n        \/\/ Count pairs with remainder 0\n        count += remainderCount[0] * (remainderCount[0] - 1) \/ 2;\n\n        \/\/ Count pairs with remainder 30\n        count += remainderCount[30] * (remainderCount[30] - 1) \/ 2;\n\n        \/\/ Count general case pairs\n        for (int i = 1; i &lt; 30; ++i) {\n            count += remainderCount[i] * remainderCount[60 - i];\n        }\n\n        return count;\n    }\n\n    public static void main(String[] args) {\n        int[] time = {30, 20, 150, 100, 40};\n        int result = countPairsDivisibleBy60(time);\n\n        System.out.println(&quot;Number of pairs divisible by 60: &quot; + result);\n    }\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Code<\/strong><\/h3>\n\n\n\n<p>Here is the Python solution for the Whole Minute Dilemma problem:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def count_pairs_divisible_by_60(time):\n    remainder_count = [0] * 60\n    count = 0\n\n    # Populate remainder_count list\n    for t in time:\n        remainder_count[t % 60] += 1\n\n    # Count pairs with remainder 0\n    count += remainder_count[0] * (remainder_count[0] - 1) \/\/ 2\n\n    # Count pairs with remainder 30\n    count += remainder_count[30] * (remainder_count[30] - 1) \/\/ 2\n\n    # Count general case pairs\n    for i in range(1, 30):\n        count += remainder_count[i] * remainder_count[60 - i]\n\n    return count\n\nif __name__ == &quot;__main__&quot;:\n    time = [30, 20, 150, 100, 40]\n    result = count_pairs_divisible_by_60(time)\n\n    print(&quot;Number of pairs divisible by 60:&quot;, result)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Number of pairs divisible by 60: 3<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Comparing both of them, the brute force approach checks all possible pairs, and for each valid pair found, increment the count variable with O(n<sup>2<\/sup>) 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).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/favtutor.com\/python-tutors\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/python-tutors\">Python tutors online<\/a> anytime anywhere.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We gave an optimized solution for whole minute dilemma problem with implementation in C++, Java and Python.<\/p>\n","protected":false},"author":12,"featured_media":985,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures-algorithms","tag-dsa","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=982"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":1039,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/982\/revisions\/1039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/985"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}