Skip to main content

Function to find second largest element of array

Some interviewers ask the question - write a program to find second largest/smallest element of the array. Then they add do it in single iteration.

No need to fret.

  • Initialize largest with 0th element of the array
  • Iterate over 1st element to nth element
    • If array element is >largest
      • set second_largest to largest
      • set largest to array element

But the question is what do we initialize the second largest element to. We know from experience that 0 is not the solution. Because some or all elements could be negative.

So let us initialize largest with 0th element of the array and also second largest with 0th element of the array.

So here is our code
 int second_largest(int *arr,int n)  
{
int lar,sec_lar=0;int i;
lar = sec_lar= arr[0];arr++;
for(i=1;i<n;i++,arr++)
{
if(*arr>lar)
{
sec_lar = lar;
lar = *arr;
}
else if(*arr>sec_lar)
{
sec_lar = *arr;
}
else if(lar==sec_lar )
{
sec_lar =*arr;
}
}
return sec_lar;
}

Note : We have used the last condition because, initially largest = second laregst = 0th element

Comments

Popular posts from this blog

Program to delete a node from linked list

How do you remove a node from a linked list? If you have to delete a node, first you need to search the node. Then you should find its previous node. Then you should link the previous node to the next node. If node containing 8 has to be deleted, then n1 should be pointing to n2. Looks quite simple. Isn't it? But there are at least two special cases you have to consider. Of course, when the node is not found. If the node is first node of the list viz head. If the node to be deleted is head node, then if you delete, the list would be lost. You should avoid that and make the second node as the head node. So it becomes mandatory that you return the changed head node from the function.   Now let us have a look at the code. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; typedef struct node * NODEPTR; NODEPTR create_node ( int value) { NODEPTR temp = (NODEPTR) malloc( size...

Binary tree deletion

Do not get all scared and worried. It is not rocket science (or as I would like to call it - it is not regex). Remember deleting a node from linked list. When you deleted a node, you did the following 2 steps Free the memory of the node Link the previous node of the deleted node to the next node You will have to do these things in binary tree too. But the difficulty here is do you link the previous node - or parent node in tree terminology, to the left child of deleted node? Or to the right child? You can not link both children, as the parent already may have one child node. So before we solve this, let us categorize the deletion with the help of a diagram. Consider the cases The node to be deleted is a leaf node. That is, it does not have left or right child. In this case, link the parent to NULL in place of deleted node.  If you want to delete 13 - which is a leaf node, you set 14->left to NULL and free memory of node 13. If you want to delete 7 - which is also a leaf node and...

Deletion of a node from linked list given only that node

We have considered how to create and print the list and delete a node from a list. But often interviewers ask you a question, given a node of the list, how do you delete that node? The problem here is we do not know the head of the list. We just the know one node which we must delete. Well, the solution is much simpler than it appears. Suppose 12 is the node which should be deleted. As we can see from the diagram, we are deleting the next node of 99 by the usual method of linking n to next of next of 12. But the requirement was not to delete next node  but the node with 12 itself. So we will retain the data of that node (99) by copying it to previous node. And now the node with 99 is marked for deletion. Which can be easily deleted as follows. We are assuming that n is the node with 12 as value. n -> data = n -> next -> data; //copy the data n -> next = n -> next -> next; //link to next free( n -> next) ; //delete next of n   ...