📜  查找整数,该整数除以数组中元素的最大数量

📅  最后修改于: 2021-04-22 08:03:49             🧑  作者: Mango

给定一个整数数组arr [] ,任务是查找元素(非1),该元素是数组中元素最大数量的因数。如果存在多个这样的因素,请按升序打印所有因素。

例子:

方法:

  • 初始化两个列表,一个列表用于存储因子的等级(整数是该因子的元素数),另一个用于存储因子。
  • 2开始,直到数组的最大元素。
  • 计算当前整数是数组中元素的个数。
  • 将计数添加到排名列表,将整数添加到因子列表。
  • 查找具有最大等级的整数。
  • 打印所有具有相同等级的元素。

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
  
// Function to print the integers that divide 
// the maximum number of elements from the array
void maximumFactor(vectorarr)
{
    // Initialize two lists 
    // to store rank and factors
    int n = arr.size();
    vector rank;
    vector factors; 
    int max = *max_element(arr.begin(), arr.end()); 
      
    // Start from 2 till the maximum element in arr
    for (int i = 2; i <= max; i++)
    {
        // Initialize a variable
        // to count the number of elements 
        // it is a factor of
        int count = 0;
        for (int j = 0; j < n; j++)
        {
            if (arr[j] % i == 0)
                count+= 1;
            rank.push_back(count);
            factors.push_back(i);
        } 
          
    }
          
          
      
    // Maximum rank in the rank list
    int m = *max_element(rank.begin(),rank.end());
    for (int i = 0; i < rank.size(); i++)
    {
        // Print all the elements with rank m
        if (rank[i] == m)
            cout << factors[i] <<" ";
    }
          
} 
  
// Driver code
int main()
{
    vectorarr = {120, 15, 24, 63, 18};
    maximumFactor(arr);
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach 
import java.util.*; 
class GFG
{
      
// Function to print the integers that 
// divide the maximum number of 
// elements from the array
static void maximumFactor(int []arr)
{
      
    // Initialize two lists to store 
    // rank and factors 
    int[] rank = new int[Arrays.stream(arr).max().getAsInt() + 1];
    int[] factors = new int[Arrays.stream(arr).max().getAsInt() + 1];
    int g = 0;
      
    // Start from 2 till the maximum
    // element in arr 
    for (int i = 2; 
             i <= Arrays.stream(arr).max().getAsInt(); i++) 
    {
        // Initialize a variable to count
        // the number of elements it is a 
        // factor of 
        int count = 0;
        for (int j = 0; j < arr.length; j++) 
            if (arr[j] % i == 0)
                count += 1;
                  
        rank[g] = count; 
        factors[g] = i;
        g++;
    }
      
    // Maximum rank in the rank list 
    int m = Arrays.stream(rank).max().getAsInt();
    for (int i = 0; i < rank.length; i++) 
    {
        // Print all the elements with rank m 
        if (rank[i] == m) 
            System.out.print(factors[i] + " "); 
    }
} 
  
// Driver code
public static void main (String[] args) 
{
    int []arr = {120, 15, 24, 63, 18};
    maximumFactor(arr); 
}
}
  
// This code is contributed by 
// chandan_jnu


Python
# Python3 implementation of the approach
  
# Function to print the integers that divide 
# the maximum number of elements from the array
def maximumFactor(arr):
      
    # Initialize two lists 
    # to store rank and factors
    rank, factors = [], []
      
    # Start from 2 till the maximum element in arr
    for i in range(2, max(arr)+1):
          
        # Initialize a variable
        # to count the number of elements 
        # it is a factor of
        count = 0
        for j in arr:
            if j % i == 0:count+= 1
        rank.append(count)
        factors.append(i)
      
    # Maximum rank in the rank list
    m = max(rank)
    for i in range(len(rank)):
          
        # Print all the elements with rank m
        if rank[i]== m:
            print(factors[i], end =" ")
  
# Driver code
arr = [120, 15, 24, 63, 18]
  
maximumFactor(arr)


C#
// C# implementation of the approach 
using System;
using System.Collections;
using System.Linq;
  
class GFG
{
      
// Function to print the integers that 
// divide the maximum number of 
// elements from the array
static void maximumFactor(int []arr)
{
      
    // Initialize two lists to store 
    // rank and factors 
    int[] rank = new int[arr.Max() + 1];
    int[] factors = new int[arr.Max() + 1];
    int g = 0;
      
    // Start from 2 till the maximum
    // element in arr 
    for (int i = 2; i <= arr.Max(); i++) 
    {
        // Initialize a variable to count
        // the number of elements it is a 
        // factor of 
        int count = 0 ;
        for (int j = 0; j < arr.Length; j++) 
            if (arr[j] % i == 0)
                count += 1;
                  
        rank[g]=count; 
        factors[g]=i;
        g++;
    }
      
    // Maximum rank in the rank list 
    int m = rank.Max();
    for (int i = 0; i < rank.Length; i++) 
    {
        // Print all the elements with rank m 
        if ((int)rank[i] == m) 
            Console.Write(factors[i]+" "); 
    }
} 
  
// Driver code
static void Main()
{
  
int []arr = {120, 15, 24, 63, 18};
maximumFactor(arr); 
}
}
  
// This code is contributed by chandan_jnu


PHP


输出:
3