codeparrot/xlcost-text-to-code huggingface dataset:
Find next Smaller of next Greater in an array | function find Next greater element ; create empty stack ; Traverse all array elements in reverse order order == ' G ' we compute next greater elements of every element order == ' S ' we compute right smaller element of every element ; Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S ) ; store the next greater element of current element ; If all elements in S were smaller than arr [ i ] ; Push this element ; Function to find Right smaller element of next greater element ; stores indexes of next greater elements ; stores indexes of right smaller elements ; Find next greater element Here G indicate next greater element ; Find right smaller element using same function nextGreater ( ) Here S indicate right smaller elements ; If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ] ; Driver code
function nextGreater ( arr , next , order ) { let stack = [ ] ; for ( let i = arr . length - 1 ; i >= 0 ; i -- ) { while ( stack . length != 0 && ( ( order == ' ' ) ? arr [ stack [ stack . length - 1 ] ] <= arr [ i ] : arr [ stack [ stack . length - 1 ] ] >= arr [ i ] ) ) stack . pop ( ) ; if ( stack . length != 0 ) next [ i ] = stack [ stack . length - 1 ] ; else next [ i ] = - 1 ; stack . push ( i ) ; } } function nextSmallerOfNextGreater ( arr ) { let NG = new Array ( arr . length ) ; let RS = new Array ( arr . length ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { NG [ i ] = 0 ; RS [ i ] = 0 ; } nextGreater ( arr , NG , ' ' ) ; nextGreater ( arr , RS , ' ' ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( NG [ i ] != - 1 && RS [ NG [ i ] ] != - 1 ) document . write ( arr [ RS [ NG [ i ] ] ] + " " ) ; else document . write ( " " ) ; } } let arr = [ 5 , 1 , 9 , 2 , 5 , 1 , 7 ] ; nextSmallerOfNextGreater ( arr ) ;
Flip Binary Tree | A binary tree node structure ; method to flip the binary tree ; recursively call the same method ; rearranging main root Node after returning from recursive call ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current lelvel . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } ; function flipBinaryTree ( root ) { if ( root == null ) return root ; if ( root . left == null && root . right == null ) return root ; let flippedRoot = flipBinaryTree ( root . left ) ; root . left . left = root . right ; root . left . right = root ; root . left = root . right = null ; return flippedRoot ; } function printLevelOrder ( root ) { if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( true ) { let nodeCount = q . length ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { let node = q . shift ( ) ; document . write ( node . data + " " ) ; if ( node . left != null ) q . push ( node . left ) ; if ( node . right != null ) q . push ( node . right ) ; nodeCount -- ; } document . write ( " " ) ; } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; document . write ( " " ) ; printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; document . write ( " " ) ; printLevelOrder ( root ) ;
HeapSort | To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is larger than root ; If right child is larger than largest so far ; If largest is not root ; Recursively heapify the affected sub - tree ; The main function to sort an array of given size ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver code
function heapify ( arr , n , i ) { var largest = i ; var l = 2 * i + 1 ; var r = 2 * i + 2 ; if ( l < n && arr [ l ] > arr [ largest ] ) largest = l ; if ( r < n && arr [ r ] > arr [ largest ] ) largest = r ; if ( largest != i ) { var swap = arr [ i ] ; arr [ i ] = arr [ largest ] ; arr [ largest ] = swap ; heapify ( arr , n , largest ) ; } } function sort ( arr ) { var n = arr . length ; for ( var i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( var i = n - 1 ; i > 0 ; i -- ) { var temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } function printArray ( arr ) { var n = arr . length ; for ( var i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ; } var arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; var n = arr . length ; sort ( arr ) ; document . write ( " " ) ; printArray ( arr , n ) ;
How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; driver code
function isHeap ( arr , n ) { for ( let i = 0 ; i <= Math . floor ( ( n - 2 ) / 2 ) ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ; if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false ; } return true ; } let arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ] ; let n = arr . length ; if ( isHeap ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Merge k sorted arrays | Set 1 | This function takes an array of arrays as an argument and All arrays are assumed to be sorted . It merges them together and prints the final sorted output . ; traverse the matrix ; sort the array ; A utility function to print array elements ; Driver program to test above functions
function mergeKArrays ( arr , a , output ) { var c = 0 ; for ( i = 0 ; i < a ; i ++ ) { for ( j = 0 ; j < 4 ; j ++ ) output [ c ++ ] = arr [ i ] [ j ] ; } output . sort ( ( a , b ) => a - b ) ; } function printArray ( arr , size ) { for ( i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ [ 2 , 6 , 12 , 34 ] , [ 1 , 9 , 20 , 1000 ] , [ 23 , 34 , 90 , 2000 ] ] ; var k = 4 ; var n = 3 ; var output = Array ( n * k ) . fill ( 0 ) ; mergeKArrays ( arr , n , output ) ; document . write ( " " ) ; printArray ( output , n * k ) ;
Merge k sorted arrays | Set 1 | Javascript program to merge k sorted arrays of size n each . ; Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; A utility function to print array elements ; This function takes an array of arrays as an argument and all arrays are assumed to be sorted . It merges them together and prints the final sorted output . ; If one array is in range ; If only two arrays are left them merge them ; Output arrays ; Divide the array into halves ; Merge the output array ; Driver code Change n at the top to change number of elements in an array
let n = 4 function mergeArrays ( arr1 , arr2 , n1 , n2 , arr3 ) { let i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } function mergeKArrays ( arr , i , j , output ) { if ( i == j ) { for ( let p = 0 ; p < n ; p ++ ) output [ p ] = arr [ i ] [ p ] ; return ; } if ( j - i == 1 ) { mergeArrays ( arr [ i ] , arr [ j ] , n , n , output ) ; return ; } let out1 = new Array ( n * ( ( ( i + j ) / 2 ) - i + 1 ) ) let out2 = new Array ( n * ( j - ( ( i + j ) / 2 ) ) ) ; mergeKArrays ( arr , i , ( i + j ) / 2 , out1 ) ; mergeKArrays ( arr , ( i + j ) / 2 + 1 , j , out2 ) ; mergeArrays ( out1 , out2 , n * ( ( ( i + j ) / 2 ) - i + 1 ) , n * ( j - ( ( i + j ) / 2 ) ) , output ) ; } let arr = [ [ 2 , 6 , 12 , 34 ] , [ 1 , 9 , 20 , 1000 ] , [ 23 , 34 , 90 , 2000 ] ] ; let k = arr . length ; let output = new Array ( n * k ) ; mergeKArrays ( arr , 0 , 2 , output ) ; document . write ( " " + " " ) ; printArray ( output , n * k ) ;
Smallest Derangement of Sequence | Javascript program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Prlet Derangement ; Driver code
function generate_derangement ( N ) { let S = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) S [ i ] = i ; let D = [ ] ; for ( let i = 1 ; i <= N ; i += 2 ) { if ( i == N ) { D [ N ] = S [ N - 1 ] ; D [ N - 1 ] = S [ N ] ; } else { D [ i ] = i + 1 ; D [ i + 1 ] = i ; } } for ( let i = 1 ; i <= N ; i ++ ) document . write ( D [ i ] + " " ) ; document . write ( " " ) ; } generate_derangement ( 10 ) ;
Program to calculate Profit Or Loss | Function to calculate Profit . ; Function to calculate Loss . ; Driver Code .
function Profit ( costPrice , sellingPrice ) { let profit = ( sellingPrice - costPrice ) ; return profit ; } function Loss ( costPrice , ellingPrice ) { let Loss = ( costPrice - sellingPrice ) ; return Loss ; } let costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) document . write ( " " ) ; else if ( sellingPrice > costPrice ) document . write ( Profit ( costPrice , sellingPrice ) + " " ) ; else document . write ( Loss ( costPrice , sellingPrice ) + " " ) ;
Find the Next perfect square greater than a given number | Function to find the next perfect square ; Driver Code
function nextPerfectSquare ( N ) { let nextN = Math . floor ( Math . sqrt ( N ) ) + 1 ; return nextN * nextN ; } let n = 35 ; document . write ( nextPerfectSquare ( n ) ) ;
Print all substring of a number without any conversion | Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code
function printSubstrings ( n ) { var s = parseInt ( Math . log10 ( n ) ) ; var d = parseInt ( ( Math . pow ( 10 , s ) + 0.5 ) ) ; var k = d ; while ( n > 0 ) { while ( d > 0 ) { document . write ( parseInt ( n / d ) + " " ) ; d = parseInt ( d / 10 ) ; } n = n % k ; k = parseInt ( k / 10 ) ; d = k ; } } var n = 123 ; printSubstrings ( n ) ;
Modulo power for large numbers represented as strings | Javascript program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long let . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long let . We calculate a ^ b using modulo exponentiation ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string .
let MOD = ( 1e9 + 7 ) ; function powerLL ( x , n ) { let result = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { result = result * x % MOD ; } n = Math . floor ( n / 2 ) ; x = x * x % MOD ; } return result ; } function powerStrings ( sa , sb ) { let a = 0 , b = 0 ; for ( let i = 0 ; i < sa . length ; i ++ ) { a = ( a * 10 + ( sa [ i ] - ' ' ) ) % MOD ; } for ( let i = 0 ; i < sb . length ; i ++ ) { b = ( b * 10 + ( sb [ i ] - ' ' ) ) % ( MOD - 1 ) ; } return powerLL ( a , b ) ; } let sa = " " , sb = " " ; document . write ( powerStrings ( sa , sb ) ) ;
Check if a number can be expressed as 2 ^ x + 2 ^ y | Utility function to check if a number is power of 2 or not ; Utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if a number is power of 2 it can be expressed as 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; driver code
function isPowerOfTwo ( n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } function previousPowerOfTwo ( n ) { while ( ( n & n - 1 ) > 1 ) { n = n & n - 1 ; } return n ; } function checkSum ( n ) { if ( n == 0 n == 1 ) return false ; else if ( isPowerOfTwo ( n ) ) { document . write ( n / 2 + " " + n / 2 + " " ) ; } else { var x = previousPowerOfTwo ( n ) ; var y = n - x ; if ( isPowerOfTwo ( y ) ) { document . write ( x + " " + y + " " ) ; return true ; } } return false ; } var n1 = 20 ; if ( checkSum ( n1 ) == false ) document . write ( " " ) ; document . write ( ) ; var n2 = 11 ; if ( checkSum ( n2 ) == false ) document . write ( " " ) ;
10 's Complement of a decimal number | Function to find 10 's complement ; Calculating total digits in num ; restore num ; calculate 10 's complement ; Driver code
function complement ( num ) { var i , len = 0 , temp , comp ; temp = num ; while ( true ) { len ++ ; num = parseInt ( num / 10 ) ; if ( Math . abs ( num ) == 0 ) break ; } num = temp ; comp = parseInt ( Math . pow ( 10 , len ) - num ) ; return comp ; } document . write ( complement ( 25 ) + " " ) ; document . write ( complement ( 456 ) ) ;
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Recursive function to return gcd of a and b ; Everything divides 0 ; Base case ; a is greater ; Driver code
function gcd ( a , b ) { if ( a == 0 && b == 0 ) return 0 ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } var a = 98 , b = 56 ; document . write ( " " + a + " " + b + " " + gcd ( a , b ) ) ;
Number of sub arrays with odd sum | Function to find number of subarrays with odd sum ; ' odd ' stores number of odd numbers upto ith index ' c _ odd ' stores number of odd sum subarrays starting at ith index ' Result ' stores the number of odd sum subarrays ; First find number of odd sum subarrays starting at 0 th index ; Find number of odd sum subarrays starting at ith index add to result ; Driver code
function countOddSum ( a , n ) { let c_odd = 0 , result = 0 ; let odd = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd = ! odd ; } if ( odd ) { c_odd ++ ; } } for ( let i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 == 1 ) { c_odd = ( n - i - c_odd ) ; } } return result ; } let ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] ; let n = ar . length ; document . write ( " " + " " + countOddSum ( ar , n ) ) ;
Calculating n | Javascript Program to find n - th real root of x ; Initialize boundary values ; used for taking approximations of the answer ; Do binary search ; Driver code
function findNthRoot ( x , n ) { let low , high ; if ( x >= 0 && x <= 1 ) { low = x ; high = 1 ; } else { low = 1 ; high = x ; } let epsilon = 0.00000001 ; let guess = parseInt ( ( low + high ) / 2 , 10 ) ; while ( Math . abs ( ( Math . pow ( guess , n ) ) - x ) >= epsilon ) { if ( Math . pow ( guess , n ) > x ) { high = guess ; } else { low = guess ; } guess = ( low + high ) / 2 ; } document . write ( guess ) ; } let x = 5 ; let n = 2 ; findNthRoot ( x , n ) ;
Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Loop to calculate power of 2 upto n and add them ; Driver Code
function calculateSum ( n ) { let sum = 0 ; for ( let row = 0 ; row < n ; row ++ ) { sum = sum + ( 1 << row ) ; } return sum ; } let n = 10 ; document . write ( " " + calculateSum ( n ) ) ;
Number of sequences which has HEAD at alternate positions to the right of the first HEAD | function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code
function findAllSequence ( N ) { if ( N % 2 == 0 ) { return ( Math . pow ( 2 , N / 2 + 1 ) + Math . pow ( 2 , N / 2 ) - 2 ) ; } else { return ( Math . pow ( 2 , ( N + 1 ) / 2 ) + Math . pow ( 2 , ( N + 1 ) / 2 ) - 2 ) ; } } let N = 2 ; document . write ( findAllSequence ( N ) ) ;
Compute power of power k times % m | Create an array to store phi or totient values ; Function to calculate Euler Totient values ; indicates not evaluated yet and initializes for product formula . ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to calculate ( x ^ x ^ x ^ x ... k times ) % m ; to store different mod values ; run loop in reverse to calculate result ; compute euler totient function values ; Calling function to compute answer
let N = 1000000 ; let phi = new Array ( N + 5 ) ; phi . fill ( 0 ) ; function computeTotient ( ) { for ( let i = 1 ; i <= N ; i ++ ) phi [ i ] = i ; for ( let p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( let i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } function power ( x , y , p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function calculate ( x , k , mod ) { let arr = new Array ( N ) ; arr . fill ( 0 ) ; let count = 0 ; while ( mod > 1 ) { arr [ count ++ ] = mod ; mod = phi [ mod ] ; } let result = 1 ; let loop = count + 1 ; arr [ count ] = 1 ; for ( let i = Math . min ( k , loop ) - 1 ; i >= 0 ; i -- ) result = power ( x , result , arr [ i ] ) ; return result ; } computeTotient ( ) ; let x = 3 , k = 2 , m = 3 ; document . write ( calculate ( x , k , m ) ) ;
Number of ones in the smallest repunit | Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function
function countOnes ( n ) { let count = 1 ; let rem = 1 ; while ( rem != 0 ) { rem = ( rem * 10 + 1 ) % n ; count ++ ; } return count ; } let n = 13 ; document . write ( countOnes ( n ) ) ;
Largest of two distinct numbers without using any conditional statements or operators | Function to find the largest number ; Driver code
function largestNum ( a , b ) { return a * ( parseInt ( a / b ) > 0 ? 1 : 0 ) + b * ( parseInt ( b / a ) > 0 ? 1 : 0 ) ; } var a = 22 , b = 1231 ; document . write ( largestNum ( a , b ) ) ;
Number of Distinct Meeting Points on a Circular Road | Returns the GCD of two number . ; Returns the number of distinct meeting points . ; Find the relative speed . ; convert the negative value to positive . ; Driver Code
function gcd ( a , b ) { var c = a % b ; while ( c != 0 ) { a = b ; b = c ; c = a % b ; } return b ; } function numberOfmeet ( a , b ) { var ans ; if ( a > b ) ans = a - b ; else ans = b - a ; if ( a < 0 ) a = a * ( - 1 ) ; if ( b < 0 ) b = b * ( - 1 ) ; return ans / gcd ( a , b ) ; } var a = 1 , b = - 1 ; document . write ( numberOfmeet ( a , b ) ) ;
Minimum number of Square Free Divisors | Initializing MAX with SQRT ( 10 ^ 6 ) ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; This function returns the minimum number of Square Free divisors ; Precomputing Prime Factors ; holds max of max power of all prime factors ; holds the max power of current prime factor ; If number itself is prime , it will be included as answer and thus minimum required answer is 1 ; Driver Code to test above functions
let MAX = 1005 ; function SieveOfEratosthenes ( primes ) { let prime = new Array ( MAX ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) { prime [ i ] = false ; } } } for ( let p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { primes . push ( p ) ; } } } function minimumSquareFreeDivisors ( N ) { let primes = [ ] ; SieveOfEratosthenes ( primes ) ; let max_count = 0 ; for ( let i = 0 ; i < primes . length && primes [ i ] * primes [ i ] <= N ; i ++ ) { if ( N % primes [ i ] == 0 ) { let tmp = 0 ; while ( N % primes [ i ] == 0 ) { tmp ++ ; N = parseInt ( N / primes [ i ] , 10 ) ; } max_count = Math . max ( max_count , tmp ) ; } } if ( max_count == 0 ) { max_count = 1 ; } return max_count ; } let N = 24 ; document . write ( " " + minimumSquareFreeDivisors ( N ) + " " ) ; N = 6 ; document . write ( " " + minimumSquareFreeDivisors ( N ) ) ;
Subsequence of size k with maximum possible GCD | function to find GCD of sub sequence of size k with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least k numbers , it is a GCD of at least one sub sequence of size k ; Array in which sub sequence with size k with max GCD is to be found
function findMaxGCD ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let high = arr [ n - 1 ] ; let divisors = new Array ( high + 1 ) ; for ( let i = 0 ; i < divisors . length ; i ++ ) { divisors [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 1 ; j <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != Math . floor ( arr [ i ] / j ) ) divisors [ Math . floor ( arr [ i ] / j ) ] ++ ; } } } for ( let i = high ; i >= 1 ; i -- ) if ( divisors [ i ] >= k ) return i ; return 0 ; } let arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] ; let k = 3 ; let n = arr . length ; document . write ( findMaxGCD ( arr , n , k ) ) ;