Error in Finding the nth largest number in the unsorted array (containing duplicates) using median of medians algo O(n)
<p>If an array contains 1,7,7,3,6 and if user asks what 2nd largest element is the output should be 7(not 6) since duplicate values are treated as distinct.<br>
This is my code.<br>
I am using Deterministic search to find the suitable pivot .<br>
Its complexity is O(n).<br>
I am stuck at the error generated by my code .<br>
Please do help me.</p>

<pre><code>import java.util.Random;
import java.util.Scanner;


public class deven  {

    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        int len=in.nextInt();
        int n=in.nextInt();
        int array[]=new int[len];
        for (int i = 0; i &lt; len; i++) {
            array[i]=in.nextInt();
        }


        System.out.println(select(array,len,n));

    }

    static int below[];
    static int above[];
    static int pivot;
    static int i;
    static int j;
    static int x;
    static int y;
    static int index;
    static Random rand=new Random();
    static int select(int array[],int len,int n){

        if(len==1)
            return array[0];
        pivot=pivot(array, len);

        below=new int[len];
        above=new int[len];
        //System.out.println("Block");
        x=0;
        y=0;
        int temp=0;
        for(i=0;i&lt;len;i++){
            if(array[i]&gt;pivot){
                below[x++]=array[i];
            }
            else if(array[i]&lt;pivot){
                above[y++]=array[i];
            }
            else {
                if(temp!=0){
                below[x++]=array[i];
            }
                temp=1;
        }
        }

        i = x;
           j = len - y;
        if(n&lt;i) return select(below,x,n);
        else if(n&gt;=j) return(select(above,y,n-j));
        else  return(pivot);



    }

    static int pivot(int array[],int len){
        if(len==1){
            return array[0];
        }
        int numOfGroups=len/5;
        if(len%5!=0){
            numOfGroups++;
        }

        int setOfMedians[]=new int[numOfGroups];
        for (int i = 0 ; i &lt; numOfGroups ; i++)
        {
            int[] subset;
            if(array.length % 5 &gt; 0)
            {
                if (i == numOfGroups - 1)
                {
                    subset = new int[array.length % 5];
                }
                else
                {
                    subset = new int[5];
                }
            }
            else
            {
                subset = new int[5];
            }
            for (int j = 0; j &lt; subset.length ; j++)
            {
                subset[j] = array[5*i+j];
            }
            setOfMedians[i] = median(subset);
        }

        int goodpivot=select(setOfMedians, numOfGroups,numOfGroups/2 );
        return goodpivot;

    }
    static int median(int[] array)
    {
        if (array.length == 1)
        {
            return array[0];
        }
        int smallerCount = 0;
        for (int i = 0 ; i &lt; array.length ; i++)
        {
            for (int j = 0 ; j &lt; array.length ; j++)
            {
                if (array[i] &lt; array[j])
                {
                    smallerCount++;
                }
            }
            if (smallerCount == (array.length - 1)/2)
            {
                return array[i];
            }
            smallerCount = 0;
        }
        return -1; 
    }


}
</code></pre>

<p><strong>Input</strong><br>
6<br>
3<br>
1 2 3 1 2 3<br>
<strong>Output</strong>    </p>

<pre><code>Exception in thread "main" java.lang.StackOverflowError  
    at deven.pivot(deven.java:99)  
    at deven.select(deven.java:34)  
    at deven.pivot(deven.java:102)  
    at deven.select(deven.java:34)  
    at deven.select(deven.java:59)  
    at deven.select(deven.java:59)  
    at deven.select(deven.java:59)  
</code></pre>
