
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to maximize the number of equivalent pairs after swapping in Python
Suppose we have a list of numbers A and list of numbers B of same length. We also have a 2D list of numbers C where each element is of the form [i, j] this indicates we can swap A[i] and A[j] as many times as we want. We have to find the maximum number of pairs where A[i] = B[i] after the swapping.
So, if the input is like A = [5, 6, 7, 8], B = [6, 5, 8, 7], C = [[0, 1],[2, 3]], then the output will be 4, as we can swap A[0] with A[1] then A[2] with A[3].
To solve this, we will follow these steps −
- N := size of A
- graph := a graph by attaching given edges bidirectionally.
- ans := 0
- seen := a list of size N and fill with False
- for u in range 0 to N, do
- if seen[u] is zero, then
- queue := a queue and insert u
- seen[u] := True
- for each node in queue, do
- for each nei in graph[node], do
- if seen[nei] is false, then
- insert nei at the end of queue
- seen[nei] := True
- if seen[nei] is false, then
- for each nei in graph[node], do
- count := a map that contains count of B[i] elements for all i in queue
- for each i in queue, do
- if count[A[i]] is non-zero, then
- count[A[i]] := count[A[i]] - 1
- ans := ans + 1
- if count[A[i]] is non-zero, then
- if seen[u] is zero, then
- return ans
Let us see the following implementation to get better understanding −
Example
from collections import Counter class Solution: def solve(self, A, B, edges): N = len(A) graph = [[] for _ in range(N)] for u, v in edges: graph[u].append(v) graph[v].append(u) ans = 0 seen = [False] * N for u in range(N): if not seen[u]: queue = [u] seen[u] = True for node in queue: for nei in graph[node]: if not seen[nei]: queue.append(nei) seen[nei] = True count = Counter(B[i] for i in queue) for i in queue: if count[A[i]]: count[A[i]] -= 1 ans += 1 return ans ob = Solution() A = [5, 6, 7, 8] B = [6, 5, 8, 7] C = [[0, 1],[2, 3]] print(ob.solve(A, B, C))
Input
[5, 6, 7, 8], [6, 5, 8, 7], [[0, 1],[2, 3]]
Output
4
- Related Articles
- Number of Equivalent Domino Pairs in Python
- Program to find longest number of 1s after swapping one pair of bits in Python
- Program to find array by swapping consecutive index pairs in Python
- Program to maximize number of nice divisors in Python
- Program to find maximize score after n operations in Python
- Program to maximize the minimum value after increasing K sublists in Python
- Program to find number of good pairs in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to check string is palindrome or not with equivalent pairs in Python
- Maximize the number of sum pairs which are divisible by K in C++
- Maximize Sum Of Array After K Negations in Python
- Maximize a given unsigned number by swapping bits at its extreme positions in C++
- Program to find out the number of pairs of equal substrings in Python
- Program to find max number of K-sum pairs in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)

Advertisements