Given two arrays **A** and **B** of size **N**. Array **A** is in increasing order and **B** is in decreasing order. Both arrays are the subsequences of the numbers from **1** to **2N**. The task is to find the sum of the absolute difference of two arrays.

Sum = |A

_{1}– B_{1}| + |A_{2}– B_{2}| + |A_{3}– B_{3}| + …. + |A_{N}– B_{N}|

**Examples:**

Input :A[] = {1, 2, 3, 4, 5}, B[] = {10, 9, 8, 7, 6}

Output :25

Input :A[] = {1, 5, 6, 8, 10, 12}, B[] = {11, 9, 7, 4, 3, 2}

Output :36

**Naive Approach:** A navie approach is to run a loop and find the sum of the absolute differences.

**Efficient Approach:** Proizvolov’s identity is an identity concerning sums of the differences of positive integers. It states that if we take first **2N** integers and partition them into two subsets of **N** numbers each.

Arrange one subset in increasing order : **A _{1} < A_{2} < A_{3} < …. < A_{N}**

Arrange another subset in decreasing order : **B _{1} > B_{2} > B_{3} > …. > B_{N}**

Then the sum **|A _{1} – B_{1}| + |A_{2} – B_{2}| + |A_{3} – B_{3}| + …. + |A_{N} – B_{N}|** is always equals to

**N**

^{2}Below is the implementation of the above approach:

## C++

`// CPP program to implement proizvolov's identity ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to implement proizvolov's identity ` `int` `proizvolov(` `int` `a[], ` `int` `b[], ` `int` `n) ` `{ ` ` ` `// According to proizvolov's identity ` ` ` `return` `n*n; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = {1, 5, 6, 8, 10}, b[] = {9, 7, 4, 3, 2}; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// Function call ` ` ` `cout << proizvolov(a, b, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement proizvolov's identity ` `class` `GFG ` `{ ` ` ` `// Function to implement proizvolov's identity ` ` ` `static` `int` `proizvolov(` `int` `a [], ` `int` `b [], ` `int` `n) ` ` ` `{ ` ` ` `// According to proizvolov's identity ` ` ` `return` `n * n; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `a [] = {` `1` `, ` `5` `, ` `6` `, ` `8` `, ` `10` `}; ` ` ` `int` `b [] = {` `9` `, ` `7` `, ` `4` `, ` `3` `, ` `2` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `// Function call ` ` ` `System.out.println(proizvolov(a, b, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# proizvolov's identity ` ` ` `# Function to implement ` `# proizvolov's identity ` `def` `proizvolov(a, b, n): ` ` ` `return` `n ` `*` `n ` ` ` `# Driver code ` `a ` `=` `[ ` `1` `, ` `5` `, ` `6` `, ` `8` `, ` `10` `] ` `b ` `=` `[ ` `9` `, ` `7` `, ` `4` `, ` `3` `, ` `2` `] ` `n ` `=` `len` `(a) ` ` ` `# Function call ` `print` `(proizvolov(a, b, n, )) ` ` ` `# This code is contributed by nidhiva ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement proizvolov's identity ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to implement proizvolov's identity ` ` ` `static` `int` `proizvolov(` `int` `[] a, ` ` ` `int` `[] b, ` `int` `n) ` ` ` `{ ` ` ` `// According to proizvolov's identity ` ` ` `return` `n * n; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[] a = {1, 5, 6, 8, 10}; ` ` ` `int` `[] b = {9, 7, 4, 3, 2}; ` ` ` ` ` `int` `n = a.Length; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(proizvolov(a, b, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

25

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Significance of Pascal’s Identity
- Cassini’s Identity
- Program for Identity Matrix
- Euler's Four Square Identity
- Brahmagupta Fibonacci Identity
- Factorial of Large numbers using Logarithmic identity
- Count minimum number of moves to front or end to sort an array
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Maximum distinct prime factors of elements in a K-length subarray
- Count possible splits of sum N into K integers such that the minimum is at least P
- Construct an AP series consisting of A and B having minimum possible Nth term
- Smallest subarray of size greater than K with sum greater than a given value
- Minimize count of given operations required to make two given strings permutations of each other

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.