📌  相关文章
📜  每个元素之前数组中的倍数计数

📅  最后修改于: 2021-09-06 05:20:25             🧑  作者: Mango

给定一个大小为N的数组arr ,任务是为所有有效索引i计算索引j (j,使得a[i]除以a[j]
例子:

朴素的方法:遍历所有有效的索引j,在 [0, i-1] 范围内,对于每个索引i ;并计算每个索引的除数。
时间复杂度: O(N 2 )
空间复杂度: O(1)
高效的方法:这种方法是使用地图。在遍历数组时增加映射中的因子计数,并在映射中查找该计数以找到所有有效的j (< i)而不返回。
下面是上述方法的实现。

C++
// C++ program to count of multiples
// in an Array before every element
 
#include 
using namespace std;
 
// Function to find all factors of N
// and keep their count in map
void add_factors(int n,
                 unordered_map& mp)
{
    // Traverse from 1 to sqrt(N)
    // if i divides N,
    // increment i and N/i in map
    for (int i = 1; i <= int(sqrt(n)); i++) {
        if (n % i == 0) {
            if (n / i == i)
                mp[i]++;
            else {
                mp[i]++;
                mp[n / i]++;
            }
        }
    }
}
 
// Function to count of multiples
// in an Array before every element
void count_divisors(int a[], int n)
{
 
    // To store factors all of all numbers
    unordered_map mp;
 
    // Traverse for all possible i's
    for (int i = 0; i < n; i++) {
        // Printing value of a[i] in map
        cout << mp[a[i]] << " ";
 
        // Now updating the factors
        // of a[i] in the map
        add_factors(a[i], mp);
    }
}
 
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    count_divisors(arr, n);
 
    return 0;
}


Java
// Java program to count of multiples
// in an Array before every element
import java.util.*;
 
class GFG{
  
// Function to find all factors of N
// and keep their count in map
static void add_factors(int n,
                 HashMap mp)
{
    // Traverse from 1 to Math.sqrt(N)
    // if i divides N,
    // increment i and N/i in map
    for (int i = 1; i <= (Math.sqrt(n)); i++) {
        if (n % i == 0) {
            if (n / i == i) {
                if(mp.containsKey(i))
                    mp.put(i, mp.get(i) + 1);
                else
                    mp.put(i, 1);
            }
            else {
                if(mp.containsKey(i))
                    mp.put(i, mp.get(i) + 1);
                else
                    mp.put(i, 1);
                if(mp.containsKey(n / i))
                    mp.put(n / i, mp.get(n / i) + 1);
                else
                    mp.put(n / i, 1);
            }
        }
    }
}
  
// Function to count of multiples
// in an Array before every element
static void count_divisors(int a[], int n)
{
  
    // To store factors all of all numbers
    HashMap mp = new HashMap();
  
    // Traverse for all possible i's
    for (int i = 0; i < n; i++) {
        // Printing value of a[i] in map
        System.out.print(mp.get(a[i]) == null ? 0 + " " : mp.get(a[i]) + " ");
  
        // Now updating the factors
        // of a[i] in the map
        add_factors(a[i], mp);
    }
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.length;
  
    // Function call
    count_divisors(arr, n);
  
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to count of multiples
# in an Array before every element
from collections import defaultdict
import math
  
# Function to find all factors of N
# and keep their count in map
def add_factors(n, mp):
 
    # Traverse from 1 to sqrt(N)
    # if i divides N,
    # increment i and N/i in map
    for i in range(1, int(math.sqrt(n)) + 1,):
        if (n % i == 0):
            if (n // i == i):
                mp[i] += 1
            else :
                mp[i] += 1
                mp[n // i] += 1
  
# Function to count of multiples
# in an Array before every element
def count_divisors(a, n):
  
    # To store factors all of all numbers
    mp = defaultdict(int)
  
    # Traverse for all possible i's
    for i in range(n) :
        # Printing value of a[i] in map
        print(mp[a[i]], end=" ")
  
        # Now updating the factors
        # of a[i] in the map
        add_factors(a[i], mp)
  
# Driver code
if __name__ == "__main__":
     
    arr = [ 8, 1, 28, 4, 2, 6, 7 ]
    n = len(arr)
  
    # Function call
    count_divisors(arr, n)
  
# This code is contributed by chitranayal


C#
// C# program to count of multiples
// in an Array before every element
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to find all factors of N
// and keep their count in map
static void add_factors(int n,
                 Dictionary mp)
{
    // Traverse from 1 to Math.Sqrt(N)
    // if i divides N,
    // increment i and N/i in map
    for (int i = 1; i <= (Math.Sqrt(n)); i++) {
        if (n % i == 0) {
            if (n / i == i) {
                if(mp.ContainsKey(i))
                    mp[i] = mp[i] + 1;
                else
                    mp.Add(i, 1);
            }
            else {
                if(mp.ContainsKey(i))
                    mp[i] = mp[i] + 1;
                else
                    mp.Add(i, 1);
                if(mp.ContainsKey(n / i))
                    mp[n / i] = mp[n / i] + 1;
                else
                    mp.Add(n / i, 1);
            }
        }
    }
}
   
// Function to count of multiples
// in an Array before every element
static void count_divisors(int []a, int n)
{
   
    // To store factors all of all numbers
    Dictionary mp = new Dictionary();
   
    // Traverse for all possible i's
    for (int i = 0; i < n; i++) {
        // Printing value of a[i] in map
        Console.Write(!mp.ContainsKey(a[i]) ? 0 + " " : mp[a[i]] + " ");
   
        // Now updating the factors
        // of a[i] in the map
        add_factors(a[i], mp);
    }
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.Length;
   
    // Function call
    count_divisors(arr, n);
   
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:

0 1 0 2 3 0 1

时间复杂度: O(N * sqrt(N))

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live