Question

In: Computer Science

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 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 A1, A2, A3, ..., An where Ai describe i-th ball's color.

Format Output

Output T line with the 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 ≤ 100000
  • 1 ≤ Ai ≤ 1000

Sample Input & Output (standard input & output)

5
5
1 1 2 2 2
Case #1: 1
5
1 2 2 2 2
Case #2: 4
10
1 3 3 3 3 3 2 2 2 2
Case #3: 14
5
1 2 2 3 3
Case #4: 0
10
2 2 2 2 2 2 2 2 2 2
Case #5: 120

Solutions

Expert Solution

#include<stdio.h>
#include<stdlib.h>
int binomialCoeff(int n, int k);
void range_finder(int no_balls, int *color, int case_no);
int main(void)
{
    int no_testcases, no_balls, a, b;
    int *color;
    printf("Enter No. of test cases less than 10:");
    scanf("%d", &no_testcases);
    if(no_testcases > 10)
        no_testcases = 10;//Constraint
    for(a = 0; a < no_testcases; a++)
    {
        printf("Enter no. of balls:");
        scanf("%d", &no_balls);
        if(no_balls > 100000)
        {
            no_balls = 100000;//Constraint
        }
        color = (int *)malloc(sizeof(int) * no_balls);
        printf("Please enter the color code of balls within 1000\n");
        for(b = 0; b < no_balls; b++)
        {
            scanf("%d", &color[b]);
        }
        for(b = 0; b < no_balls; b++)// you can comment it, this is for checking whether the color array is taking correct value or not
        {
            printf("%d", color[b]);
        }
        printf("\n");
        range_finder(no_balls, color, a + 1);
        free(color);
    }
    return 0;
}
void range_finder(int no_balls, int *color, int case_no)
{
    int a, count = 1, reg, sum = 0;
    for(a = 0; a < no_balls; a++)
    {
        if(color[a] == color[a + 1])
        {
            count++;
            reg = count;
        }
        else
        {
            count = 1;
            if(reg >= 3)
            {
                sum = sum + binomialCoeff(reg, 3);
            }
        }
    }
    printf("Case #%d:%d\n", case_no, sum);
}
int binomialCoeff(int n, int k)
{
    // Base Cases
    if (k == 0 || k == n)
        return 1;

    // Recursive
    return (binomialCoeff(n - 1, k - 1) +  binomialCoeff(n - 1, k));
}

O/P:


Related Solutions

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...
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...
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...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
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 : Museum Heist Jojo loves art. In his free time, he...
C Programming Language Problem Title : Museum Heist Jojo loves art. In his free time, he usually goes to the museum and admires the artwork there. since Jojo loves are so much, he is planning a heist at the museum. Jojo knows the price of every piece of art and doesn't want to raise suspicion, so he decided to steal the second most expensive art piece. It is guaranteed that there are at least two art pieces with different prices....
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an...
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an array, they will put it from index 0 until N - 1 using for. But, Bibi is bored to code like that. So, she didin't want to input the number that way. So Bibi challenged you to make a program that will read a sequence (represent index) that she made, then input the number to an array but input it with the same sequence...
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements....
C PROGRAMMING LANGUAGE Problem title : Bibi's Array Bibi also has an array containing N elements. Like Lili, Bibi wants to know the highest frequency (most occurrences) and all elements which have that frequency. Format Input The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤ i ≤...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is...
C Programming Language Title : Making wave In Physics, Mathematics, and related fields, a wave is a disturbance of one of more fields such that the field values oscillate repeatedly about a stable equilibrium value. Waves are usually represented using mathematical functions of the form F (x, t), where x = position and t = time. Your task is to write a program that will visualize a given wave for exactly N seconds. You do not need to worry about...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT