codeparrot/xlcost-text-to-code huggingface dataset:
Check if given Binary string follows then given condition or not | Function to check if the string follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Given String str ; Function Call
function checkrules ( s ) { if ( s . length == 0 ) return true ; if ( s [ 0 ] != ' ' ) return false ; if ( s . length > 2 ) { if ( s [ 1 ] == ' ' && s [ 2 ] == ' ' ) return checkrules ( s . substring ( 3 ) ) ; } return checkrules ( s . substring ( 1 ) ) ; } var str = " " ; if ( checkrules ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Length of longest subsequence in an Array having all elements as Nude Numbers | Function to check if the number is a Nude number ; Variable initialization ; Integer ' copy ' is converted to a string ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Given array arr [ ] ; Function Call
function isNudeNum ( n ) { let copy , length , flag = 0 ; copy = n ; let temp ; temp = copy . toString ( ) ; length = temp . length ; for ( let i = 0 ; i < length ; i ++ ) { let num = temp [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( num == 0 n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } function longestNudeSubseq ( arr , n ) { let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } let arr = [ 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 ] ; let n = arr . length ; document . write ( longestNudeSubseq ( arr , n ) ) ;
Longest subarray with difference exactly K between any two distinct values | Function to return the length of the longest sub - array ; Initialize set ; Store 1 st element of sub - array into set ; Check absolute difference between two elements ; If the new element is not present in the set ; If the set contains two elements ; Otherwise ; Update the maximum length ; Remove the set elements ; Driver Code
function longestSubarray ( arr , n , k ) { let i , j , Max = 1 ; let s = new Set ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { s . add ( arr [ i ] ) ; for ( j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] - arr [ j ] ) == 0 || Math . abs ( arr [ i ] - arr [ j ] ) == k ) { if ( ! s . has ( arr [ j ] ) ) { if ( s . size == 2 ) break ; else s . add ( arr [ j ] ) ; } } else break ; } if ( s . size == 2 ) { Max = Math . max ( Max , j - i ) ; s . clear ; } else s . clear ; } return Max ; } let arr = [ 1 , 0 , 2 , 2 , 5 , 5 , 5 ] ; let N = arr . length ; let K = 1 ; let length = longestSubarray ( arr , N , K ) ; if ( length == 1 ) document . write ( - 1 ) ; else document . write ( length ) ;
Longest subarray of non | Function to find the maximum length of a subarray of 1 s after removing at most one 0 ; Stores the count of consecutive 1 's from left ; Stores the count of consecutive 1 's from right ; Traverse left to right ; If cell is non - empty ; Increase count ; If cell is empty ; Store the count of consecutive 1 's  till (i - 1)-th index ; Traverse from right to left ; Store the count of consecutive 1 s till ( i + 1 ) - th index ; Stores the length of longest subarray ; Store the maximum ; If array a contains only 1 s return n else return ans ; Driver code
function longestSubarray ( a , n ) { var l = Array ( n ) . fill ( 0 ) ; var r = Array ( n ) . fill ( 0 ) ; for ( i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } var ans = - 1 ; for ( i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = Math . max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } var arr = [ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ] ; var n = arr . length ; document . write ( longestSubarray ( arr , n ) ) ;
Find largest factor of N such that N / F is less than K | Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Print the value of packages ; Given N and K ; Function Call
function findMaxValue ( N , K ) { let packages ; let maxi = 1 ; for ( let i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = Math . max ( maxi , i ) ; } packages = parseInt ( N / maxi ) ; document . write ( packages ) ; } let N = 8 , K = 7 ; findMaxValue ( N , K ) ;
Largest square which can be formed using given rectangular blocks | Function to find maximum side length of square ; Sort array in asc order ; Traverse array in desc order ; Driver code ; Given array arr ; Function Call
function maxSide ( a , n ) { let sideLength = 0 ; a . sort ( ) ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } document . write ( sideLength ) ; } let N = 6 ; let arr = [ 3 , 2 , 1 , 5 , 2 , 4 ] ; maxSide ( arr , N ) ;
Minimize difference after changing all odd elements to even | Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Given array ; Function call
function minDiff ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } a . sort ( ( a , b ) => a - b ) ; let mindifference = a [ 1 ] - a [ 0 ] ; for ( let i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } document . write ( mindifference ) ; } let arr = [ 3 , 8 , 13 , 30 , 50 ] ; let n = arr . length ; minDiff ( arr , n ) ;
Longest subarray having sum K | Set 2 | To store the prefix sum array ; Function for searching the lower bound of the subarray ; Iterate until low less than equal to high ; For each mid finding sum of sub array less than or equal to k ; Return the final answer ; Function to find the length of subarray with sum K ; Initialize sum to 0 ; Push the prefix sum of the array arr [ ] in prefix [ ] ; Search r for each i ; Update ans ; Print the length of subarray found in the array ; Given array arr [ ] ; Given sum K ; Function Call
let v = [ ] ; function bin ( val , k , n ) { let lo = 0 ; let hi = n ; let mid ; let ans = - 1 ; while ( lo <= hi ) { mid = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( v [ mid ] - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } function findSubarraySumK ( arr , N , K ) { let sum = 0 ; v . push ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . push ( sum ) ; } let l = 0 , ans = 0 , r ; for ( let i = 0 ; i < N ; i ++ ) { r = bin ( v [ i ] , K , N ) ; ans = Math . max ( ans , r - i ) ; } document . write ( ans ) ; } let arr = [ 6 , 8 , 14 , 9 , 4 , 11 , 10 ] ; let N = arr . length ; let K = 13 ; findSubarraySumK ( arr , N , K ) ;
Partition a set into two subsets such that difference between max of one and min of other is minimized | Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Given array arr [ ] ; Size of array ; Function Call
function splitArray ( arr , N ) { arr . sort ( ) ; let result = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } let arr = [ 3 , 1 , 2 , 6 , 4 ] ; let N = arr . length ; document . write ( splitArray ( arr , N ) ) ;
Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | Function to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; First element of a given row ; Element in the given column ; Driver Code ; Function call
function findValue ( R , C ) { let k = ( R * ( R - 1 ) ) / 2 + 1 ; let diff = R + 1 ; for ( let i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } let R = 4 ; let C = 4 ; let k = findValue ( R , C ) ; document . write ( k ) ;
Maximize number of groups formed with size not smaller than its largest element | Function that prlets the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Prleting maximum number of groups ; Driver Code
function makeGroups ( a , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } let no_of_groups = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { no_of_groups += Math . floor ( v [ i ] / i ) ; v [ i ] = v [ i ] % i ; } let i = 1 ; let total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { let rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } document . write ( no_of_groups + " " ) ; } let arr = [ 2 , 3 , 1 , 2 , 2 ] ; let size = arr . length ; makeGroups ( arr , size ) ;
Leftmost Column with atleast one 1 in a row | JavaScript program to calculate leftmost column having at least 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver code
let N = 3 ; let M = 4 ; function FindColumn ( mat ) { let row = 0 , col = M - 1 ; let flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row ] [ col ] == 1 ) { col -- ; flag = 1 ; } else { row ++ ; } } col ++ ; if ( flag != 0 ) return col + 1 ; else return - 1 ; } let mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 ] , [ 0 , 0 , 1 , 1 ] ] ; document . write ( FindColumn ( mat ) ) ;
Find two numbers made up of a given digit such that their difference is divisible by N | Function to implement the above approach ; To store remainder - length of the number as key - value pairs ; Iterate till N + 1 length ; Search remainder in the map ; If remainder is not already present insert the length for the corresponding remainder ; Keep increasing M ; To keep M in range of integer ; Length of one number is the current Length ; Length of the other number is the length paired with current remainder in map ; Driver code
function findNumbers ( N , M ) { let m = M ; let remLen = new Map ( ) ; let len , remainder = 0 ; for ( len = 1 ; len <= N + 1 ; ++ len ) { remainder = M % N ; if ( ! remLen . has ( remainder ) ) { remLen . set ( remainder , len ) ; } else { break ; } M = M * 10 + m ; M = M % N ; } let LenA = len ; let LenB = remLen . get ( remainder ) ; for ( let i = 0 ; i < LenB ; ++ i ) document . write ( m ) ; document . write ( " " ) ; for ( let i = 0 ; i < LenA ; ++ i ) document . write ( m ) ; } let N = 8 , M = 2 ; findNumbers ( N , M ) ;
Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements | 2D prefix sum for submatrix sum query for matrix ; Function to find the prefix sum of the matrix from i and j ; Function to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; Combining the two arrays ; Calculating the 2D prefix sum ; Iterating through all the elements of matrix and considering them to be the bottom right ; Applying binary search over side length ; If sum of this submatrix >= k then new search space will be [ low , mid ] ; Else new search space will be [ mid + 1 , high ] ; Adding the total submatrices ; Driver Code
var prefix_2D = Array ( 2005 ) ; function subMatrixSum ( i , j , len ) { return prefix_2D [ i ] [ j ] - prefix_2D [ i ] [ j - len ] - prefix_2D [ i - len ] [ j ] + prefix_2D [ i - len ] [ j - len ] ; } function numberOfWays ( a , b , n , m , k ) { for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( a [ i - 1 ] == b [ j - 1 ] ) prefix_2D [ i ] [ j ] = 1 ; else prefix_2D [ i ] [ j ] = 0 ; } } for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i ] [ j - 1 ] ; } } for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { prefix_2D [ i ] [ j ] += prefix_2D [ i - 1 ] [ j ] ; } } var answer = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { var low = 1 ; var high = Math . min ( i , j ) ; while ( low < high ) { var mid = ( low + high ) >> 1 ; if ( subMatrixSum ( i , j , mid ) >= k ) { high = mid ; } else { low = mid + 1 ; } } if ( subMatrixSum ( i , j , low ) >= k ) { answer += ( Math . min ( i , j ) - low + 1 ) ; } } } return answer ; } var N = 2 , M = 3 ; var A = [ 1 , 2 ] ; var B = [ 1 , 2 , 3 ] ; for ( i = 0 ; i < 205 ; i ++ ) prefix_2D [ i ] = Array ( 205 ) . fill ( 0 ) ; var K = 1 ; document . write ( numberOfWays ( A , B , N , M , K ) ) ;
Find lexicographically smallest string in at most one swaps | Function to return the lexicographically smallest string that can be formed by swapping at most one character . The characters might not necessarily be adjacent . ; Store last occurrence of every character ; Set - 1 as default for every character . ; Character index to fill in the last occurrence array ; If this is true then this character is being visited for the first time from the last Thus last occurrence of this character is stored in this index ; Character to replace ; Find the last occurrence of this character . ; Swap this with the last occurrence ; Driver code
function findSmallest ( s ) { let len = s . length ; let loccur = new Array ( 26 ) ; loccur . fill ( - 1 ) ; for ( let i = len - 1 ; i >= 0 ; -- i ) { let chI = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } let sorted_s = s ; sorted_s . sort ( ) ; for ( let i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { let chI = sorted_s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let last_occ = loccur [ chI ] ; let temp = s [ i ] ; s [ i ] = s [ last_occ ] ; s [ last_occ ] = temp ; break ; } } return s . join ( " " ) ; } let s = " " ; document . write ( findSmallest ( s . split ( ' ' ) ) ) ;
Maximum size of square such that all submatrices of that size have sum less than K | Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to check whether square sub matrices of size mid satisfy the condition or not ; Iterating throught all possible submatrices of given size ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Search space ; Binary search for size ; Check if the mid satisfies the given condition ; Driver Code
let N = 4 ; let M = 5 ; function preProcess ( mat , aux ) { for ( let i = 0 ; i < M ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( let i = 1 ; i < N ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 1 ; j < M ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } function sumQuery ( aux , tli , tlj , rbi , rbj ) { let res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } function check ( mid , aux , K ) { let satisfies = true ; for ( let x = 0 ; x < N ; x ++ ) { for ( let y = 0 ; y < M ; y ++ ) { if ( x + mid - 1 <= N - 1 && y + mid - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + mid - 1 , y + mid - 1 ) > K ) satisfies = false ; } } } return ( satisfies == true ) ; } function maximumSquareSize ( mat , K ) { let aux = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { aux [ i ] = new Array ( M ) ; } preProcess ( mat , aux ) ; let low = 1 , high = Math . min ( N , M ) ; let mid ; while ( high - low > 1 ) { mid = Math . floor ( ( low + high ) / 2 ) ; if ( check ( mid , aux , K ) ) { low = mid ; } else high = mid ; } if ( check ( high , aux , K ) ) return high ; return low ; } let K = 30 ; let mat = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 5 , 3 , 8 , 1 , 2 ] , [ 4 , 6 , 7 , 5 , 5 ] , [ 2 , 4 , 8 , 9 , 4 ] ] ; document . write ( maximumSquareSize ( mat , K ) ) ;
Length of Longest Subarray with same elements in atmost K increments | Initialize array for segment tree ; Function that builds the segment tree to return the max element in a range ; update the value in segment tree from given array ; find the middle index ; If there are more than one elements , then recur for left and right subtrees and store the max of values in this node ; Function to return the max element in the given range ; If the range is out of bounds , return - 1 ; Function that returns length of longest subarray with same elements in atmost K increments . ; Initialize the result variable Even though the K is 0 , the required longest subarray , in that case , will also be of length 1 ; Initialize the prefix sum array ; Build the prefix sum array ; Build the segment tree for range max query ; Loop through the array with a starting point as i for the required subarray till the longest subarray is found ; Performing the binary search to find the endpoint for the selected range ; Find the mid for binary search ; Find the max element in the range [ i , mid ] using Segment Tree ; Total sum of subarray after increments ; Actual sum of elements before increments ; Check if such increment is possible If true , then current i is the actual starting point of the required longest subarray ; Now for finding the endpoint for this range Perform the Binary search again with the updated start ; Store max end point for the range to give longest subarray ; If false , it means that the selected range is invalid ; Perform the Binary Search again with the updated end ; Store the length of longest subarray ; Return result ; Driver code
let segment_tree = new Array ( 4 * 1000000 ) ; function build ( A , start , end , node ) { if ( start == end ) segment_tree [ node ] = A [ start ] ; else { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; segment_tree [ node ] = Math . max ( build ( A , start , mid , 2 * node + 1 ) , build ( A , mid + 1 , end , 2 * node + 2 ) ) ; } return segment_tree [ node ] ; } function query ( start , end , l , r , node ) { if ( start > r end < l ) return - 1 ; if ( start >= l && end <= r ) return segment_tree [ node ] ; let mid = parseInt ( ( start + end ) / 2 , 10 ) ; return Math . max ( query ( start , mid , l , r , 2 * node + 1 ) , query ( mid + 1 , end , l , r , 2 * node + 2 ) ) ; } function longestSubArray ( A , N , K ) { let res = 1 ; let preSum = new Array ( N + 1 ) ; preSum [ 0 ] = A [ 0 ] ; for ( let i = 0 ; i < N ; i ++ ) preSum [ i + 1 ] = preSum [ i ] + A [ i ] ; build ( A , 0 , N - 1 , 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { let start = i , end = N - 1 , mid , max_index = i ; while ( start <= end ) { mid = parseInt ( ( start + end ) / 2 , 10 ) ; let max_element = query ( 0 , N - 1 , i , mid , 0 ) ; let expected_sum = ( mid - i + 1 ) * max_element ; let actual_sum = preSum [ mid + 1 ] - preSum [ i ] ; if ( expected_sum - actual_sum <= K ) { start = mid + 1 ; max_index = Math . max ( max_index , mid ) ; } else { end = mid - 1 ; } } res = Math . max ( res , max_index - i + 1 ) ; } return res ; } let arr = [ 2 , 0 , 4 , 6 , 7 ] , K = 6 ; let N = arr . length ; document . write ( longestSubArray ( arr , N , K ) ) ;
Count of numbers from range [ L , R ] that end with any of the given digits | Function to return the count of the required numbers ; Last digit of the current number ; If the last digit is equal to any of the given digits ; Driver code
function countNums ( l , r ) { let cnt = 0 ; for ( let i = l ; i <= r ; i ++ ) { let lastDigit = ( i % 10 ) ; if ( ( lastDigit % 10 ) == 2 || ( lastDigit % 10 ) == 3 || ( lastDigit % 10 ) == 9 ) { cnt ++ ; } } return cnt ; } let l = 11 , r = 33 ; document . write ( countNums ( l , r ) ) ;
Minimum K such that sum of array elements after division by K does not exceed S | Function to return the minimum value of k that satisfies the given condition ; Find the maximum element ; Lowest answer can be 1 and the highest answer can be ( maximum + 1 ) ; Binary search ; Get the mid element ; Calculate the sum after dividing the array by new K which is mid ; Search in the second half ; First half ; Driver code
function findMinimumK ( a , n , s ) { var maximum = a [ 0 ] ; for ( i = 0 ; i < n ; i ++ ) { maximum = Math . max ( maximum , a [ i ] ) ; } var low = 1 , high = maximum + 1 ; var ans = high ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += parseInt ( ( a [ i ] / mid ) ) ; } if ( sum > s ) low = mid + 1 ; else { ans = Math . min ( ans , mid ) ; high = mid - 1 ; } } return ans ; } var a = [ 10 , 7 , 8 , 10 , 12 , 19 ] ; var n = a . length ; var s = 27 ; document . write ( findMinimumK ( a , n , s ) ) ;
Calculate the Sum of GCD over all subarrays | int a [ 100001 ] ; ; Build Sparse Table ; Building the Sparse Table for GCD [ L , R ] Queries ; Utility Function to calculate GCD in range [ L , R ] ; Calculating where the answer is stored in our Sparse Table ; Utility Function to find next - farther position where gcd is same ; BinarySearch for Next Position for EndPointer ; Utility function to calculate sum of gcd ; Initializing all the values ; Finding the next position for endPointer ; Adding the suitable sum to our answer ; Changing prevEndPointer ; Recalculating tempGCD ; Driver code
let SparseTable = new Array ( 100001 ) ; for ( let i = 0 ; i < 100001 ; i ++ ) { SparseTable [ i ] = new Array ( 51 ) ; for ( let j = 0 ; j < 51 ; j ++ ) { SparseTable [ i ] [ j ] = 0 ; } } function buildSparseTable ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { SparseTable [ i ] [ 0 ] = a [ i ] ; } for ( let j = 1 ; j <= 19 ; j ++ ) { for ( let i = 0 ; i <= n - ( 1 << j ) ; i ++ ) { SparseTable [ i ] [ j ] = __gcd ( SparseTable [ i ] [ j - 1 ] , SparseTable [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) ; } } } function queryForGCD ( L , R ) { let returnValue ; let j = Math . floor ( Math . log ( R - L + 1 ) ) ; returnValue = __gcd ( SparseTable [ L ] [ j ] , SparseTable [ R - ( 1 << j ) + 1 ] [ j ] ) ; return returnValue ; } function nextPosition ( tempGCD , startPointer , prevEndPointer , n ) { let high = n - 1 ; let low = prevEndPointer ; let mid = prevEndPointer ; let nextPos = prevEndPointer ; while ( high >= low ) { mid = ( ( high + low ) >> 1 ) ; if ( queryForGCD ( startPointer , mid ) == tempGCD ) { nextPos = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return nextPos + 1 ; } function calculateSum ( a , n ) { buildSparseTable ( a , n ) ; let endPointer , startPointer , prevEndPointer , tempGCD ; let tempAns = 0 ; for ( let i = 0 ; i < n ; i ++ ) { endPointer = i ; startPointer = i ; prevEndPointer = i ; tempGCD = a [ i ] ; while ( endPointer < n ) { endPointer = nextPosition ( tempGCD , startPointer , prevEndPointer , n ) ; tempAns += ( ( endPointer - prevEndPointer ) * tempGCD ) ; prevEndPointer = endPointer ; if ( endPointer < n ) { tempGCD = __gcd ( tempGCD , a [ endPointer ] ) ; } } } return tempAns ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let n = 6 ; let a = [ 2 , 2 , 2 , 3 , 5 , 5 ] ; document . write ( calculateSum ( a , n ) ) ;