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

Capybara Cheat Sheet

Capybara helps to test web applications by simulating a real user. Navigating # visit products_path visit '/products/new' visit post_comments_path(post) Clicking links and buttons # click_button 'New Product' click_link 'Delete'... Continue →