fork download
  1. /**
  2. We have a catalog of song titles (and their lengths) that we play at a local radio station. We have been asked to play two of those songs in a row, and they must add up to exactly seven minutes long.
  3.  
  4. Given a list of songs and their durations, write a function that returns the names of any two distinct songs that add up to exactly seven minutes. If there is no such pair, return an empty collection.
  5.  
  6. Example:
  7. song_times_1 = [
  8.   ("Stairway to Heaven", "8:05"), ("Immigrant Song", "2:27"),
  9.   ("Rock and Roll", "3:41"), ("Communication Breakdown", "2:29"),
  10.   ("Good Times Bad Times", "2:48"), ("Hot Dog", "3:19"),
  11.   ("The Crunge", "3:18"), ("Achilles Last Stand", "10:26"),
  12.   ("Black Dog", "4:55")
  13. ]
  14. find_pair(song_times_1) => ["Rock and Roll", "Hot Dog"] (3:41 + 3:19 = 7:00)
  15.  
  16. Additional Input:
  17. song_times_2 = [
  18.   ("Stairway to Heaven", "8:05"), ("Immigrant Song", "2:27"),
  19.   ("Rock and Roll", "3:41"), ("Communication Breakdown", "2:29"),
  20.   ("Good Times Bad Times", "2:48"), ("Black Dog", "4:55"),
  21.   ("The Crunge", "3:18"), ("Achilles Last Stand", "10:26"),
  22.   ("The Ocean", "4:31"), ("Hot Dog", "3:19"),
  23. ]
  24. song_times_3 = [
  25.   ("Stairway to Heaven", "8:05"), ("Immigrant Song", "2:27"),
  26.   ("Rock and Roll", "3:41"), ("Communication Breakdown", "2:29"),
  27.   ("Hey Hey What Can I Do", "4:00"), ("Poor Tom", "3:00"),
  28.   ("Black Dog", "4:55")
  29. ]
  30. song_times_4 = [
  31.   ("Hey Hey What Can I Do", "4:00"), ("Rock and Roll", "3:41"),
  32.   ("Communication Breakdown", "2:29"), ("Going to California", "3:30"),
  33.   ("On The Run", "3:50"), ("The Wrestler", "3:50"),
  34.   ("Black Mountain Side", "2:11"), ("Brown Eagle", "2:20")
  35. ]
  36. song_times_5 = [("Celebration Day", "3:30"), ("Going to California", "3:30")]
  37. song_times_6 = [
  38.   ("Rock and Roll", "3:41"), ("If I lived here", "3:59"),
  39.   ("Day and night", "5:03"), ("Tempo song", "1:57")
  40. ]
  41.  
  42.  
  43. Complexity Variable:
  44. n = number of song/time pairs
  45.  
  46. All Test Cases - snake_case:
  47. find_pair(song_times_1) => ["Rock and Roll", "Hot Dog"]
  48. find_pair(song_times_2) => ["Rock and Roll", "Hot Dog"] or ["Communication Breakdown", "The Ocean"]
  49. find_pair(song_times_3) => ["Hey Hey What Can I Do", "Poor Tom"]
  50. find_pair(song_times_4) => []
  51. find_pair(song_times_5) => ["Celebration Day", "Going to California"]
  52. find_pair(song_times_6) => ["Day and night", "Tempo song"]
  53.  
  54. All Test Cases - camelCase:
  55. findPair(songTimes1) => ["Rock and Roll", "Hot Dog"]
  56. findPair(songTimes2) => ["Rock and Roll", "Hot Dog"] or ["Communication Breakdown", "The Ocean"]
  57. findPair(songTimes3) => ["Hey Hey What Can I Do", "Poor Tom"]
  58. findPair(songTimes4) => []
  59. findPair(songTimes5) => ["Celebration Day", "Going to California"]
  60. findPair(songTimes6) => ["Day and night", "Tempo song"]
  61. **/
  62. #include <iostream>
  63. #include <string>
  64. #include <vector>
  65. #include <algorithm>
  66.  
  67. using namespace std;
  68.  
  69. vector<pair<string, int>> convertMinuteToSecond(vector<pair<string, string>> songTimes) {
  70. vector<pair<string, int>> res;
  71. for(int i=0; i<songTimes.size(); i++) {
  72. pair<string, string> currentSong = songTimes[i];
  73. string duration = currentSong.second;
  74. cout<<duration<<endl;
  75. cout<<duration[0]<<endl;
  76. cout<<duration[2]<<endl;
  77. cout<<duration[3]<<endl;
  78. if(duration.size() <= 4) {
  79. int second = 0;
  80. second = (duration[0]-'0')*60;
  81. second += (duration[2]-'0')*10;
  82. second += duration[3]-'0';
  83.  
  84. // cout<<currentSong.first<<" "<<second<<endl;
  85. res.push_back({currentSong.first, second});
  86. }
  87. }
  88. return res;
  89. }
  90. vector<string> findSongs(vector<pair<string, string>> songTimes) {
  91. vector<pair<string, int>> songTimesSecond = convertMinuteToSecond(songTimes);
  92. cout<<endl;
  93. int sevenMinuteInseconds = 420;
  94. for(int i=0; i<songTimesSecond.size(); i++) {
  95. pair<string, int> currentSong = songTimesSecond[i];
  96. // cout<<currentSong.second<<endl;
  97. if(currentSong.second < sevenMinuteInseconds) {
  98. // cout<<currentSong.first<< " "<<currentSong.second<<endl;
  99. for(int j=i+1; j<songTimesSecond.size(); j++) {
  100. // cout<<currentSong.second + songTimesSecond[j].second<<endl;
  101. if(currentSong.second + songTimesSecond[j].second == sevenMinuteInseconds) {
  102. cout<<currentSong.first + songTimesSecond[j].first<<endl;
  103. return {currentSong.first, songTimesSecond[j].first};
  104. }
  105. }
  106. }
  107. }
  108. return {};
  109. }
  110.  
  111. int main() {
  112.  
  113. const vector<pair<string, string>> songTimes1 = {
  114. {"Stairway to Heaven", "8:05"}, {"Immigrant Song", "2:27"},
  115. {"Rock and Roll", "3:41"}, {"Communication Breakdown", "2:29"},
  116. {"Good Times Bad Times", "2:48"}, {"Hot Dog", "3:19"},
  117. {"The Crunge", "3:18"}, {"Achilles Last Stand", "10:26"},
  118. {"Black Dog", "4:55"}
  119. };
  120. // vector<string> res = findSongs(songTimes1);
  121. // for(int i=0; i<res.size(); i++) {
  122. // cout<<res[i]<<"\t";
  123. // }
  124. cout<<endl;
  125. const vector<pair<string, string>> songTimes2 = {
  126. {"Stairway to Heaven", "8:05"}, {"Immigrant Song", "2:27"},
  127. {"Rock and Roll", "3:41"}, {"Communication Breakdown", "2:29"},
  128. {"Good Times Bad Times", "2:48"}, {"Black Dog", "4:55"},
  129. {"The Crunge", "3:18"}, {"Achilles Last Stand", "10:26"},
  130. {"The Ocean", "4:31"}, {"Hot Dog", "3:19"}
  131. };
  132. const vector<pair<string, string>> songTimes3 = {
  133. {"Stairway to Heaven", "8:05"}, {"Immigrant Song", "2:27"},
  134. {"Rock and Roll", "3:41"}, {"Communication Breakdown", "2:29"},
  135. {"Hey Hey What Can I Do", "4:00"}, {"Poor Tom", "3:00"},
  136. {"Black Dog", "4:55"}
  137. };
  138. const vector<pair<string, string>> songTimes4 = {
  139. {"Hey Hey What Can I Do", "4:00"}, {"Rock and Roll", "3:41"},
  140. {"Communication Breakdown", "2:29"}, {"Going to California", "3:30"},
  141. {"On The Run", "3:50"}, {"The Wrestler", "3:50"},
  142. {"Black Mountain Side", "2:11"}, {"Brown Eagle", "2:20"}
  143. };
  144. const vector<pair<string, string>> songTimes5 = {
  145. {"Celebration Day", "3:30"}, {"Going to California", "3:30"}
  146. };
  147. const vector<pair<string, string>> songTimes6 = {
  148. {"Rock and Roll", "3:41"}, {"If I lived here", "3:59"},
  149. {"Day and night", "5:03"}, {"Tempo song", "1:57"}
  150. };
  151.  
  152. vector<string> res1 = findSongs(songTimes1);
  153. // vector<string> res2 = findSongs(songTimes2);
  154. // vector<string> res3 = findSongs(songTimes3);
  155. // vector<string> res4 = findSongs(songTimes4);
  156. // vector<string> res5 = findSongs(songTimes5);
  157. // vector<string> res6 = findSongs(songTimes6);
  158. return 0;
  159. }
  160.  
Success #stdin #stdout 0s 5320KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
8:05
8
0
5
2:27
2
2
7
3:41
3
4
1
2:29
2
2
9
2:48
2
4
8
3:19
3
1
9
3:18
3
1
8
10:26
1
:
2
4:55
4
5
5

Rock and RollHot Dog