Check if array can be converted into strictly decreasing sequence

Given an array arr[], the task is to check whether given array can be converted into a strictly decreasing sequence with the help of the below operation:

  • Decrease any element(if it is greater than 1) by 1 in one operation

Examples:

Input: arr[] = {11, 11, 11, 11}
Output : Yes
Explanation:
The given sequence can be converted into
arr[] = {11, 10, 9, 8}

Input: arr[] = {1, 1}
Output: No
Explanation:
The given sequence cannot be converted into strictly decreasing sequence

Approach: The idea is to check whether every element of the array is greater than or equal to (N – index of element), because if it is greater than or equal to (N – index of element) that means array can be converted into [N, N-1, N-2, ….1]



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementaion to check that
// array can be converted into a
// strictly decreasing sequence
#include <bits/stdc++.h>
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;
    }
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.