Given a circular array arr[] consisting of N positive integers, the task is to modify the array by replacing each array element with the nearest power of its previous or next array element.
Examples:
Input: arr[] = {2, 3, 4, 1, 2}
Output: {2, 4, 3, 1, 2}
Explanation:
For arr[0](= 2): The previous and the next array elements are 2 and 3 respectively. Therefore, nearest power is 21.
For arr[1](= 3): The previous and the next elements are 2 and 4 respectively. Therefore, the nearest power is 41.
For arr[2](= 4): The previous and the next elements are 3 and 1 respectively. Therefore, the nearest power is 31.
For arr[3](= 1): The previous and the next elements are 4, and 2. Therefore, the nearest power is 40.
For arr[4](= 2): The previous and the next elements are 1, and 2. Therefore, the nearest power of 1 is 21.
Input: arr[] = {21, 3, 54, 78, 9}
Output: {27, 1, 78, 81, 1}
Approach: The idea is to traverse the array and replace each array element with the nearest power of its previous element or next array element.
Follow the steps below to solve this problem:
- Traverse the array arr[] and perform the following steps:
- Find the value of K for which XK will be closest to Y.
- For calculating K, take the floor value of logx(Y).
- Therefore, K and K + 1 will be the two integers for which the nearest power is the closest.
- Calculate YK and Y(K + 1) and check which is closest to X and update the array element arr[i] with the closest value.
- After completing the above steps, print the array arr[] as the modified array .
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int nearestPow( int x, int y)
{
if (y == 1)
return 1;
int k = log10 (x) / log10 (y);
if ( abs ( pow (y, k) - x) <
abs ( pow (y, (k + 1)) - x))
return pow (y, k);
return pow (y, (k + 1));
}
void replacebyNearestPower(vector< int > arr)
{
int prev = arr[arr.size() - 1];
int lastNext = arr[0];
int next = 0;
for ( int i = 0; i < arr.size(); i++)
{
int temp = arr[i];
if (i == arr.size() - 1)
next = lastNext;
else
next = arr[(i + 1) % arr.size()];
int prevPow = nearestPow(arr[i], prev);
int nextPow = nearestPow(arr[i], next);
if ( abs (arr[i] - prevPow) <
abs (arr[i] - nextPow))
arr[i] = prevPow;
else
arr[i] = nextPow;
prev = temp;
}
for ( int i = 0; i < arr.size(); i++)
cout << arr[i] << " " ;
}
int main()
{
vector< int > arr{ 2, 3, 4, 1, 2 };
replacebyNearestPower(arr);
}
|
Java
class GFG{
static int nearestPow( int x, int y)
{
if (y == 1 )
return 1 ;
int k = ( int )(Math.log10(x) /
Math.log10(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++)
System.out.print(arr[i] + " " );
}
public static void main(String args[])
{
int [] arr = { 2 , 3 , 4 , 1 , 2 };
replacebyNearestPower(arr);
}
}
|
Python3
import math
def nearestPow(x, y):
if y = = 1 :
return 1
k = int (math.log(x, y))
if abs (y * * k - x) < abs (y * * (k + 1 ) - x):
return y * * k
return y * * (k + 1 )
def replacebyNearestPower(arr):
prev = arr[ - 1 ]
lastNext = arr[ 0 ]
for i in range ( len (arr)):
temp = arr[i]
if i = = len (arr) - 1 :
next = lastNext
else :
next = arr[(i + 1 ) % len (arr)]
prevPow = nearestPow(arr[i], prev)
nextPow = nearestPow(arr[i], next )
if abs (arr[i] - prevPow) < abs (arr[i] - nextPow):
arr[i] = prevPow
else :
arr[i] = nextPow
prev = temp
print (arr)
arr = [ 2 , 3 , 4 , 1 , 2 ]
replacebyNearestPower(arr)
|
C#
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);
}
}
|
Javascript
<script>
function nearestPow(x, y) {
if (y == 1)
return 1
var k = Math.floor(Math.log(x) / Math.log(y))
if (Math.abs(Math.pow(y,k) - x) <
Math.abs(Math.pow(y,(k + 1)) - x))
return Math.pow(y,k)
return Math.pow(y,(k + 1))
}
function replacebyNearestPower(arr) {
var prev = arr[arr.length -1]
var lastNext = arr[0]
for ( var i = 0; i < arr.length; i++){
var temp = arr[i]
if (i == arr.length -1)
var next = lastNext
else
var next = arr[(i + 1) % arr.length]
var prevPow = nearestPow(arr[i], prev)
var 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
}
document.write(arr)
}
var arr = [2, 3, 4, 1, 2]
replacebyNearestPower(arr)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!