📌  相关文章
📜  在任何元素的左侧都可整除的最大元素数

📅  最后修改于: 2021-06-27 01:15:57             🧑  作者: Mango

给定N个元素的数组arr [] 。元素arr [i]的好值是有效索引j

例子:

天真的方法:对于每个元素,找到其左侧可整除的数字计数,并打印这些值的最大值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum count 
// of required elements 
int findMax(int arr[], int n)
{ 
    int res = 0;
    int i, j;
      
    // For every element in the array starting 
    // from the second element 
    for(i = 0; i < n ; i++)
    {
          
        // Check all the elements on the left 
        // of current element which are divisible 
        // by the current element 
        int count = 0;
        for(j = 0; j < i; j++)
        { 
            if (arr[j] % arr[i] == 0) 
                count += 1;
        }
        res = max(count, res);
    }
    return res;
}
  
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMax(arr, n);
  
    return 0;
}
  
// This code is contributed by Rajput-Ji


Java
// Java implementation of the approach 
class GFG 
{
  
// Function to return the maximum count 
// of required elements 
static int findMax(int arr[], int n)
{ 
    int res = 0;
    int i, j;
      
    // For every element in the array starting 
    // from the second element 
    for(i = 0; i < n ; i++)
    {
          
        // Check all the elements on the left 
        // of current element which are divisible 
        // by the current element 
        int count = 0;
        for(j = 0; j < i; j++)
        { 
            if (arr[j] % arr[i] == 0) 
                count += 1;
        }
        res = Math.max(count, res);
    }
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    int arr[] = {8, 1, 28, 4, 2, 6, 7};
    int n = arr.length; 
    System.out.println(findMax(arr, n));
}
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to return the maximum count 
# of required elements
def findMax(arr, n):
    res = 0
      
    # For every element in the array starting 
    # from the second element
    for i in range(1, n):
          
        # Check all the elements on the left 
        # of current element which are divisible 
        # by the current element
        count = 0
        for j in range(0, i):
            if arr[j] % arr[i] == 0:
                count += 1
        res = max(count, res)
    return res
  
# Driver code
arr = [8, 1, 28, 4, 2, 6, 7]
n = len(arr)
print(findMax(arr, n))


C#
// C# implementation of the above approach 
using System;
      
class GFG 
{
  
// Function to return the maximum count 
// of required elements 
static int findMax(int []arr, int n)
{ 
    int res = 0;
    int i, j;
      
    // For every element in the array  
    // starting from the second element 
    for(i = 0; i < n ; i++)
    {
          
        // Check all the elements on the left 
        // of current element which are divisible 
        // by the current element 
        int count = 0;
        for(j = 0; j < i; j++)
        { 
            if (arr[j] % arr[i] == 0) 
                count += 1;
        }
        res = Math.Max(count, res);
    }
    return res;
}
  
// Driver Code
public static void Main (String[] args)
{
    int []arr = {8, 1, 28, 4, 2, 6, 7};
    int n = arr.Length; 
    Console.WriteLine(findMax(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum count
// of required elements
int findMax(int arr[], int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool divisible[n] = { false };
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--) {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0) {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = max(res, cnt);
    }
  
    return res;
}
  
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMax(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the maximum count
// of required elements
static int findMax(int arr[], int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    boolean []divisible = new boolean[n];
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) 
        {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = Math.max(res, cnt);
    }
    return res;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.length;
  
    System.out.println(findMax(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to return the maximum count 
# of required elements 
def findMax(arr, n) : 
  
    # divisible[i] will store true 
    # if arr[i] is divisible by 
    # any element on its right 
    divisible = [ False ] * n; 
  
    # To store the maximum required count 
    res = 0; 
  
    # For every element of the array 
    for i in range(n - 1, -1, -1) : 
  
        # If the current element is 
        # divisible by any element 
        # on its right 
        if (divisible[i]) :
            continue; 
  
        # Find the count of element 
        # on the left which are divisible 
        # by the current element 
        cnt = 0; 
        for j in range(i) :
  
            # If arr[j] is divisible then 
            # set divisible[j] to true 
            if ((arr[j] % arr[i]) == 0) :
                divisible[j] = True; 
                cnt += 1; 
  
        # Update the maximum required count 
        res = max(res, cnt); 
  
    return res; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 8, 1, 28, 4, 2, 6, 7 ]; 
    n = len(arr); 
  
    print(findMax(arr, n)); 
  
# This code is contributed by kanugargng


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the maximum count
// of required elements
static int findMax(int []arr, int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool []divisible = new bool[n];
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) 
        {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = Math.Max(res, cnt);
    }
    return res;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.Length;
  
    Console.WriteLine(findMax(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


输出:
3

时间复杂度: O(N 2 )

高效的方法:可以观察到,对于任何元素对(arr [i],arr [j]) ,其中i (arr [i]%arr [j])= 0 ,如果元素的数目可被arr整除[i]在其左侧为X,则在其左侧可被arr [j]整除的元素数肯定会大于X,因为所有可被arr [i]整除的元素也将被arr [j]整除。因此,对于每个元素可以被其右边的任何其他元素整除的元素,不需要计算其左边可以被其整除的元素的数目,这将提高整个程序的时间复杂度,但是应注意对于没有任何元素可以被其他元素整除的输入(例如,当所有元素都是质数时),最坏情况下的时间复杂度仍为O(N 2 )

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum count
// of required elements
int findMax(int arr[], int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool divisible[n] = { false };
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--) {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0) {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = max(res, cnt);
    }
  
    return res;
}
  
// Driver code
int main()
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMax(arr, n);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
  
// Function to return the maximum count
// of required elements
static int findMax(int arr[], int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    boolean []divisible = new boolean[n];
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) 
        {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = Math.max(res, cnt);
    }
    return res;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.length;
  
    System.out.println(findMax(arr, n));
}
}
  
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the approach 
  
# Function to return the maximum count 
# of required elements 
def findMax(arr, n) : 
  
    # divisible[i] will store true 
    # if arr[i] is divisible by 
    # any element on its right 
    divisible = [ False ] * n; 
  
    # To store the maximum required count 
    res = 0; 
  
    # For every element of the array 
    for i in range(n - 1, -1, -1) : 
  
        # If the current element is 
        # divisible by any element 
        # on its right 
        if (divisible[i]) :
            continue; 
  
        # Find the count of element 
        # on the left which are divisible 
        # by the current element 
        cnt = 0; 
        for j in range(i) :
  
            # If arr[j] is divisible then 
            # set divisible[j] to true 
            if ((arr[j] % arr[i]) == 0) :
                divisible[j] = True; 
                cnt += 1; 
  
        # Update the maximum required count 
        res = max(res, cnt); 
  
    return res; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 8, 1, 28, 4, 2, 6, 7 ]; 
    n = len(arr); 
  
    print(findMax(arr, n)); 
  
# This code is contributed by kanugargng

C#

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the maximum count
// of required elements
static int findMax(int []arr, int n)
{
  
    // divisible[i] will store true
    // if arr[i] is divisible by
    // any element on its right
    bool []divisible = new bool[n];
  
    // To store the maximum required count
    int res = 0;
  
    // For every element of the array
    for (int i = n - 1; i > 0; i--)
    {
  
        // If the current element is
        // divisible by any element
        // on its right
        if (divisible[i])
            continue;
  
        // Find the count of element
        // on the left which are divisible
        // by the current element
        int cnt = 0;
        for (int j = 0; j < i; j++) 
        {
  
            // If arr[j] is divisible then
            // set divisible[j] to true
            if ((arr[j] % arr[i]) == 0)
            {
                divisible[j] = true;
                cnt++;
            }
        }
  
        // Update the maximum required count
        res = Math.Max(res, cnt);
    }
    return res;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 8, 1, 28, 4, 2, 6, 7 };
    int n = arr.Length;
  
    Console.WriteLine(findMax(arr, n));
}
}
  
// This code is contributed by Rajput-Ji
输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。