Find the absolute difference between the nearest powers of two given integers for every array element
Last Updated :
06 Jul, 2021
Given an array arr[] consisting of N positive integers and two positive integers A and B, the task is to replace each array element with the absolute difference of the nearest powers of A and B. If there exists two nearest powers, then choose the maximum of the two.
Examples:
Input: arr[] = {5, 12, 25}, A = 2, B = 3
Output: 1 7 5
Explanation:
- For element arr[0]( = 5): The nearest power of 2 is 4 and the nearest power of 3 is 3. Therefore, the absolute difference of 4 and 3 is 1.
- For arr[1]( = 12): The nearest power of 2 is 16 and the nearest power of 3 is 9. Therefore, the absolute difference of 16 and 9 is 7.
- For arr[2]( = 5): The nearest power of 2 is 32 and the nearest power of 3 is 27. Therefore, the absolute difference of 27 and 32 is 5.
Therefore, the modified array is {1, 7, 5}.
Input: arr[] = {32, 3, 7}, a = 2, b = 3
Output: 0 1 1
Approach: The given problem can be solved by finding both the nearest power of A and B for every array element and update it to the absolute difference of the two values obtained.
Follow the steps to solve the problem
- Traverse the given array arr[] and perform the following steps:
- Find the two values that are the perfect powers of a less than and greater than arr[i] and find the closest of the two values.
- Find the two values that are the perfect powers of b less than and greater than arr[i] and find the closest of the two values.
- Find the absolute difference between the two closest values obtained and update arr[i] with it.
- After completing the above steps, print the modified array arr[].
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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 print the 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
<script>
// JavaScript implementation of the above approach
// Function to print the array
function printArray(arr, N)
{
// Traverse the array
for(let i = 0; i < N; i++)
{
document.write(arr[i] + " ");
}
}
// Function to modify array elements
// by absolute difference of the
// nearest perfect power of a and b
function nearestPowerDiff(arr, N,
a, b)
{
// Traverse the array arr[]
for(let i = 0; i < N; i++)
{
// Find the log a of arr[i]
let log_a = Math.floor(Math.log(arr[i]) /
Math.log(a));
// Find the power of a less
// than and greater than a
let A = Math.floor(Math.pow(a, log_a));
let B = Math.floor(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]
let log_b = Math.floor(Math.log(arr[i]) /
Math.log(b));
// Find the power of b less than
// and greater than b
A = Math.floor(Math.pow(b, log_b));
B = Math.floor(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
let arr = [ 5, 12, 25 ];
let A = 2, B = 3;
let N = arr.length;
nearestPowerDiff(arr, N, A, B);
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find Maximized difference between two elements for every index of given Arrays Given two arrays A[] and B[] of the same length N. the task is to find a pair (X, Y) for two numbers Ai (number at i index of array A) and Bi(number at i index of array B) such that the value of |X-Y| is maximized for each index and the pair satisfies the following conditions: 1 ⤠X, Y ⤠Bigcd(X, Y)
6 min read
Queries to find minimum absolute difference between adjacent array elements in given ranges Given an array arr[] consisting of N integers and an array query[] consisting of queries of the form {L, R}, the task for each query is to find the minimum of the absolute difference between adjacent elements over the range [L, R]. Examples: Input: arr[] = {2, 6, 1, 8, 3, 4}, query[] = {{0, 3}, {1,
15+ min read
Find minimum difference between any two elements (pair) in given array Given an unsorted array, find the minimum difference between any pair in the given array. Examples : Input: {1, 5, 3, 19, 18, 25}Output: 1Explanation: Minimum difference is between 18 and 19 Input: {30, 5, 20, 9}Output: 4Explanation: Minimum difference is between 5 and 9 Input: {1, 19, -4, 31, 38, 2
15+ min read
Find the nearest power of 2 for every array element Given an array arr[] of size N, the task is to print the nearest power of 2 for each array element. Note: If there happens to be two nearest powers of 2, consider the larger one. Examples: Input: arr[] = {5, 2, 7, 12} Output: 4 2 8 16 Explanation: The nearest power of arr[0] ( = 5) is 4. The nearest
4 min read
Minimize sum of absolute differences between given Arrays by replacing at most 1 element from first Array Given two arrays a[] and b[] of equal size. The task is to calculate the minimum sum of absolute differences for each index. It is allowed to replace at most 1 element of the first array with any value from the first array itself. Example: Input: a[] = {1, 9, 4, 2}, b[] = {2, 3, 7, 1}Output: 6Explan
9 min read