Question

In: Computer Science

Take Three Jojo just graduated and moved up to grade 4. Today is his first day...

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 (){

Solutions

Expert Solution

#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?


Related Solutions

Take Three Jojo just graduated and moved up to grade 4. Today is his first day...
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...
Jojo just graduated and moved up to grade 4. Today is his first day in 4th...
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. The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Prime factorization of a number is...
Jojo just graduated and moved up to grade 4. Today is his first day in 4th...
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. The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Prime factorization of a number is...
C Programming Language Problem Title : Take Three Jojo just graduated and moved up to grade...
C Programming Language Problem Title : 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 the 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...
C Programming Language Problem Title : 4th Grade Jojo just graduated and moved up to grade...
C Programming Language Problem Title : 4th Grade 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. The first task is to find the prime factorization of a number. Prime number is a natural number greater than 1 that is not a product of two smaller...
Ivana has just moved to Edmonton to take up a position in the provincial government, earning...
Ivana has just moved to Edmonton to take up a position in the provincial government, earning $72,000 a year. Edmonton is a big city, so she has decided to lease a car, as well as buy a downtown condo. She has saved some money living at home with her parents up until now. But Ivana is a bit worried about her financial affairs as she has always had trouble managing money. For example, her last credit card statement showed a...
ANSWER LETTER (A) ONLY Ivana has just moved to Edmonton to take up a position in...
ANSWER LETTER (A) ONLY Ivana has just moved to Edmonton to take up a position in the provincial government, earning $72,000 a year. Edmonton is a big city, so she has decided to lease a car, as well as buy a downtown condo. She has saved some money living at home with her parents up until now. But Ivana is a bit worried about her financial affairs as she has always had trouble managing money. For example, her last credit...
Burt is saving up for his retirement. Today is his 36th birthday. Burt first started saving...
Burt is saving up for his retirement. Today is his 36th birthday. Burt first started saving when he was 27 years old. On his 27th birthday, Burt made the first contribution to his retirement account when he deposited $2,000. Each year on his birthday, Burt has contributed another $2,000 to the account. The 10th (and last) of these contributions was made earlier today on his 36th birthday. The account has paid an effective annual rate of return of 5.4%. a)...
Burt Sbeez is saving up for his retirement. Today is his 40th birthday. Burt first started...
Burt Sbeez is saving up for his retirement. Today is his 40th birthday. Burt first started saving when he was just 25 years old. On his 25th birthday, Burt made the first contribution to his retirement account when he deposited $3,000. Each year on his birthday, Burt has contributed another $3,000 to the account. The 16th (and last) of these contributions is made today. The account has paid interest at the rate of 4.2% APR, compounded monthly. Burt wants to...
Grady​ Zebrowski, age​ 25, just graduated from​ college, accepted his first job with a ​$45 comma...
Grady​ Zebrowski, age​ 25, just graduated from​ college, accepted his first job with a ​$45 comma 000 ​salary, and is already looking forward to retirement in 40 years. He assumes a 3.5 percent inflation rate and plans to live in retirement for 20 years. He does not want to plan on any Social Security benefits. Assume Grady can earn a 6 percent rate of return on his investments prior to retirement and a 6 percent rate of return on his...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT