Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Check if all array elements can be converted to K using given operations
Next article icon

Check if an array contains all elements of a given range

Last Updated : 14 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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
Recommended Practice
Elements in the Range
Try It!

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; // invalid range
    }
 
    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; // element not found in array
        }
    }
    return true; // all elements in range found in array
}
 
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; // invalid range
        }
 
        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; // element not found in array
            }
        }
        return true; // all elements in range found in array
    }
 
    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  # invalid range
     
    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  # element not found in array
     
    return True  # all elements in range found in array
 
 
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; // invalid range
            }
 
            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; // element not found in array
                }
            }
            return true; // all elements in range found in array
        }
 
        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; // invalid range
    }
 
    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; // element not found in array
        }
    }
    return true; // all elements in range found in array
}
 
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");
}
 
 
Output
Yes 

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:

  1. Initialize an empty unordered set.
  2. Insert all the elements of the array into the set.
  3. Traverse all the elements between A and B, inclusive, and check if each element is present in the set or not.
  4. If any element is not present in the set, return false.
  5. If all the elements are present in the set, return true.

C++




// C++ code for the following approach
 
#include <bits/stdc++.h>
using namespace std;
 
// function that check all elements between A and B including
// them are present in set or not
bool check_elements(int arr[] , int n , int A, int B){
  unordered_set<int>st;
   
  // put all the elements of array into set
  for(int i=0 ;i<n ;i++){
      st.insert(arr[i]);
  }
   
  // now check every between between A to B including them also, that they are
  // present in set or not
  for(int i=A ;i<=B ; i++){
      // element not present in set so return false
      // and no need to traverse further
      if(st.find(i) == st.end()){
          return false;
      }
  }
   
  // all elements between A and B including them are
  // present in set so return true
  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;
}
 
// this code is contributed by bhardwajji
 
 

Java




// Java code for the following approach
 
import java.util.*;
 
class Main {
 
    // function that check all elements between A and B
    // including them are present in set or not
    public static boolean checkElements(int arr[], int n,
                                        int A, int B)
    {
        Set<Integer> st = new HashSet<Integer>();
 
        // put all the elements of array into set
        for (int i = 0; i < n; i++) {
            st.add(arr[i]);
        }
 
        // now check every between between A to B including
        // them also, that they are present in set or not
        for (int i = A; i <= B; i++) {
            // element not present in set so return false
            // and no need to traverse further
            if (!st.contains(i)) {
                return false;
            }
        }
 
        // all elements between A and B including them are
        // present in set so return true
        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 (checkElements(arr, n, A, B))
            System.out.println("Yes");
        // False denotes any element was not present
        else
            System.out.println("No");
    }
}
 
 

Python3




# Python code for the following approach
import collections
 
# function that check all elements between A and B
# including them are present in set or not
def checkElements(arr, n, A, B):
    st = set()
 
    # put all the elements of array into set
    for i in range(n):
        st.add(arr[i])
 
    # now check every between between A to B including
    # them also, that they are present in set or not
    for i in range(A, B+1):
        # element not present in set so return false
        # and no need to traverse further
        if i not in st:
            return False
 
    # all elements between A and B including them are
    # present in set so return true
    return True
 
# Driver code
if __name__ == "__main__":
   
    # 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 checkElements(arr, n, A, B):
        print("Yes")
         
    # False denotes any element was not present
    else:
        print("No")
 
 

C#




using System;
using System.Collections.Generic;
 
class MainClass {
    // function that check all elements between A and B
    // including them are present in set or not
    static bool CheckElements(int[] arr, int n, int A,
                              int B)
    {
        HashSet<int> set = new HashSet<int>();
 
        // put all the elements of array into set
        for (int i = 0; i < n; i++) {
            set.Add(arr[i]);
        }
 
        // now check every between between A to B including
        // them also, that they are present in set or not
        for (int i = A; i <= B; i++) {
            // element not present in set so return false
            // and no need to traverse further
            if (!set.Contains(i)) {
                return false;
            }
        }
 
        // all elements between A and B including them are
        // present in set so return true
        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 (CheckElements(arr, n, A, B))
            Console.WriteLine("Yes");
        // False denotes any element was not present
        else
            Console.WriteLine("No");
    }
}
 
 

Javascript




// JavaScript code for the following approach
 
// function that check all elements between A and B
// including them are present in set or not
function checkElements(arr, n, A, B) {
    let st = new Set();
 
    // put all the elements of array into set
    for (let i = 0; i < n; i++) {
        st.add(arr[i]);
    }
 
    // now check every between between A to B including
    // them also, that they are present in set or not
    for (let i = A; i <= B; i++) {
        // element not present in set so return false
        // and no need to traverse further
        if (!st.has(i)) {
            return false;
        }
    }
 
    // all elements between A and B including them are
    // present in set so return true
    return true;
}
 
// Defining Array and size
let arr = [1, 4, 5, 2, 7, 8, 3];
let n = arr.length;
 
// A is lower limit and B is the upper limit of range
let A = 2;
let B = 5;
 
// True denotes all elements were present
if (checkElements(arr,n,A,B)) {
    console.log("Yes");
}
// False denotes any element was not present
else {
    console.log("No");
}
 
 
Output
Yes   

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;
 
// Function to check the array for elements in
// given range
bool check_elements(int arr[], int n, int A, int B)
{
    //Array should contain atleast B-A+1 elements
    if(n<B-A+1) return false;
    // 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 < 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
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;
}
 
// This code is contributed by Sania Kumari Gupta
 
 

C




#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
 
// 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 < 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
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))
        printf("Yes");
    // False denotes any element was not present
    else
        printf("No");
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta
 
 

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

Javascript




<script>
 
    // Javascript Code for Check
    // if an array contains
    // all elements of a given range
     
    // Function to check the array for
    // elements in given range
    function check_elements(arr, n, A, B)
    {
        // Range is the no. of elements
        // that are to be checked
        let range = B - A;
       
        // Traversing the array
        for (let i = 0; i < n; i++)
        {
       
            // If an element is in range
            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;
                    }
                }
        }
       
        // Checking whether elements in
        // range 0-range are negative
        let count=0;
   
        for (let 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;
    }
     
    // Defining Array and size
    let arr = [1, 4, 5, 2, 7, 8, 3];
    let n = arr.length;
 
    // A is lower limit and B is
    // the upper limit of range
    let A = 2, B = 5;
 
    // True denotes all elements were present
    if (check_elements(arr, n, A, B))
      document.write("Yes");
 
    // False denotes any element was not present
    else
      document.write("No");
     
</script>
 
 

PHP




<?php
// Function to check the
// array for elements in
// given range
function check_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
    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
?>
 
 
Output
Yes   

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

 



Next Article
Check if all array elements can be converted to K using given operations
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Arrays
  • DSA
  • Searching
Practice Tags :
  • Arrays
  • Searching

Similar Reads

  • Check if Array elements of given range form a permutation
    Given an array arr[] consisting of N distinct integers and an array Q[][2] consisting of M queries of the form [L, R], the task for each query is to check if array elements over the range [L, R] forms a permutation or not. Note: A permutation is a sequence of length N containing each number from 1 t
    13 min read
  • Check if all array elements are present in a given stack or not
    Given a stack of integers S and an array of integers arr[], the task is to check if all the array elements are present in the stack or not. Examples: Input: S = {10, 20, 30, 40, 50}, arr[] = {20, 30}Output: YesExplanation:Elements 20 and 30 are present in the stack. Input: S = {50, 60}, arr[] = {60,
    5 min read
  • Missing Elements of a Range in an Array
    Given an array of size N. Let min and max be the minimum and maximum in the array respectively. Task is to find how many number should be added to the given array such that all the element in the range [min, max] occur at-least once in the array. Examples: Input : arr[] = {4, 5, 3, 8, 6}Output : 1On
    7 min read
  • Check if all array elements can be converted to K using given operations
    Given an integer array arr of size N and an integer K, the task is to make all elements of the array equal to K using the following operations: Choose an arbitrary subarray [l....r] of the input arrayReplace all values of this subarray equal to the [((r - l) + 2) / 2]th value in sorted subarray [l..
    9 min read
  • How to check if an Array contains a value or not?
    There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
    3 min read
  • Check if all subarrays contains at least one unique element
    Given an array arr[] consisting of N integers, the task is to check if all subarrays of the array have at least one unique element in it or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {1, 2, 1}Output: YesExplanation:For Subarrays of size 1: {1}, {2}, {
    11 min read
  • Count numbers from a given range that are not divisible by any of the array elements
    Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements. Examples: Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20Output: 6Explanation:The 6 numbers in the range [1, 2
    7 min read
  • Count of elements in X axis for given Q ranges
    Given a 2D array arr[][] where each array element denotes a point in the X axis and the number of elements on that point. A query array queries[] of size Q is given where each element is of type {l, r}. The task is to find the count of elements in the given range for each query. Examples: Input: arr
    10 min read
  • Check if concatenation of any permutation of given list of arrays generates the given array
    Given an array arr[] of N distinct integers and a list of arrays pieces[] of distinct integers, the task is to check if the given list of arrays can be concatenated in any order to obtain the given array. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {1, 2, 4,
    9 min read
  • Check if array contains contiguous integers with duplicates allowed
    Given an array of n integers(duplicates allowed). Print "Yes" if it is a set of contiguous integers else print "No". Examples: Input : arr[] = {5, 2, 3, 6, 4, 4, 6, 6}Output : YesThe elements form a contiguous set of integerswhich is {2, 3, 4, 5, 6}.Input : arr[] = {10, 14, 10, 12, 12, 13, 15}Output
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences