Check if an array contains all elements of a given range
Last Updated :
14 Sep, 2023
An array containing positive elements is given. ‘A’ and ‘B’ are two numbers defining a range. Write a function to check if the array contains all elements in the given range.
Examples :
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
Method 1 : (Intuitive)
- The most intuitive approach is to sort the array and check from the element greater than ‘A’ to the element less than ‘B’. If these elements are in continuous order, all elements in the range exists in the array.
Algorithm
1. Check if A > B. If yes, return false as it is an invalid range.
2. Loop through each integer i in the range [A, B] (inclusive):
a. Initialize a boolean variable found to false.
b. Loop through each element j in the array arr:
i. If j is equal to i, set found to true and break out of the inner loop.
c. If found is still false after looping through all elements of arr, return false as i is not in the array.
3. If the loop completes without returning false, return true as all elements in the range [A, B] are found in arr.
Implementation of above approach
C++
#include <iostream>
#include <algorithm> // for std::sort
using namespace std;
bool check_elements_in_range( int arr[], int n, int A, int B) {
if (A > B) {
return false ;
}
for ( int i = A; i <= B; i++) {
bool found = false ;
for ( int j = 0; j < n; j++) {
if (arr[j] == i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
int main() {
int arr[] = {1, 4, 5, 2, 7, 8, 3};
int n = sizeof (arr) / sizeof (arr[0]);
int A = 2, B = 5;
if (check_elements_in_range(arr, n, A, B)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
public class Main {
public static boolean checkElementsInRange( int [] arr, int A, int B) {
if (A > B) {
return false ;
}
for ( int i = A; i <= B; i++) {
boolean found = false ;
for ( int j = 0 ; j < arr.length; j++) {
if (arr[j] == i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int [] arr = { 1 , 4 , 5 , 2 , 7 , 8 , 3 };
int A = 2 , B = 5 ;
if (checkElementsInRange(arr, A, B)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python
def check_elements_in_range(arr, n, A, B):
if A > B:
return False
for i in range (A, B + 1 ):
found = False
for j in range (n):
if arr[j] = = i:
found = True
break
if not found:
return False
return True
arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ]
n = len (arr)
A, B = 2 , 5
if check_elements_in_range(arr, n, A, B):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
namespace RangeCheckApp
{
class Program
{
static bool CheckElementsInRange( int [] arr, int A, int B)
{
if (A > B)
{
return false ;
}
for ( int i = A; i <= B; i++)
{
bool found = false ;
foreach ( int num in arr)
{
if (num == i)
{
found = true ;
break ;
}
}
if (!found)
{
return false ;
}
}
return true ;
}
static void Main( string [] args)
{
int [] arr = { 1, 4, 5, 2, 7, 8, 3 };
int A = 2, B = 5;
if (CheckElementsInRange(arr, A, B))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
}
|
Javascript
function checkElementsInRange(arr, A, B) {
if (A > B) {
return false ;
}
for (let i = A; i <= B; i++) {
let found = false ;
for (let j = 0; j < arr.length; j++) {
if (arr[j] === i) {
found = true ;
break ;
}
}
if (!found) {
return false ;
}
}
return true ;
}
const arr = [1, 4, 5, 2, 7, 8, 3];
const A = 2, B = 5;
if (checkElementsInRange(arr, A, B)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time complexity: O(n log n)
Auxiliary space: O(1)
Method 2 : (Hashing)
- We can maintain a count array or a hash table that stores the count of all numbers in the array that are in the range A…B. Then we can simply check if every number occurred at least once.
Algorithm:
- Initialize an empty unordered set.
- Insert all the elements of the array into the set.
- Traverse all the elements between A and B, inclusive, and check if each element is present in the set or not.
- If any element is not present in the set, return false.
- If all the elements are present in the set, return true.
C++
#include <bits/stdc++.h>
using namespace std;
bool check_elements( int arr[] , int n , int A, int B){
unordered_set< int >st;
for ( int i=0 ;i<n ;i++){
st.insert(arr[i]);
}
for ( int i=A ;i<=B ; i++){
if (st.find(i) == st.end()){
return false ;
}
}
return true ;
}
int main()
{
int arr[] = { 1, 4, 5, 2, 7, 8, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
int A = 2, B = 5;
if (check_elements(arr, n, A, B))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class Main {
public static boolean checkElements( int arr[], int n,
int A, int B)
{
Set<Integer> st = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++) {
st.add(arr[i]);
}
for ( int i = A; i <= B; i++) {
if (!st.contains(i)) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 5 , 2 , 7 , 8 , 3 };
int n = arr.length;
int A = 2 , B = 5 ;
if (checkElements(arr, n, A, B))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import collections
def checkElements(arr, n, A, B):
st = set ()
for i in range (n):
st.add(arr[i])
for i in range (A, B + 1 ):
if i not in st:
return False
return True
if __name__ = = "__main__" :
arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ]
n = len (arr)
A = 2
B = 5
if checkElements(arr, n, A, B):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static bool CheckElements( int [] arr, int n, int A,
int B)
{
HashSet< int > set = new HashSet< int >();
for ( int i = 0; i < n; i++) {
set .Add(arr[i]);
}
for ( int i = A; i <= B; i++) {
if (! set .Contains(i)) {
return false ;
}
}
return true ;
}
public static void Main( string [] args)
{
int [] arr = { 1, 4, 5, 2, 7, 8, 3 };
int n = arr.Length;
int A = 2, B = 5;
if (CheckElements(arr, n, A, B))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function checkElements(arr, n, A, B) {
let st = new Set();
for (let i = 0; i < n; i++) {
st.add(arr[i]);
}
for (let i = A; i <= B; i++) {
if (!st.has(i)) {
return false ;
}
}
return true ;
}
let arr = [1, 4, 5, 2, 7, 8, 3];
let n = arr.length;
let A = 2;
let B = 5;
if (checkElements(arr,n,A,B)) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time complexity : O(n logn)
Auxiliary space : O(max_element)
Method 3 : (Best):
Every element(x) in the range (A to B) has a corresponding unique index (x-A)
- Do a linear traversal of the array. If an element is found such that in the given range, i.e., |arr[i]| >= A and |arr[i]| <=B
- Negate the element at index (arr[i]-A) corresponding to this element arr[i].(do this only the element at that index is positive)
- Now, count the number of number of elements which are negative .This count must be equal to B-A+1.
- As, an element at an index is negative indicates that element corresponding to that index is present in array.
Implementation:
C++
#include <iostream>
using namespace std;
bool check_elements( int arr[], int n, int A, int B)
{
if (n<B-A+1) return false ;
int range = B - A;
for ( int i = 0; i < n; i++) {
if ( abs (arr[i]) >= A && abs (arr[i]) <= B) {
int z = abs (arr[i]) - A;
if (arr[z] > 0)
arr[z] = arr[z] * -1;
}
}
int count = 0;
for ( int i = 0; i <= range && i < n; i++) {
if (arr[i] > 0)
return false ;
else
count++;
}
if (count != (range + 1))
return false ;
return true ;
}
int main()
{
int arr[] = { 1, 4, 5, 2, 7, 8, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
int A = 2, B = 5;
if (check_elements(arr, n, A, B))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
bool check_elements( int arr[], int n, int A, int B)
{
int range = B - A;
for ( int i = 0; i < n; i++) {
if ( abs (arr[i]) >= A && abs (arr[i]) <= B) {
int z = abs (arr[i]) - A;
if (arr[z] > 0)
arr[z] = arr[z] * -1;
}
}
int count = 0;
for ( int i = 0; i <= range && i < n; i++) {
if (arr[i] > 0)
return false ;
else
count++;
}
if (count != (range + 1))
return false ;
return true ;
}
int main()
{
int arr[] = { 1, 4, 5, 2, 7, 8, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
int A = 2, B = 5;
if (check_elements(arr, n, A, B))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean check_elements( int arr[], int n,
int A, int B)
{
int range = B - A;
for ( int i = 0 ; i < n; i++) {
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 ;
}
}
}
int count= 0 ;
for ( int i = 0 ; i <= range && i<n; i++) {
if (arr[i] > 0 )
return false ;
else
count++;
}
if (count!= (range+ 1 ))
return false ;
return true ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 5 , 2 , 7 , 8 , 3 };
int n = arr.length;
int A = 2 , B = 5 ;
if (check_elements(arr, n, A, B))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check_elements(arr, n, A, B) :
rangeV = B - A
for i in range ( 0 , n):
if ( abs (arr[i]) > = A and
abs (arr[i]) < = B) :
z = abs (arr[i]) - A
if (arr[z] > 0 ) :
arr[z] = arr[z] * - 1
count = 0
for i in range ( 0 , rangeV + 1 ):
if i > = n:
break
if (arr[i] > 0 ):
return False
else :
count = count + 1
if (count ! = (rangeV + 1 )):
return False
return True
arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ]
n = len (arr)
A = 2
B = 5
if (check_elements(arr, n, A, B)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static bool check_elements( int []arr, int n,
int A, int B)
{
int range = B - A;
for ( int i = 0; i < n; i++)
{
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;
}
}
}
int count=0;
for ( int i = 0; i <= range
&& i < n; i++)
{
if (arr[i] > 0)
return false ;
else
count++;
}
if (count != (range + 1))
return false ;
return true ;
}
public static void Main(String []args)
{
int []arr = {1, 4, 5, 2, 7, 8, 3};
int n = arr.Length;
int A = 2, B = 5;
if (check_elements(arr, n, A, B))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check_elements(arr, n, A, B)
{
let range = B - A;
for (let i = 0; i < n; i++)
{
if (Math.abs(arr[i]) >= A &&
Math.abs(arr[i]) <= B)
{
let z = Math.abs(arr[i]) - A;
if (arr[z] > 0)
{
arr[z] = arr[z] * - 1;
}
}
}
let count=0;
for (let i = 0; i <= range &&
i < n; i++)
{
if (arr[i] > 0)
return false ;
else
count++;
}
if (count != (range + 1))
return false ;
return true ;
}
let arr = [1, 4, 5, 2, 7, 8, 3];
let n = arr.length;
let A = 2, B = 5;
if (check_elements(arr, n, A, B))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function check_elements( $arr , $n ,
$A , $B )
{
$range = $B - $A ;
for ( $i = 0; $i < $n ; $i ++)
{
if ( abs ( $arr [ $i ]) >= $A &&
abs ( $arr [ $i ]) <= $B )
{
$z = abs ( $arr [ $i ]) - $A ;
if ( $arr [ $z ] > 0)
{
$arr [ $z ] = $arr [ $z ] * -1;
}
}
}
$count = 0;
for ( $i = 0; $i <= $range &&
$i < $n ; $i ++)
{
if ( $arr [ $i ] > 0)
return -1;
else
$count ++;
}
if ( $count != ( $range + 1))
return -1;
return true;
}
$arr = array (1, 4, 5, 2,
7, 8, 3);
$n = sizeof( $arr );
$A = 2; $B = 5;
if ((check_elements( $arr , $n ,
$A , $B )) == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Time complexity : O(n)
Auxiliary space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...