📌  相关文章
📜  通过将数组元素替换为对来检查是否所有数组元素都可以被K整除

📅  最后修改于: 2021-04-17 16:55:51             🧑  作者: Mango

给定的大小为N []数组ARR和整数K,一个正的,则任务是通过重复地选择一对索引(I,J),使用K阵列整除的每个元素,并执行改编[I] = ARR [I ] + arr [j]

例子:

方法:请按照以下步骤解决问题:

  • 遍历数组arr []
    • 检查arr [i]是否可被K整除:
      • 初始化一个变量,例如temp,以在每次迭代后存储arr [i]的值。
      • 重复将arr [i]乘以2,直到arr [i]
      • 检查arr [i]%K!= 0 。如果发现为真,则打印False并返回。
  • 完成上述步骤后,将True打印,因为所有元素现在都可以被K整除。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Utility function to check if all array
// elements can be made divisible by K or not
bool makeArrayDivisibleByKUtil(
    int arr[], int N, int K)
{
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Check whether arr[i] is
        // divisible by K or not
        if (arr[i] % K != 0) {
 
            // Stores value of arr[i]
            int temp = arr[i];
 
            while (arr[i] < K * temp) {
 
                // Multiply by 2 until it
                // is less than K * temp
                arr[i] = arr[i] * 2;
            }
 
            if (arr[i] % K != 0) {
                return false;
            }
        }
    }
 
    // Return true as all array
    // elements are divisible by K
    return true;
}
 
// Function to check whether all array
// elements can be made divisible by K or not
void makeArrayDivisibleByK(
    int arr[], int N, int K)
{
    if (makeArrayDivisibleByKUtil(arr, N, K)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 3, 6, 3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of K
    int K = 6;
 
    makeArrayDivisibleByK(arr, N, K);
 
    return 0;
}


C
#include
 
// Utility function to check if all array
// elements can be made divisible by K or not
int makeArrayDivisibleByKUtil(
    int arr[], int N, int K)
{
   
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Check whether arr[i] is
        // divisible by K or not
        if (arr[i] % K != 0)
        {
 
            // Stores value of arr[i]
            int temp = arr[i];
            while (arr[i] < K * temp)
            {
 
                // Multiply by 2 until it
                // is less than K * temp
                arr[i] = arr[i] * 2;
            }
 
            if (arr[i] % K != 0)
            {
                return 0;
            }
        }
    }
 
    // Return true as all array
    // elements are divisible by K
    return 1;
}
 
// Function to check whether all array
// elements can be made divisible by K or not
void makeArrayDivisibleByK(
    int arr[], int N, int K)
{
    if (makeArrayDivisibleByKUtil(arr, N, K))
    {
        printf("True\n");
    }
    else
    {
        printf("False\n");
    }
}
 
// Driver Code
int main()
{
   
    // Given array
    int arr[] = { 3,6,3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of K
    int K = 6;
    makeArrayDivisibleByK(arr, N, K);
    return 0;
}
 
// This code is contributed by santoshcharan.


Java
import java.io.*;
class GFG
{
    public static boolean makeArrayDivisibleBykUtil(int arr[],
                                                    int n,int k)
    {
       
        // Traverse the array
        for(int i = 0; i < n; i++)
        {
           
            // Check whether arr[i] is
            // divisible by k or not
            if(arr[i] % k != 0)
            {
               
                // Stores value of arr[i]
                int temp = arr[i];
                while(arr[i] < k * arr[i])
                {
                   
                    // Multiply by 2 until it
                    // is less than k*temp
                    arr[i] = arr[i] * 2;
                }
                if(arr[i] % k != 0)
                {
                    return false;
                }
            }
        }
       
        // Return true as all array
        // elements are divisible by k
        return true;
    }
    public static void makeArrayDivisibleByk(int arr[],
                                             int n,int k)
    {
        if(makeArrayDivisibleBykUtil(arr, n, k))
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
   
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given array   
        int[] arr = { 3 , 6 , 3};
       
        // Size of the array
        int n = 3;
        int k = 6;
       
        // Calling the function
        makeArrayDivisibleByk(arr, n, k);
    }
}
 
// This code is contributed by santoshcharan.


Python3
# Python 3 Program for the above approach
 
# Utility function to check if all array
# elements can be made divisible by K or not
def makeArrayDivisibleByKUtil(arr, N, K):
   
  # Traverse the array
  for i in range(N):
     
    # Check whether arr[i] is
    # divisible by K or not
    if (arr[i] % K != 0):
       
      # Stores value of arr[i]
      temp = arr[i]
      while (arr[i] < K * temp):
         
        # Multiply by 2 until it
        # is less than K * temp
        arr[i] = arr[i] * 2
      if (arr[i] % K != 0):
        return False
 
  # Return true as all array
  # elements are divisible by K
  return True
 
# Function to check whether all array
# elements can be made divisible by K or not
def makeArrayDivisibleByK(arr, N, K):
  if (makeArrayDivisibleByKUtil(arr, N, K)):
    print("True")
  else:
    print("False")
 
# Driver Code
if __name__ == '__main__':
   
  # Given array
  arr =  [3, 6, 3]
 
  # Size of the array
  N =  len(arr)
 
  # Given value of K
  K = 6
  makeArrayDivisibleByK(arr, N, K)
   
  # This code is contributed by bgangwar59.


C#
using System;
 
class GFG
{
  public static bool makeArrayDivisibleBykUtil(int [] arr,
                                               int n,int k)
  {
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
 
      // Check whether arr[i] is
      // divisible by k or not
      if(arr[i] % k != 0)
      {
 
        // Stores value of arr[i]
        //int temp = arr[i];
        while(arr[i] < k * arr[i])
        {
 
          // Multiply by 2 until it
          // is less than k*temp
          arr[i] = arr[i] * 2;
        }
        if(arr[i] % k != 0)
        {
          return false;
        }
      }
    }
 
    // Return true as all array
    // elements are divisible by k
    return true;
  }
  public static void makeArrayDivisibleByk(int []arr,
                                           int n,int k)
  {
    if(makeArrayDivisibleBykUtil(arr, n, k))
    {
      Console.WriteLine("True");
    }
    else
    {
      Console.WriteLine("False");
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Given array   
    int[] arr = { 3 , 6 , 3};
 
    // Size of the array
    int n = 3;
    int k = 6;
 
    // Calling the function
    makeArrayDivisibleByk(arr, n, k);
  }
}
 
// This code is contributed by chitranayal.


输出:
True

时间复杂度: O(N * logN)
辅助空间: O(1)