📌  相关文章
📜  通过用上一个或下一个元素的最近幂替换元素来修改数组

📅  最后修改于: 2021-05-17 17:45:47             🧑  作者: Mango

给定一个由N个正整数组成的圆形数组arr [] ,任务是通过用其上一个或下一个数组元素的最近幂替换每个数组元素来修改数组。

例子:

方法:想法是遍历数组,并用其上一个或下一个数组元素的最近幂替换每个数组元素。
请按照以下步骤解决此问题:

  • 遍历数组arr []并执行以下步骤:
    • 找到X K最接近YK的值。
    • 为了计算K ,取对x (Y)的底值
    • 因此, KK + 1将是最接近幂的两个整数。
    • 计算Y KY (K +1),并检查最接近X的值,并使用最接近的值更新数组元素arr [i]
  • 完成上述步骤后,将数组arr []打印为修改后的array。

下面是上述方法的实现:

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)