- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang program to find the maximum number of classes that can be scheduled without conflicts

In this Golang article we are going to find the maximum number of classes that can be scheduled without conflicts, if there is a list of intervals representing classes. Here we are going to slice function to perform this task. Following Algorithm will help you understand the steps taken to creat the golang program.

## Algorithm

**Step 1**− First, we need to import the fmt and sort packages.**Step 2**− Then, create a struct named interval and store in it the start and end variable.**Step 3**− Now, start the main() function. Inside the main() Initialize the structure and store values to it. Use the slice function present in sort package sort the given slice.**Step 4**− Sort the list of intervals by their end times in ascending order. Initialize the maximum number of classes to 0.**Step 5**− Initialize the end time of the last scheduled class to 0. Loop through the sorted intervals and schedule as many classes as possible.**Step 6**− If the start time of the current interval is greater than or equal to the end time of the last scheduled class, schedule the current interval and update the maximum number of classes and the end time of the last scheduled class.**Step 7**− Return the maximum number of classes scheduled.

## Example

In the following Example we will first define the list of intervals representing classes. It then sorts the intervals by their end times in ascending order, so that the intervals that end earlier are scheduled first. It initializes the maximum number of classes to 0 and the end time of the last scheduled class to 0. It then loops through the sorted intervals and schedules as many classes as possible, by checking if the start time of the current interval is greater than or equal to the end time of the last scheduled class.

package main import ( "fmt" "sort" ) type interval struct { start int end int } func main() { // Define the list of intervals intervals := []interval{ {start: 10, end: 12}, {start: 8, end: 10}, {start: 14, end: 16}, {start: 13, end: 15}, {start: 12, end: 14}, } fmt.Println("The given intervals are:", intervals) // Sort the intervals by their end times in ascending order sort.Slice(intervals, func(i, j int) bool { return intervals[i].end < intervals[j].end }) // Initialize the maximum number of classes to 0 maxClasses := 0 // Initialize the end time of the last scheduled class to 0 lastEndTime := 0 // Loop through the sorted intervals and schedule as many classes as possible for _, interval := range intervals { if interval.start >= lastEndTime { maxClasses++ lastEndTime = interval.end } } // Print the result fmt.Printf("Maximum number of classes that can be scheduled: %d\n", maxClasses) }

## Output

The given intervals are: [{10 12} {8 10} {14 16} {13 15} {12 14}] Maximum number of classes that can be scheduled: 4

## Conclusion

In conclusion, we have discussed how to find the maximum number of classes that can be scheduled without conflicts, given a list of intervals representing classes. We have presented a solution in Go that sorts the intervals by their end times in ascending order, and uses a simple greedy Algorithm to schedule as many classes as possible without conflicts. This Algorithm has a time complexity of O(nlogn), where n is the number of intervals. This problem can be applied in various real-world scenarios, such as scheduling appointments, meetings, or events.