In: Computer Science
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 (){
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).