Smallest subarray whose product leaves remainder K when divided by size of the array | 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 ; Given array
function findsubArray ( arr , N , K ) { var res = N + 1 ; for ( i = 0 ; i < N ; i ++ ) { var curr_prod = 1 ; for ( 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 ; } var arr = [ 2 , 2 , 3 ] ; var N = arr . length ; var K = 1 ; var answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) document . write ( answer ) ; else document . write ( " " ) ;