codeparrot/xlcost-text-to-code huggingface dataset:
Smallest substring with each letter occurring both in uppercase and lowercase | Javascript program for the above approach ; Function to check if the current string is balanced or not ; For every character , check if there exists uppercase as well as lowercase characters ; Function to find smallest length substring in the given string which is balanced ; Store frequency of lowercase characters ; Stores frequency of uppercase characters ; Count frequency of characters ; Mark those characters which are not present in both lowercase and uppercase ; Initialize the frequencies back to 0 ; Marks the start and end of current substring ; Marks the start and end of required substring ; Stores the length of smallest balanced substring ; Remove all characters obtained so far ; Remove extra characters from front of the current substring ; If substring ( st , i ) is balanced ; No balanced substring ; Store answer string ; Given string
let MaxValue = 2147483647 ; function balanced ( small , caps ) { for ( let i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && ( caps [ i ] == 0 ) ) return false ; else if ( ( small [ i ] == 0 ) && ( caps [ i ] != 0 ) ) return false ; } return true ; } function smallestBalancedSubstring ( s ) { let small = new Array ( 26 ) ; let i ; let caps = new Array ( 26 ) ; small . fill ( 0 ) ; caps . fill ( 0 ) ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] . charCodeAt ( ) >= 65 && s [ i ] . charCodeAt ( ) <= 90 ) caps [ s [ i ] . charCodeAt ( ) - 65 ] ++ ; else small [ s [ i ] . charCodeAt ( ) - 97 ] ++ ; } let mp = new Map ( ) ; for ( i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && caps [ i ] == 0 ) { mp . set ( String . fromCharCode ( i + 97 ) , 1 ) ; } else if ( caps [ i ] != 0 && small [ i ] == 0 ) mp . set ( String . fromCharCode ( i + 65 ) , 1 ) ; } small . fill ( 0 ) ; caps . fill ( 0 ) ; i = 0 ; let st = 0 ; let start = - 1 , end = - 1 ; let minm = MaxValue ; while ( i < s . length ) { if ( mp . has ( s [ i ] ) ) { while ( st < i ) { if ( s [ st ] . charCodeAt ( ) >= 65 && s [ st ] . charCodeAt ( ) <= 90 ) caps [ s [ st ] . charCodeAt ( ) - 65 ] -- ; else small [ s [ st ] . charCodeAt ( ) - 97 ] -- ; st ++ ; } i += 1 ; st = i ; } else { if ( s [ i ] . charCodeAt ( ) >= 65 && s [ i ] . charCodeAt ( ) <= 90 ) caps [ s [ i ] . charCodeAt ( ) - 65 ] ++ ; else small [ s [ i ] . charCodeAt ( ) - 97 ] ++ ; while ( true ) { if ( s [ st ] . charCodeAt ( ) >= 65 && s [ st ] . charCodeAt ( ) <= 90 && caps [ s [ st ] . charCodeAt ( ) - 65 ] > 1 ) { caps [ s [ st ] . charCodeAt ( ) - 65 ] -- ; st ++ ; } else if ( s [ st ] . charCodeAt ( ) >= 97 && s [ st ] . charCodeAt ( ) <= 122 && small [ s [ st ] . charCodeAt ( ) - 97 ] > 1 ) { small [ s [ st ] . charCodeAt ( ) - 97 ] -- ; st ++ ; } else break ; } if ( balanced ( small , caps ) ) { if ( minm > ( i - st + 1 ) ) { minm = i - st + 1 ; start = st ; end = i ; } } i += 1 ; } } if ( start == - 1 end == - 1 ) document . write ( - 1 + " " ) ; else { let ans = " " ; for ( let j = start ; j <= end ; j ++ ) ans += s [ j ] ; document . write ( ans + " " ) ; } } let s = " " ; smallestBalancedSubstring ( s ) ;
Pair of strings having longest common prefix of maximum length in given array | Class of Trie ; Function to insert a string into Trie ; Stores length of the string ; Traverse the string str ; If str [ i ] is not present in current path of Trie ; Create a new node of Trie ; Update root ; Function to find the maximum length of longest common prefix in Trie with str ; Stores length of str ; Stores length of longest common prefix in Trie with str ; Traverse the string str ; If str [ i ] is present in the current path of Trie ; Update len ; Update root ; Function to print the pair having maximum length of the longest common prefix ; Stores index of the string having maximum length of longest common prefix ; Stores maximum length of longest common prefix . ; Create root node of Trie ; Insert arr [ 0 ] into Trie ; Traverse the array . ; Stores maximum length of longest common prefix in Trie with arr [ i ] ; If temp is greater than len ; Update len ; Update idx ; Traverse array arr [ ] ; Stores length of arr [ i ] ; Check if maximum length of longest common prefix > M ; Traverse string arr [ i ] and arr [ j ] ; If current character of both string does not match . ; Print pairs having maximum length of the longest common prefix ; Driver Code
class TrieNode { constructor ( ) { this . child = Array ( 256 ) ; } } ; function insertTrie ( root , str ) { var M = str . length ; for ( var i = 0 ; i < M ; i ++ ) { if ( root . child [ str [ i ] ] == null ) { root . child [ str [ i ] ] = new TrieNode ( ) ; } root = root . child [ str [ i ] ] ; } } function findStrLen ( root , str ) { var M = str . length ; var len = 0 ; for ( var i = 0 ; i < M ; i ++ ) { if ( root . child [ str [ i ] ] != null ) { len ++ ; root = root . child [ str [ i ] ] ; } else { return len ; } } return len ; } function findMaxLenPair ( arr , N ) { var idx = - 1 ; var len = 0 ; var root = new TrieNode ( ) ; insertTrie ( root , arr [ 0 ] ) ; for ( var i = 1 ; i < N ; i ++ ) { var temp = findStrLen ( root , arr [ i ] ) ; if ( temp > len ) { len = temp ; idx = i ; } insertTrie ( root , arr [ i ] ) ; } for ( var i = 0 ; i < N ; i ++ ) { var M = arr [ i ] . length ; if ( i != idx && M >= len ) { var found = true ; for ( var j = 0 ; j < len ; j ++ ) { if ( arr [ i ] [ j ] != arr [ idx ] [ j ] ) { found = false ; break ; } } if ( found ) { document . write ( " " + arr [ i ] + " " + arr [ idx ] + " " ) ; return ; } } } } var arr = [ " " , " " , " " ] ; var N = arr . length ; findMaxLenPair ( arr , N ) ;
Check if a string is a scrambled form of another string | Javascript program to check if a given string is a scrambled form of another string ; Strings of non - equal length can 't be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Converting string to character array ; Checking condition for Anagram ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; If none of the above conditions are satisfied ; Driver code
function isScramble ( S1 , S2 ) { if ( S1 . length != S2 . length ) { return false ; } let n = S1 . length ; if ( n == 0 ) { return true ; } if ( S1 == S2 ) { return true ; } let tempArray1 = S1 . split ( ' ' ) ; let tempArray2 = S2 . split ( ' ' ) ; tempArray1 . sort ( ) ; tempArray2 . sort ( ) ; let copy_S1 = tempArray1 . join ( " " ) ; let copy_S2 = tempArray2 . join ( " " ) ; if ( copy_S1 != copy_S2 ) { return false ; } for ( let i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , i + n ) , S2 . substring ( i , i + n ) ) ) { return true ; } if ( isScramble ( S1 . substring ( n - i , n - i + n ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( 0 , n - i ) , S2 . substring ( i , i + n ) ) ) { return true ; } } return false ; } let S1 = " " ; let S2 = " " ; if ( isScramble ( S1 , S2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to Convert BCD number into Decimal number | Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number . ; Update the answer ; Reverse the number formed . ; Driver Code ; Function Call
function bcdToDecimal ( s ) { let len = s . length ; let check = 0 , check0 = 0 ; let num = 0 , sum = 0 ; let mul = 1 , rev = 0 ; for ( let i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - ' ' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum == 0 && check0 == 0 ) { num = 1 ; check0 = 1 ; } else { num = num * 10 + sum ; } check = 0 ; sum = 0 ; mul = 1 ; } } while ( num > 0 ) { rev = rev * 10 + ( num % 10 ) ; num = Math . floor ( num / 10 ) ; } if ( check0 == 1 ) return rev - 1 ; return rev ; } let s = " " ; document . write ( bcdToDecimal ( s . split ( ' ' ) ) ) ;
Find Count of Single Valued Subtrees | Class containing left and right child of current node and key value ; This function increments count by number of single valued subtrees under root . It returns true if subtree under root is Singly , else false . ; Return false to indicate NULL ; Recursively count in left and right subtrees also ; If any of the subtrees is not singly , then this cannot be singly . ; If left subtree is singly and non - empty , but data doesn 't match ; Same for right subtree ; If none of the above conditions is true , then tree rooted under root is single valued , increment count and return true . ; This function mainly calls countSingleRec ( ) after initializing count as 0 ; Recursive function to count ; Let us construct the below tree 5 / \ 4 5 / \ \ 4 4 5
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } class Count { constructor ( ) { this . count = 0 ; } } var root ; var ct = new Count ( ) ; function countSingleRec ( node , c ) { if ( node == null ) return true ; var left = countSingleRec ( node . left , c ) ; var right = countSingleRec ( node . right , c ) ; if ( left == false right == false ) return false ; if ( node . left != null && node . data != node . left . data ) return false ; if ( node . right != null && node . data != node . right . data ) return false ; c . count ++ ; return true ; } function countSingle ( ) { return countSingle ( root ) ; } function countSingle ( node ) { countSingleRec ( node , ct ) ; return ct . count ; } root = new Node ( 5 ) ; root . left = new Node ( 4 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; document . write ( " " + countSingle ( root ) ) ;
Bitwise XOR of a Binary array | Function to return the bitwise XOR of all the binary strings ; Get max size and reverse each string Since we have to perform XOR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform XOR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
function strBitwiseXOR ( arr , n ) { var result = " " ; var max_len = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { max_len = Math . max ( max_len , arr [ i ] . length ) ; arr [ i ] = arr [ i ] . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; } for ( var i = 0 ; i < n ; i ++ ) { var s ; for ( var j = 0 ; j < max_len - arr [ i ] . length ; j ++ ) s += ' ' ; arr [ i ] = arr [ i ] + s ; } for ( var i = 0 ; i < max_len ; i ++ ) { var pres_bit = 0 ; for ( var j = 0 ; j < n ; j ++ ) pres_bit = pres_bit ^ ( arr [ j ] [ i ] - ' ' . charCodeAt ( 0 ) ) ; result += ( pres_bit + ' ' . charCodeAt ( 0 ) ) ; } result = result . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; document . write ( result ) ; } var arr = [ " " , " " , " " ] ; var n = arr . length ; strBitwiseXOR ( arr , n ) ;
Count of sticks required to represent the given string | stick [ ] stores the count of matchsticks required to represent the alphabets ; number [ ] stores the count of matchsticks required to represent the numerals ; Function that return the count of sticks required to represent the given string ; For every char of the given string ; Add the count of sticks required to represent the current character ; Driver Code ; Function Call
var sticks = [ 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 ] var number = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; function countSticks ( str ) { var cnt = 0 ; for ( var i = 0 ; str [ i ] ; i ++ ) { var ch = str [ i ] ; if ( ch >= ' ' && ch <= ' ' ) { cnt += sticks [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } else { cnt += number [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } } return cnt ; } var str = " " ; document . write ( countSticks ( str ) ) ;
Find the last remaining Character in the Binary String according to the given conditions | Javascript implementation of the above approach ; Converting string to array ; Delete counters for each to count the deletes ; Counters to keep track of characters left from each type ; Queue to simulate the process ; Initializing the queue ; Looping till at least 1 digit is left from both the type ; If there is a floating delete for current character we will delete it and move forward otherwise we will increase delete counter for opposite digit ; If 0 are left then answer is 0 else answer is 1 ; Input string ; Length of String ; Printing answer
function remainingDigit ( S , N ) { let c = S . split ( " " ) ; let del = [ 0 , 0 ] ; let count = [ 0 , 0 ] ; let q = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let x = ( c [ i ] == ' ' ? 1 : 0 ) ; count [ x ] ++ ; q . push ( x ) ; } while ( count [ 0 ] > 0 && count [ 1 ] > 0 ) { let t = q . shift ( ) ; if ( del [ t ] > 0 ) { del [ t ] -- ; count [ t ] -- ; } else { del [ t ^ 1 ] ++ ; q . push ( t ) ; } } if ( count [ 0 ] > 0 ) return " " ; return " " ; } let S = " " ; let N = S . length ; document . write ( remainingDigit ( S , N ) ) ;
Closest leaf to a given node in Binary Tree | A binary tree node ;  ; This function finds closest leaf to root . This distance is stored at * minDist . ; base case ; If this is a leaf node , then check if it is closer than the closest so far ; Recur for left and right subtrees ; This function finds if there is closer leaf to x through parent node . ; Base cases ; Search x in left subtree of root ; If left subtree has x ; Find closest leaf in right subtree ; Search x in right subtree of root ; If right subtree has x ; Find closest leaf in left subtree ; Returns minimum distance of a leaf from given node x ; Initialize result ( minimum distance from a leaf ) ; Find closest leaf down to x ; See if there is a closer leaf through parent ; Let us create Binary Tree shown in above example
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } class Distance { constructor ( ) { this . minDis = Number . MAX_VALUE ; } } var root ; function findLeafDown ( root , lev , minDist ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( lev < ( minDist . minDis ) ) minDist . minDis = lev ; return ; } findLeafDown ( root . left , lev + 1 , minDist ) ; findLeafDown ( root . right , lev + 1 , minDist ) ; } function findThroughParent ( root , x , minDist ) { if ( root == null ) return - 1 ; if ( root == x ) return 0 ; var l = findThroughParent ( root . left , x , minDist ) ; if ( l != - 1 ) { findLeafDown ( root . right , l + 2 , minDist ) ; return l + 1 ; } var r = findThroughParent ( root . right , x , minDist ) ; if ( r != - 1 ) { findLeafDown ( root . left , r + 2 , minDist ) ; return r + 1 ; } return - 1 ; } function minimumDistance ( root , x ) { d = new Distance ( ) ; findLeafDown ( x , 0 , d ) ; findThroughParent ( root , x , d ) ; return d . minDis ; } root = new Node ( 1 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 13 ) ; root . right . left = new Node ( 14 ) ; root . right . right = new Node ( 15 ) ; root . right . left . left = new Node ( 21 ) ; root . right . left . right = new Node ( 22 ) ; root . right . right . left = new Node ( 23 ) ; root . right . right . right = new Node ( 24 ) ; root . right . left . left . left = new Node ( 1 ) ; root . right . left . left . right = new Node ( 2 ) ; root . right . left . right . left = new Node ( 3 ) ; root . right . left . right . right = new Node ( 4 ) ; root . right . right . left . left = new Node ( 5 ) ; root . right . right . left . right = new Node ( 6 ) ; root . right . right . right . left = new Node ( 7 ) ; root . right . right . right . right = new Node ( 8 ) ; x = root . right ; document . write ( " " + x . key + " " + minimumDistance ( root , x ) ) ;
Find the time which is palindromic and comes after the given time | Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver Code
function getTime ( s , n ) { let res = " " ; let h = parseInt ( s . substring ( 0 , 0 + 2 ) ) ; let m = parseInt ( s . substring ( 3 , 3 + 2 ) ) ; let rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; let rev_hs = ( rev_h ) . toString ( ) ; if ( h == 23 && m >= 32 ) { res = " " ; } else if ( m < rev_h ) { let temp = " " ; if ( h < 10 ) temp = " " ; temp = temp + h . toString ( ) ; if ( rev_h < 10 ) res = res + temp + " " + rev_hs ; else res = res + temp + " " + rev_hs ; } else { h ++ ; rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; rev_hs = ( rev_h ) . toString ( ) ; let temp = " " ; if ( h < 10 ) temp = " " ; temp = temp + h . toString ( ) ; if ( rev_h < 10 ) res = res + temp + " " + rev_hs ; else res = res + temp + " " + rev_hs ; } return res ; } let s = " " ; let n = s . length ; document . write ( getTime ( s , n ) ) ;
Count of sub | Function to return the count of valid sub - strings ; Variable ans to store all the possible substrings Initialize its value as total number of substrings that can be formed from the given string ; Stores recent index of the characters ; If character is a update a 's index  and the variable ans ; If character is b update b 's index  and the variable ans ; If character is c update c 's index  and the variable ans ; Driver code
function CountSubstring ( str , n ) { var ans = parseInt ( ( n * ( n + 1 ) ) / 2 ) ; var a_index = 0 ; var b_index = 0 ; var c_index = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] === " " ) { a_index = i + 1 ; ans -= Math . min ( b_index , c_index ) ; } else if ( str [ i ] === " " ) { b_index = i + 1 ; ans -= Math . min ( a_index , c_index ) ; } else { c_index = i + 1 ; ans -= Math . min ( a_index , b_index ) ; } } return ans ; } var str = " " . split ( " " ) ; var n = str . length ; document . write ( CountSubstring ( str , n ) ) ;
Check if a binary string contains consecutive same or not | Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code
function isValid ( str , len ) { for ( let i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } let str = " " ; let len = str . length ; if ( isValid ( str , len ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum K such that every substring of length atleast K contains a character c | This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code to test above functions
function check ( s , K ) { for ( var ch = 0 ; ch < 26 ; ch ++ ) { var c = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + ch ) ; var last = - 1 ; var found = true ; for ( var i = 0 ; i < K ; i ++ ) if ( s [ i ] == c ) last = i ; if ( last == - 1 ) continue ; for ( var i = K ; i < s . length ; i ++ ) { if ( s [ i ] == c ) last = i ; if ( last <= ( i - K ) ) { found = false ; break ; } } if ( found ) return 1 ; } return 0 ; } function binarySearch ( s ) { var low = 1 , high = s . length ; var ans ; while ( low <= high ) { var mid = ( high + low ) >> 1 ; if ( check ( s , mid ) ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } var s = " " ; document . write ( binarySearch ( s ) + " " ) ; s = " " ; document . write ( binarySearch ( s ) ) ;
Check if number can be displayed using seven segment led | Pre - computed values of segment used by digit 0 to 9. ; Check if it is possible to display the number ; Finding sum of the segments used by each digit of the number ; Driver Code ; Function call to print required answer
const seg = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; function LedRequired ( s , led ) { var count = 0 ; for ( var i = 0 ; i < s . length ; ++ i ) { count += seg [ ( s [ i ] ) - 48 ] ; } if ( count <= led ) return " " ; else return " " ; } var S = " " ; var led = 20 ; document . write ( LedRequired ( S , led ) + " " ) ;
Count the number of vowels occurring in all the substrings of given string | Returns the total sum of occurrences of all vowels ; No . of occurrences of 0 th character in all the substrings ; No . of occurrences of the ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver code
function vowel_calc ( s ) { var n = s . length ; var arr = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( i == 0 ) arr . push ( n ) ; else arr . push ( ( n - i ) + arr [ i - 1 ] - i ) ; } var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) sum += arr [ i ] ; return sum ; } var s = " " ; document . write ( vowel_calc ( s ) ) ;
Check whether two strings can be made equal by increasing prefixes | check whether the first string can be converted to the second string by increasing the ASCII value of prefix string of first string ; length of two strings ; If lengths are not equal ; store the difference of ASCII values ; difference of first element ; traverse through the string ; the ASCII value of the second string should be greater than or equal to first string , if it is violated return false . ; store the difference of ASCII values ; the difference of ASCII values should be in descending order ; if the difference array is not in descending order ; if all the ASCII values of characters of first string is less than or equal to the second string and the difference array is in descending order , return true ; create two strings ; check whether the first string can be converted to the second string
function find ( s1 , s2 ) { var len = s1 . length , len_1 = s2 . length ; if ( len != len_1 ) return false ; var d = Array ( len ) . fill ( 0 ) ; d [ 0 ] = s2 [ 0 ] - s1 [ 0 ] ; for ( var i = 1 ; i < len ; i ++ ) { if ( s1 [ i ] > s2 [ i ] ) return false ; else { d [ i ] = s2 [ i ] - s1 [ i ] ; } } for ( var i = 0 ; i < len - 1 ; i ++ ) { if ( d [ i ] < d [ i + 1 ] ) return false ; } return true ; } var s1 = " " , s2 = " " ; if ( find ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
N | Function that returns the N - th character ; initially null string ; starting integer ; add integers in string ; one digit numbers added ; more than 1 digit number , generate equivalent number in a string s1 and concatenate s1 into s . ; add the number in string ; reverse the string ; attach the number ; if the length exceeds N ; Driver Code
function NthCharacter ( n ) { let s = " " ; let c = 1 ; for ( let i = 1 ; ; i ++ ) { if ( c < 10 ) s += c . toString ( ) ; else { let s1 = " " ; let dup = c ; while ( dup > 0 ) { s1 += ( dup % 10 ) . toString ( ) ; dup = Math . floor ( dup / 10 ) ; } s1 = s1 . split ( " " ) . reverse ( ) . join ( " " ) ; s += s1 ; } c ++ ; if ( s . length >= n ) { return s [ n - 1 ] ; } } } let n = 11 ; document . write ( NthCharacter ( n ) ) ;