Replace array elements that contains K as a digit with the nearest power of K
Last Updated :
28 Jun, 2021
Given an array arr[] of size N and an integer K, the task is to replace every array element consisting of K as a digit, with its nearest power of K.
Note: If there happen to be two nearest powers then take the greater one.
Examples:
Input: arr[] = {432, 953, 232, 333}, K = 3
Output: {243, 729, 243, 243}
Explanation:
arr[0] = 35 = 243.
arr[1] = 36 = 729.
arr[2] = 35 = 243.
arr[3] = 35 = 243.
Input: arr[] = {532, 124, 244, 485}, K = 4
Output: {532, 64, 256, 256}
Approach: The idea is to convert each element of the array into a string and then search for K in the string and if found then replace it with the nearest power of K. Follow the steps below to solve the problem:
- Declare a function to find the nearest power of K:
- Find the value of p for which Xp will be closest to the element.
- For calculating the p takes the floor value of logX(Element).
- Therefore, p and p+1 will be the two integers for which the nearest power can be obtained.
- Calculate Xk and X(K + 1) and check which is nearer to the element and return that element.
- Traverse the array arr[]:
- Convert each element into a string.
- Traverse the string and check for the presence of digit K and if found, then replace the array element with the nearest power of K and break from that point.
- Print the modified array.
Below is the implementation of the above approach:
C++
// C++program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the
// power of base nearest to x
int nearestPow(int x, int base)
{
// Stores logX to the base K
int k = int(log(x) / log(base));
if (abs(pow(base, k) - x) < abs(pow(base, (k + 1)) - x))
return pow(base, k);
else
return pow(base, (k + 1));
}
// Function to replace array
// elements with nearest power of K
void replaceWithNearestPowerOfK(int arr[], int K, int n)
{
// Traverse the array
for (int i = 0; i < n; i++) {
// Convert integer into a string
string strEle = to_string(arr[i]);
for (int c = 0; c < strEle.length(); c++) {
// If K is found, then replace
// with the nearest power of K
if ((strEle[c]-'0') == K) {
arr[i] = nearestPow(arr[i], K);
break;
}
}
}
// Print the array
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver Code
int main()
{
// Given array
int arr[] = { 432, 953, 232, 333 };
int n = sizeof(arr) / sizeof(arr[0]);
// Given value of K
int K = 3;
// Function call to replace array
// elements with nearest power of K
replaceWithNearestPowerOfK(arr, K, n);
}
// This code is contributed by ukasp.
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate the
// power of base nearest to x
static int nearestPow(int x, int base1)
{
// Stores logX to the base K
int k = (int)(Math.log(x) / Math.log(base1));
if (Math.abs(Math.pow(base1, k) - x) <
Math.abs(Math.pow(base1, (k + 1)) - x))
return (int)Math.pow(base1, k);
else
return (int)Math.pow(base1, (k + 1));
}
// Function to replace array
// elements with nearest power of K
static void replaceWithNearestPowerOfK(int []arr, int K,
int n)
{
// Traverse the array
for(int i = 0; i < n; i++)
{
// Convert integer into a string
int num = arr[i];
String strEle = String.valueOf(num);
for(int c = 0; c < strEle.length(); c++)
{
// If K is found, then replace
// with the nearest power of K
if ((strEle.charAt(c) - '0') == K)
{
arr[i] = nearestPow(arr[i], K);
break;
}
}
}
// Print the array
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver Code
public static void main (String[] args)
{
// Given array
int []arr = { 432, 953, 232, 333 };
int n = arr.length;
// Given value of K
int K = 3;
// Function call to replace array
// elements with nearest power of K
replaceWithNearestPowerOfK(arr, K, n);
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program
# for the above approach
import math
# Function to calculate the
# power of base nearest to x
def nearestPow(x, base):
# Stores logX to the base K
k = int(math.log(x, base))
if abs(base**k - x) < abs(base**(k+1) - x):
return base**k
else:
return base**(k+1)
# Function to replace array
# elements with nearest power of K
def replaceWithNearestPowerOfK(arr, K):
# Traverse the array
for i in range(len(arr)):
# Convert integer into a string
strEle = str(arr[i])
for c in strEle:
# If K is found, then replace
# with the nearest power of K
if int(c) == K:
arr[i] = nearestPow(arr[i], K)
break
# Print the array
print(arr)
# Driver Code
# Given array
arr = [432, 953, 232, 333]
# Given value of K
K = 3
# Function call to replace array
# elements with nearest power of K
replaceWithNearestPowerOfK(arr, K)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate the
// power of base nearest to x
static int nearestPow(int x, int base1)
{
// Stores logX to the base K
int k = (int)(Math.Log(x) / Math.Log(base1));
if (Math.Abs(Math.Pow(base1, k) - x) < Math.Abs(Math.Pow(base1, (k + 1)) - x))
return (int)Math.Pow(base1, k);
else
return (int)Math.Pow(base1, (k + 1));
}
// Function to replace array
// elements with nearest power of K
static void replaceWithNearestPowerOfK(int []arr, int K, int n)
{
// Traverse the array
for (int i = 0; i < n; i++) {
// Convert integer into a string
int num = arr[i];
string strEle = num.ToString();
for (int c = 0; c < strEle.Length; c++) {
// If K is found, then replace
// with the nearest power of K
if ((strEle[c]-'0') == K) {
arr[i] = nearestPow(arr[i], K);
break;
}
}
}
// Print the array
for (int i = 0; i < n; i++)
Console.Write(arr[i]+" ");
}
// Driver Code
public static void Main()
{
// Given array
int []arr = { 432, 953, 232, 333 };
int n = arr.Length;
// Given value of K
int K = 3;
// Function call to replace array
// elements with nearest power of K
replaceWithNearestPowerOfK(arr, K, n);
}
}
// This code is contributed by ipg2016107.
JavaScript
<script>
// Javascript program for the
// above approach
// Function to calculate the
// power of base nearest to x
function nearestPow( x, base)
{
// Stores logX to the base K
let k =
Math.floor(Math.log(x) / Math.log(base));
if (Math.abs(Math.pow(base, k) - x) <
Math.abs(Math.pow(base, (k + 1)) - x))
return Math.pow(base, k);
else
return Math.pow(base, (k + 1));
}
// Function to replace array
// elements with nearest power of K
function replaceWithNearestPowerOfK( arr, K, n)
{
// Traverse the array
for (let i = 0; i < n; i++) {
// Convert integer into a string
let strEle = arr[i].toString();
for (let c = 0; c < strEle.length; c++)
{
// If K is found, then replace
// with the nearest power of K
if ((strEle[c] - '0') == K) {
arr[i] = nearestPow(arr[i], K);
break;
}
}
}
// Print the array
for (let i = 0; i < n; i++)
document.write(arr[i] + " ")
}
// Driver Code
// Given array
let arr = [ 432, 953, 232, 333 ];
let n = arr.length;
// Given value of K
let K = 3;
// Function call to replace array
// elements with nearest power of K
replaceWithNearestPowerOfK(arr, K, n)
// This code is contributed by Hritik
</script>
Output: [243, 729, 243, 243]
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Replace all array elements with the nearest power of its previous element Given a circular array arr[] consisting of N integers, the task is to replace all the array elements with the nearest possible power of its previous adjacent element Examples: Input: arr[] = {2, 4, 6, 3, 11}Output: 1 4 4 6 9 Explanation:Power of 11 which is nearest to 2 ---> 110 = 1Power of 2 whi
6 min read
Modify array by replacing elements with the nearest power of its previous or next element Given a circular array arr[] consisting of N positive integers, the task is to modify the array by replacing each array element with the nearest power of its previous or next array element. Examples: Input: arr[] = {2, 3, 4, 1, 2}Output: {2, 4, 3, 1, 2}Explanation:For arr[0](= 2): The previous and t
8 min read
Modify array by replacing each element with nearest power of GCD of all previous elements Given an array arr[] consisting of N positive integers, the task is to replace each array element with the nearest power of GCD of all the preceding array elements. If there exists more than one possible answer, then print any one of them. Examples: Input: arr[] = {4, 2, 8, 2}Output: 4 1 8 2Explanat
9 min read
Find nth number that contains the digit k or divisible by k. You have given two number n and k. You need to find the n-th number that contains the digit k or divisible by k (2 <= k <=9 ).Examples: Input : n = 15, k = 3 Output : 33 Explanation : ( 3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 33 ). These are those number who contain the digit k = 3 or
7 min read
Find minimum changes required in an array for it to contain k distinct elements Given an array arr of size N and a number K. The task is to find the minimum elements to be replaced in the array with any number such that the array consists of K distinct elements.Note: The array might consist of repeating elements. Examples: Input : arr[]={1, 2, 2, 8}, k = 1 Output : 2 The elemen
6 min read