Sunday, January 12, 2025
Google search engine
HomeData Modelling & AICheck if all disks can be placed at a single rod based...

Check if all disks can be placed at a single rod based on given conditions

Given an array arr[] consisting of N integers representing N rods each with a disk of radius arr[i], the task is to check if it is possible to move all disks to a single rod based on the condition that a disk at ith rod can be moved to jth rod only if:

  • abs(i – j) = 1 and ith rod has only a single disk.
  • The disk at ith rod has radii less than the top disk of the jth rod or the jth rod is empty.

Examples:

Input: arr[] = {1, 3, 5, 2}, N = 4
Output: YES
Explanation:
One of the possible ways is:
First move the disk of radii 3 at rod 2 to the top of rod 3. 
The array arr[] modifies to {1, 0, (5, 3), 2}.
Move the disk of radii 2 at rod 4 to the top of rod 3. The array arr[] modifies to {1, 0, (5, 3, 2), 0}.
Move the disk of radii 1 at rod 1 to top of the rod 2. The array arr[] modifies to {0, 1, (5, 3, 2), 0}.
Now, move the disk of radii 1 at rod 2 to the top of rod 3. The array arr[] modifies to {0, 0, (5, 3, 2, 1), 0}.

Input: arr[] = {4, 1, 2}, N = 3
Output: NO

 

Approach: The given problem can be solved based on the following observations: 

  • It can be observed that if there exists an index i such that arr[i] < arr[i – 1] and arr[i] < arr[i + 1], then it is impossible to move all the disks at a single rod.
  • Therefore, the task is reduced to finding if there exists any index i such that arr[i] < arr[i – 1] and arr[i] < arr[i + 1].

Follow the steps below to solve the problem:

  • Initialize a variable, say flag = false, to store if any such index exists in the array or not.
  • Traverse the array over the indices [1, N – 2]. For every ith index, check if arr[i] < arr[i – 1] and arr[i] < arr[i + 1], then set flag = true and break.
  • Print “YES” if flag is false. Otherwise, print “NO”.

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if it is possible
// to move all disks to a single rod
bool check(int a[], int n)
{
    // Stores if it is possible to move
    // all disks to a single rod
    bool flag = 0;
 
    // Traverse the array
    for (int i = 1; i < n - 1; i++) {
 
        // If i-th element is smaller than
        // both its adjacent elements
        if (a[i + 1] > a[i]
            && a[i] < a[i - 1])
            flag = 1;
    }
 
    // If flag is true
    if (flag)
        return false;
 
    // Otherwise
    else
        return true;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 5, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    if (check(arr, N))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java




// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
// Function to check if it is possible
// to move all disks to a single rod
static boolean check(int a[], int n)
{
   
    // Stores if it is possible to move
    // all disks to a single rod
    boolean flag = false;
 
    // Traverse the array
    for (int i = 1; i < n - 1; i++)
    {
 
        // If i-th element is smaller than
        // both its adjacent elements
        if (a[i + 1] > a[i]
            && a[i] < a[i - 1])
            flag = true;
    }
 
    // If flag is true
    if (flag)
        return false;
 
    // Otherwise
    else
        return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 3, 5, 2 };
    int N = arr.length;
 
    if (check(arr, N))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by code_hunt.


Python3




# Python 3 program for the above approach
 
# Function to check if it is possible
# to move all disks to a single rod
def check(a, n) :
     
    # Stores if it is possible to move
    # all disks to a single rod
    flag = 0
  
    # Traverse the array
    for i in range(1, n - 1):
  
        # If i-th element is smaller than
        # both its adjacent elements
        if (a[i + 1] > a[i]
            and a[i] < a[i - 1]) :
            flag = 1
     
    # If flag is true
    if (flag != 0) :
        return False
  
    # Otherwise
    else :
        return True
 
# Driver Code
arr = [ 1, 3, 5, 2 ]
N = len(arr)
  
if (check(arr, N) != 0):
    print("YES")
else :
    print("NO")
     
    # This code is contributed by splevel62.


C#




// C# program to implement
// the above approach
using System;
public class GFG
{
   
// Function to check if it is possible
// to move all disks to a single rod
static bool check(int[] a, int n)
{
   
    // Stores if it is possible to move
    // all disks to a single rod
    bool flag = false;
 
    // Traverse the array
    for (int i = 1; i < n - 1; i++)
    {
 
        // If i-th element is smaller than
        // both its adjacent elements
        if (a[i + 1] > a[i]
            && a[i] < a[i - 1])
            flag = true;
    }
 
    // If flag is true
    if (flag)
        return false;
 
    // Otherwise
    else
        return true;
}
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = { 1, 3, 5, 2 };
    int N = arr.Length;
 
    if (check(arr, N))
        Console.Write("YES");
    else
        Console.Write("NO");
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Javascript




<script>
 
// javascript program to implement
// the above approach
 
 
// Function to check if it is possible
// to move all disks to a single rod
function check(a , n)
{
   
    // Stores if it is possible to move
    // all disks to a single rod
    flag = false;
 
    // Traverse the array
    for (i = 1; i < n - 1; i++)
    {
 
        // If i-th element is smaller than
        // both its adjacent elements
        if (a[i + 1] > a[i]
            && a[i] < a[i - 1])
            flag = true;
    }
 
    // If flag is true
    if (flag)
        return false;
 
    // Otherwise
    else
        return true;
}
 
// Driver Code
 
 
var arr = [ 1, 3, 5, 2 ];
var N = arr.length;
 
if (check(arr, N))
    document.write("YES");
else
    document.write("NO");
 
// This code contributed by Princi Singh
 
</script>


 
 

Output: 

YES

 

Time Complexity: O(N)
Auxiliary Space: O(1) 

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!

Last Updated :
22 Mar, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments