# Help me in solving LARGESECOND problem

### My issue

My code fails at a test case that inputs 200 elements in an array and only contains 1’s and 2’s. So the correct answer should’ve been 3 (the output by my code) but the right answer according to the test case is 4. Can somebody help me with this?

### My code

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0; i<t; i++){
int n = sc.nextInt();
int[] arr = new int[n];
for(int j=0; j<n; j++){
arr[j] = sc.nextInt();
}
int max = arr;
for(int j=1; j<n; j++){
if(arr[j] > max){
max = arr[j];
}
}
int secondMax = arr;
for(int j=1; j<n; j++){
if(arr[j] > secondMax && arr[j] != max){
secondMax = arr[j];
}
}
System.out.println(max+secondMax);
}
}
}

``````

Learning course: Arrays using Java
Problem Link: CodeChef: Practical coding for everyone

The problem with your code is this part:

``````int secondMax = arr;
``````

Your code will output 4 instead of 3 because it doesn’t take into account that `a` might be equal to the first maximum number.

Corrected Code:

``````import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();

for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int[] arr = new int[n];

for (int j = 0; j < n; j++) {
arr[j] = scanner.nextInt();
}

Arrays.sort(arr);
int idx = n - 2;
while (arr[n - 1] == arr[idx]) {
idx--;
}

System.out.println(arr[n - 1] + arr[idx]);
}
}
}
``````

I simply sorted the array and till the second maximum isn’t equal to the first, I decrement the indext of it.

1 Like

Thanks mate!
{"ft_lang_label":"__label__en","ft_lang_prob":0.6008156,"math_prob":0.9537954,"size":1051,"snap":"2023-40-2023-50","text_gpt3_token_len":297,"char_repetition_ratio":0.11174785,"word_repetition_ratio":0.044692736,"special_character_ratio":0.33872503,"punctuation_ratio":0.18828452,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.997304,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T04:08:06Z\",\"WARC-Record-ID\":\"<urn:uuid:9a84c460-30eb-4b34-bc8f-2404bf44307e>\",\"Content-Length\":\"20217\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:117d9b86-77a1-4053-a0ef-4fe70a282305>\",\"WARC-Concurrent-To\":\"<urn:uuid:3ce7762f-912b-470e-9c21-0e6ad068d1e5>\",\"WARC-IP-Address\":\"44.209.236.196\",\"WARC-Target-URI\":\"https://discuss.codechef.com/t/help-me-in-solving-largesecond-problem/113683\",\"WARC-Payload-Digest\":\"sha1:2BK5ETU4CMXEQ3A3WQFYSVMGOWD73GDI\",\"WARC-Block-Digest\":\"sha1:SQELMUDOVR6AN54EVKDZZA24NAAQ6OQ2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100484.76_warc_CC-MAIN-20231203030948-20231203060948-00542.warc.gz\"}"}