codeparrot/xlcost-text-to-code huggingface dataset:
Longest subsequence of a number having same left and right rotation | Function to find the longest subsequence having equal left and right rotation ; Length of the string ; Iterate for all possible combinations of a two - digit numbers ; Check for alternate occurrence of current combination ; Increment the current value ; Increment the current value ; If alternating sequence is obtained of odd length ; Reduce to even length ; Update answer to store the maximum ; Return the answer ; Driver Code
function findAltSubSeq ( s ) { var n = s . length , ans = - 1000000000 ; for ( var i = 0 ; i < 10 ; i ++ ) { for ( var j = 0 ; j < 10 ; j ++ ) { var cur = 0 , f = 0 ; for ( var k = 0 ; k < n ; k ++ ) { if ( f == 0 && s [ k ] - ' ' == i ) { f = 1 ; cur ++ ; } else if ( f == 1 && s [ k ] - ' ' == j ) { f = 0 ; cur ++ ; } } if ( i != j && cur % 2 == 1 ) cur -- ; ans = Math . max ( cur , ans ) ; } } return ans ; } var s = " " ; document . write ( findAltSubSeq ( s ) ) ;
Mth element after K Right Rotations of an Array | Function to return Mth element of array after k right rotations ; The array comes to original state after N rotations ; If K is greater or equal to M ; Mth element after k right rotations is ( N - K ) + ( M - 1 ) th element of the array ; Otherwise ; ( M - K - 1 ) th element of the array ; Return the result ; Driver Code
function getFirstElement ( a , N , K , M ) { K %= N ; let index ; if ( K >= M ) index = ( N - K ) + ( M - 1 ) ; else index = ( M - K - 1 ) ; let result = a [ index ] ; return result ; } let a = [ 1 , 2 , 3 , 4 , 5 ] ; let N = 5 ; let K = 3 , M = 2 ; document . write ( getFirstElement ( a , N , K , M ) ) ;
Find the Mth element of the Array after K left rotations | Function to return Mth element of array after k left rotations ; The array comes to original state after N rotations ; Mth element after k left rotations is ( K + M - 1 ) % N th element of the original array ; Return the result ; Array initialization ; Size of the array ; Given K rotation and Mth element to be found after K rotation ; Function call
function getFirstElement ( a , N , K , M ) { K %= N ; var index = ( K + M - 1 ) % N ; var result = a [ index ] ; return result ; } var a = [ 3 , 4 , 5 , 23 ] ; var N = a . length ; var K = 2 , M = 1 ; document . write ( getFirstElement ( a , N , K , M ) ) ;
Rotate all odd numbers right and all even numbers left in an Array of 1 to N | function to left rotate ; function to right rotate ; Function to rotate the array ; Driver code
function left_rotate ( arr ) { let last = arr [ 1 ] ; for ( let i = 3 ; i < 6 ; i = i + 2 ) { arr [ i - 2 ] = arr [ i ] ; } arr [ 6 - 1 ] = last ; } function right_rotate ( arr ) { let start = arr [ 6 - 2 ] ; for ( let i = 6 - 4 ; i >= 0 ; i = i - 2 ) { arr [ i + 2 ] = arr [ i ] ; } arr [ 0 ] = start ; } function rotate ( arr ) { left_rotate ( arr ) ; right_rotate ( arr ) ; for ( let i = 0 ; i < 6 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; rotate ( arr ) ;
Maximize count of corresponding same elements in given permutations using cyclic rotations | Function to maximize the matching pairs between two permutation using left and right rotation ; Left array store distance of element from left side and right array store distance of element from right side ; Map to store index of elements ; idx1 is index of element in first permutation idx2 is index of element in second permutation ; If element if present on same index on both permutations then distance is zero ; Calculate distance from left and right side ; Calculate distance from left and right side ; Maps to store frequencies of elements present in left and right arrays ; Find maximum frequency ; Return the result ; Given permutations P1 and P2 ; Function Call
function maximumMatchingPairs ( perm1 , perm2 , n ) { var left = Array ( n ) ; var right = Array ( n ) ; var mp1 = new Map ( ) , mp2 = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { mp1 . set ( perm1 [ i ] , i ) ; } for ( var j = 0 ; j < n ; j ++ ) { mp2 . set ( perm2 [ j ] , j ) ; } for ( var i = 0 ; i < n ; i ++ ) { var idx2 = mp2 . get ( perm1 [ i ] ) ; var idx1 = i ; if ( idx1 == idx2 ) { left [ i ] = 0 ; right [ i ] = 0 ; } else if ( idx1 < idx2 ) { left [ i ] = ( n - ( idx2 - idx1 ) ) ; right [ i ] = ( idx2 - idx1 ) ; } else { left [ i ] = ( idx1 - idx2 ) ; right [ i ] = ( n - ( idx1 - idx2 ) ) ; } } var freq1 = new Map ( ) , freq2 = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freq1 . has ( left [ i ] ) ) freq1 . set ( left [ i ] , freq1 . get ( left [ i ] ) + 1 ) else freq1 . set ( left [ i ] , 1 ) if ( freq2 . has ( right [ i ] ) ) freq2 . set ( right [ i ] , freq2 . get ( right [ i ] ) + 1 ) else freq2 . set ( right [ i ] , 1 ) } var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Math . max ( freq1 . get ( left [ i ] ) , freq2 . get ( right [ i ] ) ) ) ; } return ans ; } var P1 = [ 5 , 4 , 3 , 2 , 1 ] ; var P2 = [ 1 , 2 , 3 , 4 , 5 ] ; var n = P1 . length ; document . write ( maximumMatchingPairs ( P1 , P2 , n ) ) ;
Count of rotations required to generate a sorted array | Function to return the count of rotations ; Find the smallest element ; Return its index ; If array is not rotated at all ; Driver Code
function countRotation ( arr , n ) { for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { return i ; } } return 0 ; } let arr1 = [ 4 , 5 , 1 , 2 , 3 ] ; document . write ( countRotation ( arr1 , arr1 . length ) ) ;
Count of rotations required to generate a sorted array | Function to return the count of rotations ; If array is not rotated ; Check if current element is greater than the next element ; the next element is the smallest ; Check if current element is smaller than it 's previous  element ; Current element is the smallest ; Check if current element is greater than lower bound ; The sequence is increasing so far Search for smallest element on the right subarray ; Smallest element lies on the left subarray ; Search for the smallest element on both subarrays ; Driver Program
function countRotation ( arr , low , high ) { if ( low > high ) { return 0 ; } let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( mid < high && arr [ mid ] > arr [ mid + 1 ] ) { return mid + 1 ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid ; } if ( arr [ mid ] > arr [ low ] ) { return countRotation ( arr , mid + 1 , high ) ; } if ( arr [ mid ] < arr [ high ] ) { return countRotation ( arr , low , mid - 1 ) ; } else { let rightIndex = countRotation ( arr , mid + 1 , high ) ; let leftIndex = countRotation ( arr , low , mid - 1 ) ; if ( rightIndex == 0 ) { return leftIndex ; } return rightIndex ; } } let arr1 = [ 4 , 5 , 1 , 2 , 3 ] ; document . write ( countRotation ( arr1 , 0 , arr1 . length - 1 ) ) ;
Range sum queries for anticlockwise rotations of Array by K indices | Function to execute the queries ; Construct a new array of size 2 * N to store prefix sum of every index ; Copy elements to the new array ; Calculate the prefix sum for every index ; Set start poleter as 0 ; Query to perform anticlockwise rotation ; Query to answer range sum ; If poleting to 1 st index ; Display the sum upto start + R ; Subtract sum upto start + L - 1 from sum upto start + R ; Driver code ; Number of query ; Store all the queries
function rotatedSumQuery ( arr , n , query , Q ) { let prefix = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { prefix [ i ] = arr [ i ] ; prefix [ i + n ] = arr [ i ] ; } for ( let i = 1 ; i < 2 * n ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; let start = 0 ; for ( let q = 0 ; q < Q ; q ++ ) { if ( query [ q ] [ 0 ] == 1 ) { let k = query [ q ] [ 1 ] ; start = ( start + k ) % n ; } else if ( query [ q ] [ 0 ] == 2 ) { let L , R ; L = query [ q ] [ 1 ] ; R = query [ q ] [ 2 ] ; if ( start + L == 0 ) document . write ( prefix [ start + R ] + " " ) ; else document . write ( prefix [ start + R ] - prefix [ start + L - 1 ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let Q = 5 ; let query = [ [ 2 , 1 , 3 ] , [ 1 , 3 ] , [ 2 , 0 , 3 ] , [ 1 , 4 ] , [ 2 , 3 , 5 ] ] ; let n = arr . length ; rotatedSumQuery ( arr , n , query , Q ) ;
Check if a string can be formed from another string by at most X circular clockwise shifts | Function to check that all characters of s1 can be converted to s2 by circular clockwise shift atmost X times ; Check for all characters of the strings whether the difference between their ascii values is less than X or not ; If both the characters are same ; Calculate the difference between the ASCII values of the characters ; If difference exceeds X ; Driver Code ; Function call
function isConversionPossible ( s1 , s2 , x ) { let diff = 0 , n ; n = s1 . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; diff = ( ( s2 [ i ] . charCodeAt ( 0 ) - s1 [ i ] . charCodeAt ( 0 ) ) + 26 ) % 26 ; if ( diff > x ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let s1 = " " ; let s2 = " " ; let x = 6 ; isConversionPossible ( s1 , s2 , x ) ;
Count rotations which are divisible by 10 | Function to return the count of all the rotations which are divisible by 10. ; Loop to iterate through the number ; If the last digit is 0 , then increment the count ; Driver code
function countRotation ( n ) { let count = 0 ; do { let digit = n % 10 ; if ( digit == 0 ) count ++ ; n = parseInt ( n / 10 ) ; } while ( n != 0 ) ; return count ; } let n = 10203 ; document . write ( countRotation ( n ) ) ;
Clockwise rotation of Linked List | Link list node ; A utility function to push a node ; allocate node ; put in the data ; link the old list off the new node ; move the head to povar to the new node ; A utility function to prvar linked list ; Function that rotates the given linked list clockwise by k and returns the updated head pointer ; If the linked list is empty ; len is used to store length of the linked list tmp will point to the last node after this loop ; If k is greater than the size of the linked list ; Subtract from length to convert it into left rotation ; If no rotation needed then return the head node ; current will either povar to kth or null after this loop ; If current is null then k is equal to the count of nodes in the list Don 't change the list in this case ; current points to the kth node ; Change next of last node to previous head ; Change head to ( k + 1 ) th node ; Change next of kth node to null ; Return the updated head pointer ; * The constructed linked list is : 1.2 . 3.4 . 5 ; Rotate the linked list ; Print the rotated linked list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( " " ) ; } function rightRotate ( head , k ) { if ( head == null ) return head ; var tmp = head ; var len = 1 ; while ( tmp . next != null ) { tmp = tmp . next ; len ++ ; } if ( k > len ) k = k % len ; k = len - k ; if ( k == 0 k == len ) return head ; var current = head ; var cnt = 1 ; while ( cnt < k && current != null ) { current = current . next ; cnt ++ ; } if ( current == null ) return head ; var kthnode = current ; tmp . next = head ; head = kthnode . next ; kthnode . next = null ; return head ; } var head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; var k = 2 ; var updated_head = rightRotate ( head , k ) ; printList ( updated_head ) ;
Check if it is possible to make array increasing or decreasing by rotating the array | Function that returns true if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum && the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index && minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index && minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code
function isPossible ( a , n ) { if ( n <= 2 ) return true ; var flag = 0 ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] > a [ i + 1 ] && a [ i + 1 ] > a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; flag = 0 ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] < a [ i + 1 ] && a [ i + 1 ] < a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; var val1 = Number . MAX_VALUE , mini = - 1 , val2 = Number . MIN_VALUE , maxi = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < val1 ) { mini = i ; val1 = a [ i ] ; } if ( a [ i ] > val2 ) { maxi = i ; val2 = a [ i ] ; } } flag = 1 ; for ( i = 0 ; i < maxi ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi + 1 == mini ) { flag = 1 ; for ( i = mini ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } flag = 1 ; for ( i = 0 ; i < mini ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi - 1 == mini ) { flag = 1 ; for ( i = maxi ; i < n - 1 ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } return false ; } var a = [ 4 , 5 , 6 , 2 , 3 ] ; var n = a . length ; if ( isPossible ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count number of rotated strings which have more number of vowels in the first half than second half | Function to return the count of rotated Strings which have more number of vowels in the first half than the second half ; Create a new String ; Pre array to store count of all vowels ; Compute the prefix array ; To store the required answer ; Find all rotated Strings ; Right and left index of the String ; x1 stores the number of vowels in the rotated String ; Left stores the number of vowels in the first half of rotated String ; Right stores the number of vowels in the second half of rotated String ; If the count of vowels in the first half is greater than the count in the second half ; Return the required answer ; Driver code
function cntRotations ( s , n ) { let str = s + s ; let pre = new Array ( 2 * n ) ; pre . fill ( 0 ) ; for ( let i = 0 ; i < 2 * n ; i ++ ) { if ( i != 0 ) pre [ i ] += pre [ i - 1 ] ; if ( str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' ) { pre [ i ] ++ ; } } let ans = 0 ; for ( let i = n - 1 ; i < 2 * n - 1 ; i ++ ) { let r = i , l = i - n ; let x1 = pre [ r ] ; if ( l >= 0 ) x1 -= pre [ l ] ; r = i - parseInt ( n / 2 , 10 ) ; let left = pre [ r ] ; if ( l >= 0 ) left -= pre [ l ] ; let right = x1 - left ; if ( left > right ) { ans ++ ; } } return ans ; } let s = " " ; let n = s . length ; document . write ( cntRotations ( s , n ) ) ;
Count number of rotated strings which have more number of vowels in the first half than second half | Function to return the count of rotated strings which have more number of vowels in the first half than the second half ; Compute the number of vowels in first - half ; Compute the number of vowels in second - half ; Check if first - half has more vowels ; Check for all possible rotations ; Return the answer ; Driver code ; Function call
function cntRotations ( s , n ) { let lh = 0 , rh = 0 , i , ans = 0 ; for ( i = 0 ; i < parseInt ( n / 2 , 10 ) ; ++ i ) if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { lh ++ ; } for ( i = parseInt ( n / 2 , 10 ) ; i < n ; ++ i ) if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { rh ++ ; } if ( lh > rh ) ans ++ ; for ( i = 1 ; i < n ; ++ i ) { if ( s [ i - 1 ] == ' ' s [ i - 1 ] == ' ' s [ i - 1 ] == ' ' s [ i - 1 ] == ' ' s [ i - 1 ] == ' ' ) { rh ++ ; lh -- ; } if ( s [ ( i - 1 + n / 2 ) % n ] == ' ' || s [ ( i - 1 + n / 2 ) % n ] == ' ' || s [ ( i - 1 + n / 2 ) % n ] == ' ' || s [ ( i - 1 + n / 2 ) % n ] == ' ' || s [ ( i - 1 + n / 2 ) % n ] == ' ' ) { rh -- ; lh ++ ; } if ( lh > rh ) ans ++ ; } return ans ; } let s = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let n = s . length ; document . write ( cntRotations ( s , n ) ) ;
Queries for rotation and Kth character of the given string in constant time | Javascript implementation of the approach ; Function to perform the required queries on the given string ; Pointer pointing to the current starting character of the string ; For every query ; If the query is to rotate the string ; Update the pointer pointing to the starting character of the string ; Index of the kth character in the current rotation of the string ; Print the kth character ; Driver code
var size = 2 ; function performQueries ( str , n , queries , q ) { var ptr = 0 ; for ( var i = 0 ; i < q ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) { ptr = ( ptr + queries [ i ] [ 1 ] ) % n ; } else { var k = queries [ i ] [ 1 ] ; var index = ( ptr + k - 1 ) % n ; document . write ( str [ index ] + " " ) ; } } } var str = " " ; var n = str . length ; var queries = [ [ 1 , 2 ] , [ 2 , 2 ] , [ 1 , 4 ] , [ 2 , 7 ] ] ; var q = queries . length ; performQueries ( str , n , queries , q ) ;
Check if a string can be obtained by rotating another string d places | Function to reverse an array from left index to right index ( both inclusive ) ; Function that returns true if str1 can be made equal to str2 by rotating either d places to the left or to the right ; Left Rotation string will contain the string rotated Anti - Clockwise Right Rotation string will contain the string rotated Clockwise ; Copying the str1 string to left rotation string and right rotation string ; Rotating the string d positions to the left ; Rotating the string d positions to the right ; Compairing the rotated strings ; If cannot be made equal with left rotation ; If cannot be made equal with right rotation ; If both or any one of the rotations of str1 were equal to str2 ; Driver code ; d is the rotating factor ; In case length of str1 < d
function ReverseArray ( arr , left , right ) { var temp ; while ( left < right ) { temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ; left ++ ; right -- ; } } function RotateAndCheck ( str1 , str2 , d ) { if ( str1 . length !== str2 . length ) return false ; var left_rot_str1 = [ ] ; var right_rot_str1 = [ ] ; var left_flag = true , right_flag = true ; var str1_size = str1 . length ; for ( var i = 0 ; i < str1_size ; i ++ ) { left_rot_str1 . push ( str1 [ i ] ) ; right_rot_str1 . push ( str1 [ i ] ) ; } ReverseArray ( left_rot_str1 , 0 , d - 1 ) ; ReverseArray ( left_rot_str1 , d , str1_size - 1 ) ; ReverseArray ( left_rot_str1 , 0 , str1_size - 1 ) ; ReverseArray ( right_rot_str1 , 0 , str1_size - d - 1 ) ; ReverseArray ( right_rot_str1 , str1_size - d , str1_size - 1 ) ; ReverseArray ( right_rot_str1 , 0 , str1_size - 1 ) ; for ( var i = 0 ; i < str1_size ; i ++ ) { if ( left_rot_str1 [ i ] !== str2 [ i ] ) { left_flag = false ; } if ( right_rot_str1 [ i ] !== str2 [ i ] ) { right_flag = false ; } } if ( left_flag right_flag ) return true ; return false ; } var str1 = " " ; var str2 = " " ; var d = 2 ; d = d % str1 . length ; if ( RotateAndCheck ( str1 , str2 , d ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count rotations of N which are Odd and Even | Function to count of all rotations which are odd and even ; Driver Code
function countOddRotations ( n ) { var odd_count = 0 , even_count = 0 ; do { var digit = n % 10 ; if ( digit % 2 == 1 ) odd_count ++ ; else even_count ++ ; n = parseInt ( n / 10 ) ; } while ( n != 0 ) ; document . write ( " " + odd_count + " " ) ; document . write ( " " + even_count + " " ) ; } var n = 1234 ; countOddRotations ( n ) ;
Generate all rotations of a number | Function to return the count of digits of n ; Function to print the left shift numbers ; Formula to calculate left shift from previous number ; Update the original number ; Driver Code
function numberOfDigits ( n ) { let cnt = 0 ; while ( n > 0 ) { cnt ++ ; n = parseInt ( n / 10 , 10 ) ; } return cnt ; } function cal ( num ) { let digits = numberOfDigits ( num ) ; let powTen = Math . pow ( 10 , digits - 1 ) ; for ( let i = 0 ; i < digits - 1 ; i ++ ) { let firstDigit = parseInt ( num / powTen , 10 ) ; let left = ( ( num * 10 ) + firstDigit ) - ( firstDigit * powTen * 10 ) ; document . write ( left + " " ) ; num = left ; } } let num = 1445 ; cal ( num ) ;
Check whether all the rotations of a given number is greater than or equal to the given number or not | javascript implementation of the approach ; Splitting the number at index i and adding to the front ; Checking if the value is greater than or equal to the given value ; Driver code
function CheckKCycles ( n , s ) { var ff = true ; var x = 0 ; for ( i = 1 ; i < n ; i ++ ) { x = ( s . substring ( i ) + s . substring ( 0 , i ) ) . length ; if ( x >= s . length ) { continue ; } ff = false ; break ; } if ( ff ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var n = 3 ; var s = " " ; CheckKCycles ( n , s ) ;
Rotate the sub | Definition of node of linkedlist ; This function take head pointer of list , start and end points of sublist that is to be rotated and the number k and rotate the sublist to right by k places . ; If k is greater than size of sublist then we will take its modulo with size of sublist ; If k is zero or k is equal to size or k is a multiple of size of sublist then list remains intact ; m - th node ; This loop will traverse all node till end node of sublist . Current traversed node ; Count of traversed nodes ; Previous of m - th node ; We will save ( m - 1 ) th node and later make it point to ( n - k + 1 ) th node ; That is how we bring ( n - k + 1 ) th node to front of sublist . ; This keeps rest part of list intact . ; Function for creating and linking new nodes ; Driver code
class ListNode { constructor ( ) { this . data = 0 ; this . next = null ; } } function rotateSubList ( A , m , n , k ) { var size = n - m + 1 ; if ( k > size ) { k = k % size ; } if ( k == 0 k == size ) { var head = A ; while ( head != null ) { document . write ( head . data ) ; head = head . next ; } return ; } var link = null ; if ( m == 1 ) { link = A ; } var c = A ; var count = 0 ; var end = null ; var pre = null ; while ( c != null ) { count ++ ; if ( count == m - 1 ) { pre = c ; link = c . next ; } if ( count == n - k ) { if ( m == 1 ) { end = c ; A = c . next ; } else { end = c ; pre . next = c . next ; } } if ( count == n ) { var d = c . next ; c . next = link ; end . next = d ; var head = A ; while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } return ; } c = c . next ; } } function push ( head , val ) { var new_node = new ListNode ( ) ; new_node . data = val ; new_node . next = head ; head = new_node ; return head ; } var head = null ; head = push ( head , 70 ) ; head = push ( head , 60 ) ; head = push ( head , 50 ) ; head = push ( head , 40 ) ; head = push ( head , 30 ) ; head = push ( head , 20 ) ; head = push ( head , 10 ) ; var tmp = head ; document . write ( " " ) ; while ( tmp != null ) { document . write ( tmp . data + " " ) ; tmp = tmp . next ; } document . write ( " " ) ; var m = 3 , n = 6 , k = 2 ; document . write ( " " ) ; rotateSubList ( head , m , n , k ) ;
Generating numbers that are divisor of their right | Function to check if N is a divisor of its right - rotation ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
function rightRotationDivisor ( N ) { let lastDigit = N % 10 ; let rightRotation = ( lastDigit * Math . pow ( 10 , Math . floor ( ( Math . log10 ( N ) ) ) ) + Math . floor ( N / 10 ) ) ; return ( rightRotation % N == 0 ) ; } function generateNumbers ( m ) { for ( let i = Math . floor ( Math . pow ( 10 , ( m - 1 ) ) ) ; i < Math . floor ( Math . pow ( 10 , m ) ) ; i ++ ) if ( rightRotationDivisor ( i ) ) document . write ( i + " " ) ; } let m = 3 ; generateNumbers ( m ) ;