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++ program for the above approach #include<bits/stdc++.h> using namespace std;
// Function to calculate the power // of y which is nearest to x int nearestPow( int x, int y)
{ // Base Case
if (y == 1)
return 1;
// Stores the logarithmic
// value of x with base y
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));
} // Function to replace each array // element by the nearest power of // its previous or next element void replacebyNearestPower(vector< int > arr)
{ // Stores the previous
// and next element
int prev = arr[arr.size() - 1];
int lastNext = arr[0];
int next = 0;
// Traverse the array
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()];
// Calculate nearest power for
// previous and next elements
int prevPow = nearestPow(arr[i], prev);
int nextPow = nearestPow(arr[i], next);
// Replacing the array values
if ( abs (arr[i] - prevPow) <
abs (arr[i] - nextPow))
arr[i] = prevPow;
else
arr[i] = nextPow;
prev = temp;
}
// Print the updated array
for ( int i = 0; i < arr.size(); i++)
cout << arr[i] << " " ;
} // Driver Code int main()
{ // Given array
vector< int > arr{ 2, 3, 4, 1, 2 };
replacebyNearestPower(arr);
} // This code is contributed by ipg2016107 |
// Java program for the above approach class GFG{
// Function to calculate the power // of y which is nearest to x static int nearestPow( int x, int y)
{ // Base Case
if (y == 1 )
return 1 ;
// Stores the logarithmic
// value of x with base y
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 )));
} // Function to replace each array // element by the nearest power of // its previous or next element static void replacebyNearestPower( int [] arr)
{ // Stores the previous
// and next element
int prev = arr[arr.length - 1 ];
int lastNext = arr[ 0 ];
int next = 0 ;
// Traverse the array
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];
// Calculate nearest power for
// previous and next elements
int prevPow = nearestPow(arr[i], prev);
int nextPow = nearestPow(arr[i], next);
// Replacing the array values
if (Math.abs(arr[i] - prevPow) <
Math.abs(arr[i] - nextPow))
arr[i] = prevPow;
else
arr[i] = nextPow;
prev = temp;
}
// Print the updated array
for ( int i = 0 ; i < arr.length; i++)
System.out.print(arr[i] + " " );
} // Driver Code public static void main(String args[])
{ // Given array
int [] arr = { 2 , 3 , 4 , 1 , 2 };
replacebyNearestPower(arr);
} } // This code is contributed by abhinavjain194 |
# Python3 program for the above approach import math
# Function to calculate the power # of y which is nearest to x def nearestPow(x, y):
# Base Case
if y = = 1 :
return 1
# Stores the logarithmic
# value of x with base y
k = int (math.log(x, y))
if abs (y * * k - x) < abs (y * * (k + 1 ) - x):
return y * * k
return y * * (k + 1 )
# Function to replace each array # element by the nearest power of # its previous or next element def replacebyNearestPower(arr):
# Stores the previous
# and next element
prev = arr[ - 1 ]
lastNext = arr[ 0 ]
# Traverse the array
for i in range ( len (arr)):
temp = arr[i]
if i = = len (arr) - 1 :
next = lastNext
else :
next = arr[(i + 1 ) % len (arr)]
# Calculate nearest power for
# previous and next elements
prevPow = nearestPow(arr[i], prev)
nextPow = nearestPow(arr[i], next )
# Replacing the array values
if abs (arr[i] - prevPow) < abs (arr[i] - nextPow):
arr[i] = prevPow
else :
arr[i] = nextPow
prev = temp
# Print the updated array
print (arr)
# Driver Code # Given array arr = [ 2 , 3 , 4 , 1 , 2 ]
replacebyNearestPower(arr) |
// C# program for the above approach using System;
class GFG{
// Function to calculate the power // of y which is nearest to x static int nearestPow( int x, int y)
{ // Base Case
if (y == 1)
return 1;
// Stores the logarithmic
// value of x with base y
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)));
} // Function to replace each array // element by the nearest power of // its previous or next element static void replacebyNearestPower( int [] arr)
{ // Stores the previous
// and next element
int prev = arr[arr.Length - 1];
int lastNext = arr[0];
int next = 0;
// Traverse the array
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];
// Calculate nearest power for
// previous and next elements
int prevPow = nearestPow(arr[i], prev);
int nextPow = nearestPow(arr[i], next);
// Replacing the array values
if (Math.Abs(arr[i] - prevPow) <
Math.Abs(arr[i] - nextPow))
arr[i] = prevPow;
else
arr[i] = nextPow;
prev = temp;
}
// Print the updated array
for ( int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " " );
} // Driver Code public static void Main()
{ // Given array
int [] arr = { 2, 3, 4, 1, 2 };
replacebyNearestPower(arr);
} } // This code is contributed by ukasp |
<script> // JavaScript program for the above approach // Function to calculate the power // of y which is nearest to x function nearestPow(x, y) {
// Base Case
if (y == 1)
return 1
// Stores the logarithmic
// value of x with base y
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 to replace each array // element by the nearest power of // its previous or next element function replacebyNearestPower(arr) {
// Stores the previous
// and next element
var prev = arr[arr.length -1]
var lastNext = arr[0]
// Traverse the array
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]
// Calculate nearest power for
// previous and next elements
var prevPow = nearestPow(arr[i], prev)
var nextPow = nearestPow(arr[i], next)
// Replacing the array values
if (Math.abs(arr[i]-prevPow) <
Math.abs(arr[i]-nextPow))
arr[i] = prevPow
else
arr[i] = nextPow
prev = temp
}
// Print the updated array
document.write(arr)
} // Driver Code // Given array var arr = [2, 3, 4, 1, 2]
replacebyNearestPower(arr) // This code is contributed by AnkThon </script> |
[2, 4, 3, 1, 2]
Time Complexity: O(N)
Auxiliary Space: O(1)