Extract the index which has the next greater prime
int x = - 1 ; for ( int j = 0 ; j < primes . size ( ) ; j ++ ) { if ( arr [ i ] == primes . get ( j ) ) { x = j ; break ; } else if ( arr [ i ] < primes . get ( j ) ) { x = j ; break ; } }