Arrange Balls | Practice | GeeksforGeeks

There are p balls of type P, q balls of type Q and r balls of type R. Using the balls we want to create a straight line such that no two balls of same type are adjacent.
 
Example 1:
Input: p = 2, q = 2, r = 2
Output: 30
Explanation: There are 30 possible arrangements
of balls. Some of them are PQR, PRQ, PRP, PRQ,
PQR,...

Example 2:
Input: p = 1, q = 1, r = 1
Output: 6
Explanation: There are 6 possible arrangements
and these are PQR, PRQ, QPR, QRP, RPQ, RQP.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function CountWays() which takes count of P type balls, Q type balls and R type balls and returns total number of possible arrangements such that no two balls of same type are adjacent modulo 109 + 7.
 
Expected Time Complexity: O(N3) where N = max(p, q, r)
Expected Space Complexity: O(N3)
 
Constranits: 
1 <= p, q, r <= 100 
0
somparna7 months ago
GFG article mentions this problem as hard then why is it medium here. :(
 
0
Yashwant Kumar8 months ago
Yashwant Kumar
Traceback (most recent call last):  File "/home/bbb9a1b5f4d7e0ff26f9ceefd79...", line 42, in <module>    p, q, r = input().split(" ")ValueError: too many values to unpack (expected 3)Why this is coming ??something wrong with input test cases ??
0
Abhishek Ranjan Singh9 months ago
Abhishek Ranjan Singh
For those getting segmentation fault in the submission, define the function (where you coded the algorithm) and dp array globally. It solved my problem.
0
Abhishek Ranjan Singh9 months ago
Abhishek Ranjan Singh
Not able to submit the question. Giving segmentation fault. I have used a dp array dp[101][101][101][3];
Please check the issue!!!
0
pradeep gupta1 year ago
pradeep gupta
Siple java Solution-
https://practice.geeksforge...
0
Debarshi Maitra2 years ago
Debarshi Maitra
Top Down DP :  https://ide.geeksforgeeks.o...
https://uploads.disquscdn.c...
0
Anik Patra2 years ago
Anik Patra
recursion with memoization .here is my code link->https://ide.geeksforgeeks.o...we have total four choices--->0,1,2,3when choice is 0 in starting we can choose any of the three type of balls.but when last ball choice is 1 we can only choose 2 or 3.so they will be added.similarly for last ball choice 2 and 3.so we need 4 dimensional dp state. 3 dimensional for 3 types of balls and extar one dimansion for the choice available now;
0
Mohit Gautam2 years ago
Mohit Gautam
solution here#include <bits stdc++.h="">using namespace std;
int total(int p,int q,int r,char last,int tot){    if(tot==0)    return 1;    int count=0;    if(p>0 && last!='P')    {        count+=total(p-1,q,r,'P',tot-1);    }    if(q>0 && last!='Q')    {        count+=total(p,q-1,r,'Q',tot-1);    }    if(r>0 && last!='R')    {        count+=total(p,q,r-1,'R',tot-1);    }    return count;}int main() {int t;cin>>t;while(t--){    int x,y,z;    cin>>x>>y>>z;    cout<<total(x,y,z,'x',x+y+z)<<endl; }="" return="" 0;="" }="">
0
hemanth gadarla2 years ago
hemanth gadarla
https://ide.geeksforgeeks.o...JAVA Solution this approach works for even large 1<=p,q,r<=100
0
Shubham Patel2 years ago
Shubham Patel
Can anyone please explain the algorithm or provide a better resource to learn it ?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.