📜  在数组中找到具有最大前复数的元素

📅  最后修改于: 2022-05-13 01:55:11.758000             🧑  作者: Mango

在数组中找到具有最大前复数的元素

给定一个数组arr[] ,任务是找到集合中存在最大数量的预乘数的元素。对于任何索引i , pre-multiple 是i的倍数并且出现在数组的第 i索引之前。此外,打印该数组中该元素的最大倍数的计数。
例子:

方法:这个想法是使用另一个数组来存储索引之前 i 的倍数的计数。可以按照以下步骤计算结果:

  1. 遍历数组的每个元素,并且对于每个有效i计数等于有效索引j < i的数量,这样,索引j处的元素可以被索引i处的元素整除。
  2. 将元素的计数值存储在temp_count数组的索引i处。
  3. 在数组temp_count [] 中找到最大元素并将其值存储在max中。
  4. 迭代数组temp_count的每个元素,这样,如果 temp_count 的索引i处的元素等于max ,则打印原始数组arr的相应第 i元素。
  5. 最后,打印存储在 max 中的最大值。

下面是上述方法的实现:

C++
// C++ program to find the element which has maximum
// number of premultiples and also print its count.
#include 
using namespace std;
#define MAX 1000
 
// Function to find the elements having
// maximum number of premultiples.
void printMaxMultiple(int arr[], int n)
{
 
    int i, j, count, max;
 
    // Initialize of temp_count array with zero
    int temp_count[n] = { 0 };
 
    for (i = 1; i < n; i++) {
        // Initialize count with zero for
        // every ith element of arr[]
        count = 0;
 
        // Loop to calculate the count of multiples
        // for every ith element of arr[] before it
        for (j = 0; j < i; j++) {
            // Condition to check whether the element
            // at a[i] divides element at a[j]
            if (arr[j] % arr[i] == 0)
                count = count + 1;
        }
        temp_count[i] = count;
    }
 
    cout<<"Element = ";
    // To get the maximum value in temp_count[]
    max = *max_element(temp_count, temp_count + n);
 
    // To print all the elements having maximum
    // number of multiples before them.
    for (i = 0; i < n; i++) {
        if (temp_count[i] == max)
            cout << arr[i] << ", ";
    }
    cout << "Count of Premultiples = ";
    // To print the count of maximum number
    // of multiples
    cout << max << "\n";
}
 
// Driver function
int main()
{
    int arr[] = { 8, 6, 2, 5, 8, 6, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printMaxMultiple(arr, n);
    return 0;
}


Java
// Java program to find the element which has maximum
// number of premultiples and also print its count.
import java.io.*;
import java.util.Arrays;
 
class GFG{
     
public static int MAX = 1000;
     
// Function to find the elements having
// maximum number of premultiples.
public static void printMaxMultiple(int[] arr, int n)
{
    int i, j, count, max;
     
    // Initialize of temp_count array with zero
    int[] temp_count = new int[n];
    for(i = 0; i < temp_count.length; i++)
    {
        temp_count[i] = 0;
    }
     
    for(i = 1; i < n; i++)
    {
       // Initialize count with zero for
       // every ith element of arr[]
       count = 0;
     
       // Loop to calculate the count of multiples
       // for every ith element of arr[] before it
       for(j = 0; j < i; j++)
       {
          // Condition to check whether the element
          // at a[i] divides element at a[j]
          if (arr[j] % arr[i] == 0)
              count = count + 1;
       }
       temp_count[i] = count;
    }
    System.out.print("Element = ");
     
    // To get the maximum value in temp_count[]
    max = Arrays.stream(temp_count).max().getAsInt();
     
    // To print all the elements having maximum
    // number of multiples before them.
    for(i = 0; i < n; i++)
    {
       if (temp_count[i] == max)
           System.out.print(arr[i] + ", ");
    }
    System.out.print("Count of Premultiples = ");
     
    // To print the count of maximum number
    // of multiples
    System.out.println(max);
}
     
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 8, 6, 2, 5, 8, 6, 3, 4 };
    int n = arr.length;
    printMaxMultiple(arr, n);
}
}
 
// This code is contributed by shubhamsingh10


Python3
# Python3 program to find the element which has maximum
# number of premultiples and also print count.
 
MAX = 1000
 
# Function to find the elements having
# maximum number of premultiples.
def printMaxMultiple(arr, n):
     
    # Initialize of temp_count array with zero
    temp_count= [0]*n
     
    for i in range(1, n):
         
        # Initialize count with zero for
        # every ith element of arr[]
        count = 0
         
        # Loop to calculate the count of multiples
        # for every ith element of arr[] before it
        for j in range(i):
             
            # Condition to check whether the element
            # at a[i] divides element at a[j]
            if (arr[j] % arr[i] == 0):
                count = count + 1
         
        temp_count[i] = count
         
    print("Element = ",end="")
    # To get the maximum value in temp_count[]
    maxx = max(temp_count)
     
    # To print all the elements having maximum
    # number of multiples before them.
    for i in range(n):
        if (temp_count[i] == maxx):
            print(arr[i],end=", ",sep="")
     
    print("Count of Premultiples = ",end="")
    # To print the count of the maximum number
    # of multiples
     
    print(maxx)
 
# Driver function
 
arr = [8, 6, 2, 5, 8, 6, 3, 4 ]
n = len(arr)
printMaxMultiple(arr, n)
 
# This code is contributed by shubhamsingh10


C#
// C# program to find the element which has maximum
// number of premultiples and also print its count.
using System;
using System.Linq;
 
class GFG {
     
    // Function to find the elements having
    // maximum number of premultiples.
    public static void printMaxMultiple(int[] arr, int n)
    {
     
        int i, j, count, max;
     
        // Initialize of temp_count array with zero
        int[] temp_count = new int[n];
        for(i = 0; i < temp_count.Length; i++)
            temp_count[i] = 0;
     
        for (i = 1; i < n; i++) {
             
            // Initialize count with zero for
            // every ith element of arr[]
            count = 0;
     
            // Loop to calculate the count of multiples
            // for every ith element of arr[] before it
            for (j = 0; j < i; j++) {
                 
                // Condition to check whether the element
                // at a[i] divides element at a[j]
                if (arr[j] % arr[i] == 0)
                    count = count + 1;
            }
            temp_count[i] = count;
        }
     
        Console.Write("Element = ");
         
        // To get the maximum value in temp_count[]
        max = temp_count.Max();;
     
        // To print all the elements having maximum
        // number of multiples before them.
        for (i = 0; i < n; i++) {
            if (temp_count[i] == max)
                Console.Write(arr[i]+ ", ");
        }
        Console.Write("Count of Premultiples = ");
         
        // To print the count of maximum
        // number of multiples
        Console.WriteLine(max);
    }
     
    // Driver function
    public static void Main()
    {
        int[] arr = { 8, 6, 2, 5, 8, 6, 3, 4 };
        int n = arr.Length;
        printMaxMultiple(arr, n);
    }
}
 
// This code is contributed by Shubhamsingh10


Javascript


输出:
Element = 2, 3, 4, Count of Premultiples = 2

时间复杂度: O(N 2 )