+ 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?
11 ответов
+ 3
You can check out this code👇🏻
https://code.sololearn.com/cQai7jYzQm8w/?ref=app
+ 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);
+ 2
First sort the array in ascending order then get 2nd smallest and 2nd largest value and multiply them.
+ 1
Are your array elements unique?
+ 1
Sort the Array and you'll have them on Index 1 & n-1
+ 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.
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
0
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
0
sort is not necessary
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];
}
}
}
}
}