📌  相关文章
📜  找到每个数组元素的两个给定整数的最接近幂之间的绝对差

📅  最后修改于: 2021-05-14 07:48:14             🧑  作者: Mango

给定由N个正整数和两个正整数AB组成的数组arr [] ,任务是用AB的最接近幂的绝对差替换每个数组元素。如果存在两个最接近的幂,则选择两者中的最大值。

例子:

方法:给定问题可以通过找到每个数组元素的AB的最近幂并将其更新为获得的两个值的绝对差来解决。
请按照以下步骤解决问题

  • 遍历给定数组arr []并执行以下步骤:
    • 查找这两个值是一个小于和大于改编[I]完美的权力,并找到最接近的两个值。
    • 找到两个小于b且大于arr [i]b的完美幂的值,并找到两个值中最接近的值。
    • 找到获得的两个最接近的值之间的绝对差,并使用它更新arr [i]
  • 完成上述步骤后,打印修改后的数组arr []

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the array
void printArray(int arr[], int N)
{
    // Traverse the array
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Function to modify array elements
// by absolute difference of the
// nearest perfect power of a and b
void nearestPowerDiff(int arr[], int N,
                      int a, int b)
{
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Find the log a of arr[i]
        int log_a = log(arr[i]) / log(a);
 
        // Find the power of a less
        // than and greater than a
        int A = pow(a, log_a);
        int B = pow(a, log_a + 1);
 
        if ((arr[i] - A) < (B - arr[i]))
            log_a = A;
        else
            log_a = B;
 
        // Find the log b of arr[i]
        int log_b = log(arr[i]) / log(b);
 
        // Find the power of b less than
        // and greater than b
        A = pow(b, log_b);
        B = pow(b, log_b + 1);
 
        if ((arr[i] - A) < (B - arr[i]))
            log_b = A;
        else
            log_b = B;
 
        // Update arr[i] with absolute
        // difference of log_a & log _b
        arr[i] = abs(log_a - log_b);
    }
 
    // Print the modified array
    printArray(arr, N);
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 12, 25 };
    int A = 2, B = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    nearestPowerDiff(arr, N, A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to print the array
static void printArray(int[] arr, int N)
{
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        System.out.print(arr[i] + " ");
    }
}
 
// Function to modify array elements
// by absolute difference of the
// nearest perfect power of a and b
static void nearestPowerDiff(int[] arr, int N,
                             int a, int b)
{
     
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // Find the log a of arr[i]
        int log_a = (int)(Math.log(arr[i]) /
                          Math.log(a));
 
        // Find the power of a less
        // than and greater than a
        int A = (int)(Math.pow(a, log_a));
        int B = (int)(Math.pow(a, log_a + 1));
 
        if ((arr[i] - A) < (B - arr[i]))
            log_a = A;
        else
            log_a = B;
 
        // Find the log b of arr[i]
        int log_b = (int)(Math.log(arr[i]) /
                          Math.log(b));
 
        // Find the power of b less than
        // and greater than b
        A = (int)(Math.pow(b, log_b));
        B = (int)(Math.pow(b, log_b + 1));
 
        if ((arr[i] - A) < (B - arr[i]))
            log_b = A;
        else
            log_b = B;
 
        // Update arr[i] with absolute
        // difference of log_a & log _b
        arr[i] = Math.abs(log_a - log_b);
    }
 
    // Print the modified array
    printArray(arr, N);
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 5, 12, 25 };
    int A = 2, B = 3;
    int N = arr.length;
 
    nearestPowerDiff(arr, N, A, B);
}
}
 
// This code is contributed by subham348


Python3
# Python3 program for the above approach
import math
 
# Function to prthe array
def printArray(arr, N):
     
    # Traverse the array
    for i in range(N):
        print(arr[i], end = " ")
     
# Function to modify array elements
# by absolute difference of the
# nearest perfect power of a and b
def nearestPowerDiff(arr, N, a, b):
     
    # Traverse the array arr[]
    for i in range(N):
 
        # Find the log a of arr[i]
        log_a = int(math.log(arr[i]) /
                    math.log(a))
 
        # Find the power of a less
        # than and greater than a
        A = int(pow(a, log_a))
        B = int(pow(a, log_a + 1))
 
        if ((arr[i] - A) < (B - arr[i])):
            log_a = A
        else:
            log_a = B
 
        # Find the log b of arr[i]
        log_b = int(math.log(arr[i]) /
                    math.log(b))
 
        # Find the power of b less than
        # and greater than b
        A = int(pow(b, log_b))
        B = int(pow(b, log_b + 1))
 
        if ((arr[i] - A) < (B - arr[i])):
            log_b = A
        else:
            log_b = B
 
        # Update arr[i] with absolute
        # difference of log_a & log _b
        arr[i] = abs(log_a - log_b)
     
    # Print the modified array
    printArray(arr, N)
 
# Driver Code
arr = [ 5, 12, 25 ]
A = 2
B = 3
N = len(arr)
 
nearestPowerDiff(arr, N, A, B)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Function to print the array
static void printArray(int[] arr, int N)
{
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Function to modify array elements
// by absolute difference of the
// nearest perfect power of a and b
static void nearestPowerDiff(int[] arr, int N,
                             int a, int b)
{
     
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // Find the log a of arr[i]
        int log_a = (int)(Math.Log(arr[i]) /
                          Math.Log(a));
 
        // Find the power of a less
        // than and greater than a
        int A = (int)(Math.Pow(a, log_a));
        int B = (int)(Math.Pow(a, log_a + 1));
 
        if ((arr[i] - A) < (B - arr[i]))
            log_a = A;
        else
            log_a = B;
 
        // Find the log b of arr[i]
        int log_b = (int)(Math.Log(arr[i]) /
                          Math.Log(b));
 
        // Find the power of b less than
        // and greater than b
        A = (int)(Math.Pow(b, log_b));
        B = (int)(Math.Pow(b, log_b + 1));
 
        if ((arr[i] - A) < (B - arr[i]))
            log_b = A;
        else
            log_b = B;
 
        // Update arr[i] with absolute
        // difference of log_a & log _b
        arr[i] = Math.Abs(log_a - log_b);
    }
 
    // Print the modified array
    printArray(arr, N);
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 5, 12, 25 };
    int A = 2, B = 3;
    int N = arr.Length;
 
    nearestPowerDiff(arr, N, A, B);
}
}
 
// This code is contributed by AnkThon


Javascript


输出:
1 7 5

时间复杂度: O(N)
辅助空间: O(1)