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...
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...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT