Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCheck if elements of an array can be arranged in a Circle...

Check if elements of an array can be arranged in a Circle with consecutive difference as 1

Given an array of N  numbers. The task is to check if it is possible to arrange all the numbers in a circle so that any two neighboring numbers differ exactly by 1. Print “YES” if it is possible to get such arrangement and “NO” otherwise.

Examples: 

Input: arr[] = {1, 2, 3, 2}
Output: YES
The circle formed is:
         1
     2       2
         3   

Input: arr[] = {3, 5, 8, 4, 7, 6, 4, 7}
Output: NO

Below is the step by step algorithm to solve this problem: 

  1. First insert all the elements in a multiset.
  2. Remove the first element of the set and store it in a curr variable.
  3. Traverse until the size of multiset reduced to 0. 
    • Remove elements that are 1 greater or 1 smaller than the curr value.
    • If there is a value with difference more than 1 then “no circle possible”.
  4. Check if it’s initial and final values of curr variable are same, print “YES” if it is, otherwise print “NO”.

Below is the implementation of above approach:

CPP




// C++ program to check if elements of array
// can be arranged in Circle with consecutive
// difference as 1
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if elements of array can
// be arranged in Circle with consecutive
// difference as 1
int circlePossible(int arr[], int n)
{
    multiset<int> s;
 
    // Initialize the multiset with array
    // elements
    for (int i = 0; i < n; i++)
        s.insert(arr[i]);
 
    // Get a pointer to first element
    int cur = *s.begin();
 
    // Store the first element in a temp variable
    int start = cur;
 
    // Remove the first element
    s.erase(s.begin());
 
    // Traverse until multiset is non-empty
    while (s.size()) {
 
        // Elements which are 1 greater than the
        // current element, remove their first occurrence
        // and increment curr
        if (s.find(cur + 1) != s.end())
            s.erase(s.find(++cur));
 
        // Elements which are 1 less than the
        // current element, remove their first occurrence
        // and decrement curr
        else if (s.find(cur - 1) != s.end())
            s.erase(s.find(--cur));
 
        // If the set is non-empty and contains element
        // which differs by curr from more than 1
        // then circle is not possible return
        else {
            cout << "NO";
            return 0;
        }
    }
 
    // Finally, check if curr and first differs by 1
    if (abs(cur - start) == 1)
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1, 2, 2, 2, 3 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    circlePossible(arr, n);
 
    return 0;
}


Output

YES

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments