
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Ways to place items in n^2 positions such that no row/column contains more than one in C++
In this problem, we are given an integer n such that there are n lines vertically and n horizontally that are placed such that there are n2 intersection between these lines. Our task is to find the total number of ways by which 4 items can be placed on these intersections in
such a way that no row and column contains more that one item.
Let’s take an example to understand the problem,
Input
n=4
Output
24
Explanation
To solve this problem, we will have to choose 4 horizontal lines from n lines that will have items which will be nC4. Now, every horizontal line has n vertical lines so, there will be n way to place an item in the first selected horizontal line. Then, we will move to the next selected horizontal line where there will be n-1 possible placements. And it the same way third could be placed in n-2 and forth in n-3 ways. So, the total number of wats will be nC4*n*(n-1)*(n-2)*(n-3)
Program to show the implementation of the algorithm,
Example
#include <iostream> using namespace std; long long placeItems(int n) { return (1LL * (1LL * ((n) * (n - 1) * (n - 2) * (n - 3)) / (4 * 3 * 2 * 1)) * ((1LL * (n) * (n - 1) * (n - 2) * (n - 3)))); } int main() { int n = 4; cout<<"The number of way is which 4 items can be placed in the intersection of "<<n; cout<<" lines vertically and horizotally are "<<placeItems(n); return 0; }
Output
The number of way is which 4 items can be placed in the intersection of 4 lines vertically and horizotally are 24
- Related Articles
- Place N^2 numbers in matrix such that every row has an equal sum in C++
- Select MySQL rows where column contains same data in more than one record?
- Match the items in column A with one or more items in column B.
- MySQL search if more than one string contains special characters?\n
- Match the items in Column I with one or more items of Column II.
- Match the items given in column I with one or more items given in column II:
- Match the items given in Column I with one or more items of Column II.
- How to sort more than one column at a time in MySQL?
- MySQL query to include more than one column in a table that doesn't already exist
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- How to remove row that contains maximum for each column in R data frame?
- How to select more than one row at a time in a JTable with Java?
- Find the column name that contains value greater than a desired value in each row of an R data frame.
- Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
