Smallest subarray whose product leaves remainder K when divided by size of the array | Java program to implement the above approach ; Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array
import java . util . * ; class GFG { static int findsubArray ( int arr [ ] , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 } ; int N = arr . length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) System . out . println ( answer ) ; else System . out . println ( " - 1" ) ; } }