codeparrot/xlcost-text-to-code huggingface dataset:
Find Second largest element in an array | Set 2 | Function to find the largest element in the array arr [ ] ; Base Condition ; Initialize an empty list ; Divide the array into two equal length subarrays and recursively find the largest among the two ; Store length of compared1 [ ] in the first index ; Store the maximum element ; Return compared1 which contains the maximum element ; Store length of compared2 [ ] in the first index ; Store the maximum element ; Return compared2 [ ] which contains the maximum element ; Function to print the second largest element in the array arr [ ] ; Find the largest element in arr [ ] ; Find the second largest element in arr [ ] ; Print the second largest element ; Driver code
function findLargest ( beg , end , arr , n ) { if ( beg == end ) { let compared = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) compared [ i ] = 0 ; compared [ 0 ] = 1 ; compared [ 1 ] = arr [ beg ] ; return compared ; } let compared1 = findLargest ( beg , Math . floor ( ( beg + end ) / 2 ) , arr , n ) ; let compared2 = findLargest ( Math . floor ( ( beg + end ) / 2 ) + 1 , end , arr , n ) ; if ( compared1 [ 1 ] > compared2 [ 1 ] ) { let k = compared1 [ 0 ] + 1 ; compared1 [ 0 ] = k ; compared1 [ k ] = compared2 [ 1 ] ; return compared1 ; } else { let k = compared2 [ 0 ] + 1 ; compared2 [ 0 ] = k ; compared2 [ k ] = compared1 [ 1 ] ; return compared2 ; } } function findSecondLargest ( end , arr ) { let compared1 = findLargest ( 0 , end - 1 , arr , end ) ; let compared2 = findLargest ( 2 , compared1 [ 0 ] + 2 , compared1 , compared1 [ 0 ] ) ; document . write ( compared2 [ 1 ] ) ; } let N = 10 ; let arr = [ 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 ] ; findSecondLargest ( N , arr ) ;
Count of Reverse Bitonic Substrings in a given String | Function to calculate the number of reverse bitonic substrings ; Stores the count ; All possible lengths of substrings ; Starting point of a substring ; Ending point of a substring ; Condition for reverse bitonic substrings of length 1 ; Check for decreasing sequence ; If end of substring is reached ; For increasing sequence ; If end of substring is reached ; Return the number of bitonic substrings ; Driver Code
function CountsubString ( str , n ) { var c = 0 ; for ( var len = 1 ; len <= n ; len ++ ) { for ( var i = 0 ; i <= n - len ; i ++ ) { var j = i + len - 1 ; var temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } var k = i + 1 ; while ( temp > str [ k ] && k <= j ) { temp = str [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( temp < str [ k ] && k <= j && f != 2 ) { temp = str [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } return c ; } var str = " " . split ( ' ' ) ; document . write ( CountsubString ( str , str . length ) ) ;
Maximum subsequence sum from a given array which is a perfect square | JavaScript program to implement the above approach ; If sum is 0 , then answer is true ; If sum is not 0 and arr [ ] is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to find the sum ; Find sum of all values ; return the value ; ; Driver Code
function isSubsetSum ( arr , n , sum ) { let subset = new Array ( n + 1 ) ; for ( var i = 0 ; i < subset . length ; i ++ ) { subset [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( let i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= sum ; j ++ ) { if ( j < arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - arr [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } function findSum ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } let val = Math . floor ( Math . sqrt ( sum ) ) ; for ( let i = val ; i >= 0 ; i -- ) { if ( isSubsetSum ( arr , n , i * i ) ) { return i * i ; } } return 0 ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( findSum ( arr , n ) ) ;
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 ( " " ) ;
Count of array elements whose order of deletion precedes order of insertion | Function returns maximum number of required elements ; Insert the elements of array B in the queue and set ; Stores the answer ; If A [ i ] is already processed ; Until we find A [ i ] in the queue ; Remove elements from the queue ; Increment the count ; Remove the current element A [ i ] from the queue and set . ; Return total count ; Driver Code
function maximumCount ( A , B , n ) { var q = [ ] ; var s = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { s . add ( B [ i ] ) ; q . push ( B [ i ] ) ; } var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s . has ( A [ i ] ) ) { while ( q . length != 0 && q [ 0 ] != A [ i ] ) { s . delete ( q [ 0 ] ) ; q . shift ( ) ; count ++ ; } if ( A [ i ] == q [ 0 ] ) { q . shift ( ) ; s . delete ( A [ i ] ) ; } if ( q . length == 0 ) break ; } } document . write ( count ) ; } var N = 4 ; var A = [ 1 , 2 , 3 , 4 ] ; var B = [ 1 , 2 , 4 , 3 ] ; maximumCount ( A , B , N ) ;
Check if a Matrix is Bitonic or not | Java Script program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
let N = 3 , M = 3 ; function checkBitonic ( arr , n ) { let i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } function check ( arr ) { let f = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! checkBitonic ( arr [ i ] , M ) ) { document . write ( " " ) ; return ; } } for ( let i = 0 ; i < N ; i ++ ) { let temp = [ N ] ; for ( let j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j ] [ i ] ; } if ( ! checkBitonic ( temp , N ) ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let m = [ [ 1 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 2 , 6 , 4 ] ] ; check ( m ) ;
Maximum possible GCD for a pair of integers with product N | javascript program to implement the above approach ; Function to return the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function getMaxGcd ( N ) { var maxGcd = Number . MIN_VALUE , A , B ; for ( i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i ; B = N / i ; maxGcd = Math . max ( maxGcd , gcd ( A , B ) ) ; } } return maxGcd ; } var N = 18 ; document . write ( getMaxGcd ( N ) ) ;
Maximum of minimum difference of all pairs from subsequences of given size | Function to check a subsequence can be formed with min difference mid ; If a subsequence of size B with min diff = mid is possible return true else false ; Function to find the maximum of all minimum difference of pairs possible among the subsequence ; Sort the Array ; Stores the boundaries of the search space ; Store the answer ; Binary Search ; If subsequence can be formed with min diff mid and size B ; Right half ; Left half ; Driver code
function can_place ( A , n , B , mid ) { let count = 1 ; let last_position = A [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( A [ i ] - last_position >= mid ) { last_position = A [ i ] ; count ++ ; if ( count == B ) { return true ; } } } return false ; } function find_min_difference ( A , n , B ) { A . sort ( ) ; let s = 0 ; let e = A [ n - 1 ] - A [ 0 ] ; let ans = 0 ; while ( s <= e ) { let mid = parseInt ( ( s + e ) / 2 , 10 ) ; if ( can_place ( A , n , B , mid ) ) { ans = mid ; s = mid + 1 ; } else { e = mid - 1 ; } } return ans ; } let A = [ 1 , 2 , 3 , 5 ] ; let n = A . length ; let B = 3 ; let min_difference = find_min_difference ( A , n , B ) ; document . write ( min_difference ) ;
Count of triplets in an Array such that A [ i ] * A [ j ] = A [ k ] and i < j < k | Returns total number of valid triplets possible ; Stores the count ; Map to store frequency of array elements ; Increment the frequency of A [ j + 1 ] as it can be a valid A [ k ] ; If target exists in the map ; Return the final count ; Driver code
function countTriplets ( A , N ) { let ans = 0 ; let map = new Map ( ) ; for ( let j = N - 2 ; j >= 1 ; j -- ) { if ( map . has ( A [ j + 1 ] ) ) map . set ( A [ j + 1 ] , map . get ( A [ j + 1 ] ) + 1 ) ; else map . set ( A [ j + 1 ] , 1 ) ; for ( let i = 0 ; i < j ; i ++ ) { let target = A [ i ] * A [ j ] ; if ( map . has ( target ) ) ans += map . get ( target ) ; } } return ans ; } let N = 5 ; let A = [ 2 , 3 , 4 , 6 , 12 ] ; document . write ( countTriplets ( A , N ) ) ;
Longest alternating subsequence with maximum sum | Set 2 | Function to check the sign of the element ; Function to calculate and return the maximum sum of longest alternating subsequence ; Iterate through the array ; Stores the first element of a sequence of same sign ; Traverse until an element with opposite sign is encountered ; Update the maximum ; Update the maximum sum ; Update i ; Return the maximum sum ; Driver Code
function sign ( x ) { if ( x > 0 ) return 1 ; else return - 1 ; } function findMaxSum ( arr , size ) { let max_sum = 0 , pres , i , j ; for ( i = 0 ; i < size ; i ++ ) { pres = arr [ i ] ; j = i ; while ( j < size && sign ( arr [ i ] ) == sign ( arr [ j ] ) ) { pres = Math . max ( pres , arr [ j ] ) ; j ++ ; } max_sum = max_sum + pres ; i = j - 1 ; } return max_sum ; } let arr = [ - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 ] ; let size = arr . length ; document . write ( findMaxSum ( arr , size ) ) ;
Check if an array can be split into subsets of K consecutive elements | Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code
function groupInKConsecutive ( arr , K ) { var count = new Map ( ) ; arr . forEach ( element => { if ( count . has ( element ) ) count . set ( element , count . get ( element ) + 1 ) else count . set ( element , 1 ) } ) ; count . forEach ( ( value , key ) => { var cur = key ; var n = value ; if ( n > 0 ) { for ( var i = 1 ; i < K ; ++ i ) { if ( ! count . has ( cur + i ) ) { return false ; } count . set ( cur + i , count . get ( cur + i ) - n ) ; if ( count . get ( cur + i ) < 0 ) return false ; } } } ) ; return true ; } var arr = [ 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 ] ; var k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of substrings of a given Binary string with all characters same | Function to count number of sub - strings of a given binary string that contains only 1 ; Iterate untill L and R cross each other ; Check if reached the end of string ; Check if encountered '1' then extend window ; Check if encountered '0' then add number of strings of current window and change the values for both l and r ; Return the answer ; Function to flip the bits of string ; Function to count number of sub - strings of a given binary string that contains only 0 s & 1 s ; count of substring which contains only 1 s ; Flip the character of string s 0 to 1 and 1 to 0 to count the substring with consecutive 0 s ; count of substring which contains only 0 s ; Given string str ; Function Call
function countSubAllOnes ( s ) { var l = 0 , r = 0 , ans = 0 ; while ( l <= r ) { if ( r == s . length ) { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; break ; } if ( s [ r ] == ' ' ) r ++ ; else { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; l = r + 1 ; r ++ ; } } return ans ; } function flip ( s ) { for ( var i = 0 ; s [ i ] ; i ++ ) { if ( s [ i ] == ' ' ) s [ i ] = ' ' ; else s [ i ] = ' ' ; } return s ; } function countSubAllZerosOnes ( s ) { var only_1s = countSubAllOnes ( s ) ; s = flip ( s . split ( ' ' ) ) ; var only_0s = countSubAllOnes ( s ) ; return only_0s + only_1s ; } var s = " " ; document . write ( countSubAllZerosOnes ( s ) ) ;
Count of primes in a given range that can be expressed as sum of perfect squares | Function to check if a prime number satisfies the condition to be expressed as sum of two perfect squares ; Function to check if a number is prime or not ; Corner cases ; Function to return the count of primes in the range which can be expressed as the sum of two squares ; If i is a prime ; If i can be expressed as the sum of two squares ; Return the count ; Driver code
function sumSquare ( p ) { return ( p - 1 ) % 4 == 0 ; } function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function countOfPrimes ( L , R ) { var count = 0 ; for ( var i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { if ( sumSquare ( i ) ) count ++ ; } } return count ; } var L = 5 , R = 41 ; document . write ( countOfPrimes ( L , R ) ) ;
Count of array elements which are greater than all elements on its left | Function to return the count of array elements with all elements to its left smaller than it ; Stores the count ; Stores the maximum ; Iterate over the array ; If an element greater than maximum is obtained ; Increase count ; Update maximum ; Driver Code
function count_elements ( arr , n ) { let count = 1 ; let max = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { count += 1 ; max = arr [ i ] ; } } return count ; } let arr = [ 2 , 1 , 4 , 6 , 3 ] ; let n = arr . length ; document . write ( count_elements ( arr , n ) ) ;
Count of bitonic substrings from the given string | Function to find all the bitonic sub strings ; Pick starting point ; Iterate till length of the string ; Pick ending point for string ; Substring from i to j is obtained ; Substrings of length 1 ; Increase count ; For increasing sequence ; Check for strictly increasing ; Increase count ; Check for decreasing sequence ; Increase count ; Print the result ; Given string ; Function Call
function subString ( str , n ) { var c = 0 ; for ( var len = 1 ; len <= n ; len ++ ) { for ( var i = 0 ; i <= n - len ; i ++ ) { var j = i + len - 1 ; var temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } var k = i + 1 ; while ( temp < str [ k ] && k <= j ) { temp = str [ k ] ; k ++ ; f = 2 ; } if ( k > j ) { c ++ ; f = 2 ; } while ( temp > str [ k ] && k <= j && f != 2 ) { k ++ ; f = 0 ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } document . write ( c ) ; } var str = " " . split ( ' ' ) ; subString ( str , str . length ) ;
Check if ceil of number divided by power of two exist in sorted array | Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
function findNumberDivByPowerofTwo ( ar , k , n ) { var found = - 1 , m = k ; while ( m > 0 ) { var l = 0 ; var r = n - 1 ; while ( l <= r ) { var mid = parseInt ( ( l + r ) / 2 ) ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != - 1 ) { break ; } m = parseInt ( m / 2 ) ; } return found ; } var arr = [ 3 , 5 , 7 , 8 , 10 ] ; var k = 4 , n = 5 ; document . write ( findNumberDivByPowerofTwo ( arr , k , n ) ) ;
Largest subset having with sum less than equal to sum of respective indices | Function to find the length of the longest subset ; Stores the sum of differences between elements and their respective index ; Stores the size of the subset ; Iterate over the array ; If an element which is smaller than or equal to its index is encountered ; Increase count and sum ; Store the difference with index of the remaining elements ; Sort the differences in increasing order ; Include the differences while sum remains positive or ; Return the size ; Driver Code ; Function Calling
function findSubset ( a , n ) { let sum = 0 ; let cnt = 0 ; let v = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { if ( a [ i - 1 ] - i <= 0 ) { sum += a [ i - 1 ] - i ; cnt += 1 ; } else { v . push ( a [ i - 1 ] - i ) ; } } v . sort ( ) ; let ptr = 0 ; while ( ptr < v . length && sum + v [ ptr ] <= 0 ) { cnt += 1 ; ptr += 1 ; sum += v [ ptr ] ; } return cnt ; } let arr = [ 4 , 1 , 6 , 7 , 8 , 2 ] ; let n = arr . length ; document . write ( findSubset ( arr , n ) ) ;
Queries for count of array elements with values in given range with updates | Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver Code
function setElement ( arr , n , index , x ) { arr [ index ] = x ; } function getCount ( arr , n , l , r ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } function SolveQuery ( arr , n , Q ) { let x ; for ( let i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { x = getCount ( arr , n , Q [ i ] [ 1 ] [ 0 ] , Q [ i ] [ 1 ] [ 1 ] ) ; document . write ( x + " " ) ; } else { setElement ( arr , n , Q [ i ] [ 1 ] [ 0 ] , Q [ i ] [ 1 ] [ 1 ] ) ; } } } let arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 ] ; let n = arr . length ; let Q = [ [ 1 , [ 3 , 5 ] ] , [ 1 , [ 2 , 4 ] ] , [ 1 , [ 1 , 2 ] ] , [ 2 , [ 1 , 7 ] ] , [ 1 , [ 1 , 2 ] ] ] ; SolveQuery ( arr , n , Q ) ;
Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function call
function generateSequence ( freq , n ) { let m = new Map ( ) ; let total = 0 ; for ( let i = 0 ; i < n ; i ++ ) { m . set ( i , freq [ i ] ) ; total += freq [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( i ) ) { let sequence = [ ] ; let mcopy = new Map ( ) ; for ( let [ key , value ] of m . entries ( ) ) { mcopy . set ( key , value ) ; } if ( mcopy . has ( i ) && mcopy . get ( i ) > 0 ) mcopy . set ( i , mcopy . get ( i ) - 1 ) ; sequence . push ( i ) ; let last = i ; for ( let i1 = 0 ; i1 < total - 1 ; i1 ++ ) { if ( mcopy . has ( last - 1 ) && mcopy . get ( last - 1 ) > 0 ) { mcopy . set ( last - 1 , mcopy . get ( last - 1 ) - 1 ) ; sequence . push ( last - 1 ) ; last -- ; } else if ( mcopy . has ( last + 1 ) ) { mcopy . set ( last + 1 , mcopy . get ( last + 1 ) - 1 ) ; sequence . push ( last + 1 ) ; last ++ ; } else break ; } if ( sequence . length == total ) { return sequence ; } } } let empty = [ ] ; return empty ; } function PrintSequence ( freq , n ) { let sequence = generateSequence ( freq , n ) ; if ( sequence . length == 0 ) { document . write ( " " ) ; } else { for ( let i = 0 ; i < sequence . length ; i ++ ) { document . write ( sequence [ i ] + " " ) ; } } } let freq = [ 2 , 2 , 2 , 3 , 1 ] ; let N = 5 ; PrintSequence ( freq , N ) ;
Minimum distance between any most frequent and least frequent element of an array | Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Given array ; Function Call
function getMinimumDistance ( a , n ) { var min_set = new Set ( ) ; var max_set = new Set ( ) ; var max = 0 , min = 1000000000 ; var frequency = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( frequency . has ( a [ i ] ) ) frequency . set ( a [ i ] , frequency . get ( a [ i ] ) + 1 ) else frequency . set ( a [ i ] , 1 ) } for ( var i = 0 ; i < n ; i ++ ) { var count = frequency . get ( a [ i ] ) ; if ( count == max ) { max_set . add ( a [ i ] ) ; } else if ( count > max ) { max_set = new Set ( ) ; max = count ; max_set . add ( a [ i ] ) ; } if ( count == min ) { min_set . add ( a [ i ] ) ; } else if ( count < min ) { min_set = new Set ( ) ; min = count ; min_set . add ( a [ i ] ) ; } } var min_dist = 1000000000 ; var last_min_found = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( min_set . has ( a [ i ] ) ) last_min_found = i ; if ( max_set . has ( a [ i ] ) && last_min_found != - 1 ) { if ( ( i - last_min_found ) < min_dist ) min_dist = i - last_min_found ; } } last_min_found = - 1 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . has ( a [ i ] ) ) last_min_found = i ; if ( max_set . has ( a [ i ] ) && last_min_found != - 1 ) { if ( ( last_min_found - i ) > min_dist ) min_dist = last_min_found - i ; } } document . write ( min_dist ) ; } var arr = [ 1 , 1 , 2 , 3 , 2 , 3 , 3 ] ; var N = arr . length ; getMinimumDistance ( arr , N ) ;