Member-only story

Find The Number of Combinations, Given N and R | Coding Interview | Maths

Ganesh Prasad
2 min readJul 30, 2022

--

implementation of combinations formula.

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: 20
Explanation: Given 6 items we can make 20 combinations using 3 items from the original set.Example 2:
Input: N = 5, R = 5
Output: 1
Explanation: 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.

--

--

Ganesh Prasad
Ganesh Prasad

Written by Ganesh Prasad

Backend Developer at Appscrip | C++ veteran, 💜 Dart

No responses yet