title: How can I find the smallest covering prefix of an array in Java?
body: <blockquote>
    <p>Find the first covering prefix of a given array.</p>
    
    <p>A non-empty zero-indexed array A consisting of N integers is given. The first covering
    prefix of array A is the smallest integer P such that  and such that every value that
    occurs in array A also occurs in sequence.</p>
    
    <p>For example, the first covering prefix of array A with
    A[0]=2, A[1]=2, A[2]=1, A[3]=0,  A[4]=1 is 3, because sequence A[0],
    A[1], A[2], A[3] equal to 2, 2, 1, 0 contains all values that occur in
    array A.</p>
  </blockquote>
  
  <p>My solution is </p>
  
  <pre><code>int ps ( int[] A ) 
  {
      int largestvalue=0;
      int index=0;   
  
      for(each element in Array){
          if(A[i]&gt;largestvalue)
          {
              largestvalue=A[i];
              index=i;
          }
      }
  
      for(each element in Array)
      {
          if(A[i]==index)
              index=i; 
      }   
      return index;
  }
  </code></pre>
  
  <p>But this only works for this input, this is not a generalized solution.</p>