using
System;
class
GFG{
static
int
nearestPow(
int
x,
int
y)
{
if
(y == 1)
return
1;
int
k = (
int
)(Math.Log(x, y));
if
(Math.Abs(Math.Pow(y, k) - x) <
Math.Abs(Math.Pow(y, (k + 1)) - x))
return
(
int
)(Math.Pow(y, k));
return
(
int
)(Math.Pow(y, (k + 1)));
}
static
void
replacebyNearestPower(
int
[] arr)
{
int
prev = arr[arr.Length - 1];
int
lastNext = arr[0];
int
next = 0;
for
(
int
i = 0; i < arr.Length; i++)
{
int
temp = arr[i];
if
(i == arr.Length - 1)
next = lastNext;
else
next = arr[(i + 1) % arr.Length];
int
prevPow = nearestPow(arr[i], prev);
int
nextPow = nearestPow(arr[i], next);
if
(Math.Abs(arr[i] - prevPow) <
Math.Abs(arr[i] - nextPow))
arr[i] = prevPow;
else
arr[i] = nextPow;
prev = temp;
}
for
(
int
i = 0; i < arr.Length; i++)
Console.Write(arr[i] +
" "
);
}
public
static
void
Main()
{
int
[] arr = { 2, 3, 4, 1, 2 };
replacebyNearestPower(arr);
}
}