📌  相关文章
📜  检查是否可以使用给定的操作将所有数组元素转换为 K

📅  最后修改于: 2021-09-06 05:22:24             🧑  作者: Mango

给定一个大小为N的整数数组arr和一个整数K ,任务是使用以下操作使数组的所有元素等于K

  • 选择输入数组的任意数组[l….r]
  • 替换此子数组的所有值等于已排序子数组 [l…r] 中的[((r – l) + 2) / 2]

例子:

方法:

  • 我们可以观察到,只有满足以下两个条件,才有可能使数组的所有元素都等于K
    1. 必须至少有一个元素等于K
    2. 必须存在一个连续的三元组,使得该三元组的任意两个值都大于或等于 K。
  • 为了解决这个问题,我们需要创建一个辅助数组,比如aux[] ,它包含三个值 0、1、2。
  • 最后的任务是检查是否有可能使aux数组的所有元素都等于 1。如果aux[]中的三个连续元素中有两个大于 0,那么我们可以取一个大小为 3 的子数组,并使所有元素都该子数组的值等于 1。然后我们将这个逻辑扩展到整个数组。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function that prints
// whether is to possible
// to make all elements
// of the array equal to K
void makeAllK(int a[], int k, int n)
{
    vector aux;
 
    bool one_found = false;
 
    // Fill vector aux
    // according to the
    // above approach
    for (int i = 0; i < n; i++) {
 
        if (a[i] < k)
            aux.push_back(0);
 
        else if (a[i] == k) {
            aux.push_back(1);
            one_found = true;
        }
 
        else
            aux.push_back(2);
    }
 
    // Condition if K
    // does not exist in
    // the given array
    if (one_found == false) {
        cout << "NO"
             << "\n";
        return;
    }
 
    bool ans = false;
 
    if (n == 1
        && aux[0] == 1)
        ans = true;
 
    if (n == 2
        && aux[0] > 0
        && aux[1] > 1)
        ans = true;
 
    for (int i = 0; i < n - 2; i++) {
 
        // Condition for minimum
        // two elements is
        // greater than 0 in
        // pair of three elements
        if (aux[i] > 0
            && aux[i + 1] > 0) {
 
            ans = true;
            break;
        }
 
        else if (aux[i] > 0
                 && aux[i + 2] > 0) {
            ans = true;
            break;
        }
 
        else if (aux[i + 2] > 0
                 && aux[i + 1] > 0) {
            ans = true;
            break;
        }
    }
 
    if (ans == true)
        cout << "YES"
             << "\n";
    else
        cout << "NO"
             << "\n";
}
 
// Driver Code
int main()
{
    int arr[]
        = { 1, 2, 3,
            4, 5, 6,
            7, 8, 9, 10 };
 
    int K = 3;
 
    int size = sizeof(arr)
               / sizeof(arr[0]);
 
    makeAllK(arr, K, size);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG{
 
// Function that prints
// whether is to possible
// to make all elements
// of the array equal to K
static void makeAllK(int a[], int k, int n)
{
    Vector aux = new Vector();
 
    boolean one_found = false;
 
    // Fill vector aux according
    // to the above approach
    for(int i = 0; i < n; i++)
    {
       if (a[i] < k)
           aux.add(0);
        
       else if (a[i] == k)
       {
           aux.add(1);
           one_found = true;
       }
       else
           aux.add(2);
    }
 
    // Condition if K does not 
    // exist in the given array
    if (one_found == false)
    {
        System.out.print("NO" + "\n");
        return;
    }
 
    boolean ans = false;
 
    if (n == 1 && aux.get(0) == 1)
        ans = true;
 
    if (n == 2 && aux.get(0) > 0 &&
                  aux.get(1) > 1)
        ans = true;
 
    for(int i = 0; i < n - 2; i++)
    {
        
       // Condition for minimum
       // two elements is
       // greater than 0 in
       // pair of three elements
       if (aux.get(i) > 0 &&
           aux.get(i + 1) > 0)
       {
           ans = true;
           break;
       }
       else if (aux.get(i) > 0 &&
                aux.get(i + 2) > 0)
       {
           ans = true;
           break;
       }
       else if (aux.get(i + 2) > 0 &&
                aux.get(i + 1) > 0)
       {
           ans = true;
           break;
       }
    }
 
    if (ans == true)
        System.out.print("YES" + "\n");
    else
        System.out.print("NO" + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5,
                  6, 7, 8, 9, 10 };
    int K = 3;
    int size = arr.length;
 
    makeAllK(arr, K, size);
 
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation of above approach
 
# Function that prints whether is
# to possible to make all elements
# of the array equal to K
def makeAllK(a, k, n):
     
    aux = []
    one_found = False
 
    # Fill vector aux according
    # to the above approach
    for i in range(n):
        if (a[i] < k):
            aux.append(0)
 
        elif (a[i] == k):
            aux.append(1)
            one_found = True
 
        else:
            aux.append(2)
 
    # Condition if K does
    # not exist in the given
    # array
    if (one_found == False):
        print("NO")
        return
 
    ans = False
 
    if (n == 1 and aux[0] == 1):
        ans = True
 
    if (n == 2 and aux[0] > 0 and aux[1] > 1):
        ans = True
 
    for i in range(n - 2):
         
        # Condition for minimum two
        # elements is greater than
        # 0 in pair of three elements
        if (aux[i] > 0 and aux[i + 1] > 0):
            ans = True
            break
 
        elif (aux[i] > 0 and aux[i + 2] > 0):
            ans = True
            break
 
        elif (aux[i + 2] > 0 and aux[i + 1] > 0):
            ans = True
            break
 
    if (ans == True):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
    K = 3
    size = len(arr)
 
    makeAllK(arr, K, size)
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function that prints
// whether is to possible
// to make all elements
// of the array equal to K
static void makeAllK(int []a, int k, int n)
{
    List aux = new List();
 
    bool one_found = false;
 
    // Fill vector aux according
    // to the above approach
    for(int i = 0; i < n; i++)
    {
       if (a[i] < k)
       {
           aux.Add(0);
       }
       else if (a[i] == k)
       {
           aux.Add(1);
           one_found = true;
       }
       else
           aux.Add(2);
    }
 
    // Condition if K does not
    // exist in the given array
    if (one_found == false)
    {
        Console.Write("NO" + "\n");
        return;
    }
 
    bool ans = false;
    if (n == 1 && aux[0] == 1)
        ans = true;
 
    if (n == 2 && aux[0] > 0 &&
                  aux[1] > 1)
        ans = true;
 
    for(int i = 0; i < n - 2; i++)
    {
        
       // Condition for minimum
       // two elements is
       // greater than 0 in
       // pair of three elements
       if (aux[i] > 0 &&
           aux[i + 1] > 0)
       {
           ans = true;
           break;
       }
       else if (aux[i] > 0 &&
                aux[i + 2] > 0)
       {
           ans = true;
           break;
       }
       else if (aux[i + 2] > 0 &&
                aux[i + 1] > 0)
       {
           ans = true;
           break;
       }
    }
    if (ans == true)
        Console.Write("YES" + "\n");
    else
        Console.Write("NO" + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5,
                  6, 7, 8, 9, 10 };
    int K = 3;
    int size = arr.Length;
 
    makeAllK(arr, K, size);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
YES

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live