给定一个由N 个整数组成的数组A[] ,任务是生成一个数组B[] ,使得B[i]包含A[]中索引j的计数,使得j < i和A[j] % A[i ] = 0
例子:
Input: arr[] = {3, 5, 1}
Output: 0 0 2
Explanation:
3 and 5 do not divide any element on
their left but 1 divides 3 and 5.
Input: arr[] = {8, 1, 28, 4, 2, 6, 7}
Output: 0 1 0 2 3 0 1
朴素的方法:这里已经讨论了这种方法。但是这种方法的复杂度是 O(N 2 )。
有效的方法:
- 我们可以说,如果数字 A 除以数字 B,那么 A 是 B 的因数。因此,我们需要找到因数为当前元素的先前元素的数量。
- 我们将维护一个计数数组,其中包含每个元素的因子计数。
- 现在,遍历数组,并为每个元素
- 使当前元素的答案等于 count [ arr[i] ] 和
- 增加计数数组中每个 arr[i] 因子的频率。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Utility function to print the
// elements of the array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Function to increment the count
// for each factor of given val
void IncrementFactors(int count[],
int val)
{
for (int i = 1; i * i <= val; i++) {
if (val % i == 0) {
if (i == val / i) {
count[i]++;
}
else {
count[i]++;
count[val / i]++;
}
}
}
}
// Function to generate and print
// the required array
void generateArr(int A[], int n)
{
int B[n];
// Find max element of array
int maxi = *max_element(A, A + n);
// Create count array of maxi size
int count[maxi + 1] = { 0 };
// For every element of the array
for (int i = 0; i < n; i++) {
// Count[ A[i] ] denotes how many
// previous elements are there whose
// factor is the current element.
B[i] = count[A[i]];
// Increment in count array for
// factors of A[i]
IncrementFactors(count, A[i]);
}
// Print the generated array
printArr(B, n);
}
// Driver code
int main()
{
int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
generateArr(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG{
// Utility function to print the
// elements of the array
static void printArr(int arr[], int n)
{
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Function to increment the count
// for each factor of given val
static void IncrementFactors(int count[],
int val)
{
for(int i = 1; i * i <= val; i++)
{
if (val % i == 0)
{
if (i == val / i)
{
count[i]++;
}
else
{
count[i]++;
count[val / i]++;
}
}
}
}
// Function to generate and print
// the required array
static void generateArr(int A[], int n)
{
int []B = new int[n];
// Find max element of array
int maxi = Arrays.stream(A).max().getAsInt();
// Create count array of maxi size
int count[] = new int[maxi + 1];
// For every element of the array
for(int i = 0; i < n; i++)
{
// Count[ A[i] ] denotes how many
// previous elements are there whose
// factor is the current element.
B[i] = count[A[i]];
// Increment in count array for
// factors of A[i]
IncrementFactors(count, A[i]);
}
// Print the generated array
printArr(B, n);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
int n = arr.length;
generateArr(arr, n);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 implementation of the approach
# Utility function to prthe
# elements of the array
def printArr(arr, n):
for i in range(n):
print(arr[i], end = " ")
# Function to increment the count
# for each factor of given val
def IncrementFactors(count, val):
i = 1
while(i * i <= val):
if (val % i == 0):
if (i == val // i):
count[i] += 1
else:
count[i] += 1
count[val // i] += 1
i += 1
# Function to generate and print
# the required array
def generateArr(A, n):
B = [0] * n
# Find max element of arr
maxi = max(A)
# Create count array of maxi size
count = [0] * (maxi + 1)
# For every element of the array
for i in range(n):
# Count[ A[i] ] denotes how many
# previous elements are there whose
# factor is the current element.
B[i] = count[A[i]]
# Increment in count array for
# factors of A[i]
IncrementFactors(count, A[i])
# Print the generated array
printArr(B, n)
# Driver code
arr = [ 8, 1, 28, 4, 2, 6, 7 ]
n = len(arr)
generateArr(arr, n)
# This code is contributed by code_hunt
C#
// C# implementation of the approach
using System;
using System.Linq;
class GFG{
// Utility function to print the
// elements of the array
static void printArr(int []arr, int n)
{
for(int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
// Function to increment the count
// for each factor of given val
static void IncrementFactors(int []count,
int val)
{
for(int i = 1; i * i <= val; i++)
{
if (val % i == 0)
{
if (i == val / i)
{
count[i]++;
}
else
{
count[i]++;
count[val / i]++;
}
}
}
}
// Function to generate and print
// the required array
static void generateArr(int []A, int n)
{
int []B = new int[n];
// Find max element of array
int maxi = A.Max();
// Create count array of maxi size
int []count = new int[maxi + 1];
// For every element of the array
for(int i = 0; i < n; i++)
{
// Count[ A[i] ] denotes how many
// previous elements are there whose
// factor is the current element.
B[i] = count[A[i]];
// Increment in count array for
// factors of A[i]
IncrementFactors(count, A[i]);
}
// Print the generated array
printArr(B, n);
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 8, 1, 28, 4, 2, 6, 7 };
int n = arr.Length;
generateArr(arr, n);
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
0 1 0 2 3 0 1
时间复杂度: O(N * root(MaxElement))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。