Question

In: Computer Science

Maximum Lili is a greedy girl. She has N box, each box contains a coin. She...

Maximum

Lili is a greedy girl. She has N box, each box contains a coin. She want to get as many coin values as possible, but she must only choose 2 boxes. However, she is bad at mathematics and asked you to help her determine what is the correct answer.

Format Input:

Input starts with an integer T, describing the number of test cases. Each test case starts with an integer N, the number of boxes that Lili has. The next line will contain N numbers Vi , each of them describe the value of the coin in the i-th box. It is guaranteed that the value will always be between -1000000 and 1000000.

Format Output:

For each test case, output a single line consisting of ”Case #X: Y” where X is the test case number and Y is the maximum value Lili can get by choosing exactly 2 boxes.

Constraints

• 1 ≤ T ≤ 10

• 2 ≤ N ≤ 1, 000, 000

• −1, 000, 000 ≤ Vi ≤ 1, 000, 000

Sample Input (standard input):

3

5

1 2 3 4 5

4

4 4 4 4

3

10 1 2

Sample Output (standard output):

Case #1: 9

Case #2: 8

Case #3: 12

note : use c language, integer must be the same as the constraint, font use void/result code it under int main (){

Solutions

Expert Solution

Solution :-

We need to find the sum of the largest number and the second largest number in the array that will be given to us. Because Lili is a greedy girl and she wants to get as many coin values as possible.

The constraints are given as

1 ≤ T ≤ 10

2 ≤ N ≤ 1, 000, 000

−1, 000, 000 ≤ Vi ≤ 1, 000, 000

Let's see the steps how we can get the maximum values of the coins that are present in the array.

Step 1. We will have two variables, the largest value will be stored in the variable largest and the second largest value will be stored in the variable large.

Step 2. We will search the entire array for the largest and the large values, so that at the end we can print the value of the result.

Step 3. Initially, the value of largest and large is set to −1, 000, 001 and if we found a value that is greater than or equal to the value of largest, then we replace the value of largest with the value of the array element.

Step 4. On searching other elements, if the value of the array element is greater than the value of large, then the value of large is replaced with the value of the array element, but before that it is checked whether the previous value of largest was greater than the value of large or not, if yes then large is assigned to the previous value of largest.

Step 5. If the value of the array element is not greater than largest, then it is compared to the value of large. If large is smaller than the current array element, then the value of the array element is assigned to large.

The source code is shown below :-

#include <stdlib.h>
#include <stdio.h>

int main() {
        
        int tc;  // the variable tc is the number of test cases that the program will run for.
        
        scanf("%d", &tc);
        
        int count = 1;  // counter which is used while printing the case number of the output.
        
        while(tc--){
        
        long int largest = -1000001, large = -1000001, N; // the value large and largest is set as the minimum so that it will be easy to change the value while comparing.
        
        
        scanf("%ld", &N);
        
        long int A[N]; // the array A in which all the coin values are present.
        
        for(long int i = 0; i<N; i++)
        {
                scanf("%ld", &A[i]);  // taking input in the array.
        }
        
        for(long int i = 0; i<N; i++)  // choosing the value of largest and large from the array.
        {
                if(A[i] > largest)  // if the array element is greater than the current value of largest.
                {
                        if(largest > large)  // the previous value of largest can be stored in large as the value of large is going to change.
                                large = largest;
                                
                        largest = A[i];
                }
                else
                if(A[i] > large)  // if the value of array element is not greater than largest then it is compared to large.
                {
                        large = A[i];
                }
        }
        
        printf ("Case #%d: %ld\n", count, (largest + large));  // finally the output is printed out using this statement.
        
        count++;
        }
        return 0;
}

The screenshots of the output of the program are shown below :-

In this case the input is same as that of the question.

In this case, the input is given and accordingly, the output is found out.

Test cases = 4.

1st :- 2 elements. 6, 4.

Output = 10 (6 + 4).

2nd :- 8 elements. 4, 6, 11, 3, 14, 3, 10, 9.

Output = 25 (14 + 11).

3rd :- 5 elements. 8, 6, 5, 4, 2.

Output = 14 (8 + 6).

4th :- 3 elements. 10, 8, 11.

Output = 21 (11 + 10).


Related Solutions

A box contains three fair coins and one biased coin. For the biased coin, the probability...
A box contains three fair coins and one biased coin. For the biased coin, the probability that any flip will result in a head is 1/3. Al draws two coins from the box, flips each of them once, observes an outcome of one head and one tail and returns the coins to the box. Bo then draws one coin from the box and flips it. The result is a tail. Determine the probability that neither Al nor Bo removed the...
A player tosses a fair coin n times. Each time, if the coin lands on Head,...
A player tosses a fair coin n times. Each time, if the coin lands on Head, the player win a dollar, otherwise, the player loses a dollar. What is the expected earning of the player? What is the variance of the earning of the player? Now the player apply a new strategy: the player will play the game until he gets a head. What is the expected earning of the new strategy? Does the result depend on n?
A box contains N identical gas molecules equally divided between its two halves. For N =...
A box contains N identical gas molecules equally divided between its two halves. For N = 30, what are the following? (a) the multiplicity W of the central configuration (b) the total number of microstates (c) the percentage of the time the system spends in the central configuration % For N = 52, what are the following? (d) W of the central configuration (e) the total number of microstates (f) the percentage of the time the system spends in the...
you have a purse which contains six coins, each coin is either a penny , a...
you have a purse which contains six coins, each coin is either a penny , a two pence coin, a ten pence coin or a twenty pence coin. find the expected total value of all the coins in the purse.
Company produces and sells 90,000 boxes of specialty foods each year. Each box contains the same...
Company produces and sells 90,000 boxes of specialty foods each year. Each box contains the same assortment of food. The company has computed the following annual costs: Cost Item Total Costs Variable production costs $630,000 Fixed production costs 470,000 Variable selling costs 180,000 Fixed selling and administrative costs 170,000 Total costs $1,450,000 Mendenhall normally charges $ 30 per box. A new distributor has offered to purchase 9,000 boxes at a special price of $ 27 per box. Mendenhall will incur...
We have a bag that contains n red balls and n blue balls. At each of...
We have a bag that contains n red balls and n blue balls. At each of 2n rounds we remove one of the balls from the bag randomly, and place it in one of available n bins. At each round, each one of the balls that remain in the bag is equally likely to be picked, as is each of the bins, independent of the results of previous rounds. Let Nk be the number of balls in the k-th bin...
There are 5 boxes. One box is empty; two boxes each contains one marble; and the...
There are 5 boxes. One box is empty; two boxes each contains one marble; and the remaining two boxes each contains two marbles. Suppose that the colors of the marbles are not known, but it is known that each marble can only be either red or green, with equal probability. Suppose that ONE box is randomly chosen from the 5 boxes, and let R denote the number of the red marble(s) the box has, and G be the number of...
Company produces and sells 55 comma 00055,000 boxes of specialty foods each year. Each box contains...
Company produces and sells 55 comma 00055,000 boxes of specialty foods each year. Each box contains the same assortment of food. The company has computed the following annual​ costs: Cost Item Total Costs Variable production costs $330,000 Fixed production costs 520,000 Variable selling costs 165,000 Fixed selling and administrative costs 160,000 Total costs $1,175,000 GarrardGarrard normally charges $ 21$21 per box. A new distributor has offered to purchase 5 comma 5005,500 boxes at a special price of $ 17$17 per...
A con man has 3 coins. One coin has been specially made and has a head on each side. A second coin has..
A con man has 3 coins. One coin has been specially made and has a head on each side. A second coin has been specially made, and on each side it has a tail. Finally, a third coin has a head and a tail on it. All coins are of the same denomination. The con man places the 3 coins in his pocket, selects one, and shows you one side. It is heads. He is willing to bet you even...
A box contains 19 large marbles and 18 small marbles. Each marble is either green or...
A box contains 19 large marbles and 18 small marbles. Each marble is either green or white. 9 of the large marbles are green, and 8 of the small marbles are white. If a marble is randomly selected from the box, what is the probability that it is small or green? Express your answer as a fraction or a decimal number rounded to four decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT