📌  相关文章
📜  检查是否可以根据给定条件将所有磁盘放置在单个杆上

📅  最后修改于: 2021-09-06 11:32:19             🧑  作者: Mango

给定一个数组arr[]N 个整数组成,代表N 个杆,每个杆的圆盘半径为arr[i] ,任务是检查是否可以根据以下条件将所有圆盘移动到单个杆上: i处的圆盘杆可以移动到j杆仅当:

  • abs (i – j) = 1并且i杆只有一个圆盘。
  • 该盘在i杆具有半径小于j杆或j杆是空的顶部磁盘。

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个变量,比如flag = false ,以存储数组中是否存在任何此类索引。
  • 在索引[1, N – 2] 上遍历数组。对于每个i索引,检查arr[i] < arr[i – 1] 和 arr[i] < arr[i + 1] ,然后设置flag = true并中断。
  • 如果flagfalse则打印“YES” 。否则,打印“NO”

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
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


输出:
YES

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live