using
System;
using
System.Collections.Generic;
public
class
GFG
{
public
static
bool
is_given_set_permutation(SortedSet<
int
> hash,
int
N)
{
var
last_element = hash.Max;
if
(hash.Count == N && last_element == N)
{
return
true
;
}
return
false
;
}
public
static
List<
int
> findDivisors(
int
n)
{
var
divisors =
new
List<
int
>();
for
(
int
i = 1; i <= Math.Floor(Math.Sqrt(n)); i++)
{
if
(n % i == 0)
{
if
((
int
)(n / i) == i)
{
divisors.Add(i);
}
else
{
divisors.Add(i);
divisors.Add((
int
)(n / i));
}
}
}
return
divisors;
}
public
static
bool
is_current_divisor_answer(
int
[] arr,
int
N,
int
div)
{
var
hash =
new
SortedSet<
int
>();
for
(
int
i = 0; i < N; i++)
{
hash.Add(arr[i] % div);
}
return
GFG.is_given_set_permutation(hash, N);
}
public
static
int
number_to_make_permutation(
int
[] arr,
int
N)
{
var
hash =
new
SortedSet<
int
>();
for
(
int
i = 0; i < N; i++)
{
hash.Add(arr[i]);
}
if
(hash.Count != N)
{
return
-1;
}
if
(GFG.is_given_set_permutation(hash, N))
{
return
(N + 1);
}
var
total_sum = 0;
for
(
int
i = 0; i < N; i++)
{
total_sum += arr[i];
}
var
sum_of_permutation = (
int
)((N * (N + 1)) / 2);
var
remaining_sum = total_sum - sum_of_permutation;
var
divisors = GFG.findDivisors(remaining_sum);
for
(
int
i = 0; i < divisors.Count; i++)
{
if
(divisors[i] <= N)
{
continue
;
}
if
(GFG.is_current_divisor_answer(arr, N, divisors[i]))
{
return
divisors[i];
}
}
return
-1;
}
public
static
void
Main(String[] args)
{
int
[] arr = {18, 81, 29, 36, 11};
var
N = arr.Length;
Console.WriteLine(GFG.number_to_make_permutation(arr, N));
}
}