# Help me in solving LARGESECOND problem

### My issue

Hyy,
In this problem I’m arranging the array in increasing order and then taking last number as largest number and second last number which is not same as largest number.Then where is the problem.

### My code

``````#include <iostream>
using namespace std;

void swap ( int &a, int &b ){
int temp = a;
a = b;
b = temp;
}

void solve( int arr[], int n ){
int i, j;
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < n-1; j++ ) {
if ( arr[j] > arr[ j+1 ] ) {
swap( arr[j], arr[ j + 1 ] );
}
}
}
}

int main() {
int t;
std::cin >> t;
while(t--)
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
solve(arr,n);
int largest=arr[n-1];
int second;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=largest)
{
second=arr[i];
}
}

std::cout << largest + second << std::endl;

}
return 0;
}

``````

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

@anujraghuwansh
the sorting part needs to be optimized .
U can perform sorting in nlogn time complexity too.