DS and Algo

Given a list of area telephone codes along with their location, find the location of a given telephone number. The area codes can be minimum three digits and maximum six digits. Area codes are unique for a particular length between three and six, inclusive. For eg: if 080 is an area code, 0804 cannot be another code , as for length 3, 080 occurs in both codes. You are allowed to process the list however suitable to enhance the efficiency.

Find the second maximum element in an unsorted array of integers, using single for loop

Solution One:

int min = 0;
int secondmin = 0;

if(array[0] < array[1]) {
  min = array[0];
  second_min = array[1];
} else {
  min = array[1];
  second_min = array[0];
}
for(i=3; i<array.length; ++i) {
  if(array[i] < min) {
    secondmin = min;
    min = array[i];
  } else if (array[i] < secondmin) {
    secondmin = array[i];
  }
}

Sort the array using Merge Sort and then return the second one in the list.

 
7
Kudos
 
7
Kudos

Now read this

Linked List

Linked List # Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. Why Linked List? # Arrays can be used to store linear... Continue →