Help me in solving LARGESECOND problem

My issue

what’s wrong with my code? I’ve run it in vs code and I got the correct output.

My code

#include<stdio.h>

int main()
{
    int t, n, firstLargest, secondLargest;
    scanf("%d", &t);
    while(t > 0)
    {
        scanf("%d", &n);
        int a[n];
        for(int i = 0; i < n; i++)
           scanf("%d", &a[i]);
        
        firstLargest = a[0];
        secondLargest = a[0];
        for(int i = 1; i < n; i++)
        {
            if(a[i] > firstLargest)
            {
                secondLargest = firstLargest;
                firstLargest = a[i];
            }
            else if(a[i] < firstLargest && a[i] > secondLargest)
            {
                secondLargest = a[i];
            }     
        }
        printf("%d\n", firstLargest + secondLargest); 
        t--;
    }
	return 0;
}


Learning course: Arrays using C
Problem Link: Largest and Second Largest Practice Problem in - CodeChef

@toma003
have corrected it in your code
hope u will get it

#include<stdio.h>

int main()
{
    int t, n, firstLargest, secondLargest;
    scanf("%d", &t);
    while(t > 0)
    {
        scanf("%d", &n);
        int a[n];
        for(int i = 0; i < n; i++)
           scanf("%d", &a[i]);
        
        firstLargest = 0;
        secondLargest = 0;
        for(int i = 0; i < n; i++)
        {
            if(a[i] > firstLargest)
            {
                secondLargest = firstLargest;
                firstLargest = a[i];
            }
            else if(a[i] < firstLargest && a[i] > secondLargest)
            {
                secondLargest = a[i];
            }     
        }
        printf("%d\n", firstLargest + secondLargest); 
        t--;
    }
	return 0;
}

I have run it here but still not getting the correct result. I ran it in vs code again and got the correct result.

@toma003
the code i have sent is working fine dude , plzz recheck .

1 Like