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

📅  最后修改于: 2021-05-17 16:33:39             🧑  作者: Mango

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

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

例子:

方法:

  • 我们可以观察到,只有满足以下两个条件,才有可能使数组的所有元素等于K
    1. 必须至少有一个等于K的元素。
    2. 必须存在一个连续的三元组,以使该三元组中的任何两个值都大于或等于K。
  • 为了解决这个问题,我们需要创建一个辅助数组,即aux [] ,它包含三个值0、1、2。
    if( arr[i] > K )
      aux[i] = 2
    if( arr[i] == K )
      aux[i] = 1
    else 
      aux[i] = 0
    
  • 最后的任务是检查是否可以使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


输出:
YES

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