+ 2

If we have given array and have to find product of second smallest and second largest no then hw to write program for that?

12th Dec 2019, 6:27 PM
Pragati Pawar
Pragati Pawar - avatar
11 odpowiedzi
+ 3
You can check out this code👇🏻 https://code.sololearn.com/cQai7jYzQm8w/?ref=app
14th Dec 2019, 8:18 AM
Smriti Kaur
Smriti Kaur - avatar
+ 3
Smriti Kaur Your code works fine but since you are using java, you can just replace the whole sorting algorithm with this line- Arrays.sort(arr);
14th Dec 2019, 2:03 PM
Avinesh
Avinesh - avatar
+ 2
First sort the array in ascending order then get 2nd smallest and 2nd largest value and multiply them.
12th Dec 2019, 6:47 PM
A͢J
A͢J - avatar
+ 1
Are your array elements unique?
12th Dec 2019, 7:11 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Sort the Array and you'll have them on Index 1 & n-1
14th Dec 2019, 5:54 AM
zexu knub
zexu knub - avatar
+ 1
If the array is sorted, it is trivial thing to do. If not, you can iterate trough the array and use 4 different variables to hold largest,second largest, smallest and second smallest values.
14th Dec 2019, 6:10 PM
Ville Keituri
Ville Keituri - avatar
0
for smallest: take number to n is n lower than n1 ? yes: is n1 lower than n2 ? yes: n1 to n2 n to n1
12th Dec 2019, 6:58 PM
zemiak
13th Dec 2019, 4:15 AM
Pragati Pawar
Pragati Pawar - avatar
0
Implement a sorting algorithm... maybe bubble sort as that's the easy one, and then you can simply multiply the second and second last element of the array. It works perfectly irrespective of wheater the elements are unique or not... Check this code out for the algorithm... https://code.sololearn.com/c5i1486MEr5l/?ref=app
13th Dec 2019, 9:35 PM
Dewesh Chopra
Dewesh Chopra - avatar
0
sort is not necessary
14th Dec 2019, 9:11 AM
zemiak
0
This cod find the second largest and second smallest in order of n using System; namespace SoloLearn { class Program { static void Main(string[] args) { int ss=0; int sl=0; int []arr=new int[]{2,3,1,7,8}; find_sec_smallest_and_largest(ref ss,ref sl,arr); Console.WriteLine(ss); Console.WriteLine(sl); } static void find_sec_smallest_and_largest(ref int sSmall,ref int sLargest,int [] arr){ sLargest=arr[0]; sSmall =arr[0]; int fL=arr[0]; int fS=arr[0]; for (int i=1;i<arr.Length;i++){ if(arr[i]>fL){ sLargest =fL; fL=arr[i]; } if(arr[i]<fS){ sSmall =fS; fS=arr[i]; } } } } }
14th Dec 2019, 6:57 PM
Gamal Kassem Farouk