Single dimensional array | Sololearn: Learn to code for FREE!


Single dimensional array

#How can i get second largest and second smallest number in any array? I want logic with different different methods?


12/17/2018 4:20:00 AM

Umesh Sharma

5 Answers

New Answer


Hy Umesh Sharma ● simple way to solve this is to sort array in ascending order & then taking out values at index n-2 & 1. ● U can click on profile pic of user to get to his SL profile


thank you gaurav. but with sorting i knw but i want without sorting concept.. thats why i am here actully.... i done with sorting but without sorring i want. thanks buddy again👍


You should first get the largest and the smallest numbers in the array, then go through the array and check every number that is greater then previous number and smaller then largest number. For the second smalles do same thing but check for every number that is smaller then previous and greater then smallest number! for example: int arr[6] = {1, 2, 3, 4, 5, 6}; int secondMax = 0; int secondMin = 0; for(int i = 0; i < 6; i++) { if( arr[i] > secondMax && i < 6) secondMax = arr[i]; } for(int i = 0; i < 6; i++) { if(arr[i] < secondMin && i > 1) secondMin = arr[i]; }


thank you sekiro....thank you so much.....


by the way, where you from? what you do? from which country?