Bloomberg Interview Question

Find the first two largest numbers in an array of integers.

Interview Answers

Anonymous

Oct 11, 2013

int k = 2; int[] largestK = new int[k]; for(int i = 0; i min) largestK[minPos] = input[i]; } return largestK;

Anonymous

Jan 23, 2015

vector findLargest(vector input){ int max, max2; max = max2 = input[0]; for(int i = 0; i max){ max2 = max; max = input[i]; } else if(input[i] > max2) max2 = input[i]; } cout << max << endl << max2 << endl; }