给定一个由N 个正整数组成的圆形数组arr[] ,任务是通过用其前一个或下一个数组元素的最近幂替换每个数组元素来修改数组。
例子:
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}
方法:思想是遍历数组,用其上一个元素或下一个数组元素的最近幂替换每个数组元素。
请按照以下步骤解决此问题:
- 遍历数组arr[]并执行以下步骤:
- 找出X K最接近Y的K值。
- 计算K 时,取log x (Y) 的下限值。
- 因此, K和K + 1将是最近幂最接近的两个整数。
- 计算Y K和Y (K + 1)并检查哪个最接近X并使用最接近的值更新数组元素arr[i] 。
- 完成上述步骤后,将数组arr[]打印为修改后的数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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 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 arr{ 2, 3, 4, 1, 2 };
replacebyNearestPower(arr);
}
// This code is contributed by ipg2016107
Java
// 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
# 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#
// 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
Javascript
[2, 4, 3, 1, 2]
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live