codeparrot/xlcost-text-to-code huggingface dataset:
Maximize shortest path between given vertices by adding a single edge | Javascript program for the above approach ; To store graph as adjacency list ; To store the shortest path ; Function that performs BFS Traversal ; Fill initially each distance as INF ; Perform BFS ; Traverse the current edges ; Update the distance ; Insert in queue ; Function that maximizes the shortest path between source and destination vertex by adding a single edge between given selected nodes ; To update the shortest distance between node 1 to other vertices ; To update the shortest distance between node N to other vertices ; Store the values x [ i ] - y [ i ] ; Sort all the vectors of pairs ; Traverse data [ ] ; Maximize x [ a ] - y [ b ] ; Print minimum cost ; Given nodes and edges ; Sort the selected nodes ; Given edges ; Function Call
let INF = 1e9 + 7 ; let N , M ; let edges = [ ] ; let dist = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { dist [ i ] = new Array ( 200000 ) ; for ( let j = 0 ; j < 200000 ; j ++ ) { dist [ i ] [ j ] = INF ; } } function bfs ( dist , s ) { let q = new Array ( 200000 ) ; let qh = 0 , qt = 0 ; q [ qh ++ ] = s ; dist [ s ] = 0 ; while ( qt < qh ) { let x = q [ qt ++ ] ; for ( let y = 0 ; y < edges [ x ] . length ; y ++ ) { if ( dist [ edges [ x ] [ y ] ] == INF ) { dist [ edges [ x ] [ y ] ] = dist [ x ] + 1 ; q [ qh ++ ] = edges [ x ] [ y ] ; } } } } function shortestPathCost ( selected , K ) { let data = [ ] ; bfs ( dist [ 0 ] , 0 ) ; bfs ( dist [ 1 ] , N - 1 ) ; for ( let i = 0 ; i < K ; i ++ ) { data . push ( [ dist [ 0 ] [ selected [ i ] ] - dist [ 1 ] [ selected [ i ] ] , selected [ i ] ] ) ; } data . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] ; } ) ; let best = 0 ; let MAX = - INF ; for ( let it = 0 ; it < data . length ; it ++ ) { let a = data [ it ] [ 1 ] ; best = Math . max ( best , MAX + dist [ 1 ] [ a ] ) ; MAX = Math . max ( MAX , dist [ 0 ] [ a ] ) ; } document . write ( Math . min ( dist [ 0 ] [ N - 1 ] , best + 1 ) ) ; } N = 5 ; M = 4 ; let K = 2 ; let selected = [ 1 , 3 ] ; ( selected ) . sort ( function ( a , b ) { return a - b ; } ) ; edges = [ ] ; for ( let i = 0 ; i < 200005 ; i ++ ) edges . push ( [ ] ) ; edges [ 0 ] . push ( 1 ) ; edges [ 1 ] . push ( 0 ) ; edges [ 1 ] . push ( 2 ) ; edges [ 2 ] . push ( 1 ) ; edges [ 2 ] . push ( 3 ) ; edges [ 3 ] . push ( 2 ) ; edges [ 3 ] . push ( 4 ) ; edges [ 4 ] . push ( 3 ) ; shortestPathCost ( selected , K ) ;
Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function call
function removeElements ( arr , n ) { arr . sort ( ( a , b ) => b - a ) ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans += arr [ i ] * Math . pow ( 2 , i ) ; } return ans ; } var n = 4 ; var arr = [ 3 , 1 , 2 , 3 ] ; document . write ( removeElements ( arr , n ) ) ;
Sorting boundary elements of a matrix | Javascript program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Dimensions of a Matrix ; Given Matrix ; Function Call
function printMatrix ( grid , m , n ) { let A = [ ] ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( j == n - 1 || ( i == m - 1 ) j == 0 i == 0 ) A . push ( grid [ i ] [ j ] ) ; } } A . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) for ( let i = 0 ; i < m - 2 ; i ++ ) { document . write ( A [ A . length - i - 1 ] + " " ) ; for ( let j = 1 ; j < n - 1 ; j ++ ) document . write ( grid [ i + 1 ] [ j ] + " " ) ; document . write ( A [ n + i ] + " " ) } document . write ( A . slice ( n + m - 2 , n + m - 2 + n ) . reverse ( ) . join ( " " ) ) ; [ n + m - 2 : n + m - 2 + n ] << endl ; } let m = 4 , n = 5 ; let grid = [ [ 1 , 2 , 3 , 4 , 0 ] , [ 1 , 1 , 1 , 1 , 2 ] , [ 1 , 2 , 2 , 2 , 4 ] , [ 1 , 9 , 3 , 1 , 7 ] ] ; printMatrix ( grid , m , n ) ;
Lexicographically largest string possible in one swap | Function to return the lexicographically largest string possible by swapping at most one character ; Stores last occurrence of every character ; Initialize with - 1 for every character ; Keep updating the last occurrence of each character ; If a previously unvisited character occurs ; Stores the sorted string ; Character to replace ; Find the last occurrence of this character ; Swap this with the last occurrence ; Driver Code
function findLargest ( 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 . join ( " " ) . split ( ' ' ) ; sorted_s . sort ( ) ; sorted_s . reverse ( ) ; 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 str = " " ; let s = str . split ( ' ' ) ; document . write ( findLargest ( s ) ) ;
Given a sorted and rotated array , find if there is a pair with a given sum | This function returns true if arr [ 0. . n - 1 ] has a pair with sum equals to x . ; Find the pivot element ; l is now index of smallest element ; r is now index of largest element ; Keep moving either l or r till they meet ; If we find a pair with sum x , we return true ; If current pair sum is less , move to the higher sum ; Move to the lower sum side ; Driver code
function pairInSortedRotated ( arr , n , x ) { let i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; let l = ( i + 1 ) % n ; let r = i ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) return true ; if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return false ; } let arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] ; let sum = 16 ; let n = arr . length ; if ( pairInSortedRotated ( arr , n , sum ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Given a sorted and rotated array , find if there is a pair with a given sum | This function returns count of number of pairs with sum equals to x . ; Find the pivot element . Pivot element is largest element of array . ; l is index of smallest element . ; r is index of largest element . ; Variable to store count of number of pairs . ; Find sum of pair formed by arr [ l ] and arr [ r ] and update l , r and cnt accordingly . ; If we find a pair with sum x , then increment cnt , move l and r to next element . ; This condition is required to be checked , otherwise l and r will cross each other and loop will never terminate . ; If current pair sum is less , move to the higher sum side . ; If current pair sum is greater , move to the lower sum side . ; Driver Code
function pairsInSortedRotated ( arr , n , x ) { let i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; let l = ( i + 1 ) % n ; let r = i ; let cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } let arr = [ 11 , 15 , 6 , 7 , 9 , 10 ] ; let sum = 16 ; let n = arr . length ; document . write ( pairsInSortedRotated ( arr , n , sum ) ) ;
Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver program
function maxSum ( arr , n ) { let arrSum = 0 ; let currVal = 0 ; for ( let i = 0 ; i < n ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } let maxVal = currVal ; for ( let j = 1 ; j < n ; j ++ ) { currVal = currVal + arrSum - n * arr [ n - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } let arr = [ 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; document . write ( " " + maxSum ( arr , n ) ) ;
Maximum sum of i * arr [ i ] among all rotations of a given array | Returns maximum value of i * arr [ i ] maximum sum rotation ; Initialize result ; Consider rotation beginning with i for all possible values of i . ; Initialize sum of current rotation ; Compute sum of all values . We don 't  actually rotation the array, but compute  sum by finding ndexes when arr[i] is  first element ; Update result if required ; Driver code
function maxSum ( arr , n ) { var res = Number . MIN_VALUE ; for ( i = 0 ; i < n ; i ++ ) { var curr_sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { var index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . max ( res , curr_sum ) ; } return res ; } var arr = [ 8 , 3 , 1 , 2 ] ; var n = arr . length ; document . write ( maxSum ( arr , n ) ) ;
Maximum sum of i * arr [ i ] among all rotations of a given array | An efficient JavaScript program to compute maximum sum of i * arr [ i ] ; Compute sum of all array elements ; Compute sum of i * arr [ i ] for initial configuration . ; Initialize result ; Compute values for other iterations ; Compute next value using previous value in O ( 1 ) time ; Update current value ; Update result if required ; Driver code
function maxSum ( arr , n ) { let cum_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; let curr_val = 0 ; for ( let i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; let res = curr_val ; for ( let i = 1 ; i < n ; i ++ ) { let next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . max ( res , next_val ) ; } return res ; } let arr = [ 8 , 3 , 1 , 2 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) + " " ) ;
Minimum time required to cover a Binary Array | function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; check if count is odd ; check ifcount is even ; return the time ; initialise N ; array initialisation
function solve ( arr , n ) { let k = arr . length ; let mp = Array . from ( { length : n + 2 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( let i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } let leftSegment = arr [ 0 ] - 1 ; for ( let i = 1 ; i < k ; i ++ ) { leftSegment = Math . max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } let rightSegment = n - arr [ k - 1 ] ; let maxSegment = Math . max ( leftSegment , rightSegment ) ; let tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } let N = 5 ; let arr = [ 1 , 4 ] ; document . write ( solve ( arr , N ) ) ;
Find the Rotation Count in Rotated Sorted array | Returns count of rotations for an array which is first sorted in ascending order , then rotated ; We basically find index of minimum element ; Driver Code
function countRotations ( arr , n ) { let min = arr [ 0 ] , min_index = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( min > arr [ i ] ) { min = arr [ i ] ; min_index = i ; } } return min_index ; } let arr = [ 15 , 18 , 2 , 3 , 6 , 12 ] ; let n = arr . length ; document . write ( countRotations ( arr , n ) ) ;
Find the Rotation Count in Rotated Sorted array | Returns count of rotations for an array which is first sorted in ascending order , then rotated ; This condition is needed to handle the case when the array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like { 3 , 4 , 5 , 1 , 2 } ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver code
function countRotations ( arr , low , high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } let arr = [ 15 , 18 , 2 , 3 , 6 , 12 ] ; let n = arr . length ; document . write ( countRotations ( arr , 0 , n - 1 ) ) ;
Quickly find multiple left rotations of an array | Set 1 | Fills temp with two copies of arr ; Store arr elements at i and i + n ; Function to left rotate an array k time ; Starting position of array after k rotations in temp will be k % n ; Print array after k rotations ; Driver program
function preprocess ( arr , n , temp ) { for ( i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } function leftRotate ( arr , n , k , temp ) { var start = k % n ; for ( i = start ; i < start + n ; i ++ ) document . write ( temp [ i ] + " " ) ; document . write ( " " ) ; } var arr = [ 1 , 3 , 5 , 7 , 9 ] ; var n = arr . length ; var temp = Array ( 2 * n ) . fill ( 0 ) ; preprocess ( arr , n , temp ) ; var k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ;
Arrange the array such that upon performing given operations an increasing order is obtained | Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; Insert last element at ith position ; Print desired Array ; Given Array
function Desired_Array ( v ) { let n = v . length ; v . sort ( function ( a , b ) { return a - b } ) ; let i = n - 1 ; while ( i > 0 ) { let p = v [ n - 1 ] ; for ( let j = n - 1 ; j >= i ; j -- ) { v [ j ] = v [ j - 1 ] ; } v [ i ] = p ; i -- ; } for ( let x = 0 ; x < v . length ; x ++ ) { document . write ( v [ x ] + " " ) ; } document . write ( " " ) ; } let v = [ 1 , 2 , 3 , 4 , 5 ] ; Desired_Array ( v ) ; let v1 = [ 1 , 12 , 2 , 10 , 4 , 16 , 6 ] ; Desired_Array ( v1 ) ;
Quickly find multiple left rotations of an array | Set 1 | Function to left rotate an array k times ; Print rray after k rotations ; Driver Code
function leftRotate ( arr , n , k ) { for ( let i = k ; i < k + n ; i ++ ) document . write ( arr [ i % n ] + " " ) ; } let arr = [ 1 , 3 , 5 , 7 , 9 ] ; n = arr . length ; k = 2 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 3 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 4 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ;
How to sort an array in a single loop ? | Function for Sorting the array using a single loop ; Finding the length of array ' arr ' ; Sorting using a single loop ; Checking the condition for two simultaneous elements of the array ; Swapping the elements . ; updating the value of j = - 1 so after getting updated for j ++ in the loop it becomes 0 and the loop begins from the start . ; Declaring an integer array of size 11. ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
function sortArrays ( arr ) { let length = arr . length ; for ( let j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let arr = [ 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 ] ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + arr . join ( " " ) + " " ) ;
How to sort an array in a single loop ? | Function for Sorting the array using a single loop ; Finding the length of array ' arr ' ; Sorting using a single loop ; Type Conversion of char to int . ; Comparing the ascii code . ; Swapping of the characters ; Declaring a String ; Declaring a character array to store characters of geeks in it . ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
function sortArrays ( arr ) { let length = arr . length ; for ( let j = 0 ; j < length - 1 ; j ++ ) { let d1 = arr [ j ] ; let d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let geeks = " " ; let arr = geeks . split ( " " ) ; document . write ( " " + arr . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ;
Convert given Matrix into sorted Spiral Matrix | Javascript program to Convert given Matrix into sorted Spiral Matrix ; Function to convert the array to Spiral ; For Array pointer ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Function to convert 2D array to 1D array ; Store value 2D Matrix To 1D array ; Function to print the Matrix ; Print Spiral Matrix ; Function to sort the array ; Sort array Using InBuilt Function ; Function to Convert given Matrix into sorted Spiral Matrix ; Driver code
let MAX = 1000 ; function ToSpiral ( m , n , Sorted , a ) { let index = 0 ; let k = 0 , l = 0 ; while ( k < m && l < n ) { for ( let i = l ; i < n ; ++ i ) { a [ k ] [ i ] = Sorted [ index ] ; index ++ ; } k ++ ; for ( let i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = Sorted [ index ] ; index ++ ; } n -- ; if ( k < m ) { for ( let i = n - 1 ; i >= l ; -- i ) { a [ m - 1 ] [ i ] = Sorted [ index ] ; index ++ ; } m -- ; } if ( l < n ) { for ( let i = m - 1 ; i >= k ; -- i ) { a [ i ] [ l ] = Sorted [ index ] ; index ++ ; } l ++ ; } } } function convert2Dto1D ( y , m , n ) { let index = 0 ; let x = new Array ( m * n ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { x [ index ] = y [ i ] [ j ] ; index ++ ; } } return x ; } function PrintMatrix ( a , m , n ) { for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( a [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function SortArray ( x ) { x . sort ( function ( a , b ) { return a - b ; } ) ; return x ; } function convertMatrixToSortedSpiral ( y , m , n ) { let a = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) a [ i ] = new Array ( MAX ) ; let x = new Array ( m * n ) ; x = convert2Dto1D ( y , m , n ) ; x = SortArray ( x ) ; ToSpiral ( m , n , x , a ) ; PrintMatrix ( a , m , n ) ; } let m = 4 , n = 3 ; let y = [ [ 2 , 5 , 12 ] , [ 22 , 45 , 55 ] , [ 1 , 6 , 8 ] , [ 13 , 56 , 10 ] ] ; convertMatrixToSortedSpiral ( y , m , n ) ;
Reversal algorithm for right rotation of an array | Function to reverse arr [ ] from index start to end ; Function to right rotate arr [ ] of size n by d ; function to print an array ; driver code
function reverseArray ( arr , start , end ) { while ( start < end ) { let temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } return arr ; } function rightRotate ( arr , d , n ) { arr = reverseArray ( arr , 0 , n - 1 ) ; arr = reverseArray ( arr , 0 , d - 1 ) ; arr = reverseArray ( arr , d , n - 1 ) ; return arr ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = arr . length ; let k = 3 ; arr = rightRotate ( arr , k , n ) ; printArray ( arr , n ) ;
Find a rotation with maximum hamming distance | Return the maximum hamming distance of a rotation ; arr [ ] to brr [ ] two times so that we can traverse through all rotations . ; We know hamming distance with 0 rotation would be 0. ; We try other rotations one by one and compute Hamming distance of every rotation ; We can never get more than n . ; driver program
function maxHamming ( arr , n ) { let brr = new Array ( 2 * n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( let i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; let maxHam = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let currHam = 0 ; for ( let j = i , k = 0 ; j < ( i + n ) ; j ++ , k ++ ) if ( brr [ j ] != arr [ k ] ) currHam ++ ; if ( currHam == n ) return n ; maxHam = max ( maxHam , currHam ) ; } return maxHam ; } let arr = [ 2 , 4 , 6 , 8 ] ; let n = arr . length ; document . write ( maxHamming ( arr , n ) ) ;
Print left rotation of array in O ( n ) time and O ( 1 ) space | Function to leftRotate array multiple times ; To get the starting point of rotated array ; Prints the rotated array from start position ; Driver Code ; Function Call ; Function Call ; Function Call
function leftRotate ( arr , n , k ) { let mod = k % n ; for ( let i = 0 ; i < n ; i ++ ) document . write ( ( arr [ ( mod + i ) % n ] ) + " " ) ; document . write ( " " ) ; } let arr = [ 1 , 3 , 5 , 7 , 9 ] ; let n = arr . length ; let k = 2 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 3 ; leftRotate ( arr , n , k ) ; document . write ( " " ) ; k = 4 ; leftRotate ( arr , n , k ) ;
Find element at given index after a number of rotations | Function to compute the element at given index ; Range [ left ... right ] ; Rotation will not have any effect ; Returning new element ; Driver Code ; No . of rotations ; Ranges according to 0 - based indexing
let findElement = ( arr , ranges , rotations , index ) => { for ( let i = rotations - 1 ; i >= 0 ; i -- ) { let left = ranges [ i ] [ 0 ] ; let right = ranges [ i ] [ 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let rotations = 2 ; let ranges = [ [ 0 , 2 ] , [ 0 , 3 ] ] ; let index = 1 ; document . write ( findElement ( arr , ranges , rotations , index ) ) ;
Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged | Function that returns the string s in sorted form such that the positions of alphabets and numeric digits remain unchanged ; String to character array ; Sort the array ; Count of alphabets and numbers ; Get the index from where the alphabets start ; Now replace the string with sorted string ; If the position was occupied by an alphabet then replace it with alphabet ; Else replace it with a number ; Return the sorted string ; Driver code
function sort ( s ) { var c = s . split ( ' ' ) ; c . sort ( ) ; var al_c = 0 , nu_c = 0 ; while ( c [ al_c ] . charCodeAt ( 0 ) < 97 ) al_c ++ ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) < 97 ) s = s . substring ( 0 , i ) + c [ nu_c ++ ] + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + c [ al_c ++ ] + s . substring ( i + 1 ) ; } return s ; } var s = " " ; document . write ( sort ( s ) ) ;
Split the array and add the first part to the end | JavaScript program to split array and move first part to end . ; Rotate array by 1. ; Driver code
function splitArr ( arr , n , k ) { for ( let i = 0 ; i < k ; i ++ ) { let x = arr [ 0 ] ; for ( let j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } let arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] ; let n = arr . length ; let position = 2 ; splitArr ( arr , 6 , position ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;