# Maximum difference elements that can added to a set

Given a set containing N elements, you are allowed to add an element Z > 0 to this set only if it can be represented as |X-Y| where X and Y are already present in the set. After adding an element Z, you can use it as an element of the set to add new elements. Find out the maximum number of elements that can be added in this way.

**Examples**:

Input: set = {2, 3}Output: 1 The only element that can be added is 1.Input: set = {4, 6, 10}Output: 2 The 2 elements that can be added are (6-4) = 2 and (10-2) = 8.

This problem is based on the following observations:

- The maximum element that can be inserted has to be less than the current maximum element in the set, since the difference of 2 integers can not exceed any of the integers.
- Every number that you insert has to be a multiple of the gcd of the given array. Since at any step, X and Y both are multiples of gcd, (X-Y) will also be a multiple of the gcd.
- You can insert all multiples of gcd less than the maximum element.
- The number of terms less than or equal to max divisible by gcd is floor (max/gcd), which will be the total number of elements in the set after performing all insertions, we need to remove the count of original N elements to get our answer.

Below is the implementation of the above approach:

## C++

`// CPP program to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference ` `// of 2 elements already in the set ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference ` `// of 2 elements already in the set ` `int` `maxNewElements(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `gcd = a[0]; ` ` ` ` ` `int` `mx = a[0]; ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `gcd = __gcd(gcd, a[i]); ` ` ` `mx = max(mx, a[i]); ` ` ` `} ` ` ` ` ` `int` `total_terms = mx / gcd; ` ` ` ` ` `return` `total_terms - n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 4, 6, 10 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << maxNewElements(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference ` `// of 2 elements already in the set ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` ` ` `class` `GFG{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) { ` ` ` `if` `(b==` `0` `) ` `return` `a; ` ` ` `return` `__gcd(b,a%b); ` `} ` `// Function to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference ` `// of 2 elements already in the set ` `static` `int` `maxNewElements(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `int` `gcd = a[` `0` `]; ` ` ` ` ` `int` `mx = a[` `0` `]; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) { ` ` ` `gcd = __gcd(gcd, a[i]); ` ` ` `mx = Math.max(mx, a[i]); ` ` ` `} ` ` ` ` ` `int` `total_terms = mx / gcd; ` ` ` ` ` `return` `total_terms - n; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `a[] = { ` `4` `, ` `6` `, ` `10` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.print(maxNewElements(a, n)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to find the maximum number ` `# of elements that can be added to a set ` `# such that it is the absolute difference ` `# of 2 elements already in the set ` ` ` `# from math lib import gcd method ` `from` `math ` `import` `gcd ` ` ` ` ` `# Function to find the maximum number ` `# of elements that can be added to a set ` `# such that it is the absolute difference ` `# of 2 elements already in the set ` `def` `maxNewElements(a, n) : ` ` ` ` ` `__gcd ` `=` `a[` `0` `] ` ` ` ` ` `mx ` `=` `a[` `0` `] ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n) : ` ` ` `__gcd ` `=` `gcd(__gcd,a[i]) ` ` ` `mx ` `=` `max` `(mx, a[i]) ` ` ` ` ` `total_terms ` `=` `mx ` `/` `__gcd ` ` ` ` ` `return` `total_terms ` `-` `n ` ` ` ` ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `4` `, ` `6` `, ` `10` `] ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `print` `(` `int` `(maxNewElements(a,n))) ` ` ` `# This code is contributed by ` `# ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the maximum ` `// number of elements that can be ` `// added to a set such that it is ` `// the absolute difference of 2 ` `// elements already in the set ` `class` `GFG ` `{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` `} ` ` ` `// Function to find the maximum number ` `// of elements that can be added to a set ` `// such that it is the absolute difference ` `// of 2 elements already in the set ` `static` `int` `maxNewElements(` `int` `[] a, ` `int` `n) ` `{ ` ` ` `int` `gcd = a[0]; ` ` ` ` ` `int` `mx = a[0]; ` ` ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `gcd = __gcd(gcd, a[i]); ` ` ` `mx = System.Math.Max(mx, a[i]); ` ` ` `} ` ` ` ` ` `int` `total_terms = mx / gcd; ` ` ` ` ` `return` `total_terms - n; ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` ` ` `int` `[] a = { 4, 6, 10 }; ` ` ` `int` `n = a.Length; ` ` ` `System.Console.WriteLine(maxNewElements(a, n)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

2

**Time Complexity: ** O(N*LogN)

## Recommended Posts:

- Maximum sum of difference of adjacent elements
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Find set of m-elements with difference of any two elements is divisible by k
- Maximum absolute difference in an array
- Queries for maximum difference between prime numbers in given ranges
- Count ways of choosing a pair with maximum difference
- Program to find the maximum difference between the index of any two different numbers
- Absolute Difference of all pairwise consecutive elements in an array
- Find an index such that difference between product of elements before and after it is minimum
- Generate original array from difference between every two consecutive elements
- Minimize the difference between the maximum and minimum values of the modified array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Maximum value of |arr[0] - arr[1]| + |arr[1] - arr[2]| + ... +|arr[n - 2] - arr[n - 1]| when elements are from 1 to n

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.