📌  相关文章
📜  检查数组是否包含给定范围的所有元素

📅  最后修改于: 2021-04-24 21:32:24             🧑  作者: Mango

给出了一个包含正元素的数组。 “ A”和“ B”是定义范围的两个数字。编写函数以检查数组是否包含给定范围内的所有元素。

例子 :

Input : arr[] = {1 4 5 2 7 8 3}
           A : 2, B : 5
Output : Yes

Input : arr[] = {1 4 5 2 7 8 3}
          A : 2, B : 6
Output : No

方法1 :(直观)
最直观的方法是对数组进行排序,然后从大于“ A”的元素到大于“ B”的元素进行检查。如果这些元素按连续顺序排列,则范围中的所有元素都存在于数组中。

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

方法2 :(散列)
我们可以维护一个count数组或一个散列表,其中存储该数组中A…B范围内所有数字的计数。然后,我们可以简单地检查每个数字是否至少出现过一次。

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

方法3 :(最佳)
对数组进行线性遍历。如果找到的元素使得| arr [i] | > = A和| arr [i] |

C++
#include 
using namespace std;
   
// Function to check the array for elements in
// given range
bool check_elements(int arr[], int n, int A, int B)
{
    // Range is the no. of elements that are
    // to be checked
    int range = B - A;
   
    // Traversing the array
    for (int i = 0; i < n; i++) {
   
        // If an element is in range
        if (abs(arr[i]) >= A && abs(arr[i]) <= B) {
   
            // Negating at index ‘element – A’
            int z = abs(arr[i]) - A;
            if (arr[z] > 0) {
                arr[z] = arr[z] * -1;
            }
        }
    }
   
    // Checking whether elements in range 0-range
    // are negative
    int count=0;
    for (int i = 0; i <= range && i 0)
            return false;
        else
            count++;
    }
     if(count!= (range+1))
        return false;
    // All range elements are present
    return true;
}
   
// Driver code
int main()
{
    // Defining Array and size
    int arr[] = { 1, 4, 5, 2, 7, 8, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // A is lower limit and B is the upper limit
    // of range
    int A = 2, B = 5;
   
    // True denotes all elements were present
    if (check_elements(arr, n, A, B))
        cout << "Yes";
   
    // False denotes any element was not present
    else
        cout << "No";
  
    return 0;
}


Java
// JAVA Code for Check if an array contains
// all elements of a given range
import java.util.*;
  
class GFG {
      
    // Function to check the array for elements in
    // given range
     public static boolean check_elements(int arr[], int n,
                                             int A, int B)
    {
        // Range is the no. of elements that are
        // to be checked
        int range = B - A;
        
        // Traversing the array
        for (int i = 0; i < n; i++) {
        
            // If an element is in range
            if (Math.abs(arr[i]) >= A && 
                           Math.abs(arr[i]) <= B) {
        
                  
                int z = Math.abs(arr[i]) - A;
                if (arr[z] > 0) {
                    arr[z] = arr[z] * -1;
                }
            }
        }
        
        // Checking whether elements in range 0-range
        // are negative
        int count=0;
  
        for (int i = 0; i <= range && i 0)
                return false;
            else
                count++;
        }
  
        if(count!= (range+1))
            return false;
  
        // All range elements are present
        return true;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        // Defining Array and size
        int arr[] = { 1, 4, 5, 2, 7, 8, 3 };
        int n = arr.length;
        
        // A is lower limit and B is the upper limit
        // of range
        int A = 2, B = 5;
        
        // True denotes all elements were present
        if (check_elements(arr, n, A, B))
            System.out.println("Yes");
        
        // False denotes any element was not present
        else
            System.out.println("No");
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python3
# Function to check the array for 
# elements in given range 
def check_elements(arr, n, A, B) :
      
    # Range is the no. of elements 
    # that are to be checked 
    rangeV = B - A 
      
    # Traversing the array 
    for i in range(0, n): 
      
        # If an element is in range 
        if (abs(arr[i]) >= A and 
            abs(arr[i]) <= B) : 
      
            # Negating at index ‘element – A’ 
            z = abs(arr[i]) - A 
            if (arr[z] > 0) : 
                arr[z] = arr[z] * -1
              
    # Checking whether elements in 
    # range 0-range are negative 
    count = 0
    for i in range(0, rangeV + 1): 
        if i >= n:
            break
              
        # Element from range is
        # missing from array
        if (arr[i] > 0): 
            return False
        else:
            count = count + 1
      
    if(count != (rangeV + 1)): 
        return False
          
    # All range elements are present 
    return True
  
# Driver code 
  
# Defining Array and size 
arr = [ 1, 4, 5, 2, 7, 8, 3 ] 
n = len(arr) 
      
# A is lower limit and B is 
# the upper limit of range 
A = 2
B = 5
      
# True denotes all elements 
# were present 
if (check_elements(arr, n, A, B)) :
    print("Yes") 
      
# False denotes any element 
# was not present 
else:
    print("No") 
  
# This code is contributed 
# by Yatin Gupta


C#
// C# Code for Check if an array contains
// all elements of a given range
using System;
  
class GFG {
      
    // Function to check the array for 
    // elements in given range
    public static bool check_elements(int []arr, int n,
                                      int A, int B)
    {
        // Range is the no. of elements 
        // that are to be checked
        int range = B - A;
      
        // Traversing the array
        for (int i = 0; i < n; i++)
        {
      
            // If an element is in range
            if (Math.Abs(arr[i]) >= A && 
                Math.Abs(arr[i]) <= B) 
                {
                    int z = Math.Abs(arr[i]) - A;
                    if (arr[z] > 0)
                    {
                      arr[z] = arr[z] * - 1;
                    }
                }
        }
      
        // Checking whether elements in 
        // range 0-range are negative
        int count=0;
  
        for (int i = 0; i <= range 
             && i < n; i++) 
        {
      
            // Element from range is 
            // missing from array
            if (arr[i] > 0)
                return false;
            else
                count++;
        }
  
        if(count != (range + 1))
            return false;
  
        // All range elements are present
        return true;
    }
      
    // Driver Code
    public static void Main(String []args) 
    {
        // Defining Array and size
        int []arr = {1, 4, 5, 2, 7, 8, 3};
        int n = arr.Length;
      
        // A is lower limit and B is 
        // the upper limit of range
        int A = 2, B = 5;
      
        // True denotes all elements were present
        if (check_elements(arr, n, A, B))
         Console.WriteLine("Yes");
      
        // False denotes any element was not present
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.


PHP
= $A && 
            abs($arr[$i]) <= $B) 
        {
  
            // Negating at index
            // ‘element – A’
            $z = abs($arr[$i]) - $A;
            if ($arr[$z] > 0) 
            {
                $arr[$z] = $arr[$z] * -1;
            }
        }
    }
  
    // Checking whether elements 
    // in range 0-range are negative
    $count = 0;
    for ($i = 0; $i <= $range && 
                 $i< $n; $i++) 
    {
  
        // Element from range is
        // missing from array
        if ($arr[$i] > 0)
            return -1;
        else
            $count++;
    }
    if($count!= ($range + 1))
        return -1;
    // All range elements
    // are present
    return true;
}
  
// Driver code
  
// Defining Array and size
$arr = array(1, 4, 5, 2,
             7, 8, 3);
$n = sizeof($arr);
  
// A is lower limit and
// B is the upper limit
// of range
$A = 2; $B = 5;
  
// True denotes all
// elements were present
if ((check_elements($arr, $n,
                    $A, $B)) == true)
  
    echo "Yes";
  
// False denotes any 
// element was not present
else
    echo "No";
  
// This code is contributed by aj_36
?>


输出 :

Yes

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