Elements in the Range | Practice | GeeksforGeeks

Given an array arr[] containing positive elements. ‘A’ and ‘B’ are two numbers defining a range. The task is to check if the array contains all elements in the given range.
Example 1:
Input: N = 7, A = 2, B = 5
arr[] =  {1, 4, 5, 2, 7, 8, 3}
Output: Yes
Explanation: It has elements between 
range 2-5 i.e 2,3,4,5
Example 2:
Input: N = 7, A = 2, B = 6
arr[] = {1, 4, 5, 2, 7, 8, 3}
Output: No
Explanation: Array does not contain 6.


Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function check_elements() that takes array arr, integer N, integer A, and integer B  as parameters and returns the boolean True if array elements contain all elements in the given range else boolean False.
Note: If the array contains all elements in the given range then driver code outputs Yes otherwise, it outputs No
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 107
 
0
kssaini32015 days ago
C++  0.53/1.66
 
class Solution{public:bool check_elements(int arr[], int n, int A, int B){ // Your code goes here int count=0; int total = B-A+1; sort(arr,arr+n); // removing duplicate elements int j = 0; int temp[n];       for (int i=0; i<n-1; i++)           if (arr[i] != arr[i+1])               temp[j++] = arr[i];       temp[j++] = arr[n-1];       // rearranging array       for (int i=0; i<j; i++)           arr[i] = temp[i]; for(int i=0;i<j;i++) {     if(arr[i]>=A && arr[i]<=B)         count++; } if(count == total)     return true; else     return false;}
 
};
0
kerim21 week ago
JavaScript Solution:
 
check_elements(arr, n, A, B){
        // return true;
        if(A > B) B = A;
        var numbersInAB = B-A+1;
        var numbersSeen = 0;
        for(let i = 0; i < n; i++){
            if(arr[i] >= A && arr[i] <= B)
              numbersSeen++;
        }
        if(numbersSeen==numbersInAB) return true;
        return false;
}
0
00thirt13n4 weeks ago
bool check_elements(int arr[], int n, int A, int B)
	{
		// Your code goes here
		set<int>s;
		while(A<=B){
		    s.insert(A);
		    A++;
		}
		for(int i=0;i<n;i++){
		    if(s.count(arr[i]))
		        s.erase(arr[i]);
		}
		
		return s.empty();
	}
0
hasnainraza1998hr1 month ago
C++, 0.4
//First Method
bool check_elements(int arr[], int n, int A, int B){    unordered_set<int> s;    for(int i=0;i<n;i++){       if(arr[i]>=A && arr[i]<=B)          s.insert(arr[i]);    }    if(s.size()==((B-A)+1))        return true;    return false;}
//Second Method
vector<int> v; for(int i=0;i<n;i++){     v.push_back(arr[i]); } for(int i=A;i<=B;i++){     if(find(v.begin(),v.end(),i)==v.end())         return false; } return true;
0
dangrio2 months ago
unordered_set<int> s; for(int i=0;i<n;i++) s.insert(arr[i]); for(int i=A;i<=B;i++){     if(s.find(i) == s.end()) return false; } return true;
0
ciph3rcodes2 months ago
// JAVA SOLUTION 0.9 SECS
class Solution{   boolean check_elements(long arr[], int n, int A, int B)   {        ArrayList<Integer> arrli= new ArrayList<>();       int sum=0;       for(int i=A;i<=B;i++){           sum=sum+i;       }     for(int i=0;i<n;i++){         if((int)arr[i]>=A && (int)arr[i]<=B ){             if(!arrli.contains((int)arr[i]))             {                 arrli.add((int)arr[i]);                  sum=sum-(int)arr[i];             }         }              }     if(sum==0)      return true;            return false;
   }      }
+3
ramyan18143 months ago
// C++ solution
 
class Solution{public:bool check_elements(int arr[], int n, int A, int B){    set<int>s;        for(int i=0;i<n;i++){        if(arr[i]>=A && arr[i]<=B)              s.insert(arr[i]);    }   if(s.size()==abs(B-A)+1)      return true;       return false;
}
+1
mj200220003 months ago
Java Solution:
boolean check_elements(long arr[], int n, int A, int B)   { boolean b=true;   Arrays.sort(arr);     for(int i=0;i<arr.length;i++)     {         if(arr[i]==A && A<=B)         A++;      }     if(A-1==B)     return b;     return !b;   }
 
0
amiransarimy3 months ago
Python Solutions
Total Time Taken:
0.4/4.9
 
 def check_elements(self, arr, n, A, B):
       for num in range(A,B+1):
           if num not in arr:
               return False
       return True
0
definitelynotbandana3 months ago
Total Time Taken:
0.4/4.9 in PYTHON
def check_elements(self, arr, n, A, B):       f=True       for i in range(A,B+1):           if i not in arr:               f=False               break       return f
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.