Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Number of ways in which an item returns back to its initial position in N swaps in array of size K

  • Last Updated : 14 Apr, 2021

Given two numbers K and N, the task is to find the number of ways such that an item at position i returns back to its initial position in an array of length K in N steps, where, in each step, the item can be swapped with any other item in K

Examples: 

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.

Input: N = 2, K = 5 
Output:
Explanation: 
For the given K, lets assume there are 5 positions 1, 2, 3, 4, 5. Since it is given in the question that the item is at some initial position B and the final answer for all B’s is same, lets assume that the item is at position 1 in the beginning. Therefore, in 2 steps (N value): 
The item can either be placed at position 2 and again at position 1. 
The item can either be placed at position 3 and again at position 1. 
The item can either be placed at position 4 and again at position 1. 
The item can either be placed at position 5 and again at position 1. 
Therefore, there are a total of 4 ways. Hence the output is 4.



Input: N = 5, K = 5 
Output: 204 

Approach: The idea to solve this problem is to use the concept of combinations. The idea is that at every step, there are K – 1 possibilities to place the item in the next place. To implement this, an array F[] is used where F[i] represents the number of ways to place the items at position 1 for ‘i’th step. Since it is given that the item doesn’t belong to the person to whom it belonged to the previous step, therefore, the number of ways of the previous step has to be subtracted for every step. Therefore, the array F[] can be filled as:

F[i] = (K - 1)(i - 1) - F[i - 1]

Finally, the last element of the array F[] is returned. 

Below is the implementation of the approach: 

C++




// C++ program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
 
#include <bits/stdc++.h>
using namespace std;
 
#define mod 1000000007
 
// Function to calculate (x^y)%p in O(log y)
long long power(long x, long y)
{
    long p = mod;
 
    // Initialize result
    long res = 1;
 
    // Update x if it is more than or
    // equal to p
    x = x % p;
 
    while (y > 0) {
        // If y is odd, multiply
        // x with result
        if (y & 1)
            res = (res * x) % p;
 
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
 
// Function to return the number of ways
long long solve(int n, int k)
{
    // Base case
    if (n == 1)
        return 0LL;
 
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod
            - solve(n - 1, k) + mod)
           % mod;
}
 
// Drivers code
int main()
{
    int n = 4, k = 5;
 
    // Function calling
    cout << solve(n, k);
 
    return 0;
}

Java




// Java program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
class GFG{
 
static int mod = 1000000007;
 
// Function to calculate (x^y)%p in O(log y)
public static int power(int x, int y)
{
    int p = mod;
   
    // Initialize result
    int res = 1;
   
    // Update x if it is more than
    // or equal to p
    x = x % p;
   
    while (y > 0)
    {
         
        // If y is odd, multiply
        // x with result
        if ((y & 1) != 0)
            res = (res * x) % p;
   
        // y must be even now
        // y = y/2
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
   
// Function to return the number of ways
public static int solve(int n, int k)
{
     
    // Base case
    if (n == 1)
        return 0;
   
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod -
             solve(n - 1, k) + mod) % mod;
}
 
// Driver code
public static void main(String []args)
{
    int n = 4, k = 5;
     
    // Function calling
    System.out.println(solve(n, k));
}
}
 
// This code is contributed by divyeshrabadiya07

Python3




# Python3 program to find the number of ways
# in which an item returns back to its
# initial position in N swaps
# in an array of size K
 
mod = 1000000007
 
# Function to calculate (x^y)%p in O(log y)
def power(x, y):
 
    p = mod
 
    # Initialize result
    res = 1
 
    # Update x if it is more than or
    # equal to p
    x = x % p
 
    while (y > 0) :
        # If y is odd, multiply
        # x with result
        if (y & 1) != 0 :
            res = (res * x) % p
 
        # y must be even now
        # y = y/2
        y = y >> 1
        x = (x * x) % p
 
    return res
 
# Function to return the number of ways
def solve(n, k):
 
    # Base case
    if (n == 1) :
        return 0
 
    # Recursive case
    # F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod - solve(n - 1, k) + mod) % mod
 
n, k = 4, 5
 
# Function calling
print(solve(n, k))
 
# This code is contributed by divyesh072019

C#




// C# program to find the number of ways
// in which an item returns back to its
// initial position in N swaps
// in an array of size K
using System;
class GFG
{
  
  static int mod = 1000000007;
 
  // Function to calculate
  // (x^y)%p in O(log y)
  public static int power(int x,
                          int y)
  {
    int p = mod;
 
    // Initialize result
    int res = 1;
 
    // Update x if it
    // is more than
    // or equal to p
    x = x % p;
 
    while (y > 0)
    {
      // If y is odd, multiply
      // x with result
      if ((y & 1) != 0)
        res = (res * x) % p;
 
      // y must be even now
      // y = y/2
      y = y >> 1;
      x = (x * x) % p;
    }
    return res;
  }
 
  // Function to return
  // the number of ways
  public static int solve(int n,
                          int k)
  {
    // Base case
    if (n == 1)
      return 0;
 
    // Recursive case
    // F(n) = (k-1)^(n-1) - F(n-1).
    return (power((k - 1), n - 1) % mod -
            solve(n - 1, k) + mod) % mod;
  }
 
  // Driver code
  public static void Main(string []args)
  {
    int n = 4, k = 5;
 
    // Function calling
    Console.Write(solve(n, k));
  }
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
    // Javascript program to find the number of ways
    // in which an item returns back to its
    // initial position in N swaps
    // in an array of size K
     
    let mod = 1000000007;
  
    // Function to calculate (x^y)%p in O(log y)
    function power(x, y)
    {
        let p = mod;
 
        // Initialize result
        let res = 1;
 
        // Update x if it is more than or
        // equal to p
        x = x % p;
 
        while (y > 0) {
            // If y is odd, multiply
            // x with result
            if ((y & 1) > 0)
                res = (res * x) % p;
 
            // y must be even now
            // y = y/2
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
 
    // Function to return the number of ways
    function solve(n, k)
    {
        // Base case
        if (n == 1)
            return 0;
 
        // Recursive case
        // F(n) = (k-1)^(n-1) - F(n-1).
        return (power((k - 1), n - 1) % mod
                - solve(n - 1, k) + mod)
               % mod;
    }
     
    let n = 4, k = 5;
  
    // Function calling
    document.write(solve(n, k));
     
</script>

 
 

Output: 
52

 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :