codeparrot/xlcost-text-to-code huggingface dataset:
Pairs of Amicable Numbers | Calculate the sum of proper divisors ; 1 is a proper divisor ; To handle perfect squares ; Check if pair is amicable ; This function prlets count of amicable pairs present in the input array ; Map to store the numbers ; Iterate through each number , and find the sum of proper divisors and check if it 's  also present in the array ; It 's sum of proper divisors ; As the pairs are counted twice , thus divide by 2 ; Driver code
function sumOfDiv ( x ) { let sum = 1 ; for ( let i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } function isAmicable ( a , b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } function countPairs ( arr , n ) { let s = new Set ( ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( s . has ( sumOfDiv ( arr [ i ] ) ) ) { let sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return Math . floor ( count / 2 ) ; } let arr1 = [ 220 , 284 , 1184 , 1210 , 2 , 5 ] ; let n1 = arr1 . length ; document . write ( countPairs ( arr1 , n1 ) + " " ) ; let arr2 = [ 2620 , 2924 , 5020 , 5564 , 6232 , 6368 ] ; let n2 = arr2 . length ; document . write ( countPairs ( arr2 , n2 ) + " " ) ;
Minimum number of characters required to be removed such that every character occurs same number of times | Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Input
function minimumDeletion ( s , n ) { var countMap = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( countMap . has ( s [ i ] ) ) { countMap . set ( s [ i ] , countMap . get ( s [ i ] ) + 1 ) ; } else { countMap . set ( s [ i ] , 1 ) ; } } var countMultiset = [ ] ; countMap . forEach ( ( value , key ) => { countMultiset . push ( value ) ; } ) ; countMultiset . sort ( ) ; var ans = 1000000000 ; var i = 0 ; var m = countMultiset . length ; countMultiset . forEach ( j => { ans = Math . min ( ans , n - ( m - i ) * j ) ; i ++ ; } ) ; return ans ; } var S = " " ; var N = S . length ; document . write ( minimumDeletion ( S , N ) ) ;
Calculate cost of visiting all array elements in increasing order | Function to calculate total cost of visiting array elements in increasing order ; Stores the pair of element and their positions ; Traverse the array arr [ ] ; Push the pair { arr [ i ] , i } in v ; Sort the vector in ascending order . ; Stores the total cost ; Stores the index of last element visited ; Traverse the vector v ; Increment ans ; Assign ; Return ans ; Driver Code
function calculateDistance ( arr , N ) { var v = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { v . push ( [ arr [ i ] , i ] ) ; } v = v . sort ( ) ; var ans = 0 ; var last = 0 ; for ( var i = 0 ; i < N ; i ++ ) { ans += Math . abs ( v [ i ] [ 1 ] - last ) ; last = v [ i ] [ 1 ] ; } return ans ; } var arr = [ 4 , 3 , 2 , 5 , 1 ] ; var N = arr . length ; document . write ( calculateDistance ( arr , N ) ) ;
Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
function sortArr ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) { var num = arr [ i ] ; var s = num . toString ( ) ; var temp = s . split ( " " ) ; temp . sort ( ( a , b ) => b - a ) ; s = temp . join ( " " ) ; arr [ i ] = parseInt ( s ) ; var sr = parseInt ( Math . sqrt ( arr [ i ] ) ) ; var a = sr * sr ; var b = ( sr + 1 ) * ( sr + 1 ) ; if ( arr [ i ] - a < b - arr [ i ] ) arr [ i ] = a ; else arr [ i ] = b ; } arr . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } var arr = [ 29 , 43 , 28 , 12 ] ; var N = arr . length ; sortArr ( arr , N ) ;
Minimize cost required to make all array elements greater than or equal to zero | Function to find the minimum cost to make all array elements greater than or equal to 0 ; Sort the array in ascending order ; Stores the cost to make current array element >= 0 ; Stores the cost to make all array elements >= 0 ; Traverse the array and insert all the elements which are < 0 ; If current array element is negative ; Cost to make all array elements >= 0 ; Update curr if ans is minimum ; Prvar the minimum cost ; Given array ; Size of the array ; Given value of X ; Function call to find minimum cost to make all array elements >= 0
function minCost ( arr , N , X ) { arr . sort ( ) ; var sum = 0 ; var cost = 0 ; var INT_MAX = Number . MAX_VALUE ; var min_cost = INT_MAX ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cost = Math . abs ( arr [ i ] ) * X + ( sum - Math . abs ( arr [ i ] ) * i ) ; sum += Math . abs ( arr [ i ] ) ; min_cost = Math . min ( min_cost , cost ) ; } } document . write ( min_cost ) ; } var arr = [ - 1 , - 3 , - 2 , 4 , - 1 ] ; var N = arr . length ; var X = 2 ; minCost ( arr , N , X ) ;
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Given vector ; Given value of X & K
let ans = 0 ; function countArraysUtil ( arr , X , K , xorVal ) { if ( X == 0 ) { let cnt = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } let temp = arr [ 0 ] ; arr . shift ( ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . unshift ( temp ) ; temp = arr [ arr . length - 1 ] ; arr . pop ( ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . push ( temp ) ; arr [ 0 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ 0 ] -- ; arr [ arr . length - 1 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ arr . length - 1 ] -- ; } function countArrays ( arr , X , K ) { let xorVal = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) xorVal = xorVal ^ arr [ i ] ; countArraysUtil ( arr , X , K , xorVal ) ; document . write ( ans ) ; } let arr = [ 10 , 2 , 10 , 5 ] ; let X = 3 , K = 3 ; countArrays ( arr , X , K ) ;
Difference between maximum and minimum of a set of anagrams from an array | Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; Given array ; Size of the array
function hashFunction ( N ) { let prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 ] ; let value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = parseInt ( N / 10 , 10 ) ; } return value ; } function findDiff ( arr , n ) { let m = new Map ( ) ; let h , min , max ; for ( let i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . has ( h ) ) { m . set ( h , [ ] ) ; } ( m . get ( h ) ) . push ( arr [ i ] ) ; } m . forEach ( ( values , keys ) => { if ( values . length > 1 ) { min = Number . MAX_VALUE ; max = Number . MIN_VALUE ; for ( let j = 0 ; j < values . length ; j ++ ) { if ( ( m . get ( keys ) ) [ j ] < min ) { min = m . get ( keys ) [ j ] ; } if ( m . get ( keys ) [ j ] > max ) { max = m . get ( keys ) [ j ] ; } } document . write ( max - min ) ; } } ) } let arr = [ 121 , 312 , 234 , 211 , 112 , 102 ] ; let N = arr . length ; findDiff ( arr , N ) ;
Maximum area rectangle by picking four sides from array | function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; driver function
function findArea ( arr , n ) { arr . sort ( ( a , b ) => { return b - a ; } ) var dimension = [ 0 , 0 ] ; for ( var i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; } var arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ] ; var n = arr . length ; document . write ( findArea ( arr , n ) ) ;
Maximum of sum of length of rectangles and squares formed by given sticks | Function to find the maximum of sum of lengths of rectangles and squares formed using given set of sticks ; Stores the count of frequencies of all the array elements ; Stores frequencies which are at least 2 ; Convert all frequencies to nearest smaller even values . ; Sum of elements up to index of multiples of 4 ; Print the sum ; Driver Code
function findSumLength ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( freq . has ( arr [ i ] ) ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; else freq . set ( arr [ i ] , 1 ) ; } let freq_2 = new Map ( ) ; for ( let [ key , value ] of freq . entries ( ) ) { if ( freq . get ( key ) >= 2 ) freq_2 . set ( key , freq . get ( key ) ) ; } let arr1 = [ ] ; for ( let [ key , value ] of freq_2 . entries ( ) ) arr1 . push ( key * Math . floor ( freq_2 . get ( key ) / 2 ) * 2 ) ; arr1 . sort ( function ( a , b ) { return a - b ; } ) ; let summ = 0 ; for ( let i = 0 ; i < arr1 . length ; i ++ ) { summ += arr1 [ i ] ; } document . write ( summ ) ; } let arr = [ 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 ] ; let n = arr . Count ; findSumLength ( arr , n ) ;
Maximum area rectangle by picking four sides from array | Function for finding max area ; Traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; Return the product of dimensions ; Driver Code
function findArea ( arr , n ) { let s = new Set ( ) ; let first = 0 , second = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! s . has ( arr [ i ] ) ) { s . add ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } return ( first * second ) ; } let arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ] ; let n = arr . length ; document . write ( findArea ( arr , n ) ) ;
Length of longest strict bitonic subsequence | function to find length of longest strict bitonic subsequence ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last / first element of that subsequence ; arrays to store the length of increasing and decreasing subsequences which end at them or start from them ; to store the length of longest strict bitonic subsequence ; traverse the array elements from left to right ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' inc ' ; update arr [ i ] subsequence length in ' inc ' and in len_inc [ ] ; traverse the array elements from right to left ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' dcr ' ; update arr [ i ] subsequence length in ' dcr ' and in len_dcr [ ] ; calculating the length of all the strict bitonic subsequence ; required longest length strict bitonic subsequence ; Driver program to test above
function longLenStrictBitonicSub ( arr , n ) { var inc = new Map ( ) , dcr = new Map ( ) ; var len_inc = Array ( n ) , len_dcr = Array ( n ) ; var longLen = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var len = 0 ; if ( inc . has ( arr [ i ] - 1 ) ) len = inc . get ( arr [ i ] - 1 ) ; len_inc [ i ] = len + 1 ; inc . set ( arr [ i ] , len_inc [ i ] ) ; } for ( var i = n - 1 ; i >= 0 ; i -- ) { var len = 0 ; if ( dcr . has ( arr [ i ] - 1 ) ) len = dcr . get ( arr [ i ] - 1 ) ; len_dcr [ i ] = len + 1 ; dcr . set ( arr [ i ] , len_dcr [ i ] ) ; } for ( var i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } var arr = [ 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 ] ; var n = arr . length ; document . write ( " " + longLenStrictBitonicSub ( arr , n ) ) ;
Selection Sort VS Bubble Sort | JavaScript program for above approach ; Driver Code
function Selection_Sort ( arr , n ) { for ( let i = 0 ; i < n - 1 ; ++ i ) { let min_index = i ; for ( let j = i + 1 ; j < n ; ++ j ) { if ( arr [ j ] < arr [ min_index ] ) min_index = j ; } let temp = arr [ i ] ; arr [ i ] = arr [ min_index ] ; arr [ min_index ] = temp ; } } let n = 5 ; let arr = [ 2 , 0 , 1 , 4 , 3 ] ; Selection_Sort ( arr , n ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;
Selection Sort VS Bubble Sort | Javascript program for the above approach ; Driver Code
function Bubble_Sort ( arr , n ) { for ( var i = 1 ; i < n ; ++ i ) { for ( var j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { var temm = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temm ; } } } } var n = 5 ; var arr = [ 2 , 0 , 1 , 4 , 3 ] ; Bubble_Sort ( arr , n ) ; document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Stores all the array elements in sorted order ; Traverse the array ; Insert current element into multiset ; Perform each operation ; Stores smallest element of ms ; Stores the largest element of ms ; Stores updated value of smallest element of ms by given operations ; Stores updated value of largest element of ms by given operations ; If the value of a + b less than the sum of smallest and largest array element ; Erase the smallest element ; Erase the largest element ; Insert the updated value of the smallest element ; Insert the updated value of the smallest element ; Stores sum of array elements ; Traverse the multiset ; Update ans ; Driver Code
function minimum_possible_sum ( arr , n , k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } var ms = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { ms . push ( arr [ i ] ) ; } ms . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < k ; i ++ ) { var smallest_element = ms [ 0 ] ; var largest_element = ms [ ms . length - 1 ] ; var a = smallest_element * 2 ; var b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { ms . shift ( ) ; ms . pop ( ) ; ms . push ( a ) ; ms . push ( b ) ; ms . sort ( ( a , b ) => a - b ) } } var ans = 0 ; ms . forEach ( x => { ans += x ; } ) ; return ans ; } var arr = [ 50 , 1 , 100 , 100 , 1 ] ; var K = 2 ; var n = arr . length ; document . write ( minimum_possible_sum ( arr , n , K ) ) ;
Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Given String S ; Function Call
function firstSubsequence ( s ) { let isMax = - 1 ; for ( let i = 0 ; i < s . length - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s = s . substring ( 0 , isMax ) + s . substring ( isMax + 1 ) ; s . rerase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; s = s . substring ( 0 , s . length - 1 ) ; } document . write ( s ) ; } let S = " " ; firstSubsequence ( S ) ;
Print all root to leaf paths with there relative positions | Javascript program to print all root to leaf paths with there relative position ; tree structure ; Function create new node ; Store path information ; Prints given root to leaf path with underscores ; Find the minimum horizontal distance value in current root to leaf path ; Find minimum horizontal distance ; Print the root to leaf path with " _ " that indicate the related position ; Current tree node ; Print underscore ; Print current key ; A utility function print all path from root to leaf working of this function is similar to function of " Print _ vertical _ order " : Print paths of binary tree in vertical order www . geeksforgeeks . org / print - binary - tree - vertical - order - set - 2 / https : ; Base case ; Leaf node ; Add leaf node and then print path ; Store current path information ; Call left sub_tree ; Call left sub_tree ; Base case ; Driver code
let MAX_PATH_SIZE = 1000 ; class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } class PATH { constructor ( Hd , key ) { this . Hd = Hd ; this . key = key ; } } function printPath ( path , size ) { let minimum_Hd = Number . MAX_VALUE ; let p ; for ( let it = 0 ; it < size ; it ++ ) { p = path [ it ] ; minimum_Hd = Math . min ( minimum_Hd , p . Hd ) ; } for ( let it = 0 ; it < size ; it ++ ) { p = path [ it ] ; let noOfUnderScores = Math . abs ( p . Hd - minimum_Hd ) ; for ( let i = 0 ; i < noOfUnderScores ; i ++ ) document . write ( " " ) ; document . write ( p . key + " " ) ; } document . write ( " " ) ; } function printAllPathsUtil ( root , AllPath , HD , order ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { AllPath [ order ] = new PATH ( HD , root . data ) ; printPath ( AllPath , order + 1 ) ; return ; } AllPath [ order ] = new PATH ( HD , root . data ) ; printAllPathsUtil ( root . left , AllPath , HD - 1 , order + 1 ) ; printAllPathsUtil ( root . right , AllPath , HD + 1 , order + 1 ) ; } function printAllPaths ( root ) { if ( root == null ) return ; let Allpaths = [ ] ; for ( let i = 0 ; i < MAX_PATH_SIZE ; i ++ ) { Allpaths . push ( new PATH ( ) ) ; } printAllPathsUtil ( root , Allpaths , 0 , 0 ) ; } let root = new Node ( ' ' ) ; root . left = new Node ( ' ' ) ; root . right = new Node ( ' ' ) ; root . left . left = new Node ( ' ' ) ; root . left . right = new Node ( ' ' ) ; root . right . left = new Node ( ' ' ) ; root . right . right = new Node ( ' ' ) ; printAllPaths ( root ) ;
Minimum steps required to rearrange given array to a power sequence of 2 | Function to calculate the minimum steps required to convert given array into a power sequence of 2 ; Sort the array in ascending order ; Calculate the absolute difference between arr [ i ] and 2 ^ i for each index ; Return the answer ; Driver Code
function minsteps ( arr , n ) { arr . sort ( ( a , b ) => a - b ) var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans += Math . abs ( arr [ i ] - Math . pow ( 2 , i ) ) ; } return ans ; } var arr = [ 1 , 8 , 2 , 10 , 6 ] ; var n = arr . length ; document . write ( minsteps ( arr , n ) ) ;
Block swap algorithm for array rotation |  ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code
let leftRotate = ( arr , d , n ) => { if ( d == 0 d == n ) return ; if ( n - d == d ) { arr = swap ( arr , 0 , n - d , d ) ; return ; } if ( d < n - d ) { arr = swap ( arr , 0 , n - d , d ) ; leftRotate ( arr , d , n - d ) ; } else { arr = swap ( arr , 0 , d , n - d ) ; leftRotate ( arr + n - d , 2 * d - n , d ) ; } } let printArray = ( arr , size ) => { ans = ' ' for ( let i = 0 ; i < size ; i ++ ) ans += arr [ i ] + " " ; document . write ( ans ) } let swap = ( arr , fi , si , d ) => { for ( let i = 0 ; i < d ; i ++ ) { let temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } return arr } arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ;
Cost required to empty a given array by repeated removal of maximum obtained by given operations | Function to find the total cost of removing all array elements ; Sort the array in descending order ; Stores the total cost ; Contribution of i - th greatest element to the cost ; Remove the element ; If negative ; Add to the final cost ; Return the cost ; Given array arr [ ] ; Function Call
function findCost ( a , n ) { a . sort ( ( x , y ) => y - x ) ; var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { var p = a [ j ] - j ; a [ j ] = 0 ; if ( p < 0 ) { p = 0 ; continue ; } count += p ; } return count ; } var arr = [ 1 , 6 , 7 , 4 , 2 , 5 , 3 ] ; var N = arr . length ; document . write ( findCost ( arr , N ) ) ;
Block swap algorithm for array rotation | JavaScript code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B
function leftRotate ( arr , d , n ) { if ( d == 0 d == n ) return ; let i = d ; let j = n - d ; while ( i != j ) { if ( i < j ) { arr = swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { arr = swap ( arr , d - i , d , j ) ; i -= j ; } } arr = swap ( arr , d - i , d , i ) ; }
Count of index pairs with equal elements in an array | Set 2 | Function that count the pairs having same elements in the array arr [ ] ; Hash map to keep track of occurences of elements ; Traverse the array arr [ ] ; Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer ; Return the result ; Given array [ ] arr ; Function call
function countPairs ( arr , n ) { let ans = 0 ; let count = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( count . has ( arr [ i ] ) ) { ans += count . get ( arr [ i ] ) ; count . set ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . set ( arr [ i ] , 1 ) ; } } return ans ; } let arr = [ 1 , 2 , 1 , 1 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;