📌  相关文章
📜  可以被给定的N位数字集(最多由0和7组成)的最大数,可以被50整除

📅  最后修改于: 2021-05-17 20:56:49             🧑  作者: Mango

给定一个数组arr [] ,该数组arr []N个07的整数组成,任务是找到可以使用数组元素形成的最大数字,以使其可以被50整除。

例子:

天真的方法:解决此问题的最简单方法是基于以下观察结果:

  • 可视化50为等于5 * 10 ,因此插入的任何尾随零都将除以10表示为50的因数。因此,任务简化为组合7 s,以使它们可以被5整除,并且对于要被5整除的数字,其单位位置应该为05
    时间复杂度: O(2 N )
    辅助空间: O(1)

高效方法:为了优化上述方法,其思想是计算数组中存在的7 s和0 s的频率,并生成所需的数字,该数字可被50整除。请按照以下步骤解决问题:

    1. 计算数组中存在的7 s和0 s的数量。
    2. 计算与数组中存在的7 s的计数最接近的5因子(因为35是最小的5因子,只能使用7 s获得)
    3. 显示7的计算数量。
    4. 将尾随零添加到上面的数字上。

需要考虑的特殊情况:

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Print the largest number divisibe by 50
void printLargestDivisible(int arr[], int N)
{
 
    int i, count0 = 0, count7 = 0;
    for (i = 0; i < N; i++) {
 
        // Counting number of 0s and 7s
        if (arr[i] == 0)
            count0++;
        else
            count7++;
    }
 
    // If count of 7 is divisible by 50
    if (count7 % 50 == 0) {
 
        while (count7--)
            cout << 7;
        while (count0--)
            cout << 0;
    }
 
    // If count of 7 is less than 5
    else if (count7 < 5) {
 
        if (count0 == 0)
            cout << "No";
        else
            cout << "0";
    }
 
    // If count of 7 is not
    // divisible by 50
    else {
 
        // Count of groups of 5 in which
        // count of 7s can be grouped
        count7 = count7 - count7 % 5;
        while (count7--)
            cout << 7;
        while (count0--)
            cout << 0;
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 0, 7, 0, 7, 7, 7, 7, 0,
                0, 0, 0, 0, 0, 7, 7, 7 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    printLargestDivisible(arr, N);
 
    return 0;
}


Java
// Java program of the above approach
import java.io.*;
class GFG {
 
  // Print the largest number divisibe by 50
  static void printLargestDivisible(int arr[], int N)
  {
 
    int i, count0 = 0, count7 = 0;
    for (i = 0; i < N; i++) {
 
      // Counting number of 0s and 7s
      if (arr[i] == 0)
        count0++;
      else
        count7++;
    }
 
    // If count of 7 is divisible by 50
    if (count7 % 50 == 0) {
 
      while (count7 != 0)
      {
        System.out.print(7);
        count7 -= 1;
      }
      while (count0 != 0)
      {
        System.out.print(0);
        count0 -= 1;
      }
    }
 
    // If count of 7 is less than 5
    else if (count7 < 5) {
 
      if (count0 == 0)
        System.out.print("No");
      else
        System.out.print( "0");
    }
 
    // If count of 7 is not
    // divisible by 50
    else {
 
      // Count of groups of 5 in which
      // count of 7s can be grouped
      count7 = count7 - count7 % 5;
      while (count7 != 0)
      {
        System.out.print(7);
        count7 -= 1;
      }
      while (count0 != 0)
      {
        System.out.print(0);
        count0 -= 1;
      }
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    // Given array
    int arr[] = { 0, 7, 0, 7, 7, 7, 7, 0,
                 0, 0, 0, 0, 0, 7, 7, 7 };
 
    // Size of the array
    int N = arr.length;
 
    printLargestDivisible(arr, N);
  }
}
 
// This code is contributed by jana_sayantan.


Python3
# Python3 Program for the above approach
 
# Print the largest number divisibe by 50
def printLargestDivisible(arr, N) :
    count0 = 0; count7 = 0;
    for i in range(N) :
 
        # Counting number of 0s and 7s
        if (arr[i] == 0) :
            count0 += 1;
        else :
            count7 += 1;
 
    # If count of 7 is divisible by 50
    if (count7 % 50 == 0) :
        while (count7) :
            count7 -= 1;
            print(7, end = "");
             
        while (count0) :
            count0 -= 1;
            print(count0, end = "");
 
    # If count of 7 is less than 5
    elif (count7 < 5) :
 
        if (count0 == 0) :
            print("No", end = "");
        else :
            print("0", end = "");
 
    # If count of 7 is not
    # divisible by 50
    else :
 
        # Count of groups of 5 in which
        # count of 7s can be grouped
        count7 = count7 - count7 % 5;
         
        while (count7) :
            count7 -= 1;
            print(7, end = "");
             
        while (count0) :
            count0 -= 1;
            print(0, end = "");
 
# Driver Code
if __name__ == "__main__" :
 
    # Given array
    arr = [ 0, 7, 0, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 7, 7, 7 ];
 
    # Size of the array
    N = len(arr);
 
    printLargestDivisible(arr, N);
 
    # This code is contributed by AnkThon


C#
// C# program of the above approach
using System;
public class GFG {
 
  // Print the largest number divisibe by 50
  static void printLargestDivisible(int []arr, int N)
  {
 
    int i, count0 = 0, count7 = 0;
    for (i = 0; i < N; i++)
    {
 
      // Counting number of 0s and 7s
      if (arr[i] == 0)
        count0++;
      else
        count7++;
    }
 
    // If count of 7 is divisible by 50
    if (count7 % 50 == 0) {
 
      while (count7 != 0)
      {
        Console.Write(7);
        count7 -= 1;
      }
      while (count0 != 0)
      {
        Console.Write(0);
        count0 -= 1;
      }
    }
 
    // If count of 7 is less than 5
    else if (count7 < 5) {
 
      if (count0 == 0)
        Console.Write("No");
      else
        Console.Write( "0");
    }
 
    // If count of 7 is not
    // divisible by 50
    else {
 
      // Count of groups of 5 in which
      // count of 7s can be grouped
      count7 = count7 - count7 % 5;
      while (count7 != 0)
      {
        Console.Write(7);
        count7 -= 1;
      }
      while (count0 != 0)
      {
        Console.Write(0);
        count0 -= 1;
      }
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
     
    // Given array
    int []arr = { 0, 7, 0, 7, 7, 7, 7, 0,
                 0, 0, 0, 0, 0, 7, 7, 7 };
 
    // Size of the array
    int N = arr.Length;
    printLargestDivisible(arr, N);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:

7777700000000

时间复杂度: O(N)

辅助空间: O(1)