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