📌  相关文章
📜  检查每个 Array 元素是否可以通过将其替换为带有一些 X 的余数来减少到最小元素

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

检查每个 Array 元素是否可以通过将其替换为带有一些 X 的余数来减少到最小元素

给定一个非负整数数组arr[] ,任务是检查是否所有数组元素都可以通过选择任何正整数X减少到数组中的最小元素,并将数组元素arr[i]更新为arr[i ]%X 。如果可以这样做,请打印Yes 。否则,打印No

例子:

方法:给定的问题可以通过使用观察来解决,即任何非负整数,比如num可以通过选择X的值作为num/2更改为以下整数[0, ceil(num/2) – 1] .很明显, X的其余部分将具有比X等于num/2更短的变化范围。请按照以下步骤解决问题:

  • 初始化一个变量,比如mini ,它存储数组arr[]的最小元素。
  • 遍历给定数组arr[]并且如果对于任何数组元素arr[i]mini的值不在[0, ceil(arr[i]/2) – 1]范围内,则打印No 。否则,打印Yes

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if every integer
// in the array can be reduced to the
// minimum array element
string isPossible(int arr[], int n)
{
    // Stores the minimum array element
    int mini = INT_MAX;
 
    // Find the minimum element
    for (int i = 0; i < n; i++)
        mini = min(mini, arr[i]);
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
        if (arr[i] == mini)
            continue;
 
        // Stores the maximum value
        // in the range
        int Max = (arr[i] + 1) / 2 - 1;
 
        // Check whether mini lies in
        // the range or not
        if (mini < 0 || mini > Max)
            return "No";
    }
 
    // Otherwise, return Yes
    return "Yes";
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << isPossible(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
 
// Function to check if every integer
// in the array can be reduced to the
// minimum array element
static String isPossible(int arr[], int n)
{
    // Stores the minimum array element
    int mini = Integer.MAX_VALUE;
 
    // Find the minimum element
    for (int i = 0; i < n; i++)
        mini = Math.min(mini, arr[i]);
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
        if (arr[i] == mini)
            continue;
 
        // Stores the maximum value
        // in the range
        int Max = (arr[i] + 1) / 2 - 1;
 
        // Check whether mini lies in
        // the range or not
        if (mini < 0 || mini > Max)
            return "No";
    }
 
    // Otherwise, return Yes
    return "Yes";
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 1, 3, 4 };
    int N = arr.length;
 
    System.out.print(isPossible(arr, N));
 
}
}
 
// This code is contributed by code_hunt.


Python3
# Python 3 program for the above approach
 
import sys
# Function to check if every integer
# in the array can be reduced to the
# minimum array element
def isPossible(arr, n):
    # Stores the minimum array element
    mini = sys.maxsize
 
    # Find the minimum element
    for i in range(n):
        mini = min(mini, arr[i])
 
    # Traverse the array arr[]
    for i in range(n):
        if (arr[i] == mini):
            continue
 
        # Stores the maximum value
        # in the range
        Max = (arr[i] + 1) // 2 - 1
 
        # Check whether mini lies in
        # the range or not
        if (mini < 0 or mini > Max):
            return "No"
 
    # Otherwise, return Yes
    return "Yes"
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 1, 3, 4]
    N = len(arr)
 
    print(isPossible(arr, N))
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
// Function to check if every integer
// in the array can be reduced to the
// minimum array element
static String isPossible(int []arr, int n)
{
   
    // Stores the minimum array element
    int mini = int.MaxValue;
 
    // Find the minimum element
    for (int i = 0; i < n; i++)
        mini = Math.Min(mini, arr[i]);
 
    // Traverse the array []arr
    for (int i = 0; i < n; i++) {
        if (arr[i] == mini)
            continue;
 
        // Stores the maximum value
        // in the range
        int Max = (arr[i] + 1) / 2 - 1;
 
        // Check whether mini lies in
        // the range or not
        if (mini < 0 || mini > Max)
            return "No";
    }
 
    // Otherwise, return Yes
    return "Yes";
}
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 1, 1, 3, 4 };
    int N = arr.Length;
 
    Console.Write(isPossible(arr, N));
 
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
Yes

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