# Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum

Given an array arr[] of some positive integers and missing occurrence of a specific integer represented by -1, the task is to find that missing number such that maximum absolute difference between adjacent elements is minimum.

Examples:

Input: arr[] = {-1, 10, -1, 12, -1}
Output: 11
Explanation:
=> a – a = 11 – 10 = 1
=> a – a = 11 – 10 = 1
=> a – a = 12 – 11 = 1
=> a – a = 12 – 11 = 1
Maximum absolute difference of adjacent elements – 2

Input: arr[] = {1,-1, 7, 5, 2, -1, 5}
Output: 4
Explanation:
=> a – a = 4 – 1 = 3
=> a – a = 7 – 4 = 3
=> a – a = 7 – 5 = 2
=> a – a = 5 – 2 = 3
=> a – a = 4 – 2 = 2
=> a – a = 5 – 4 = 1
Maximum absolute difference of adjacent elements – 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to find the maximum and minimum adjacent element to the missing number and the missing number can be the average of these two values such that maximum absolute difference is minimum.

```=> max = Maximum adjacent element
=> min = Minimum adjacent element

Missing number = (max + min)
-------------
2
```

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the missing ` `// number such that maximum absolute ` `// difference between adjacent element  ` `// is minimum ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find the missing ` `// number such that maximum  ` `// absolute difference is minimum ` `int` `missingnumber(``int` `n, ``int` `arr[]) ` `{ ` `    ``int` `mn = INT_MAX, mx = INT_MIN; ` `    ``// Loop to find the maximum and ` `    ``// minimum adjacent element to  ` `    ``// missing number ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``if` `(i > 0 && arr[i] == -1 &&  ` `                 ``arr[i - 1] != -1) { ` `            ``mn = min(mn, arr[i - 1]); ` `            ``mx = max(mx, arr[i - 1]); ` `        ``} ` ` `  `        ``if` `(i < (n - 1) && arr[i] == -1 &&  ` `                       ``arr[i + 1] != -1) { ` `            ``mn = min(mn, arr[i + 1]); ` `            ``mx = max(mx, arr[i + 1]); ` `        ``} ` `    ``} ` `     `  `    ``long` `long` `int` `res = (mx + mn) / 2; ` `    ``return` `res; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 5; ` `    ``int` `arr = { -1, 10, -1,  ` `                       ``12, -1 }; ` `    ``int` `ans = 0; ` `     `  `    ``// Function Call ` `    ``int` `res = missingnumber(n, arr); ` `    ``cout << res; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the missing ` `// number such that maximum absolute ` `// difference between adjacent element  ` `// is minimum ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to find the missing ` `// number such that maximum  ` `// absolute difference is minimum ` `static` `int` `missingnumber(``int` `n, ``int` `arr[]) ` `{ ` `    ``int` `mn = Integer.MAX_VALUE, ` `        ``mx = Integer.MIN_VALUE; ` `     `  `    ``// Loop to find the maximum and ` `    ``// minimum adjacent element to  ` `    ``// missing number ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        ``if` `(i > ``0` `&& arr[i] == -``1` `&&  ` `                     ``arr[i - ``1``] != -``1``)  ` `        ``{ ` `            ``mn = Math.min(mn, arr[i - ``1``]); ` `            ``mx = Math.max(mx, arr[i - ``1``]); ` `        ``} ` ` `  `        ``if` `(i < (n - ``1``) && arr[i] == -``1` `&&  ` `                           ``arr[i + ``1``] != -``1``) ` `        ``{ ` `            ``mn = Math.min(mn, arr[i + ``1``]); ` `            ``mx = Math.max(mx, arr[i + ``1``]); ` `        ``} ` `    ``} ` `     `  `    ``int` `res = (mx + mn) / ``2``; ` `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``5``; ` `    ``int` `arr[] = { -``1``, ``10``, -``1``,  ` `                    ``12``, -``1` `}; ` ` `  `    ``// Function Call ` `    ``int` `res = missingnumber(n, arr); ` `    ``System.out.print(res); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## C#

 `// C# implementation of the missing ` `// number such that maximum absolute ` `// difference between adjacent element  ` `// is minimum ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find the missing ` `// number such that maximum  ` `// absolute difference is minimum ` `static` `int` `missingnumber(``int` `n, ``int` `[]arr) ` `{ ` `    ``int` `mn = Int32.MaxValue, ` `        ``mx = Int32.MinValue; ` `     `  `    ``// Loop to find the maximum and ` `    ``// minimum adjacent element to  ` `    ``// missing number ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``if` `(i > 0 && arr[i] == -1 &&  ` `                     ``arr[i - 1] != -1)  ` `        ``{ ` `            ``mn = Math.Min(mn, arr[i - 1]); ` `            ``mx = Math.Max(mx, arr[i - 1]); ` `        ``} ` ` `  `        ``if` `(i < (n - 1) && arr[i] == -1 &&  ` `                           ``arr[i + 1] != -1) ` `        ``{ ` `            ``mn = Math.Min(mn, arr[i + 1]); ` `            ``mx = Math.Max(mx, arr[i + 1]); ` `        ``} ` `    ``} ` `     `  `    ``int` `res = (mx + mn) / 2; ` `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 5; ` `    ``int` `[]arr = ``new` `int``[]{ -1, 10, -1, 12, -1 }; ` ` `  `    ``// Function Call ` `    ``int` `res = missingnumber(n, arr); ` `    ``Console.WriteLine(res); ` `} ` `} ` ` `  `// This code is contributed by Nidhi_biet `

Output:

```11
```

Time Complexity: O(N)
Auxillary Space: O(1) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : 29AjayKumar, nidhi_biet