In: Computer Science
Take Three
Jojo just graduated and moved up to grade 4. Today is his first day in 4th grade. Unfortunately, the lessons are held online because of pandemic. So that the quality of learning remains good, Jojo’s teacher gives a hard task for 4th grader. After the 4th graders finished their first task which is prime factorization. Jojo’s teacher set up a game for the stundets. The game is very simple. Given N colored balls, each student has to take 3 balls randomly. If a student got 3 balls with the same color, then the student counted as winner. Jojo is angry because he knows that this game is just pure luck to reach its goal. On the other hand, Jojo wants to know the number of possibilities to get 3 balls with the same color. As a good friend of Jojo, help Jojo to count the number of possibilities to get 3 balls with the same color.
Format Input:
There are T testcases. Every testcase contains two rows. The first row consists of one integer N which indicates the number of balls. The second row consists of N integers A 1, A 2, A 3, ..., A n where A i describes i-th ball’s color.
Format Output:
Output T line with format “Case # X: ”, where X indicates the testcase number and then followed by an integer describes the number of possibilities to get 3 balls with the same color.
Constraints
• 1 ≤ T ≤ 10
• 3 ≤ N ≤ 10 5
• 1 ≤ A i ≤ 1000
Sample Input (standard input) :
5
5
1 1 2 2 2
5
1 2 2 2 2
10
1 3 3 3 3 3 2 2 2 2
5
1 2 2 3 3
10
2 2 2 2 2 2 2 2 2 2
Sample Output (standard output):
Case #1: 1
Case #2: 4
Case #3: 14
Case #4: 0
Case #5: 12
note : use C language, integer must be the same as the constraint, DONT USE VOID/RESULT/QSORT, (the output must be the same, print the "Case.." too)code it under int main (){
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int min(int x , int y)
{
if(x<=y)
return x;
return y;
}
int calculate_nc3(int n, int k){
if(n==3)
return 1;
int C[k+1];
for(int i=0;i<=k;i++)
C[i]=0; // to remove junk values
C[0] = 1; // nC0 is 1
for (int i = 1; i <= n; i++)
{
for (int j = min(i, k); j > 0; j--)
{
C[j] = C[j] + C[j-1];
}
}
return C[k];
}
int fun(int n, int arr[]){
int map[1001];
for(int i=0;i<1001;i++){
map[i]=0;
}
for(int i=0;i<n;i++)
{
map[arr[i]]+=1;
}
int total=0;
for(int i=0;i<1001;i++){
if(map[i]>=3){
total += calculate_nc3(map[i],3);
}
}
return total;
}
int main()
{
int t,n;
scanf("%d", &t);
int i=0;
while(i<t){
scanf("%d", &n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d", &arr[i]);
int ans= fun(n,arr);
printf("Case #%d :%d\n",i+1,ans);
i++;
}
return 0;
}
Basically this is the question on P'n'C where you have to choose 3 out of many. I hope it helps
Kindly upvote pls?