📌  相关文章
📜  检查数组是否可以转换为严格递减的序列

📅  最后修改于: 2021-05-07 00:55:15             🧑  作者: Mango

给定数组arr [] ,任务是借助以下操作检查是否可以将给定数组转换为严格递减的序列:

  • 在一个操作中将任何元素(如果大于1)减少1

例子:

方法:想法是检查数组的每个元素是否大于或等于(N –元素的索引),因为如果大于或等于(N –元素的索引),则意味着可以将数组转换为[N,N-1,N-2,…. 1]

下面是上述方法的实现:

C++
// C++ implementaion to check that
// array can be converted into a
// strictly decreasing sequence
#include 
using namespace std;
  
// Function to check that array 
// can be converted into a
// strictly decreasing sequence
bool check(int* arr, int n)
{
    bool flag = true;
  
    // Loop to check that each element is
    // greater than the (N - index)
    for (int i = 0; i < n; i++) {
  
        // If element is less than (N - index)
        if (arr[i] < n - i) {
            flag = false;
        }
    }
  
    // If array can be converted
    if (flag) {
        return true;
    }
    else {
        return false;
    }
}
  
// Driver Code
int main()
{
    int arr1[] = { 11, 11, 11, 11 };
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
  
    // Function calling
    if (check(arr1, n1)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}


Java
// Java implementaion to check that
// array can be converted into a
// strictly decreasing sequence
class GFG{
  
// Function to check that array 
// can be converted into a
// strictly decreasing sequence
static boolean check(int []arr, int n)
{
    boolean flag = true;
  
    // Loop to check that each element is
    // greater than the (N - index)
    for (int i = 0; i < n; i++)
    {
  
        // If element is less than (N - index)
        if (arr[i] < n - i)
        {
            flag = false;
        }
    }
  
    // If array can be converted
    if (flag)
    {
        return true;
    }
    else 
    {
        return false;
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr1[] = { 11, 11, 11, 11 };
    int n1 = arr1.length;
  
    // Function calling
    if (check(arr1, n1))
    {
        System.out.print("Yes" + "\n");
    }
    else 
    {
        System.out.print("No" + "\n");
    }
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 implementaion to check that
# array can be converted into a
# strictly decreasing sequence
  
# Function to check that array
# can be converted into a
# strictly decreasing sequence
def check(arr, n):
  
    flag = True;
  
    # Loop to check that each element 
    # is greater than the (N - index)
    for i in range(n):
          
        # If element is less than (N - index)
        if (arr[i] < n - i):
            flag = False;
              
    # If array can be converted
    if (flag):
        return True;
    else:
        return False;
  
# Driver Code
if __name__ == '__main__':
      
    arr1 = [ 11, 11, 11, 11 ];
    n1 = len(arr1);
  
    # Function calling
    if (check(arr1, n1)):
        print("Yes");
    else:
        print("No");
  
# This code is contributed by sapnasingh4991


C#
// C# implementaion to check that
// array can be converted into a
// strictly decreasing sequence
using System;
  
class GFG{
  
// Function to check that array 
// can be converted into a
// strictly decreasing sequence
static bool check(int []arr, int n)
{
    bool flag = true;
  
    // Loop to check that each element is
    // greater than the (N - index)
    for(int i = 0; i < n; i++)
    {
  
       // If element is less than (N - index)
       if (arr[i] < n - i)
       {
           flag = false;
       }
    }
  
    // If array can be converted
    if (flag)
    {
        return true;
    }
    else
    {
        return false;
    }
}
  
// Driver Code
static public void Main(String[] args)
{
    int []arr1 = { 11, 11, 11, 11 };
    int n1 = arr1.Length;
  
    // Function calling
    if (check(arr1, n1))
    {
        Console.Write("Yes" + "\n");
    }
    else
    {
        Console.Write("No" + "\n");
    }
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”