Skip to content
Related Articles

Related Articles

Improve Article

Increasing permutation of first N natural numbers

  • Difficulty Level : Hard
  • Last Updated : 09 Apr, 2021

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




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

Java




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

Python3




# 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

C#




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

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function that returns true if it is
// possible to make the permutation
// increasing by swapping any two numbers
function isPossible(a, n)
{
 
    // To count misplaced elements
    let k = 0;
 
    // Count all misplaced elements
    for (let i = 0; i < n; i++) {
        if (a[i] != i + 1)
            k++;
    }
 
    // If possible
    if (k <= 2)
        return true;
 
    return false;
}
 
// Driver code
    let a = [ 5, 2, 3, 4, 1 ];
    let n = a.length;
 
    if (isPossible(a, n))
        document.write("Yes");
    else
        document.write("No");
 
// This code is contributed by Manoj
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :