Could you find 2nd and 3rd top element of the give following

Could you find 2^nd and 3^rd top element of the give following array by using \"for loop\" and using operation of finding maximum element of an array manually? (As seen from the given bottom array, all elements of the array is distinct.) Respective array: [1 10 12 13 14 167 189 156 1122 2 3 4 5 6 7]

Solution

Since, no specific language choice is mentioned, I am writing the code in python

Code is as following:

import numpy;

def findTopAthElement( a , b ):
   #a need to be >= 1
   answer = -1;
   for i in range(b): #find maximum for a times and extract it
       #find maximum
       maxisAt = 0;
       for j in range(len(a)):
           if a[j] > a[ maxisAt ]:
               maxisAt = j;
       answer = a[maxisAt];
       #got the maximum, now lets remove it
       a.remove( a[maxisAt] );

   return answer;

print findTopAthElement( [1, 10, 12, 13, 14, 167, 156, 1122, 2, 3, 4, 5, 6, 7 ] , 2);
print findTopAthElement( [1, 10, 12, 13, 14, 167, 156, 1122, 2, 3, 4, 5, 6, 7 ] , 3);

 Could you find 2^nd and 3^rd top element of the give following array by using \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site