Open In App

Ways to place 4 items in n^2 positions such that no row/column contains more than one

Last Updated : 22 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N where 4 ? N ? 100. There are N lines vertically and N lines horizontally. So, There are N2 intersections. The task is find the number of ways to place 4 items in these N2 positions such that each row and column contain not more than one item.
Examples: 
 

Input: N = 4 
 

Output: 24
Input: N = 5 
Output: 600 
 

 

Approach: The number of ways to choose 4 horizontal lines that will have items from n is nC4. There are n ways to place an item on the first of these lines. Given the place of the first item, there are n – 1 ways to place an item on the second of these lines because one of the vertical lines is already taken. Given the places of the first and second items, there are n – 2 ways to place an item on the third line and the same way n – 3 for the fourth item. The total number of ways to place items on selected 4 horizontal paths is n * (n – 1) * (n – 2) * (n – 3). So the result is nC4 * n * (n – 1) * (n – 2) * (n – 3).
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the number of ways
// to place 4 items in n^2 positions
long long NumberofWays(int n)
{
    long long x = (1LL * (n) * (n - 1) * (n - 2) * (n - 3))
                  / (4 * 3 * 2 * 1);
    long long y = (1LL * (n) * (n - 1) * (n - 2) * (n - 3));
 
    return (1LL * x * y);
}
 
// Driver code
int main()
{
    int n = 4;
    cout << NumberofWays(n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
// Function to return the number of ways
// to place 4 items in n^2 positions
static long NumberofWays(int n)
{
    long x = (1l * (n) * (n - 1) * (n - 2) * (n - 3))
                / (4 * 3 * 2 * 1);
    long y = (1l * (n) * (n - 1) * (n - 2) * (n - 3));
 
    return (1l * x * y);
}
 
// Driver code
public static void main(String args[])
{
    int n = 4;
    System.out.println( NumberofWays(n));
}
}
 
// This code is contributed by Arnab Kundu


Python3




# python implementation of the approach
 
# Function to return the number of ways
# to place 4 items in n^2 positions
def NumbersofWays(n):
    x = (n * (n - 1) * (n - 2) * (n - 3)) // (4 * 3 * 2 * 1)
    y = n * (n - 1) * (n - 2) * (n - 3)
 
    return x * y
 
# Driver code
n = 4
print(NumbersofWays(n))
 
# This code is contributed by Shrikant13


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the number of ways
// to place 4 items in n^2 positions
public static long NumberofWays(int n)
{
    long x = (1l * (n) * (n - 1) * (n - 2) *
               (n - 3)) / (4 * 3 * 2 * 1);
    long y = (1l * (n) * (n - 1) * (n - 2) *
                (n - 3));
 
    return (1l * x * y);
}
 
// Driver code
public static void Main(string[] args)
{
    int n = 4;
    Console.WriteLine(NumberofWays(n));
}
}
 
// This code is contributed by Shrikant13


PHP




<?php
// PHP implementation of the approach
 
// Function to return the number of ways
// to place 4 items in n^2 positions
function NumberofWays($n)
{
    $x = (1 * ($n) * ($n - 1) *
                     ($n - 2) * ($n - 3)) /
                          (4 * 3 * 2 * 1);
    $y = (1 * ($n) * ($n - 1) *
                     ($n - 2) * ($n - 3));
 
    return (1 * $x * $y);
}
 
// Driver code
$n = 4;
echo NumberofWays($n);
 
// This code is contributed by mits
?>


Javascript




<script>
 
      // JavaScript implementation of the approach
      // Function to return the number of ways
      // to place 4 items in n^2 positions
      function NumberofWays(n) {
        var x = (1 * n * (n - 1) * (n - 2) * (n - 3)) /
        (4 * 3 * 2 * 1);
        var y = 1 * n * (n - 1) * (n - 2) * (n - 3);
 
        return 1 * x * y;
      }
 
      // Driver code
      var n = 4;
      document.write(NumberofWays(n));
       
</script>


Output: 

24

 

Time Complexity: O(1)

Auxiliary Space: O(1)



Similar Reads

Distributing items when a person cannot take more than two items of same type
Given N sweets, which can be of many different types, and k customers, one customer won’t make the same type of sweet more than 2 pieces, the task is to find if it is possible to distribute all, then print "Yes" or otherwise "No".Given an array, arr[] represents an array of sweets. arr[i] is type of sweet. Examples: Input : arr[] = {1, 1, 2, 3, 1},
8 min read
Probability of distributing M items among X bags such that first bag contains N items
Given three integers N, M, X. The task is to find the probability of distributing M items among X bags such that first bag contains N items Examples: Input : M = 7, X =3, N = 3 Output : 0.2 The Number of ways to keep 7 items in 3 bags is [Tex]6\choose 2 [/Tex]. The Number of ways to keep 4 items in 2 bags is [Tex]3\choose 1 [/Tex]. As the first bag
5 min read
Length of longest subarray in which elements greater than K are more than elements not greater than K
Given an array arr[] of length N. The task is to find the length of the longest subarray in which elements greater than a given number K are more than elements not greater than K.Examples: Input : N = 5, K = 2, arr[]={ 1, 2, 3, 4, 1 } Output : 3 The subarray [2, 3, 4] or [3, 4, 1] satisfy the given condition, and there is no subarray of length 4 or
10 min read
Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
Give an integer array arr[] consisting of elements from the set {0, 1}. The task is to print the number of ways the array can be divided into sub-arrays such that each sub-array contains exactly one 1. Examples: Input: arr[] = {1, 0, 1, 0, 1} Output: 4 Below are the possible ways: {1, 0}, {1, 0}, {1}{1}, {0, 1, 0}, {1}{1, 0}, {1}, {0, 1}{1}, {0, 1}
6 min read
Number of Ways to Cut a Rectangular Pie such that each piece contains at least one Orange
You are given a Rectangular Pie, represented as a 2D matrix containing the following characters: 'O' (an orange) and ' . ' (empty cell). You have given an integer P to cut the pie into P pieces using P-1 cuts. For each cut you choose the direction vertical or horizontal, then you choose a cut position at the cell boundary and cut the pie into two p
13 min read
Check if a String contains any index with more than K active characters
Given a string S, containing lowercase English alphabets, and an integer K, the task is to find any index of the string which consists of more than K active characters. If found, print Yes. Otherwise, print No. Count of active characters for any index is the number of characters having previous occurrences before or at the current index and last oc
6 min read
Count of substrings in a Binary String that contains more 1s than 0s
Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. Examples Input: S = "110011"Output: 11Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are { S[0]}, {S[0], S[1]}, {S[0], S[2]}, {S[0], S[4]}, {S[0], S[5]}, {S[1]
20 min read
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
Given three integers N, M and K. The task is to find the number of ways to fill N positions using M colors such that there are exactly K pairs of different adjacent colors. Examples: Input: N = 3, M = 2, K = 1 Output: 4 Let the colors be 1 and 2, so the ways are: 1, 1, 2 1, 2, 2 2, 2, 1 2, 1, 1 The above 4 ways have exactly one pair of adjacent ele
12 min read
Total number of ways to place X and Y at n places such that no two X are together
Given N positions, the task is to count the total number of ways to place X and Y such that no two X are together. Examples: Input: 3Output: 5XYX, YYX, YXY, XYY and YYY Input: 4Output: 8XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY Approach: For N = 1, X and Y are 2 possible ways. For N = 2, XY, YX and YY are the 3 possible ways. For N = 3, XYX
5 min read
Number of positions such that adding K to the element is greater than sum of all other elements
Given an array arr[] and a number K. The task is to find out the number of valid positions i such that (arr[i] + K) is greater than sum of all elements of array excluding arr[i].Examples: Input: arr[] = {2, 1, 6, 7} K = 4 Output: 1 Explanation: There is only 1 valid position i.e 4th. After adding 4 to the element at 4th position it is greater than
5 min read