📌  相关文章
📜  将所有数组元素替换为其上一个元素的最近幂

📅  最后修改于: 2021-04-17 16:25:23             🧑  作者: Mango

给定一个由N个整数组成的圆形数组arr [] ,任务是将所有数组元素替换为其上一个相邻元素的最接近可能的幂

例子:

方法:解决此问题的想法是遍历数组,并为每个数组元素arr [i]找到最靠近arr [i]的元素X的幂,例如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)