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:
Sum of consecutive two elements in a array
Next article icon

Check if array elements are consecutive

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an unsorted array of numbers, the task is to check if the array consists of consecutive numbers. 

Examples: 

Input: arr[] = [5, 2, 3, 1, 4]
Output: Yes
Explanation: Array has consecutive numbers from 1 to 5.

Input: arr[] = [83, 78, 80, 81, 79, 82]
Output: Yes
Explanation: Array has consecutive numbers from 78 to 83.

Input: arr[] = [34, 23, 52, 12, 3]
Output: No
Explanation: Elements are not consecutive.

Input: arr[] = [7, 6, 5, 5, 3, 4]
Output: No
Explanation: 5 is present two times.

Table of Content

  • [Naive Approach] Using Sorting – O(n * log n) time and O(1) Space
  • [Better Approach] Using Visited Array – O(n) time and O(n) space
  • [Expected Approach – 1] Using Negative Marking – O(n) time and O(1) space
  • [Expected Approach – 2] Using XOR – O(n) time and O(1) space

[Naive Approach] Using Sorting – O(n * log n) time and O(1) Space

The idea is to sort the array and then compare all the adjacent elements in the array. If the difference between the next element and current element is not 1, then return false. Otherwise, after traversing the array, return true.

C++
// C++ program to check if array elements are consecutive #include <bits/stdc++.h> using namespace std;  // Function to Check if array // elements are consecutive bool areConsecutives(vector<int> &arr) {     int n = arr.size();      	// Sort the array 	sort(arr.begin(), arr.end()); 	 	// checking the adjacent elements 	for (int i = 1; i < n; i++) { 		if (arr[i] != arr[i - 1] + 1) { 			return false; 		} 	} 	 	return true; }  int main() { 	vector<int> arr = { 5, 2, 3, 1, 4 }; 	if (areConsecutives(arr)) { 		cout << "Yes" << endl; 	} else { 		cout << "No" << endl; 	} 	return 0; } 
Java
// Java program to check if array elements are consecutive import java.util.Arrays;  class GfG {      // Function to Check if array elements are consecutive     static boolean areConsecutives(int[] arr) {         int n = arr.length;                  // Sort the array         Arrays.sort(arr);                  // checking the adjacent elements         for (int i = 1; i < n; i++) {             if (arr[i] != arr[i - 1] + 1) {                 return false;             }         }                  return true;     }      public static void main(String[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             System.out.println("Yes");         } else {             System.out.println("No");         }     } } 
Python
# Python program to check if array elements are consecutive  # Function to Check if array elements are consecutive def areConsecutives(arr):     n = len(arr)          # Sort the array     arr.sort()          # checking the adjacent elements     for i in range(1, n):         if arr[i] != arr[i - 1] + 1:             return False          return True  if __name__ == "__main__":     arr = [5, 2, 3, 1, 4]     if areConsecutives(arr):         print("Yes")     else:         print("No") 
C#
// C# program to check if array elements are consecutive using System;  class GfG {      // Function to Check if array elements are consecutive     static bool areConsecutives(int[] arr) {         int n = arr.Length;                  // Sort the array         Array.Sort(arr);                  // checking the adjacent elements         for (int i = 1; i < n; i++) {             if (arr[i] != arr[i - 1] + 1) {                 return false;             }         }                  return true;     }      static void Main(string[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             Console.WriteLine("Yes");         } else {             Console.WriteLine("No");         }     } } 
JavaScript
// JavaScript program to check if array elements are consecutive  // Function to Check if array elements are consecutive function areConsecutives(arr) {     let n = arr.length;          // Sort the array     arr.sort((a, b) => a - b);          // checking the adjacent elements     for (let i = 1; i < n; i++) {         if (arr[i] !== arr[i - 1] + 1) {             return false;         }     }          return true; }  let arr = [5, 2, 3, 1, 4]; if (areConsecutives(arr)) {     console.log("Yes"); } else {     console.log("No"); } 

Output
Yes 

[Better Approach] Using Visited Array – O(n) time and O(n) space

The idea is to check if all elements in the array can form a consecutive sequence by first finding the minimum and maximum values in the array, then verifying two key conditions:

  • the range (max-min+1) should equal the array length, indicating no gaps in the sequence
  • Each number in the range should appear exactly once, which is validated using a visited array where each element’s position is marked relative to the minimum value.
C++
// C++ program to check if array elements are consecutive #include <bits/stdc++.h> using namespace std;  // Function to Check if array // elements are consecutive bool areConsecutives(vector<int> &arr) {     int n = arr.size();      	int mini = INT_MAX; 	int maxi = INT_MIN; 	 	// Find the minimum and maximum  	// element in the array. 	for (auto num: arr) { 	    mini = min(mini, num); 	    maxi = max(maxi, num); 	} 	 	// If elements are consecutive, then  	// there should be maxi-mini+1 elements. 	if (maxi-mini+1 != n) return false; 	 	vector<bool> visited(n, false); 	 	for (auto num: arr) { 	     	    // If element is already marked 	    if (visited[num-mini] == true) { 	        return false; 	    } 	     	    // Mark the element. 	    visited[num-mini] = true; 	} 	 	return true; }  int main() { 	vector<int> arr = { 5, 2, 3, 1, 4 }; 	if (areConsecutives(arr)) { 		cout << "Yes" << endl; 	} else { 		cout << "No" << endl; 	} 	return 0; } 
Java
// Java program to check if array elements are consecutive import java.util.Arrays;  class GfG {      // Function to Check if array elements are consecutive     static boolean areConsecutives(int[] arr) {         int n = arr.length;                  int mini = Integer.MAX_VALUE;         int maxi = Integer.MIN_VALUE;                  // Find the minimum and maximum          // element in the array.         for (int num : arr) {             mini = Math.min(mini, num);             maxi = Math.max(maxi, num);         }                  // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;                  boolean[] visited = new boolean[n];                  for (int num : arr) {                          // If element is already marked             if (visited[num - mini]) {                 return false;             }                          // Mark the element.             visited[num - mini] = true;         }                  return true;     }      public static void main(String[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             System.out.println("Yes");         } else {             System.out.println("No");         }     } } 
Python
# Python program to check if array elements are consecutive  # Function to Check if array elements are consecutive def areConsecutives(arr):     n = len(arr)          mini = float('inf')     maxi = float('-inf')          # Find the minimum and maximum      # element in the array.     for num in arr:         mini = min(mini, num)         maxi = max(maxi, num)          # If elements are consecutive, then      # there should be maxi-mini+1 elements.     if maxi - mini + 1 != n:         return False          visited = [False] * n          for num in arr:                  # If element is already marked         if visited[num - mini]:             return False                  # Mark the element.         visited[num - mini] = True          return True  if __name__ == "__main__":     arr = [5, 2, 3, 1, 4]     if areConsecutives(arr):         print("Yes")     else:         print("No") 
C#
// C# program to check if array elements are consecutive using System;  class GfG {      // Function to Check if array elements are consecutive     static bool areConsecutives(int[] arr) {         int n = arr.Length;                  int mini = int.MaxValue;         int maxi = int.MinValue;                  // Find the minimum and maximum          // element in the array.         foreach (int num in arr) {             mini = Math.Min(mini, num);             maxi = Math.Max(maxi, num);         }                  // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;                  bool[] visited = new bool[n];                  foreach (int num in arr) {                          // If element is already marked             if (visited[num - mini]) {                 return false;             }                          // Mark the element.             visited[num - mini] = true;         }                  return true;     }      static void Main(string[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             Console.WriteLine("Yes");         } else {             Console.WriteLine("No");         }     } } 
JavaScript
// JavaScript program to check if array elements are consecutive  // Function to Check if array elements are consecutive function areConsecutives(arr) {     let n = arr.length;          let mini = Math.min(...arr);     let maxi = Math.max(...arr);          // If elements are consecutive, then      // there should be maxi-mini+1 elements.     if (maxi - mini + 1 !== n) return false;          let visited = new Array(n).fill(false);          for (let num of arr) {                  // If element is already marked         if (visited[num - mini]) {             return false;         }                  // Mark the element.         visited[num - mini] = true;     }          return true; }  let arr = [5, 2, 3, 1, 4]; if (areConsecutives(arr)) {     console.log("Yes"); } else {     console.log("No"); } 

Output
Yes 

[Expected Approach – 1] Using Negative Marking – O(n) time and O(1) space

The idea is to use the original array itself for marking visited elements. This approach still checks for the necessary conditions of a consecutive sequence by finding the min-max range, but instead of creating an auxiliary visited array, it marks each visited position by negating the value at the corresponding index (calculated as value-min). If any element is already negative when we attempt to mark it, it indicates a duplicate, thus failing the consecutive number requirement.

Note: This approach will only work in cases where array elements are positive.

C++
// C++ program to check if array elements are consecutive #include <bits/stdc++.h> using namespace std;  // Function to Check if array // elements are consecutive bool areConsecutives(vector<int> &arr) {     int n = arr.size();      	int mini = INT_MAX; 	int maxi = INT_MIN; 	 	// Find the minimum and maximum  	// element in the array. 	for (auto num: arr) { 	    mini = min(mini, num); 	    maxi = max(maxi, num); 	} 	 	// If elements are consecutive, then  	// there should be maxi-mini+1 elements. 	if (maxi-mini+1 != n) return false; 	 	vector<bool> visited(n, false); 	 	for (int i=0; i<n; i++) { 	     	    int val = abs(arr[i]); 	     	    // If value is already marked 	    // as negative, return false  	    // as it is already present. 	    if (arr[val-mini] < 0) { 	        return false; 	    } 	     	    // Mark the value  	    arr[val-mini] *= -1; 	} 	 	return true; }  int main() { 	vector<int> arr = { 5, 2, 3, 1, 4 }; 	if (areConsecutives(arr)) { 		cout << "Yes" << endl; 	} else { 		cout << "No" << endl; 	} 	return 0; } 
Java
// Java program to check if array elements are consecutive import java.util.Arrays;  class GfG {      // Function to Check if array elements are consecutive     static boolean areConsecutives(int[] arr) {         int n = arr.length;                  int mini = Integer.MAX_VALUE;         int maxi = Integer.MIN_VALUE;                  // Find the minimum and maximum          // element in the array.         for (int num : arr) {             mini = Math.min(mini, num);             maxi = Math.max(maxi, num);         }                  // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;                  for (int i = 0; i < n; i++) {                          int val = Math.abs(arr[i]);                          // If value is already marked             // as negative, return false              // as it is already present.             if (arr[val - mini] < 0) {                 return false;             }                          // Mark the value              arr[val - mini] *= -1;         }                  return true;     }      public static void main(String[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             System.out.println("Yes");         } else {             System.out.println("No");         }     } } 
Python
# Python program to check if array elements are consecutive  # Function to Check if array elements are consecutive def areConsecutives(arr):     n = len(arr)          mini = float('inf')     maxi = float('-inf')          # Find the minimum and maximum      # element in the array.     for num in arr:         mini = min(mini, num)         maxi = max(maxi, num)          # If elements are consecutive, then      # there should be maxi-mini+1 elements.     if maxi - mini + 1 != n:         return False          for i in range(n):                  val = abs(arr[i])                  # If value is already marked         # as negative, return false          # as it is already present.         if arr[val - mini] < 0:             return False                  # Mark the value          arr[val - mini] *= -1          return True  if __name__ == "__main__":     arr = [5, 2, 3, 1, 4]     if areConsecutives(arr):         print("Yes")     else:         print("No") 
C#
// C# program to check if array elements are consecutive using System;  class GfG {      // Function to Check if array elements are consecutive     static bool areConsecutives(int[] arr) {         int n = arr.Length;                  int mini = int.MaxValue;         int maxi = int.MinValue;                  // Find the minimum and maximum          // element in the array.         foreach (int num in arr) {             mini = Math.Min(mini, num);             maxi = Math.Max(maxi, num);         }                  // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;                  for (int i = 0; i < n; i++) {                          int val = Math.Abs(arr[i]);                          // If value is already marked             // as negative, return false              // as it is already present.             if (arr[val - mini] < 0) {                 return false;             }                          // Mark the value              arr[val - mini] *= -1;         }                  return true;     }      static void Main(string[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             Console.WriteLine("Yes");         } else {             Console.WriteLine("No");         }     } } 
JavaScript
// JavaScript program to check if array elements are consecutive  // Function to Check if array elements are consecutive function areConsecutives(arr) {     let n = arr.length;          let mini = Math.min(...arr);     let maxi = Math.max(...arr);          // If elements are consecutive, then      // there should be maxi-mini+1 elements.     if (maxi - mini + 1 !== n) return false;          for (let i = 0; i < n; i++) {                  let val = Math.abs(arr[i]);                  // If value is already marked         // as negative, return false          // as it is already present.         if (arr[val - mini] < 0) {             return false;         }                  // Mark the value          arr[val - mini] *= -1;     }          return true; }  let arr = [5, 2, 3, 1, 4]; if (areConsecutives(arr)) {     console.log("Yes"); } else {     console.log("No"); } 

Output
Yes 

[Expected Approach – 2] Using XOR – O(n) time and O(1) space

The idea is to use the XOR operation’s property where XOR of a number with itself results in 0. After verifying that the array’s range is valid (max-min+1 equals array length), the approach takes the XOR of all numbers in the expected range [min, max] and then XORs this result with all actual elements in the array.

If the array contains exactly the consecutive numbers in the expected range, each number will appear exactly twice in the XOR operation (once from the range and once from the array), resulting in all values canceling out to produce a final XOR value of 0, confirming that the array consists of consecutive numbers.

C++
// C++ program to check if array elements are consecutive #include <bits/stdc++.h> using namespace std;  // Function to Check if array // elements are consecutive bool areConsecutives(vector<int> &arr) {     int n = arr.size();      	int mini = INT_MAX; 	int maxi = INT_MIN; 	 	// Find the minimum and maximum  	// element in the array. 	for (auto num: arr) { 	    mini = min(mini, num); 	    maxi = max(maxi, num); 	} 	 	// If elements are consecutive, then  	// there should be maxi-mini+1 elements. 	if (maxi-mini+1 != n) return false; 	 	// Take xor of all elements 	// in range [mini, maxi] 	int xorVal = 0; 	for (int i=mini; i<=maxi; i++) { 	    xorVal ^= i; 	} 	 	// Take xor of all values present  	// in the array  	for (auto val: arr) { 	    xorVal ^= val; 	} 	 	// If values in array are consecutive, 	// then the final xor value will be 0. 	if (xorVal == 0) return true; 	 	return false; }  int main() { 	vector<int> arr = { 5, 2, 3, 1, 4 }; 	if (areConsecutives(arr)) { 		cout << "Yes" << endl; 	} else { 		cout << "No" << endl; 	} 	return 0; } 
Java
// Java program to check if array elements are consecutive import java.util.*;  class GfG {      // Function to Check if array     // elements are consecutive     static boolean areConsecutives(int[] arr) {         int n = arr.length;          int mini = Integer.MAX_VALUE;         int maxi = Integer.MIN_VALUE;          // Find the minimum and maximum          // element in the array.         for (int num : arr) {             mini = Math.min(mini, num);             maxi = Math.max(maxi, num);         }          // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;          // Take xor of all elements         // in range [mini, maxi]         int xorVal = 0;         for (int i = mini; i <= maxi; i++) {             xorVal ^= i;         }          // Take xor of all values present          // in the array          for (int val : arr) {             xorVal ^= val;         }          // If values in array are consecutive,         // then the final xor value will be 0.         return xorVal == 0;     }      public static void main(String[] args) {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             System.out.println("Yes");         } else {             System.out.println("No");         }     } } 
Python
# Python program to check if array elements are consecutive  # Function to Check if array # elements are consecutive def areConsecutives(arr):     n = len(arr)      mini = float('inf')     maxi = float('-inf')      # Find the minimum and maximum      # element in the array.     for num in arr:         mini = min(mini, num)         maxi = max(maxi, num)      # If elements are consecutive, then      # there should be maxi-mini+1 elements.     if maxi - mini + 1 != n:         return False      # Take xor of all elements     # in range [mini, maxi]     xorVal = 0     for i in range(mini, maxi + 1):         xorVal ^= i      # Take xor of all values present      # in the array      for val in arr:         xorVal ^= val      # If values in array are consecutive,     # then the final xor value will be 0.     return xorVal == 0  if __name__ == "__main__":     arr = [5, 2, 3, 1, 4]     if areConsecutives(arr):         print("Yes")     else:         print("No") 
C#
// C# program to check if array elements are consecutive using System;  class GfG {      // Function to Check if array     // elements are consecutive     static bool areConsecutives(int[] arr) {         int n = arr.Length;          int mini = int.MaxValue;         int maxi = int.MinValue;          // Find the minimum and maximum          // element in the array.         foreach (int num in arr) {             mini = Math.Min(mini, num);             maxi = Math.Max(maxi, num);         }          // If elements are consecutive, then          // there should be maxi-mini+1 elements.         if (maxi - mini + 1 != n) return false;          // Take xor of all elements         // in range [mini, maxi]         int xorVal = 0;         for (int i = mini; i <= maxi; i++) {             xorVal ^= i;         }          // Take xor of all values present          // in the array          foreach (int val in arr) {             xorVal ^= val;         }          // If values in array are consecutive,         // then the final xor value will be 0.         return xorVal == 0;     }      static void Main() {         int[] arr = {5, 2, 3, 1, 4};         if (areConsecutives(arr)) {             Console.WriteLine("Yes");         } else {             Console.WriteLine("No");         }     } } 
JavaScript
// JavaScript program to check if array elements are consecutive  // Function to Check if array // elements are consecutive function areConsecutives(arr) {     let n = arr.length;      let mini = Number.MAX_SAFE_INTEGER;     let maxi = Number.MIN_SAFE_INTEGER;      // Find the minimum and maximum      // element in the array.     for (let num of arr) {         mini = Math.min(mini, num);         maxi = Math.max(maxi, num);     }      // If elements are consecutive, then      // there should be maxi-mini+1 elements.     if (maxi - mini + 1 !== n) return false;      // Take xor of all elements     // in range [mini, maxi]     let xorVal = 0;     for (let i = mini; i <= maxi; i++) {         xorVal ^= i;     }      // Take xor of all values present      // in the array      for (let val of arr) {         xorVal ^= val;     }      // If values in array are consecutive,     // then the final xor value will be 0.     return xorVal === 0; }  let arr = [5, 2, 3, 1, 4]; if (areConsecutives(arr)) {     console.log("Yes"); } else {     console.log("No"); } 

Output
Yes 




Next Article
Sum of consecutive two elements in a array

A

Aarti_Rathi
Improve
Article Tags :
  • Arrays
  • DSA
Practice Tags :
  • Arrays

Similar Reads

  • Sum of consecutive two elements in a array
    Given an array print sum of the pairwise consecutive elements. Examples: Input : 8, 5, 4, 3, 15, 20 Output : 13, 9, 7, 18, 35 Input : 5, 10, 15, 20 Output : 15, 25, 35 The solution is to traverse the array and saving the sum of consecutive numbers in the variable sum. Implementation: C/C++ Code // C
    3 min read
  • Check if stack elements are pairwise consecutive
    Given a stack of integers, write a function pairWiseConsecutive() that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should ret
    10 min read
  • Check if Queue Elements are pairwise consecutive
    Given a Queue of integers. The task is to check if consecutive elements in the queue are pairwise consecutive. Examples: Input : 1 2 5 6 9 10 Output : Yes Input : 2 3 9 11 8 7 Output : No Approach: Using two stacks : Transfer all elements of the queue to one auxiliary stack aux.Now, transfer the ele
    7 min read
  • Check if Queue Elements are pairwise consecutive | Set-2
    Given a Queue of integers. The task is to check if consecutive elements in the queue are pairwise consecutive. Examples: Input: 1 2 5 6 9 10 Output: Yes Input: 2 3 9 11 8 7 Output: No Approach : Take a variable n to store size of queue.Push an element to the queue which acts as marker.Now, If size o
    8 min read
  • Check if an array can be split into subsets of K consecutive elements
    Given an array arr[] and integer K, the task is to split the array into subsets of size K, such that each subset consists of K consecutive elements. Examples: Input: arr[] = {1, 2, 3, 6, 2, 3, 4, 7, 8}, K = 3 Output: true Explanation: The given array of length 9 can be split into 3 subsets {1, 2, 3}
    5 min read
  • Check if an array contains only one distinct element
    Given an array arr[] of size N, the task is to check if the array contains only one distinct element or not. If it contains only one distinct element then print “Yes”, otherwise print “No”. Examples: Input: arr[] = {3, 3, 4, 3, 3} Output: No Explanation: There are 2 distinct elements present in the
    8 min read
  • Check if K consecutive palindrome numbers are present in the Array
    Given an array, arr[], and an integer K, the task is to check whether K consecutive palindrome numbers are present or not. Examples: Input: arr[] = {15, 7, 11, 151, 23, 1}, K = 3Output: trueExplanation: There are 3 consecutive palindromes numbers (7, 11, 151). Input : arr[] = {19, 37, 51, 42}, K = 1
    6 min read
  • Replacing an element makes array elements consecutive
    Given an array of positive distinct integers. We need to find the only element whose replacement with any other value makes array elements distinct consecutive. If it is not possible to make array elements consecutive, return -1. Examples : Input : arr[] = {45, 42, 46, 48, 47} Output : 42 Explanatio
    8 min read
  • Modulus of all pairwise consecutive elements in an Array
    Given an array of [Tex]N [/Tex]elements. The task is to print the modulus of all of the pairwise consecutive elements. That is for all pair of consecutive elements say ((a[i], a[i+1])), print (a[i] % a[i+1]). Note: Consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 t
    4 min read
  • Query to check if a range is made up of consecutive elements
    Given an array of n non-consecutive integers and Q queries, the task is to check whether for the given range l and r, the elements are consecutive or not.Examples: Input: arr = { 2, 4, 3, 7, 6, 1}, Q = { (1, 3), (3, 5), (5, 6) } Output: Yes, No, No Explanation: Array elements from (1, 3) = {2, 4, 3}
    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