Company mergers
Practice
2.5 (25 votes)
Basic programming
Basics of implementation
Easy
Implementation
Problem
89% Success 6046 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Several car companies exist in such a way that each company can control a large share of sales in different markets. You are required to perform a simulation that modifies the results after any mergers that happen between these companies.

Your task is to determine the minimum number of mergers that must be performed between the car companies to ensure that a market is controlled by no more than two separate companies.

Note: A merger between two companies results in a single company that controls the same markets as the two previous companies.

Input format

  • First line: An integer \(n\) denoting the number of car companies
  • Second line: An integer \(m\) denoting the number of markets controlled by each company
  • Each \(i^{th}\) line of the \(n\) subsequent lines (where \(0 ≤ i < n\)): \(m\) space-separated integers denoting the market ID of the market controlled by company \(i\)

Output format
Print an integer denoting the minimum number of mergers.

Constraints
\(1 ≤ n ≤ 5\\ 1 ≤ m ≤ 3\\ 1 ≤ market\_id ≤ 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
18 votes
Tags:
Ad-HocEasyImplementation
Points:20
27 votes
Tags:
ArraysEasyImplementationMathRecursion
Points:20
186 votes
Tags:
Ad-HocEasyMathOpen