- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Find the compatibility difference between two arrays in C++

Consider there are two friends and now they want to test their bonding. So they will check, how much compatible they are. Given the numbers n, numbered from 1..n. And they are asked to rank the numbers. They have to find the compatibility difference between them. The compatibility difference is basically the number of mismatches in the relative ranking of the same movie given by them. So if A = [3, 1, 2, 4, 5], and B = [3, 2, 4, 1, 5], then the output will be 2. The compatibility difference is 2, as first ranks movie 1 before 2 and 4, but other ranks it after.

To solve this, we will traverse both arrays, when the current elements are same, then do nothing. Then find the next position of A and B, let the position be j, one by one move B[j] to B[i]

## Example

#include<iostream> using namespace std; int getArrayDiff(int A[], int B[], int n) { int result = 0; for (int i = 0; i < n; i++) { if (A[i] != B[i]) { int j = i + 1; while (A[i] != B[j]) j++; while (j != i) { swap(B[j], B[j - 1]); j--; result++; } } } return result; } int main() { int A[] = { 3, 1, 2, 4, 5 }; int B[] = { 3, 2, 4, 1, 5 }; int n = sizeof(A)/sizeof(A[0]); cout << "Compatibility difference: " << getArrayDiff(A, B, n); }

## Output

Compatibility difference: 2

- Related Questions & Answers
- Find the Symmetric difference between two arrays - JavaScript
- Finding the difference between two arrays - JavaScript
- How to find set difference between two Numpy arrays?
- How to get the difference between two arrays in JavaScript?
- How to find intersection between two Numpy arrays?
- Find the difference between two datetime values with MySQL?
- Python Pandas – Find the Difference between two Dataframes
- Find the difference between two timestamps in days with MySQL
- How to find the common elements between two or more arrays in JavaScript?
- Python program to find difference between two timestamps
- Find difference between sums of two diagonals in C++.
- Find minimum difference between any two element in C++
- Find the minimum difference between Shifted tables of two numbers in Python
- How to find common elements between two Arrays using STL in C++?
- Difference between Arrays and Collection in Java