using
System;
using
System.Collections.Generic;
class
pair {
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
public
class
GFG {
static
pair findMax(
int
[] a,
int
n)
{
Dictionary<
int
,
int
> index
=
new
Dictionary<
int
,
int
>();
int
ans = 0, x = -1, y = -1;
for
(
int
i = 0, j = 0; i < n; i++) {
if
(index.ContainsKey(a[i])) {
j = Math.Max(index[a[i]], j);
}
if
((i - j + 1) >= ans) {
if
((i - j + 1) == ans) {
if
(i == (n - 1) || j == 0) {
ans = i - j + 1;
x = i;
y = j;
}
}
else
{
ans = i - j + 1;
x = i;
y = j;
}
}
if
(index.ContainsKey(a[i]))
index[a[i]] = i+1;
else
index.Add(a[i], i + 1);
}
return
new
pair(x, y);
}
public
static
int
findMinOperations(
int
[] arr,
int
n)
{
pair p = findMax(arr, n);
int
i = p.second;
int
j = p.first;
return
2 * Math.Min(i, n - j - 1)
+ Math.Max(i, n - j - 1);
}
public
static
void
Main(String[] args)
{
int
[] arr = { 1, 3, 3, 5, 1, 9, 4, 1 };
int
N = arr.Length;
Console.Write(findMinOperations(arr, N));
}
}