Open In App

Place first N natural numbers at indices not equal to their values in an array

Last Updated : 30 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N(N > 1), the task is to arrange all integers from the range [1, N] in an array such that none of the elements are same as the index (1-based indexing) in which they are present in the array.

Examples:

Input: N = 2
Output: 2 1
Explanation: Only possible arrangement of an array of size 2 is 2 1.

Input: N=5
Output: 2 1 5 3 4
Explanation: One possible arrangement of an array of size 5 is 2 1 5 3 4l.

Approach: The simplest idea is to place N at the first index and place the remaining elements [1, N – 1] in the remaining indices.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to place first N natural
// numbers in an array such that none
// of the values are equal to its indices
void generatepermutation(int N)
{
 
  // Stores the required array
    vector<int> answer;
 
    // Place N at the first position
    answer.push_back(N);
 
    // Iterate the range [1, N)
    for (int i = 1; i < N; i++)
    {
        // Append elements to the sequence
        answer.push_back(i);
    }
 
    // Print the sequence
    for(int i:answer) cout << i << " ";
 
}
 
// Driver Code
int main()
{
  int N = 4;
  generatepermutation(N);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java




// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to place first N natural
// numbers in an array such that none
// of the values are equal to its indices
static void generatepermutation(int N)
{
 
  // Stores the required array
    Vector<Integer> answer = new Vector<Integer>();
 
    // Place N at the first position
    answer.add(N);
 
    // Iterate the range [1, N)
    for (int i = 1; i < N; i++)
    {
        // Append elements to the sequence
        answer.add(i);
    }
 
    // Print the sequence
    for(int i:answer) System.out.print(i+ " ");
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 4;
  generatepermutation(N);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python program for the above approach
 
# Function to place first N natural
# numbers in an array such that none
# of the values are equal to its indices
def generatepermutation(N):
 
  # Stores the required array
    answer = []
 
    # Place N at the first position
    answer.append(N)
 
    # Iterate the range [1, N)
    for i in range(1, N):
 
        # Append elements to the sequence
        answer.append(i)
 
    # Print the sequence
    print(*answer)
 
 
# Driver Code
N = 4
generatepermutation(N)


C#




// C# program for the above approach
using System;
class GFG
{
 
// Function to place first N natural
// numbers in an array such that none
// of the values are equal to its indices
static void generatepermutation(int N)
{
 
    // Stores the required array
    int[] answer = new int[N];
 
    // Place N at the first position
    answer[0] = N;
 
    // Iterate the range [1, N)
    for (int i = 1; i < N; i++)
    {
       
        // Append elements to the sequence
        answer[i] = i;
    }
 
    // Print the sequence
    foreach(int i in answer) Console.Write(i+ " ");
}
 
// Driver Code
static public void Main ()
{
  int N = 4;
  generatepermutation(N);
}
}
 
// This code is contributed by Dharanendra L V


Javascript




<script>
 
      // JavaScript program for the above approach
       
      // Function to place first N natural
      // numbers in an array such that none
      // of the values are equal to its indices
      function generatepermutation(N)
      {
        // Stores the required array
        var answer = [];
 
        // Place N at the first position
        answer.push(N);
        console.log(answer);
        // Iterate the range [1, N)
        for (var i = 1; i < N; i++) {
          console.log(answer);
          // Append elements to the sequence
          answer.push(i);
        }
 
        // Print the sequence
        for (var i in answer)
        document.write(answer[i] + "  ");
      }
 
      // Driver Code
      var N = 4;
      generatepermutation(N);
       
</script>


Output

4 1 2 3 

Time Complexity: O(N)
Auxiliary Space: O(N) because using extra space for vector



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads