📌  相关文章
📜  左边元素的数量可被当前元素整除|套装2

📅  最后修改于: 2021-06-28 21:02:12             🧑  作者: Mango

给定一个由N个整数组成的数组A [] ,任务是生成一个数组B [] ,使得B [i]包含A []中索引j的计数,从而j A [j]%A [i ] = 0
例子:

天真的方法:这种方法已经在这里讨论。但是这种方法的复杂度是O(N 2 )。
高效方法:

  • 可以说,如果数字A除以数字B,则A是B的因数。因此,我们需要找到其因数是当前元素的先前元素的数量。
  • 我们将维护一个count数组,其中包含每个元素的因子计数。
  • 现在,遍历数组,并遍历每个元素
    1. 使当前元素的答案等于count [arr [i]],然后
    2. 增加计数数组中每个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))