CSES Solutions - Playlist
Last Updated :
26 Mar, 2024
You are given a playlist of a radio station since its establishment. The playlist has a total of N songs. What is the longest sequence of successive songs where each song is unique?
Examples:
Input: N = 8, songs[] = {1, 2, 1, 3, 2, 7, 4, 2}
Output: 5
Explanation: The longest sequence of successive unique songs is: {1, 3, 2, 7, 4} which has 5 elements.
Input: N = 6, songs[] = {1, 2, 3, 1, 2, 3}
Output: 3
Explanation: The longest sequence of successive unique songs is: {1, 2, 3} which has 3 elements.
Approach: To solve the problem, follow the below idea:
The problem can be solved using a map to store the index of each character and two pointers: start and end to mark the starting and ending of the range of unique characters. Initially, we start from the first character and end at the first character. Then, we keep on shifting the end pointer and storing the index of each character in the map. If we encounter a character whose index > start pointer, then it means that in our window we already have that character. Then we will shrink the window by moving the start pointer to the previous index of the character + 1. This will maintain that all the character from start to end have unique characters.
Step-by-step algorithm:
- Maintain two pointers, start = 0 and end = 0 to mark the starting and ending point of window.
- Also maintain a map, say mp to store the last index of each character.
- Iterate end from 0 to N - 1,
- If the character at index = end is not present in the map, we insert the character along with the current index.
- Otherwise, if the character is present in the map and its last occurrence is greater than the start of the window, it means that the current character is already present in the window. So, we need to shrink the window by moving start to last occurrence of that character + 1.
- Also check for the maximum length of window (end - start + 1) in each iteration.
- After all the iterations, print the maximum length of a window.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll solve(ll N, ll* songs)
{
ll start = 0, ans = 0;
// Map to store the last occurrence of all the
// characters
map<ll, ll> mp;
// Increment end pointer character by character
for (ll end = 0; end < N; end++) {
// If the current song is not present in the map
if (mp.find(songs[end]) == mp.end())
mp.insert({ songs[end], end });
else {
// If the current song is present in the map and
// is inside the window
if (mp[songs[end]] >= start)
start = mp[songs[end]] + 1;
// Update the last occurrence of current
// character to the current index
mp[songs[end]] = end;
}
// Update ans to store the maximum length of range
// of unique values
ans = max(ans, end - start + 1);
}
return ans;
}
int main()
{
// Sample Input
ll N = 8;
ll songs[N] = { 1, 2, 1, 3, 2, 7, 4, 2 };
cout << solve(N, songs) << "\n";
}
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
static long solve(long N, long[] songs)
{
long start = 0, ans = 0;
// Map to store the last occurrence of all the
// characters
Map<Long, Long> mp = new HashMap<>();
// Increment end pointer character by character
for (long end = 0; end < N; end++) {
// If the current song is not present in the map
if (!mp.containsKey(songs[(int)end]))
mp.put(songs[(int)end], end);
else {
// If the current song is present in the map
// and is inside the window
if (mp.get(songs[(int)end]) >= start)
start = mp.get(songs[(int)end]) + 1;
// Update the last occurrence of current
// character to the current index
mp.put(songs[(int)end], end);
}
// Update ans to store the maximum length of
// range of unique values
ans = Math.max(ans, end - start + 1);
}
return ans;
}
public static void main(String[] args)
{
// Sample Input
long N = 8;
long[] songs = { 1, 2, 1, 3, 2, 7, 4, 2 };
System.out.println(solve(N, songs));
}
}
C#
using System;
using System.Collections.Generic;
public class MainClass {
public static long Solve(int N, int[] songs)
{
int start = 0;
long ans = 0;
Dictionary<int, int> mp
= new Dictionary<int, int>();
for (int end = 0; end < N; end++) {
if (!mp.ContainsKey(songs[end])) {
mp.Add(songs[end], end);
}
else {
if (mp[songs[end]] >= start) {
start = mp[songs[end]] + 1;
}
mp[songs[end]] = end;
}
ans = Math.Max(ans, end - start + 1);
}
return ans;
}
public static void Main(string[] args)
{
// Sample Input
int N = 8;
int[] songs = { 1, 2, 1, 3, 2, 7, 4, 2 };
Console.WriteLine(Solve(N, songs));
}
}
JavaScript
function solve(N, songs) {
let start = 0;
let ans = 0;
// Map to store the last occurrence of all the characters
let mp = new Map();
// Increment end pointer character by character
for (let end = 0; end < N; end++) {
// If the current song is not present in the map
if (!mp.has(songs[end])) {
mp.set(songs[end], end);
} else {
// If the current song is present in the map and is inside the window
if (mp.get(songs[end]) >= start) {
start = mp.get(songs[end]) + 1;
}
// Update the last occurrence of current character to the current index
mp.set(songs[end], end);
}
// Update ans to store the maximum length of range of unique values
ans = Math.max(ans, end - start + 1);
}
return ans;
}
// Sample Input
let N = 8;
let songs = [1, 2, 1, 3, 2, 7, 4, 2];
console.log(solve(N, songs));
Python3
def solve(N, songs):
start = 0
ans = 0
# Dictionary to store the last occurrence index of each song
mp = {}
# Iterate through the songs list
for end in range(N):
# If the current song is not present in the dictionary
if songs[end] not in mp:
mp[songs[end]] = end
else:
# If the current song is present in the dictionary and within the window
if mp[songs[end]] >= start:
start = mp[songs[end]] + 1
# Update the last occurrence of the current song to the current index
mp[songs[end]] = end
# Update 'ans' to store the maximum length of the range of unique values
ans = max(ans, end - start + 1)
return ans
def main():
# Sample Input
N = 8
songs = [1, 2, 1, 3, 2, 7, 4, 2]
print(solve(N, songs))
if __name__ == "__main__":
main()
Time Complexity: O(N * logN), where N is the number of songs in the playlist.
Auxiliary Space: O(N)
Similar Reads
Music Playlist App using MERN Stack This tutorial will create a Music Playlist App using the MERN (MongoDB, Express.js, React.js, Node.js) stack. The app allows users to manage playlists, add songs, and play their favorite tracks. We'll cover front and backend development, integrating features like song uploading, playlist creation, a
10 min read
Music Streaming Platform using MERN Stack In this tutorial, we'll walk through the process of creating a music streaming platform using the MERN stack. This project will allow users to browse playlists, view songs within each playlist, search for specific songs, and play them. We'll cover the basics of setting up a MongoDB database, creatin
7 min read
Music Player using Django In this article, we'll build a Music Player using Django. To get started, the first thing we'll do is create an account, just like with other music players. After creating the account, we'll log in. Once successfully logged in, we'll be redirected to the home page. On the home page, we can search fo
13 min read
Create a Music Player using React-Native React Native is a powerful framework that allows you to build mobile applications using JavaScript and React. In this tutorial, we are going to build a Music Player using React-Native. Here we will load the music files stored in our device and then use the play and pause button to control the music.
4 min read
Design Spotify Premium | System Design In today's digital world, the demand for premium music streaming services like Spotify is at an all-time high. Understanding the System Design behind Spotify Premium is crucial for software engineers seeking to build robust, scalable, and reliable music streaming platforms. This article explores the
12 min read