Given an array which is unsorted, check whether the elements are consecutive or not.

E.g. : 1 2 3 4 returns True,  1 3 4 5 returns False

CodingNinjas
author
2y

- Used O(nLog(n)) sorting with merge sort

- Traverse through the arrey, if a[i]+1 != a[i+1] return False

- Else return true

CodingNinjas
author
2y
Brute Force
  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. For every element between the minimum and maximum number, ch...read more
CodingNinjas
author
2y
Sorting
  1. Sort the array in increasing order.
  2. Get the minimum element (arr[0]) and the maximum element (arr[N-1]) of the array.
  3. Check if (max-min+1) is equal to N or not, if it is not equal then return fal...read more
CodingNinjas
author
2y
Using Visited Array
  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. Create a visited array that will store true at index...read more
CodingNinjas
author
2y
In Place Algorithm
  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. For each element, check arr[i]-min is negative or not...read more
CodingNinjas
author
2y
Sum of N Natural Numbers
  1. Get the minimum and maximum element of the array.
  2. Check if (max-min+1) is equal to N or not, if it is not equal then return false.
  3. Create a variable arraySum of long type to stor...read more
Chanubala Devi
2y

#include

using namespace std;

int main()

{

int n;

cin>>n;

int arr[n];

for(int i=0;i

{

cin>>arr[i];

}

sort(arr,arr+n);

for(int i=0;i+1

{

if(arr[i]+1!=arr[i+1...read more

Add answer anonymously...
NatWest Group Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter