📌  相关文章
📜  检查给定的数组是否可以通过将元素减半来进行 1 到 N 的排列

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

检查给定的数组是否可以通过将元素减半来进行 1 到 N 的排列

给定一个大小为N的数组nums[] ,任务是检查给定数组在执行给定操作任意次数(可能为 0 次)后是否可以转换为1 到 N排列。操作定义为:选择数组中的任何元素,例如'x' ,并将其替换为'x/2'

注意:1 到 N的排列中,范围[1, N]中的所有数字都以任意顺序出现,并且每个数字仅出现一次。

例子:

方法:解决问题的方法是基于排序。创建一个布尔类型和大小(N+1)的数组freq以跟踪所需排列的数量。请按照以下步骤操作:

  • 对给定的数组进行排序。
  • 从数组的后面遍历每一步:
    • 如果val小于N并且freq[val],则将其标记为真。
    • 如果val大于Nfreq[val],则将其除以 2 while (val > 0 && freq[val] == true)
    • 最后,检查是否达到了所需的排列。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to check is it possible
// to make array or not
bool possibleOrNot(vector& nums, int N)
{
 
  // Sorting array
  sort(nums.begin(), nums.end());
 
  // Initializing freq[] array which
  // keeps track of elements from 1 to N
  vector freq(N + 1, 0);
 
  // Iterating from backwards
  for (int i = N - 1; i >= 0; i--) {
    int val = nums[i];
 
    // Dividing val by 2 while
    // it is greater than N
    // or freq[val] is true
    while (val > N || (freq[val] && val >= 1)) {
      val /= 2;
    }
 
    // Updating freq array
    if (val != 0)
      freq[val] = true;
  }
 
  // Checking if every element from
  // 1 to N is present or not
  for (int i = 1; i < freq.size(); i++)
    if (!freq[i]) {
      return false;
    }
 
  return true;
}
 
// Driver Code
int main()
{
  int N = 4;
  vector nums = { 1, 8, 25, 2 };
 
  bool ans = possibleOrNot(nums, N);
  cout << (ans);
 
  return 0;
}
 
  // This code is contributed by rakeshsahni


Java
// Java code to implement the above approach
import java.util.*;
 
class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
        int[] nums = { 1, 8, 25, 2 };
 
        boolean ans = possibleOrNot(nums, N);
        System.out.println(ans);
    }
 
    // Function to check is it possible
    // to make array or not
    public static boolean possibleOrNot(int[] nums,
                                        int N)
    {
        // Sorting array
        Arrays.sort(nums);
 
        // Initializing freq[] array which
        // keeps track of elements from 1 to N
        boolean[] freq = new boolean[N + 1];
 
        // Iterating from backwards
        for (int i = N - 1; i >= 0; i--) {
            int val = nums[i];
 
            // Dividing val by 2 while
            // it is greater than N
            // or freq[val] is true
            while (val > N || (freq[val]
                               && val >= 1)) {
                val /= 2;
            }
 
            // Updating freq array
            if (val != 0)
                freq[val] = true;
        }
 
        // Checking if every element from
        // 1 to N is present or not
        for (int i = 1; i < freq.length; i++)
            if (!freq[i]) {
                return false;
            }
 
        return true;
    }
}


Python3
# python3 code to implement the above approach
 
# Function to check is it possible
# to make array or not
def possibleOrNot(nums, N):
 
    # Sorting array
    nums.sort()
 
    # Initializing freq[] array which
    # keeps track of elements from 1 to N
    freq = [0 for _ in range(N + 1)]
 
    # Iterating from backwards
    for i in range(N - 1, -1, -1):
        val = nums[i]
 
        # Dividing val by 2 while
        # it is greater than N
        # or freq[val] is true
        while (val > N or (freq[val] and val >= 1)):
            val //= 2
 
        # Updating freq array
        if (val != 0):
            freq[val] = True
 
    # Checking if every element from
    # 1 to N is present or not
    for i in range(1, len(freq)):
        if (not freq[i]):
            return False
 
    return True
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    nums = [1, 8, 25, 2]
 
    ans = possibleOrNot(nums, N)
    print(ans)
 
    # This code is contributed by rakeshsahni


C#
using System;
 
public class GFG{
 
  // Function to check is it possible
  // to make array or not
  static bool possibleOrNot(int[] nums,
                            int N)
  {
    // Sorting array
    Array.Sort(nums);
 
    // Initializing freq[] array which
    // keeps track of elements from 1 to N
    bool[] freq = new bool[N + 1];
 
    // Iterating from backwards
    for (int i = N - 1; i >= 0; i--) {
      int val = nums[i];
 
      // Dividing val by 2 while
      // it is greater than N
      // or freq[val] is true
      while (val > N || (freq[val]
                         && val >= 1)) {
        val /= 2;
      }
 
      // Updating freq array
      if (val != 0)
        freq[val] = true;
    }
 
    // Checking if every element from
    // 1 to N is present or not
    for (int i = 1; i < freq.Length; i++)
      if (!freq[i]) {
        return false;
      }
 
    return true;
  }
 
  // Driver Code
  static public void Main (){
 
    int N = 4;
    int[] nums = { 1, 8, 25, 2 };
 
    bool ans = possibleOrNot(nums, N);
    if(ans == true)
      Console.WriteLine(1);
    else
      Console.WriteLine(0);
  }
}
 
// This code is contributed by hrithikgrg03188.


Javascript



输出
true

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