Mango Distribution

 Coding Problem Keys 

 Mango Distribution 

 Problem Statement 

Given a number of mangoes and number of persons. Find the number of ways to distribute the identical mangoes among identical persons.

 Input Specification 

input1: the number of mangoes
input2: the number of persons.

 Output Specification 

Return the number of ways to distribute identical mangoes among identical persons.

 Sample Input/Output 1 

 Input 

input1: 2
input2: 2

 Output 

Output: 3

 Explanation 

All possible distributions of 2 identical mangoes to 2 identical persons are (1, 1), (2, 0) and (0, 2). Hence the output is 3.

 Sample Input/Output 2 

 Input 

input1: 1
input2: 12

 Output 

Output: 12

 Output 

All possible distributions of 1 identical mango to 12 identical persons are 12. Hence the output is 12.

 Solution 

 Programming Language: Python 3 Language 

def numWays(input1,input2):
    if(input2<input1):
        return 0
    res=1
    input1+=input2-1
    input2=input1-1
    if(input2>input1-input2):
        input2=input1-input2
    for i in range(input2):
        res*=(input1-i)
        res//=(i+1)
    return res
input1=int(input())
input2=int(input())
print(numWays(input1,input2))

# Published By PKJCODERS

 Programming Language: C++ Language 

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int NoOfWays(int input1, int input2){
    if(input2<input1){
        return 0;
    }
    int result=1;
    input1=input1+input2-1;
    input2=input1-1;
    if(input2>input1-input2){
        input2=input1-input2;
    }
    for(int i=0;i<input2;i++){
        result*=(input1-i);
        result/=(i+1);
    }
    return result;
}
int main() {
    int input1,input2;
    cin>>input1>>input2;
    cout<<NoOfWays(input1,input2);
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Toggle Consonants Adjacent to Vowels

String Pattern - Inverted U

Top Scoring Batsman - TEST ODI 20-20

Java - Method Overriding - Area

Balloon Count Burst by Ath Kid

Microsoft