Increasing permutation of first N natural numbers

Given a permutation {P1, P2, P3, ….. PN) of first N natural numbers. The task is to check if it is possible to make the permutation increasing by swapping any two numbers. If it is already in increasing order, do nothing.

Examples:

Input: a[] = {5, 2, 3, 4, 1}
Output: Yes
Swap 1 and 5

Input: a[] = {1, 2, 3, 4, 5}
Output: Yes
Already in increasing order

Input: a[] = {5, 2, 1, 4, 3}
Output: No



Approach: Let K be the number of positions i at which P1 ≠ i (1 based indexing). If K = 0, the answer is Yes as the permutation can be left as is. If K = 2, the answer is also Yes: swap the two misplaced elements. (Notice K = 1 is never possible as if any element is put in the wrong position, the element that was meant to be in that position must also be misplaced.). If K > 2 then the answer is No: a single swap can only affect two elements and can thus only correct at most two misplacements.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if it is
// possible to make the permutation
// increasing by swapping any two numbers
bool isPossible(int a[], int n)
{
    // To count misplaced elements
    int k = 0;
  
    // Count all misplaced elements
    for (int i = 0; i < n; i++) {
        if (a[i] != i + 1)
            k++;
    }
  
    // If possible
    if (k <= 2)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int a[] = { 5, 2, 3, 4, 1 };
    int n = sizeof(a) / sizeof(a[0]);
  
    if (isPossible(a, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function that returns true if it is
// possible to make the permutation
// increasing by swapping any two numbers
static boolean isPossible(int a[], int n)
{
    // To count misplaced elements
    int k = 0;
  
    // Count all misplaced elements
    for (int i = 0; i < n; i++) 
    {
        if (a[i] != i + 1)
            k++;
    }
  
    // If possible
    if (k <= 2)
        return true;
  
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    int a[] = { 5, 2, 3, 4, 1 };
    int n = a.length;
  
    if (isPossible(a, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function that returns true if it is 
# possible to make the permutation 
# increasing by swapping any two numbers 
def isPossible(a, n) :
  
    # To count misplaced elements 
    k = 0
  
    # Count all misplaced elements 
    for i in range(n) : 
        if (a[i] != i + 1) :
            k += 1
  
    # If possible 
    if (k <= 2) :
        return True
  
    return False
  
# Driver code 
if __name__ == "__main__"
  
    a = [5, 2, 3, 4, 1 ]; 
    n = len(a); 
  
    if (isPossible(a, n)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG
{
      
// Function that returns true if it is
// possible to make the permutation
// increasing by swapping any two numbers
static Boolean isPossible(int []a, int n)
{
    // To count misplaced elements
    int k = 0;
  
    // Count all misplaced elements
    for (int i = 0; i < n; i++) 
    {
        if (a[i] != i + 1)
            k++;
    }
  
    // If possible
    if (k <= 2)
        return true;
  
    return false;
}
  
// Driver code
public static void Main(String[] args)
{
    int []a = { 5, 2, 3, 4, 1 };
    int n = a.Length;
  
    if (isPossible(a, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// 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

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.