Question

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, font use void/result code it under int main (){

Homework Answers

Answer #1

SIMPLE SOLUTION:

We could compute the sum for every query, which means we have one inner loop for each query for(j=a[i]------->b[i]) we add arr[j] to sum and finally print sum.

Example:

If A[i]=2 and B[i]=25 we run loop from 2 to 25 and add corresponding element which is initially initialized to 0

EFFICIENT SOLUTION

We could pre-compute prefix sum. Let pre[i] stores sum of element from arr[0] to arr[i-1]. To answer sum of element from A[i] to B[i] we could print

Here is an code snippet in C# and output's Screenshot

using System.IO;
using System;

class Program
{
    static void Main()
    {
        int n=Convert.ToInt32(Console.ReadLine());
        int[] arr=new int[n];
        string line=Console.ReadLine();
        string[] tokens = line.Split(' ');
        arr = Array.ConvertAll(tokens, int.Parse);
        int q=Convert.ToInt32(Console.ReadLine());
        int[]prefix=new int[n+1];
        prefix[0]=0;
        for(int i=0;i<n;i++)
            prefix[i+1]=prefix[i]+arr[i];
        int[]a=new int[q];
        int[]b=new int[q];
        int[]res=new int[q];
        for(int i=0;i<q;i++)
        {
            line=Console.ReadLine();
            tokens=line.Split(' ');
            int[]numbers=Array.ConvertAll(tokens, int.Parse);
            a[i]=numbers[0];
            b[i]=numbers[1];
            res[i]=prefix[b[i]]-prefix[a[i]-1];// Storing answer for ith query
        }
        for(int i=0;i<q;i++)
            Console.WriteLine("Case #"+(i+1)+": "+res[i]);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C Programming Language Problem Title : Promotion Jojo is at the supermarket buying monthly groceries. As...
C Programming Language Problem Title : Promotion Jojo is at the supermarket buying monthly groceries. As he was passing alley by alley, a certain banner caught his interest. Buy K cans of cola and get 1 free while one can of cola costs D dollars. As an avid cola fan, Jojo wouldn’t want to miss this amazing opportunity. Jojo is bad at math and so he asks you to count the price that he needs to pay if he plans...
C Programming Language Problem Title : Container Today is Jojo’s birthday. To prepare the birthday party,...
C Programming Language Problem Title : Container Today is Jojo’s birthday. To prepare the birthday party, Jojo asks Bibi to bring N cubes with edge length S for the game on his birthday party. The only idea that came up to Bibi’s mind is to bring the cubes with rectangular box containers. Then Bibi went to a store. The only container available in the store is a container with size A × B × C. Bibi is a thrifty person....
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to...
Write the Java(Java 7 or Java 8) program for this problem:- Thanos, in his mission to restore the ecological balance in the universe, has reached planet earth. He considers a planet ecologically balanced if more than half of the people on the planet have the same Consumption Capacity There are N people on planet earth, each having Consumption Capacity C1, C2, ...CN and Strength S1, S2... Sn . Thanos will make earth ecological balanced by killing some people(Possibly None). To...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on how many matching numbers are provided by a player. The player provides three different integers between 1 and 10. If there is a match of all 3 numbers, the winning $ 1000. If there is a match with 2 numbers, the winning $ 10. If there is a match with 1 number, the winning $ 1. With no match, the winning is $0. Write...
C Programming Language Problem Title : Magical Cave Lili, a great magician, has a mission to...
C Programming Language Problem Title : Magical Cave Lili, a great magician, has a mission to enter a cave to get treasure inside. The cave only has 1 path without branches. But the cave is not safe because there are some traps inside that can reduce Lili’s life points. But in addition to traps, the cave also has potions that can increase Lili’s life points. Before entering the cave, Lili casts magic that can reveal all the traps and potions...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as an input. The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string. Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together. Let the frequency of a character, lying in...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...