Wednesday, October 9, 2024
Google search engine
HomeData Modelling & AIMinimum operations for adjacent E and E+1 pairing

Minimum operations for adjacent E and E+1 pairing

Given an integer N and an array arr[], containing numbers from 0 to (2*N-1). In one operation you can swap the position of any two integers in the array. Find the minimum number of operations required to make an array in such a format that, each even number ā€˜Eā€˜ is adjacent to ā€˜E+1ā€˜ i.e. 0 is adjacent to 1, 2 is adjacent to 3, and so on.

Examples:

Input: N = 2, arr[] = {3, 0, 2, 1}
Output: 1
Explanation: Swap values 0 and 2, after that new array will be {3, 2, 0, 1} where 0 is adjacent to 1 and 2 is adjacent to 3.

Input: N = 3, arr[] = {1, 0, 3, 2, 4, 5}
Output: 0
Explanations: Clearly all even number E is adjacent to E+1.

Approach: We can use Disjoint-Set-Union (DSU) data structure to solve this question efficiently.

Construct a graph where each vertex has two indexes 2i and 2i+1 for each 0 <= i < N. Now we form an edge between two vertices iff the index values of the vertices give ā€˜Eā€˜ in one vertex and ā€˜E+1ā€˜ in other vertex, where E is an even number. The total number of vertices ā€“ The total number of connected components in the graph will be our answer.

Follow the steps to solve the problem:

  • First, implement the DSU data structure using make(), find(), and Union() functions, and a parent array/map as per need.
  • For each, i from 0 to N, initialize the parent for vertex pair {2i, 2i+1} as itself using the make() function of DSU.
  • Find all the vertex pairs such that one pair has an even ā€˜Eā€™ and the other has ā€˜E+1ā€˜, and combine these vertices using the Union operation of DSU.
  • Find the number of connected components of the DSU, this can be done by finding the unique number of parents existing in the DSU
  • Print (number of vertices ā€“ number of connected components) as your answer.

Below is the implementation of the above algorithm.

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
Ā 
// Parent map to store parent of each vertex
map<pair<int, int>, pair<int, int> > parent;
Ā 
// This function is used to initialize DSU
void make(pair<int, int> i) { parent[i] = { i }; }
Ā 
// This function is used to find the parent of
// each component
pair<int, int> find(pair<int, int> v)
{
Ā Ā Ā Ā if (parent[v] == v)
Ā Ā Ā Ā Ā Ā Ā Ā return v;
Ā Ā Ā Ā return parent[v] = find(parent[v]);
}
Ā 
// This function is used to Merge two
// components together
void Union(pair<int, int> a, pair<int, int> b)
{
Ā Ā Ā Ā a = find(a);
Ā Ā Ā Ā b = find(b);
Ā Ā Ā Ā if (a != b) {
Ā Ā Ā Ā Ā Ā Ā Ā parent[b] = a;
Ā Ā Ā Ā }
}
Ā 
// This function solves our problem
int solve(vector<int>& arr, int n)
{
Ā 
Ā Ā Ā Ā // Initializing the DSU
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā pair<int, int> p = { 2 * i, 2 * i + 1 };
Ā Ā Ā Ā Ā Ā Ā Ā make(p);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // For all vertex i and j
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < n; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == j)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // vertex formed by i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pair<int, int> a = { 2 * i, 2 * i + 1 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // vertex formed by j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā pair<int, int> b = { 2 * j, 2 * j + 1 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Condition to find E and E+1 vertices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((arr[2 * i] / 2 == arr[2 * j] / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr[2 * i] / 2 == arr[2 * j + 1] / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr[2 * i + 1] / 2 == arr[2 * j] / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr[2 * i + 1] / 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā == arr[2 * j + 1] / 2)) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Union of valid vertices.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Union(a, b);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā map<pair<int, int>, int> comp;
Ā 
Ā Ā Ā Ā // Finding total unique components.
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā comp[find({ 2 * i, 2 * i + 1 })]++;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // n-unique components is our answer
Ā Ā Ā Ā return n - comp.size();
}
Ā 
// Driver function
int main()
{
Ā Ā Ā Ā int N = 2;
Ā Ā Ā Ā vector<int> arr = { 3, 0, 2, 1 };
Ā 
Ā Ā Ā Ā // Function call
Ā Ā Ā Ā cout << solve(arr, N) << endl;
Ā Ā Ā Ā return 0;
}


Java




import java.util.*;
Ā 
public class Main {
Ā Ā Ā Ā static Map<Pair, Pair> parent;
Ā 
Ā Ā Ā Ā // Custom class to represent pairs
Ā Ā Ā Ā static class Pair {
Ā Ā Ā Ā Ā Ā Ā Ā int first;
Ā Ā Ā Ā Ā Ā Ā Ā int second;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā public Pair(int first, int second) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.first = first;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.second = second;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā @Override
Ā Ā Ā Ā Ā Ā Ā Ā public int hashCode() {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return Objects.hash(first, second);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā @Override
Ā Ā Ā Ā Ā Ā Ā Ā public boolean equals(Object obj) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (this == obj)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (obj == null || getClass() != obj.getClass())
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair other = (Pair) obj;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return first == other.first && second == other.second;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // This function is used to initialize DSU
Ā Ā Ā Ā static void make(Pair i) {
Ā Ā Ā Ā Ā Ā Ā Ā parent.put(i, i);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // This function is used to find the parent of each component
Ā Ā Ā Ā static Pair find(Pair v) {
Ā Ā Ā Ā Ā Ā Ā Ā if (parent.get(v).equals(v))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return v;
Ā Ā Ā Ā Ā Ā Ā Ā return parent.put(v, find(parent.get(v)));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // This function is used to Merge two components together
Ā Ā Ā Ā static void Union(Pair a, Pair b) {
Ā Ā Ā Ā Ā Ā Ā Ā a = find(a);
Ā Ā Ā Ā Ā Ā Ā Ā b = find(b);
Ā Ā Ā Ā Ā Ā Ā Ā if (!a.equals(b)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā parent.put(b, a);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // This function solves our problem
Ā Ā Ā Ā static int solve(List<Integer> arr, int n) {
Ā Ā Ā Ā Ā Ā Ā Ā parent = new HashMap<>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Initializing the DSU
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair p = new Pair(2 * i, 2 * i + 1);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā make(p);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // For all vertex i and j
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < n; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == j)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // vertex formed by i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair a = new Pair(2 * i, 2 * i + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // vertex formed by j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair b = new Pair(2 * j, 2 * j + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Condition to find E and E+1 vertices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((arr.get(2 * i) / 2 == arr.get(2 * j) / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr.get(2 * i) / 2 == arr.get(2 * j + 1) / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr.get(2 * i + 1) / 2 == arr.get(2 * j) / 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā || (arr.get(2 * i + 1) / 2 == arr.get(2 * j + 1) / 2)) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Union of valid vertices.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Union(a, b);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Map<Pair, Integer> comp = new HashMap<>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Finding total unique components.
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā comp.put(find(new Pair(2 * i, 2 * i + 1)), comp.getOrDefault(find(new Pair(2 * i, 2 * i + 1)), 0) + 1);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // n-unique components is our answer
Ā Ā Ā Ā Ā Ā Ā Ā return n - comp.size();
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver function
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā int N = 2;
Ā Ā Ā Ā Ā Ā Ā Ā List<Integer> arr = Arrays.asList(3, 0, 2, 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(solve(arr, N));
Ā Ā Ā Ā }
}
Ā 
Ā 
// This code is contributed by akshitaguprzj3


Python3




class Pair:
Ā Ā Ā Ā def __init__(self, first, second):
Ā Ā Ā Ā Ā Ā Ā Ā self.first = first
Ā Ā Ā Ā Ā Ā Ā Ā self.second = second
Ā 
Ā Ā Ā Ā def __hash__(self):
Ā Ā Ā Ā Ā Ā Ā Ā return hash((self.first, self.second))
Ā 
Ā Ā Ā Ā def __eq__(self, other):
Ā Ā Ā Ā Ā Ā Ā Ā if self is other:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return True
Ā Ā Ā Ā Ā Ā Ā Ā if not isinstance(other, Pair):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return False
Ā Ā Ā Ā Ā Ā Ā Ā return self.first == other.first and self.second == other.second
Ā 
Ā 
def make(i, parent):
Ā Ā Ā Ā parent[i] = i
Ā 
Ā 
def find(v, parent):
Ā Ā Ā Ā if parent[v] == v:
Ā Ā Ā Ā Ā Ā Ā Ā return v
Ā Ā Ā Ā parent[v] = find(parent[v], parent)
Ā Ā Ā Ā return parent[v]
Ā 
Ā 
def union(a, b, parent):
Ā Ā Ā Ā a = find(a, parent)
Ā Ā Ā Ā b = find(b, parent)
Ā Ā Ā Ā if a != b:
Ā Ā Ā Ā Ā Ā Ā Ā parent[b] = a
Ā 
Ā 
def solve(arr, n):
Ā Ā Ā Ā parent = {}
Ā 
Ā Ā Ā Ā # Initializing the DSU
Ā Ā Ā Ā for i in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā p = Pair(2 * i, 2 * i + 1)
Ā Ā Ā Ā Ā Ā Ā Ā make(p, parent)
Ā 
Ā Ā Ā Ā # For all vertex i and j
Ā Ā Ā Ā for i in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā for j in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if i == j:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # vertex formed by i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā a = Pair(2 * i, 2 * i + 1)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # vertex formed by j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā b = Pair(2 * j, 2 * j + 1)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Condition to find E and E+1 vertices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (arr[2 * i] // 2 == arr[2 * j] // 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā or (arr[2 * i] // 2 == arr[2 * j + 1] // 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā or (arr[2 * i + 1] // 2 == arr[2 * j] // 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā or (arr[2 * i + 1] // 2 == arr[2 * j + 1] // 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Union of valid vertices.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā union(a, b, parent)
Ā 
Ā Ā Ā Ā comp = {}
Ā 
Ā Ā Ā Ā # Finding total unique components.
Ā Ā Ā Ā for i in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā component = find(Pair(2 * i, 2 * i + 1), parent)
Ā Ā Ā Ā Ā Ā Ā Ā comp[component] = comp.get(component, 0) + 1
Ā 
Ā Ā Ā Ā # n-unique components is our answer
Ā Ā Ā Ā return n - len(comp)
Ā 
Ā 
if __name__ == "__main__":
Ā Ā Ā Ā N = 2
Ā Ā Ā Ā arr = [3, 0, 2, 1]
Ā 
Ā Ā Ā Ā # Function call
Ā Ā Ā Ā print(solve(arr, N))


C#




using System;
using System.Collections.Generic;
Ā 
class Pair
{
Ā Ā Ā Ā public int first;
Ā Ā Ā Ā public int second;
Ā 
Ā Ā Ā Ā public Pair(int first, int second)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā this.first = first;
Ā Ā Ā Ā Ā Ā Ā Ā this.second = second;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public override int GetHashCode()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return (this.first, this.second).GetHashCode();
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public override bool Equals(object obj)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (this == obj)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (!(obj is Pair))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Pair other = (Pair)obj;
Ā Ā Ā Ā Ā Ā Ā Ā return this.first == other.first && this.second == other.second;
Ā Ā Ā Ā }
}
Ā 
class Program
{
Ā Ā Ā Ā static void Make(int i, Dictionary<Pair, Pair> parent)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā parent[new Pair(i * 2, i * 2 + 1)] = new Pair(i * 2, i * 2 + 1);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static Pair Find(Pair v, Dictionary<Pair, Pair> parent)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (parent[v].Equals(v))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return v;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā parent[v] = Find(parent[v], parent);
Ā Ā Ā Ā Ā Ā Ā Ā return parent[v];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static void Union(Pair a, Pair b, Dictionary<Pair, Pair> parent)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā a = Find(a, parent);
Ā Ā Ā Ā Ā Ā Ā Ā b = Find(b, parent);
Ā Ā Ā Ā Ā Ā Ā Ā if (!a.Equals(b))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā parent[b] = a;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static int Solve(int[] arr, int n)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā var parent = new Dictionary<Pair, Pair>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Initializing the DSU
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Make(i, parent);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // For all vertex i and j
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 0; j < n; j++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == j)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Vertex formed by i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair a = new Pair(i * 2, i * 2 + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Vertex formed by j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair b = new Pair(j * 2, j * 2 + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Condition to find E and E+1 vertices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((arr[i * 2] / 2 == arr[j * 2] / 2) ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (arr[i * 2] / 2 == arr[j * 2 + 1] / 2) ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (arr[i * 2 + 1] / 2 == arr[j * 2] / 2) ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (arr[i * 2 + 1] / 2 == arr[j * 2 + 1] / 2))
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Union of valid vertices.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Union(a, b, parent);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā var comp = new Dictionary<Pair, int>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Finding total unique components.
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Pair component = Find(new Pair(i * 2, i * 2 + 1), parent);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā comp[component] = comp.ContainsKey(component) ? comp[component] + 1 : 1;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // n-unique components is our answer
Ā Ā Ā Ā Ā Ā Ā Ā return n - comp.Count;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static void Main(string[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int N = 2;
Ā Ā Ā Ā Ā Ā Ā Ā int[] arr = { 3, 0, 2, 1 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Solve(arr, N));
Ā Ā Ā Ā }
}


Javascript




<script>
Ā 
// Custom class to represent pairs
class Pair {
Ā Ā Ā Ā constructor(first, second) {
Ā Ā Ā Ā Ā Ā Ā Ā this.first = first;
Ā Ā Ā Ā Ā Ā Ā Ā this.second = second;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā hashCode() {
Ā Ā Ā Ā Ā Ā Ā Ā return JSON.stringify([this.first, this.second]);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā equals(obj) {
Ā Ā Ā Ā Ā Ā Ā Ā if (this === obj) return true;
Ā Ā Ā Ā Ā Ā Ā Ā if (obj == null || typeof obj !== 'object' || obj.constructor !== Pair) return false;
Ā Ā Ā Ā Ā Ā Ā Ā return this.first === obj.first && this.second === obj.second;
Ā Ā Ā Ā }
}
Ā 
// Initialize DSU
function make(i, parent) {
Ā Ā Ā Ā parent.set(i, i);
}
Ā 
// Find the parent of each component
function find(v, parent) {
Ā Ā Ā Ā if (parent.get(v).equals(v)) return v;
Ā Ā Ā Ā return find(parent.get(v), parent);
}
Ā 
// Merge two components together
function Union(a, b, parent) {
Ā Ā Ā Ā a = find(a, parent);
Ā Ā Ā Ā b = find(b, parent);
Ā Ā Ā Ā if (!a.equals(b)) {
Ā Ā Ā Ā Ā Ā Ā Ā parent.set(b, a);
Ā Ā Ā Ā }
}
Ā 
// Solve the problem
function solve(arr, n) {
Ā Ā Ā Ā let parent = new Map();
Ā 
Ā Ā Ā Ā // Initializing the DSU
Ā Ā Ā Ā for (let i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā let p = new Pair(2 * i, 2 * i + 1);
Ā Ā Ā Ā Ā Ā Ā Ā make(p, parent);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // For all vertices i and j
Ā Ā Ā Ā for (let i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā for (let j = 0; j < n; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i === j) continue;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Vertex formed by i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let a = new Pair(2 * i, 2 * i + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Vertex formed by j
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let b = new Pair(2 * j, 2 * j + 1);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Condition to find E and E+1 vertices
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā arr[2 * i] / 2 === arr[2 * j] / 2 ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā arr[2 * i] / 2 === arr[2 * j + 1] / 2 ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā arr[2 * i + 1] / 2 === arr[2 * j] / 2 ||
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā arr[2 * i + 1] / 2 === arr[2 * j + 1] / 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Union of valid vertices.
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Union(a, b, parent);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā let comp = new Map();
Ā 
Ā Ā Ā Ā // Finding total unique components.
Ā Ā Ā Ā for (let i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā let component = find(new Pair(2 * i, 2 * i + 1), parent);
Ā Ā Ā Ā Ā Ā Ā Ā comp.set(component, (comp.get(component) || 0) + 1);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // n-unique components is our answer
Ā Ā Ā Ā return n - comp.size;
}
Ā 
// Driver function
let N = 2;
let arr = [3, 0, 2, 1];
Ā 
// Function call
console.log(solve(arr, N));
Ā 
</script>
Ā 
Ā 
// code contributed by shinjanpatra


Output

1






Time Complexity: O(N^2 logN)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfGā€™s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
19 Oct, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?