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...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
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...
c++ You have tons of toys that you want to divide into boxes. Each box can...
c++ You have tons of toys that you want to divide into boxes. Each box can store at most T toys and you got plenty of boxes. To distribute the toys, your plan is to divide the N toys in half, forming two smaller piles, then continue dividing each of the small piles in half until we get piles that can fit in the boxes. Find how many boxes you will need. This strategy may not give you the least...
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...
PYTHON------------ Task 1- Remove Number Complete the function remove_number such that given a list of integers...
PYTHON------------ Task 1- Remove Number Complete the function remove_number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2- Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT