给定一个由N 个正整数组成的数组arr[] ,任务是用GCD的最近幂替换每个数组元素 所有前面的数组元素。如果存在多个可能的答案,则打印其中任何一个。
例子:
Input: arr[] = {4, 2, 8, 2}
Output: 4 1 8 2
Explanation:
For element arr[0], the element remains the same.
For element arr[1], the GCD of preceding elements is 4. The power of 4 which is nearest to 2 is 1.
For element arr[2], the GCD of preceding elements is 2. The power of 2 which is nearest to 8 is 8.
For element arr[3], the GCD of preceding elements is 2. The power of 2 which is nearest to 2 is 2.
Hence, the modified array becomes {4, 1, 8, 2}.
Input: arr[] = {3, 6, 9, 2}
Output: 3 9 9 3
方法:给定的问题可以通过计算前缀GCD,然后为每个数组元素找到最接近当前元素的GCD的最近幂来解决。以下是一些观察结果:
- 为了计算最接近Y的X的幂,想法是获得K的值,使得X K和Y之间的绝对差最小。
- 要找到K的值,请找到log x (Y) 的下限值。
- 因此, K和(K + 1)将是最接近的幂可以作为可能值的两个整数。
请按照以下步骤解决问题:
- 用arr[0]初始化一个变量,比如prefixGCD ,以存储前缀 GCD直到数组的每个索引。
- 在索引[1, N]的范围内遍历给定数组arr[ ]并执行以下步骤:
- 找到log prefixGCD (arr[i]) 的下限值,比如K 。
- 找到(arr[i] K )和(arr[i] K + 1 ) 的值并检查哪个最接近arr[i]并将该值分配给数组的当前索引。
- 将prefixGCD更新为prefixGCD和arr[i]的 gcd。
- 完成上述步骤后,打印修改后的数组。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function to find the float
// value of log function
int log1(int x, int base)
{
return int(log(x) / log(base));
}
// Function for finding the nearest
// power of X with respect to Y
int getNP(int x, int y)
{
// Base Case
if (y == 1)
return 1;
// Find the value of K
int k = int(log1(x, y));
// Nearest power of GCD closest to Y
if (abs(pow(y, k) - x) < abs(pow(y, (k + 1)) - x))
return pow(y, k);
return pow(y, (k + 1));
}
// Function to modify the given array
// such that each array element is the
// nearest power of X with respect to Y
vector modifyEle(vector arr)
{
int prevGCD = arr[0];
// Traverse the array
for (int i = 1; i < arr.size(); i++)
{
// Find the current number
int NP = getNP(arr[i], prevGCD);
// Update the GCD
prevGCD = __gcd(arr[i], prevGCD);
// Update the array at the
// current index
arr[i] = NP;
}
// Return the updated GCD array
return arr;
}
// Driver Code
int main()
{
vectorarr{4, 2, 8, 2};
// Function Call
vector ans = modifyEle(arr);
cout<<"[";
for(int i = 0; i < ans.size(); i++)
if(i < ans.size() - 1)
cout << ans[i] << ", ";
else
cout << ans[i];
cout << "]";
}
// This code is contributed by SURENDRA_GANGWAR.
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// gcd
static int gcd(int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
// Function to find the float
// value of log function
static int log1(int x, int b)
{
return (int)(Math.log(x) / Math.log(b));
}
// Function for finding the nearest
// power of X with respect to Y
static int getNP(int x, int y)
{
// Base Case
if (y == 1)
return 1;
// Find the value of K
int k = (int)(log1(x, y));
// Nearest power of GCD closest to 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 modify the given array
// such that each array element is the
// nearest power of X with respect to Y
static ArrayList modifyEle(ArrayList arr)
{
int prevGCD = arr.get(0);
// Traverse the array
for (int i = 1; i < arr.size(); i++) {
// Find the current number
int NP = getNP(arr.get(i), prevGCD);
// Update the GCD
prevGCD = gcd(arr.get(i), prevGCD);
// Update the array at the
// current index
arr.set(i, NP);
}
// Return the updated GCD array
return arr;
}
// Driver Code
public static void main(String[] args)
{
ArrayList arr = new ArrayList() ;
arr.add(4);
arr.add(2);
arr.add(8);
arr.add(2);
// Function Call
ArrayList ans = new ArrayList();
ans = modifyEle(arr);
System.out.print("[");
for (int i = 0; i < ans.size(); i++)
if (i < ans.size() - 1)
System.out.print(ans.get(i) + ", ");
else
System.out.print(ans.get(i));
System.out.print("]");
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python program for the above approach
import math
# Function to find the float
# value of log function
def LOG(x, base):
return int(math.log(x)/math.log(base))
# Function for finding the nearest
# power of X with respect to Y
def getNP(x, y):
# Base Case
if y == 1:
return 1
# Find the value of K
k = int(math.log(x, y))
# Nearest power of GCD closest to Y
if abs(y**k - x) < abs(y**(k + 1) - x):
return y**k
return y**(k + 1)
# Function to find the GCD of a and b
def GCD(a, b):
# Base Case
if b == 0:
return a
# Recursively calculate GCD
return GCD(b, a % b)
# Function to modify the given array
# such that each array element is the
# nearest power of X with respect to Y
def modifyEle(arr):
# Stores the prefix GCD
prevGCD = arr[0]
# Traverse the array
for i in range(1, len(arr)):
# Find the current number
NP = getNP(arr[i], prevGCD)
# Update the GCD
prevGCD = GCD(arr[i], prevGCD)
# Update the array at the
# current index
arr[i] = NP
# Return the updated GCD array
return arr
# Driver Code
arr = [4, 2, 8, 2]
# Function Call
print(modifyEle(arr))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// gcd
static int gcd(int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
// Function to find the float
// value of log function
static int log1(int x, int b)
{
return (int)(Math.Log(x) / Math.Log(b));
}
// Function for finding the nearest
// power of X with respect to Y
static int getNP(int x, int y)
{
// Base Case
if (y == 1)
return 1;
// Find the value of K
int k = (int)(log1(x, y));
// Nearest power of GCD closest to 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 modify the given array
// such that each array element is the
// nearest power of X with respect to Y
static List modifyEle(List arr)
{
int prevGCD = arr[0];
// Traverse the array
for (int i = 1; i < arr.Count; i++) {
// Find the current number
int NP = getNP(arr[i], prevGCD);
// Update the GCD
prevGCD = gcd(arr[i], prevGCD);
// Update the array at the
// current index
arr[i] = NP;
}
// Return the updated GCD array
return arr;
}
// Driver Code
public static void Main(string[] args)
{
List arr = new List() { 4, 2, 8, 2 };
// Function Call
List ans = new List();
ans = modifyEle(arr);
Console.Write("[");
for (int i = 0; i < ans.Count; i++)
if (i < ans.Count - 1)
Console.Write(ans[i] + ", ");
else
Console.Write(ans[i]);
Console.Write("]");
}
}
// This code is contributed by chitranayal.
Javascript
[4, 1, 8, 2]
时间复杂度: O(N*log(M)),其中M是数组的最大元素。
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。