Replace all array elements with the nearest power of its previous element
Last Updated :
16 Apr, 2021
Given a circular array arr[] consisting of N integers, the task is to replace all the array elements with the nearest possible power of its previous adjacent element
Examples:
Input: arr[] = {2, 4, 6, 3, 11}
Output: 1 4 4 6 9
Explanation:
Power of 11 which is nearest to 2 —> 110 = 1
Power of 2 which is nearest to 4 —> 22 = 4
Power of 4 which is nearest to 6 —> 41 = 4
Power of 6 which is nearest to 3 —> 61 = 6
Power of 3 which is nearest to 11—> 32 = 9
Input: arr[] = {3, 2, 4, 3}
Output: 3 3 4 4
Explanation:
Power of 3 which is nearest to 3 —> 31 = 3
Power of 3 which is nearest to 2 —> 31 = 3
Power of 2 which is nearest to 4 —> 22 = 4
Power of 4 which is nearest to 3 —> 41 = 4
Approach: The idea to solve this problem is to traverse the array and for each array element arr[i], find the power of the previous element, say X, which is nearest to arr[i], i.e. XK which is closest to arr[i]. Follow the steps:
- Calculate the value of K, which is equal to the floor value of logx(Y).
- Therefore, K and (K + 1) will be the two integers for which the power could be nearest.
- Calculate XK and X(K + 1) and check which is nearer to Y. Then, print that value.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
using namespace std;
int LOG( int a, int b) {
return log (a) / log (b);
}
void repbyNP( int *arr, int n)
{
int x = arr[n - 1];
for ( int i = 0; i < n; i++)
{
int k = LOG(arr[i], x);
int temp = arr[i];
if ( abs ( pow (x,k) - arr[i]) < abs ( pow (x,k+1) - arr[i]))
arr[i] = pow (x, k);
else
arr[i] = pow (x, k + 1);
x = temp;
}
}
int main()
{
int arr[5] = {2, 4, 6, 3, 11};
int n = sizeof (arr)/ sizeof (arr[0]);
repbyNP(arr,n);
for ( int i = 0; i < n; i++)
cout<<arr[i]<< " " ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int LOG( int a, int b) {
return ( int )(Math.log(a) / Math.log(b));
}
static void repbyNP( int [] arr, int n)
{
int x = arr[n - 1 ];
for ( int i = 0 ; i < n; i++)
{
int k = LOG(arr[i], x);
int temp = arr[i];
if (Math.abs(Math.pow(x,k) - arr[i]) < Math.abs(Math.pow(x,k+ 1 ) - arr[i]))
arr[i] = ( int )Math.pow(x, k);
else
arr[i] = ( int )Math.pow(x, k + 1 );
x = temp;
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 6 , 3 , 11 };
int n = arr.length;
repbyNP(arr,n);
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
import math
def LOG(x, base):
return int (math.log(x) / math.log(base))
def repbyNP(arr):
x = arr[ - 1 ]
for i in range ( len (arr)):
k = LOG(arr[i], x)
temp = arr[i]
if abs (x * * k - arr[i]) < abs (x * * (k + 1 ) - arr[i]):
arr[i] = x * * k
else :
arr[i] = x * * (k + 1 )
x = temp
return arr
arr = [ 2 , 4 , 6 , 3 , 11 ]
print (repbyNP(arr))
|
C#
using System;
class GFG{
static int LOG( int a, int b) {
return ( int )(Math.Log(a) / Math.Log(b));
}
static void repbyNP( int [] arr, int n)
{
int x = arr[n - 1];
for ( int i = 0; i < n; i++)
{
int k = LOG(arr[i], x);
int temp = arr[i];
if (Math.Abs(Math.Pow(x, k) - arr[i]) <
Math.Abs(Math.Pow(x, k + 1) - arr[i]))
arr[i] = ( int )Math.Pow(x, k);
else
arr[i] = ( int )Math.Pow(x, k + 1);
x = temp;
}
}
public static void Main( string [] args)
{
int [] arr = {2, 4, 6, 3, 11};
int n = arr.Length;
repbyNP(arr,n);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
<script>
function LOG(a, b) {
return parseInt(Math.log(a) / Math.log(b));
}
function repbyNP(arr, n)
{
let x = arr[n - 1];
for (let i = 0; i < n; i++)
{
let k = LOG(arr[i], x);
let temp = arr[i];
if (Math.abs(Math.pow(x, k) - arr[i]) < Math.abs(Math.pow(x, k + 1) - arr[i]))
arr[i] = Math.pow(x, k);
else
arr[i] = Math.pow(x, k + 1);
x = temp;
}
}
let arr = [2, 4, 6, 3, 11];
let n = arr.length;
repbyNP(arr, n);
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...