Member-only story
Find The Number of Combinations, Given N and R | Coding Interview | Maths
In this article, we will simply implement the formula used to find the number of combinations, and I don’t think you will get such questions in your interview. But still, if you are a beginner, it will help you understand how we can implement any mathematical formulae.
Problem
Given N and R, where N is the total number of items and R is the number of items to choose, find the total number of combinations we can make.
Example 1:Input: N = 6, R = 3
Output: 20Explanation: Given 6 items we can make 20 combinations using 3 items from the original set.Example 2:
Input: N = 5, R = 5
Output: 1Explanation: We can choose 5 items from 5 items in only one way.
Solution
We know the formula for finding the number of combinations given n and k. We will use the formula and implement it in both the languages, cpp, and python.
nCr = n!/(r!*(n-r)!)
Code (Python)
This is the implementation of the above equation in python.