Method 1 : (Intuitive)
The most intuitive approach is to sort the array and check from the element greater than ‘A’ to the element greater than ‘B’. If these elements are in continuous order, all elements in the range exists in the array.

Time complexity : O(n log n)
Auxiliary space : O(1)

Method 2 : (Hashing)
We can maintain a count array or a hash table which 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.

Time complexity : O(n)
Auxiliary space : O(max_element)

Method 3 : (Best)
Do a linear traversal of the array. If an element is found such that |arr[i]| >= A and |arr[i]|

C++

filter_none

editclose

play_arrow

linkbrightness_4code

#include <iostream>

usingnamespacestd;

// Function to check the array for elements in

// given range

boolcheck_elements(intarr[], intn, intA, intB)

{

// Range is the no. of elements that are

// to be checked

intrange = B - A;

// Traversing the array

for(inti = 0; i < n; i++) {

// If an element is in range

if(abs(arr[i]) >= A && abs(arr[i]) <= B) {

// Negating at index ‘element – A’

intz = abs(arr[i]) - A;

if(arr[z] > 0) {

arr[z] = arr[z] * -1;

}

}

}

// Checking whether elements in range 0-range

// are negative

intcount=0;

for(inti = 0; i <= range && i<n; i++) {

// Element from range is missing from array

if(arr[i] > 0)

returnfalse;

else

count++;

}

if(count!= (range+1))

returnfalse;

// All range elements are present

returntrue;

}

// Driver code

intmain()

{

// Defining Array and size

intarr[] = { 1, 4, 5, 2, 7, 8, 3 };

intn = sizeof(arr) / sizeof(arr[0]);

// A is lower limit and B is the upper limit

// of range

intA = 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";

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// JAVA Code for Check if an array contains

// all elements of a given range

importjava.util.*;

classGFG {

// Function to check the array for elements in

// given range

publicstaticbooleancheck_elements(intarr[], intn,

intA, intB)

{

// Range is the no. of elements that are

// to be checked

intrange = B - A;

// Traversing the array

for(inti = 0; i < n; i++) {

// If an element is in range

if(Math.abs(arr[i]) >= A &&

Math.abs(arr[i]) <= B) {

intz = Math.abs(arr[i]) - A;

if(arr[z] > 0) {

arr[z] = arr[z] * -1;

}

}

}

// Checking whether elements in range 0-range

// are negative

intcount=0;

for(inti = 0; i <= range && i<n; i++) {

// Element from range is missing from array

if(arr[i] > 0)

returnfalse;

else

count++;

}

if(count!= (range+1))

returnfalse;

// All range elements are present

returntrue;

}

/* Driver program to test above function */

publicstaticvoidmain(String[] args)

{

// Defining Array and size

intarr[] = { 1, 4, 5, 2, 7, 8, 3};

intn = arr.length;

// A is lower limit and B is the upper limit

// of range

intA = 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.

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# Function to check the array for

# elements in given range

defcheck_elements(arr, n, A, B) :

# Range is the no. of elements

# that are to be checked

rangeV =B -A

# Traversing the array

fori inrange(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

fori inrange(0, rangeV +1):

ifi >=n:

break

# Element from range is

# missing from array

if(arr[i] > 0):

returnFalse

else:

count =count +1

if(count !=(rangeV +1)):

returnFalse

# All range elements are present

returnTrue

# 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

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# Code for Check if an array contains

// all elements of a given range

usingSystem;

classGFG {

// Function to check the array for

// elements in given range

publicstaticboolcheck_elements(int[]arr, intn,

intA, intB)

{

// Range is the no. of elements

// that are to be checked

intrange = B - A;

// Traversing the array

for(inti = 0; i < n; i++)

{

// If an element is in range

if(Math.Abs(arr[i]) >= A &&

Math.Abs(arr[i]) <= B)

{

intz = Math.Abs(arr[i]) - A;

if(arr[z] > 0)

{

arr[z] = arr[z] * - 1;

}

}

}

// Checking whether elements in

// range 0-range are negative

intcount=0;

for(inti = 0; i <= range

&& i < n; i++)

{

// Element from range is

// missing from array

if(arr[i] > 0)

returnfalse;

else

count++;

}

if(count != (range + 1))

returnfalse;

// All range elements are present

returntrue;

}

// Driver Code

publicstaticvoidMain(String []args)

{

// Defining Array and size

int[]arr = {1, 4, 5, 2, 7, 8, 3};

intn = arr.Length;

// A is lower limit and B is

// the upper limit of range

intA = 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.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// Function to check the

// array for elements in

// given range

functioncheck_elements($arr, $n,

$A, $B)

{

// Range is the no. of

// elements that are to

// be checked

$range= $B- $A;

// Traversing the array

for($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’

$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

returntrue;

}

// 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

?>

chevron_right

filter_none

Output :

Yes

Time complexity : O(n)Auxiliary space : O(1)

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.