给定一个由N 个整数组成的圆形数组arr[] ,任务是用其前一个相邻元素的最接近的可能幂替换所有数组元素
例子:
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
方法:解决这个问题的思路是遍历数组,对于每个数组元素arr[i] ,找到前一个元素的幂,比如X ,它最接近arr[i] ,即X K最接近arr[i] .按照步骤:
- 计算K的值,它等于log x (Y) 的下限值。
- 因此, K和(K + 1)将是幂最接近的两个整数。
- 计算X K 和X (K + 1)并检查哪个更接近Y 。然后,打印该值。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
#include
using namespace std;
// Function to calculate log x for given base
int LOG(int a, int b) {
return log(a) / log(b);
}
// Function to replace all array
// elements with the nearest power
// of previous adjacent nelement
void repbyNP(int *arr,int n)
{
// For the first element, set the last
// array element to its previous element
int x = arr[n - 1];
// Traverse the array
for(int i = 0; i < n; i++)
{
// Find K for which x ^ k is
// nearest to arr[i]
int k = LOG(arr[i], x);
int temp = arr[i];
// Find the power to x nearest to 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);
// Update x
x = temp;
}
}
int main()
{
// Driver Code
int arr[5] = {2, 4, 6, 3, 11};
int n = sizeof(arr)/sizeof(arr[0]);
// Function Call
repbyNP(arr,n);
// Display the array
for(int i = 0; i < n; i++)
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to calculate log x for given base
static int LOG(int a, int b) {
return (int)(Math.log(a) / Math.log(b));
}
// Function to replace all array
// elements with the nearest power
// of previous adjacent nelement
static void repbyNP(int[] arr,int n)
{
// For the first element, set the last
// array element to its previous element
int x = arr[n - 1];
// Traverse the array
for(int i = 0; i < n; i++)
{
// Find K for which x ^ k is
// nearest to arr[i]
int k = LOG(arr[i], x);
int temp = arr[i];
// Find the power to x nearest to 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);
// Update x
x = temp;
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {2, 4, 6, 3, 11};
int n = arr.length;
// Function Call
repbyNP(arr,n);
// Display the array
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by sanjoy_62.
Python3
# Python3 program for the above approach
import math
# Function to calculate log x for given base
def LOG(x, base):
return int(math.log(x)/math.log(base))
# Function to replace all array
# elements with the nearest power
# of previous adjacent nelement
def repbyNP(arr):
# For the first element, set the last
# array element to its previous element
x = arr[-1]
# Traverse the array
for i in range(len(arr)):
# Find K for which x ^ k is
# nearest to arr[i]
k = LOG(arr[i], x)
temp = arr[i]
# Find the power to x nearest to arr[i]
if abs(x**k - arr[i]) < abs(x**(k + 1) - arr[i]):
arr[i] = x**k
else:
arr[i] = x**(k + 1)
# Update x
x = temp
# Return the array
return arr
# Driver Code
arr = [2, 4, 6, 3, 11]
# Function Call
print(repbyNP(arr))
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate log x for given base
static int LOG(int a, int b) {
return (int)(Math.Log(a) / Math.Log(b));
}
// Function to replace all array
// elements with the nearest power
// of previous adjacent nelement
static void repbyNP(int[] arr,int n)
{
// For the first element, set the last
// array element to its previous element
int x = arr[n - 1];
// Traverse the array
for(int i = 0; i < n; i++)
{
// Find K for which x ^ k is
// nearest to arr[i]
int k = LOG(arr[i], x);
int temp = arr[i];
// Find the power to x nearest to 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);
// Update x
x = temp;
}
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = {2, 4, 6, 3, 11};
int n = arr.Length;
// Function Call
repbyNP(arr,n);
// Display the array
for(int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by code_hunt.
Javascript
[1, 4, 4, 1, 9]
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live