📌  相关文章
📜  在位大小 A[i] 的 [1, N] 范围内查找 X,使得 X^2 的位大小不存在于数组中

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

在位大小 A[i] 的 [1, N] 范围内查找 X,使得 X^2 的位大小不存在于数组中

给定一个包含N个整数的数组A ,其中:

  • 数组的每个元素代表一个虚构的无符号整数数据类型的位大小。
  • 如果第 i 个虚构数据类型的大小为A[i]位,则它可以存储大小为 02 A[i] -1 的整数。

任务是检查是否存在某个整数X ,例如:

  • X在 1 到 N 的范围内
  • X可以适合位大小为 A[i] 的数据类型
  • X 2不适合任何其他位大小为 A[j] (A[i] < A[j]) 的不同数据类型,即使是前导零也不适合。

例子:

方法:这个想法是检查数组中是否存在一对(a, b) ,这样对于该对,存在一个整数X适合a位,而X*X不适合b位。

观察:

基于以上观察,可以采用以下方法解决问题:

  • 对给定的数组进行排序。
  • 遍历数组并检查是否有任何元素小于其前一个元素的两倍。
  • 如果是,则返回 true。
  • 如果迭代完成但没有返回 true,则返回 false。

以下是基于上述方法的代码:

C++
// C++ code for above approach
 
#include 
using namespace std;
 
// Function to check if there exist
// an integer X, such that X fits in bits
// represented by some array element and
// X*X does not fit in bits represented
// by some other element
bool check(int N, int A[])
{
 
    // sorting the given array
    sort(A, A + N);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
        // If an element is equal to the
        // last element simply skip that
        // case and continue the iteration
        // as the task is to find two
        // distinct integers such that
        // X fits in one them and
        // X*X does not fit in other
        if (A[i] == A[i - 1]) {
            continue;
        }
 
        // If the value of an element is
        // less than twice of its
        // previous element, that means
        // X would fit in previous element bits,
        // but X*X would not fit in current
        // element bits
        if (A[i] < 2 * A[i - 1]) {
            return true;
        }
    }
 
    // return false if iterations completes
    return false;
}
 
// Driver Code
int main()
{
    int N = 4;
    int A[] = { 4, 2, 3, 1 };
    bool answer = check(N, A);
    if (answer == true) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}


Java
// JAVA code for above approach
import java.util.*;
class GFG
{
 
  // Function to check if there exist
  // an integer X, such that X fits in bits
  // represented by some array element and
  // X*X does not fit in bits represented
  // by some other element
  public static boolean check(int N, int A[])
  {
 
    // sorting the given array
    Arrays.sort(A);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
      // If an element is equal to the
      // last element simply skip that
      // case and continue the iteration
      // as the task is to find two
      // distinct integers such that
      // X fits in one them and
      // X*X does not fit in other
      if (A[i] == A[i - 1]) {
        continue;
      }
 
      // If the value of an element is
      // less than twice of its
      // previous element, that means
      // X would fit in previous element bits,
      // but X*X would not fit in current
      // element bits
      if (A[i] < 2 * A[i - 1]) {
        return true;
      }
    }
 
    // return false if iterations completes
    return false;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 4;
    int A[] = new int[] { 4, 2, 3, 1 };
    boolean answer = check(N, A);
    if (answer == true) {
      System.out.print("YES");
    }
    else {
      System.out.print("NO");
    }
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for above approach
 
# Function to check if there exist
# an integer X, such that X fits in bits
# represented by some array element and
# X*X does not fit in bits represented
# by some other element
def check(N, A):
 
    # sorting the given array
    A.sort()
 
    # iterating through the array
    for i in range(1,N):
 
        # If an element is equal to the
        # last element simply skip that
        # case and continue the iteration
        # as the task is to find two
        # distinct integers such that
        # X fits in one them and
        # X*X does not fit in other
        if (A[i] == A[i - 1]):
            continue
 
        # If the value of an element is
        # less than twice of its
        # previous element, that means
        # X would fit in previous element bits,
        # but X*X would not fit in current
        # element bits
        if (A[i] < 2 * A[i - 1]):
            return True
 
    # return false if iterations completes
    return False
 
# Driver Code
N = 4
A = [ 4, 2, 3, 1 ]
answer = check(N, A)
if (answer == True):
    print("YES")
else:
    print("NO")
     
# This code is contributed by shinjanpatra


C#
// C# code for above approach
using System;
 
public class GFG{
 
  // Function to check if there exist
  // an integer X, such that X fits in bits
  // represented by some array element and
  // X*X does not fit in bits represented
  // by some other element
  static bool check(int N, int[] A)
  {
 
    // sorting the given array
    Array.Sort(A);
 
    // iterating through the array
    for (int i = 1; i < N; i++) {
 
      // If an element is equal to the
      // last element simply skip that
      // case and continue the iteration
      // as the task is to find two
      // distinct integers such that
      // X fits in one them and
      // X*X does not fit in other
      if (A[i] == A[i - 1]) {
        continue;
      }
 
      // If the value of an element is
      // less than twice of its
      // previous element, that means
      // X would fit in previous element bits,
      // but X*X would not fit in current
      // element bits
      if (A[i] < 2 * A[i - 1]) {
        return true;
      }
    }
 
    // return false if iterations completes
    return false;
  }
 
  // Driver Code
  static public void Main (){
 
    int N = 4;
    int[] A = { 4, 2, 3, 1 };
    bool answer = check(N, A);
    if (answer == true) {
      Console.Write("YES");
    }
    else {
      Console.Write("NO");
    }
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
YES

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