codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all elements in an array between zeros | Function to find the sum between two zeros in the given array arr [ ] ; To store the sum of element between two zeros ; To store the sum ; Find first index of 0 ; Traverse the given array arr [ ] ; If 0 occurs then add it to A [ ] ; Else add element to the sum ; Print all the sum stored in A ; If there is no such element print - 1 ; Driver Code ; Function call
function sumBetweenZero ( arr , N ) { let i = 0 ; let A = new Array ( ) ; let sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { i ++ ; break ; } } for ( ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { A . push ( sum ) ; sum = 0 ; } else { sum += arr [ i ] ; } } for ( let i = 0 ; i < A . length ; i ++ ) { document . write ( A [ i ] + ' ' ) ; } if ( A . length == 0 ) document . write ( " " ) ; } let arr = [ 1 , 0 , 3 , 4 , 0 , 4 , 4 , 0 , 2 , 1 , 4 , 0 , 3 ] ; let N = arr . length ; sumBetweenZero ( arr , N ) ;
Find the Nth term of the series 2 , 15 , 41 , 80 , 132. . . | Recursive function to find Nth term ; Base Case ; Recursive Call according to Nth term of the series ; Input Nth term ; Function call
function nthTerm ( N ) { if ( N == 1 ) { return 2 ; } return ( ( N - 1 ) * 13 ) + nthTerm ( N - 1 ) ; } let N = 17 ; document . write ( nthTerm ( N ) ) ;
Subsequence pair from given Array having all unique and all same elements respectively | Function to find the maximum length of subsequences with given property ; To store the frequency ; Traverse the array to store the frequency ; M . size ( ) given count of distinct element in arr [ ] ; Traverse map to find max frequency ; Find the maximum length on the basis of two cases in the approach ; Driver Code ; Function call
function maximumSubsequence ( arr , N ) { let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M . has ( arr [ i ] ) ) { M . set ( arr [ i ] , M . get ( arr [ i ] ) + 1 ) ; } else { M . set ( arr [ i ] , 1 ) ; } } let distinct_size = M . size ; let maxFreq = 1 ; for ( let it of M ) { maxFreq = Math . max ( maxFreq , it [ 1 ] ) ; } document . write ( Math . max ( Math . min ( distinct_size , maxFreq - 1 ) , Math . min ( distinct_size - 1 , maxFreq ) ) ) ; } let arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 ] ; let N = arr . length ; maximumSubsequence ( arr , N ) ;
Total length of string from given Array of strings composed using given characters | Function to count the total length ; Unordered_map for keeping frequency of characters ; Calculate the frequency ; Iterate in the N strings ; Iterates in the string ; Checks if given character of string string appears in it or not ; Adds the length of string if all characters are present ; Return the final result ; Driver code
function countCharacters ( strings , chars ) { var res = 0 ; var freq = new Map ( ) ; for ( var i = 0 ; i < chars . length ; i ++ ) { if ( freq . has ( chars [ i ] ) ) freq . set ( chars [ i ] , freq . get ( chars [ i ] ) + 1 ) else freq . set ( chars [ i ] , 1 ) } strings . forEach ( st => { var flag = true ; st . split ( ' ' ) . forEach ( c => { if ( ! freq . has ( c ) ) { flag = false ; } } ) ; if ( flag ) res += st . length ; } ) ; return res ; } var strings = [ " " , " " , " " ] ; var chars = " " ; document . write ( countCharacters ( strings , chars ) ) ;
Construct an Array of size N in which sum of odd elements is equal to sum of even elements | Function to construct the required array ; To construct first half , distinct even numbers ; To construct second half , distinct odd numbers ; Calculate the last number of second half so as to make both the halves equal ; Function to construct the required array ; check if size is multiple of 4 then array exist ; function call to construct array ; Driver code
function arrayConstruct ( N ) { for ( let i = 2 ; i <= N ; i = i + 2 ) document . write ( i + " " ) ; for ( let i = 1 ; i < N - 1 ; i = i + 2 ) document . write ( i + " " ) ; document . write ( N - 1 + ( N / 2 ) + " " ) ; } function createArray ( N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else document . write ( - 1 + " " ) ; } let N = 8 ; createArray ( N ) ;
Count of all sub | Function to count all substrings ; Hashmap to store substrings ; Iterate over all substrings ; Variable to maintain sum of all characters encountered ; Variable to maintain substring till current position ; Get position of character in string W ; Add weight to current sum ; Add current character to substring ; Check if sum of characters is <= K insert in Hashmap ; Initialise string ; Initialise weight
function distinctSubstring ( P , Q , K , N ) { let S = new Set ( ) ; for ( let i = 0 ; i < N ; ++ i ) { let sum = 0 ; let s = " " ; for ( let j = i ; j < N ; ++ j ) { let pos = P [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; sum += Q [ pos ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; s += P [ j ] ; if ( sum <= K ) { S . add ( s ) ; } else { break ; } } } return S . size ; } let S = " " ; let W = " " ; let K = 5 ; let N = S . length ; document . write ( distinctSubstring ( S , W , K , N ) ) ;
Count all sub | Function to find the count of all the subStrings with weight of characters atmost K ; Hashmap to store all subStrings ; Iterate over all subStrings ; Maintain the sum of all characters encountered so far ; Maintain the subString till the current position ; Get the position of the character in String Q ; Add weight to current sum ; Add current character to subString ; If sum of characters is <= K then insert in leto the set ; Finding the size of the set ; Driver code
function distinctSubString ( P , Q , K , N ) { let S = new Set ( ) ; for ( let i = 0 ; i < N ; ++ i ) { let sum = 0 ; let s = " " ; for ( let j = i ; j < N ; ++ j ) { let pos = P [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; sum += Q [ pos ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; s += P [ j ] ; if ( sum <= K ) { S . add ( s ) ; } else { break ; } } } return S . size ; } let P = " " ; let Q = " " ; let K = 5 ; let N = P . length ; document . write ( distinctSubString ( P , Q , K , N ) ) ;
Program to print a Hollow Triangle inside a Triangle | Function to print the pattern ; Loop for rows ; Loop for column ; For printing equal sides of outer triangle ; For printing equal sides of inner triangle ; For printing base of both triangle ; For spacing between the triangle ; Driver Code
function printPattern ( n ) { var i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j < 2 * n ; j ++ ) { if ( j == n - i + 1 j == n + i - 1 ) { document . write ( " " ) ; } else if ( i >= 4 && i <= n - 4 && ( j == n - i + 4 j == n + i - 4 ) ) { document . write ( " " ) ; } else if ( i == n || ( i == n - 4 && j >= n - ( n - 2 * 4 ) && j <= n + n - 2 * 4 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; document . write ( " " ) ; } } document . write ( " " ) ; } } var N = 9 ; printPattern ( N ) ;
Check if a string is made up of K alternating characters | Function to check if a string is made up of k alternating characters ; Check if all the characters at indices 0 to K - 1 are different ; If that bit is already set in checker , return false ; Otherwise update and continue by setting that bit in the checker ; Driver code
function isKAlternating ( s , k ) { if ( s . length < k ) return false ; var checker = 0 ; for ( var i = 0 ; i < k ; i ++ ) { var bitAtIndex = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( var i = k ; i < s . length ; i ++ ) if ( s [ i - k ] != s [ i ] ) return false ; return true ; } var str = " " ; var K = 4 ; if ( isKAlternating ( str , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | Javascript program to find the factors of large perfect square number in O ( Math . sqrt ( Math . sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the Math . sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 ] [ 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Driver Code
let MAX = 100000 ; function findFactors ( N ) { let temp = Math . floor ( Math . sqrt ( N ) ) ; let factor = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { factor [ i ] = 1 ; } let i , j , k ; let len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp = Math . floor ( temp / 2 ) ; } for ( j = 3 ; j < Math . sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp = Math . floor ( temp / j ) ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } let M = new Array ( len1 ) ; for ( let i = 0 ; i < len1 ; i ++ ) { M [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { M [ i ] [ j ] = 0 ; } } let tpc = 0 , tpr = 0 ; M [ 0 ] [ 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr ] [ 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr ] [ tpc ] = M [ tpr ] [ tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } let arr1 = new Array ( MAX ) ; let arr2 = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { arr1 [ i ] = 0 ; arr2 [ i ] = 0 ; } let l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i ] [ j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i ] [ j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { document . write ( arr2 [ i ] + " " ) ; } } let N = 900 ; findFactors ( N ) ;
Construct a matrix such that union of ith row and ith column contains every element from 1 to 2 N | Javascript implementation of the above approach ; Function to find the square matrix ; For Matrix of order 1 , it will contain only 1 ; For Matrix of odd order , it is not possible ; For Matrix of even order ; All diagonal elements of the matrix can be N itself . ; Assign values at desired place in the matrix ; Loop to add N in the lower half of the matrix such that it contains elements from 1 to 2 * N - 1 ; Loop to print the matrix ; Driver Code
let matrix = new Array ( ) ; for ( let i = 0 ; i < 100 ; i ++ ) { let temp = new Array ( ) ; for ( let j = 0 ; j < 100 ; j ++ ) { temp . push ( [ ] ) } matrix . push ( temp ) } function printRequiredMatrix ( n ) { if ( n == 1 ) { document . write ( " " + " " ) ; } else if ( n % 2 != 0 ) { document . write ( " " + " " ) ; } else { for ( let i = 0 ; i < n ; i ++ ) { matrix [ i ] [ i ] = n ; } let u = n - 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { matrix [ i ] [ u ] = i + 1 ; for ( let j = 1 ; j < n / 2 ; j ++ ) { let a = ( i + j ) % ( n - 1 ) ; let b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) { let temp = a ; a = b ; b = temp } matrix [ b ] [ a ] = i + 1 ; } } for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) document . write ( matrix [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } document . write ( " " ) ; } let n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ;
Count of sub | Function to find the count of substrings with equal no . of consecutive 0 ' s ▁ and ▁ 1' s ; To store the total count of substrings ; Traversing the string ; Count of consecutive 0 ' s ▁ & ▁ 1' s ; Counting subarrays of type "01" ; Count the consecutive 0 's ; If consecutive 0 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 1' s ; Counting consecutive 1 's ; Counting subarrays of type "10" ; Count consecutive 1 's ; If consecutive 1 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 0' s ; Count consecutive 0 's ; Update the total count of substrings with minimum of ( cnt0 , cnt1 ) ; Return answer ; Driver code ; Function to print the count of substrings
function countSubstring ( S , n ) { var ans = 0 ; var i = 0 ; while ( i < n ) { var cnt0 = 0 , cnt1 = 0 ; if ( S . charAt ( i ) == ' ' ) { while ( i < n && S . charAt ( i ) == ' ' ) { cnt0 ++ ; i ++ ; } var j = i ; while ( j < n && S . charAt ( j ) == ' ' ) { cnt1 ++ ; j ++ ; } } else { while ( i < n && S . charAt ( i ) == ' ' ) { cnt1 ++ ; i ++ ; } var j = i ; while ( j < n && S . charAt ( j ) == ' ' ) { cnt0 ++ ; j ++ ; } } ans += Math . min ( cnt0 , cnt1 ) ; } return ans ; } var S = " " ; var n = S . length ; document . write ( countSubstring ( S , n ) ) ;
Print N numbers such that their sum is a Perfect Cube | Function to find the N numbers such that their sum is a perfect cube ; Loop to find the Ith term of the Centered Hexagonal number ; Driver Code ; Function Call
function findNumbers ( n ) { let i = 1 ; while ( i <= n ) { document . write ( ( 3 * i * ( i - 1 ) + 1 ) + " " ) ; i ++ ; } } let n = 4 ; findNumbers ( n ) ;
Largest N digit Octal number which is a Perfect square | Function to convert decimal number to a octal number ; Array to store octal number ; Counter for octal number array ; Store remainder in octal array ; Print octal number array in reverse order ; Largest n - digit perfect square ; Driver Code
function decToOctal ( n ) { var octalNum = Array ( 100 ) . fill ( 0 ) ; var i = 0 ; while ( n != 0 ) { octalNum [ i ] = n % 8 ; n = parseInt ( n / 8 ) ; i ++ ; } for ( var j = i - 1 ; j >= 0 ; j -- ) document . write ( octalNum [ j ] ) ; document . write ( " " ) ; } function nDigitPerfectSquares ( n ) { var decimal = Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 8 , n ) ) ) - 1 , 2 ) ; decToOctal ( decimal ) ; } var n = 2 ; nDigitPerfectSquares ( n ) ;
Reverse the substrings of the given String according to the given Array of indices | Function to reverse a string ; Swap character starting from two corners ; Function to reverse the string with the given array of indices ; Reverse the string from 0 to A [ 0 ] ; Reverse the string for A [ i ] to A [ i + 1 ] ; Reverse String for A [ n - 1 ] to length ; Driver Code
function reverseStr ( str , l , h ) { var n = h - l ; for ( var i = 0 ; i < n / 2 ; i ++ ) { [ str [ i + l ] , str [ n - i - 1 + l ] ] = [ str [ n - i - 1 + l ] , str [ i + l ] ] ; } return str ; } function reverseString ( s , A , n ) { s = reverseStr ( s , 0 , A [ 0 ] ) ; for ( var i = 1 ; i < n ; i ++ ) s = reverseStr ( s , A [ i - 1 ] , A [ i ] ) ; s = reverseStr ( s , A [ n - 1 ] , s . length ) ; return s ; } var s = " " ; var A = [ 2 , 4 , 6 ] ; var n = A . length ; s = reverseString ( s . split ( ' ' ) , A , n ) ; document . write ( s . join ( ' ' ) ) ;
Check if the Depth of Parentheses is correct in the given String | Function to check if the Depth of Parentheses is correct in the given String ; Appending if the Character is not integer ; Iterating till the entire Digit is read ; Check if character is ' ( ' ; Increment depth by 1 ; Increment open by 1 ; Check if character is ' ) ' ; Decrement depth by 1 ; Increment close by 1 ; Check if open parentheses NOT equals close parentheses ; Driver Code
function Formatted ( s ) { let k = [ ] ; let i = 0 ; while ( i < s . length ) { if ( s [ i ] == ' ' || s [ i ] == ' ' ) { k . push ( s [ i ] ) ; i += 1 ; } else { let st = 0 ; while ( s [ i ] != ' ' && s [ i ] != ' ' ) { st = s [ i ] ; i = i + 1 ; } k . push ( st ) ; } } let depth = 0 , flag = 1 ; let open = 0 , close = 0 ; for ( let i2 = 0 ; i2 < k . length ; i2 ++ ) { if ( k [ i2 ] == ' ' ) { depth += 1 ; open += 1 ; } else if ( k [ i2 ] == ' ' ) { depth -= 1 ; close += 1 ; } else { if ( k [ i2 ] - ' ' != depth ) { flag = 0 ; break ; } } } if ( open != close ) flag = 0 ; return ( flag == 1 ) ? true : false ; } let s = " " ; let k = Formatted ( s ) ; if ( k == true ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize the sum of differences of consecutive elements after removing exactly K elements | Function to return the maximized sum ; Remove any k internal elements ; Driver code
function findSum ( arr , n , k ) { if ( k <= n - 2 ) return ( arr [ n - 1 ] - arr [ 0 ] ) ; return 0 ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var k = 1 ; document . write ( findSum ( arr , n , k ) ) ;
Split the binary string into substrings with equal number of 0 s and 1 s | Function to return the count of maximum substrings str can be divided into ; To store the count of 0 s and 1 s ; To store the count of maximum substrings str can be divided into ; It is not possible to split the string ; Driver code
function maxSubStr ( str , n ) { var count0 = 0 , count1 = 0 ; var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { count0 ++ ; } else { count1 ++ ; } if ( count0 == count1 ) { cnt ++ ; } } if ( cnt == 0 ) { return - 1 ; } return cnt ; } var str = " " ; var n = str . length ; document . write ( maxSubStr ( str , n ) ) ;
Sum of the digits of square of the given number which has only 1 's as its digits | Function to return the sum of the digits of num ^ 2 ; To store the number of 1 's ; Find the sum of the digits of num ^ 2 ; Driver code
function squareDigitSum ( s ) { let lengthN = s . length ; let result = parseInt ( lengthN / 9 ) * 81 + Math . pow ( ( lengthN % 9 ) , 2 ) ; return result ; } let s = " " ; document . write ( squareDigitSum ( s ) ) ;
Check whether two strings contain same characters in same order | Javascript implementation of the approach ; string class has a constructor that allows us to specify size of string as first parameter and character to be filled in given size as second parameter . ; Function that returns true if the given strings contain same characters in same order ; Get the first character of both strings ; Now if there are adjacent similar character remove that character from s1 ; Now if there are adjacent similar character remove that character from s2 ; If both the strings are equal then return true ; Driver code
function getString ( x ) { return x } function solve ( s1 , s2 ) { var a = getString ( s1 [ 0 ] ) , b = getString ( s2 [ 0 ] ) ; for ( var i = 1 ; i < s1 . length ; i ++ ) if ( s1 [ i ] != s1 [ i - 1 ] ) { a += getString ( s1 [ i ] ) ; } for ( var i = 1 ; i < s2 . length ; i ++ ) if ( s2 [ i ] != s2 [ i - 1 ] ) { b += getString ( s2 [ i ] ) ; } if ( a == b ) return true ; return false ; } var s1 = " " , s2 = " " ; if ( solve ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count number of distinct substrings of a given length | Javascript implementation of above approach ; Function to find the required count ; Variable to the hash ; Finding hash of substring ( 0 , l - 1 ) using random number x ; Computing x ^ ( l - 1 ) ; Unordered set to add hash values ; Generating all possible hash values ; Print the result ; Driver Code
var x = 26 ; var mod = 3001 ; function CntSubstr ( s , l ) { var hash = 0 ; for ( var i = 0 ; i < l ; i ++ ) { hash = ( hash * x + ( s [ i ] . charCodeAt ( 0 ) - 97 ) ) % mod ; } var pow_l = 1 ; for ( var i = 0 ; i < l - 1 ; i ++ ) pow_l = ( pow_l * x ) % mod ; var result = new Set ( ) ; result . add ( hash ) ; for ( var i = l ; i < s . length ; i ++ ) { hash = ( ( hash - pow_l * ( s [ i - l ] . charCodeAt ( 0 ) - 97 ) + 2 * mod ) * x + ( s [ i ] . charCodeAt ( 0 ) - 97 ) ) % mod ; result . add ( hash ) ; } document . write ( result . size ) ; } var s = " " ; var l = 2 ; CntSubstr ( s , l ) ;
Check if a binary string has two consecutive occurrences of one everywhere | Function that returns 1 if str is valid ; Index of first appearance of ' b ' ; If str starts with ' b ' ; While ' b ' occurs in str ; If ' b ' doesn ' t ▁ appear ▁ after ▁ an ▁ ' a ' ; If ' b ' is not succeeded by another ' b ' ; If sub - string is of the type " abbb " ; If str ends with a single b ; Driver code
function isValidString ( str , n ) { let index = str . indexOf ( " " ) ; if ( index == 0 ) return false ; while ( index != - 1 ) { if ( str [ index - 1 ] != ' ' ) return false ; if ( index + 1 < n && str [ index + 1 ] != ' ' ) return false ; if ( index + 2 < n && str [ index + 2 ] == ' ' ) return false ; if ( index == n - 1 ) return false ; index = str . indexOf ( " " , index + 2 ) ; } return true ; } let str = " " ; let n = str . length ; document . write ( isValidString ( str , n ) ) ;
Count strings that end with the given pattern | Function that return true if str ends with pat ; Pattern is larger in length than the string ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str doesn 't match  with pattern ; If str ends with the given pattern ; Function to return the count of required strings ; If current string ends with the given pattern ; Driver code
function endsWith ( str , pat ) { let patLen = pat . length ; let strLen = str . length ; if ( patLen > strLen ) return false ; patLen -- ; strLen -- ; while ( patLen >= 0 ) { if ( pat [ patLen ] != str [ strLen ] ) return false ; patLen -- ; strLen -- ; } return true ; } function countOfStrings ( pat , n , sArr ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; } return count ; } let pat = " " ; let n = 4 ; let sArr = [ " " , " " , " " , " " ] ; document . write ( countOfStrings ( pat , n , sArr ) ) ;
Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - string starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code
function getEndingIndex ( str , n , i ) { i ++ ; while ( i < n ) { let curr = str [ i ] ; let prev = str [ i - 1 ] ; if ( ( curr == ' ' && prev == ' ' ) || ( curr . charCodeAt ( 0 ) - prev . charCodeAt ( 0 ) == 1 ) ) i ++ ; else break ; } return i - 1 ; } function largestSubStr ( str , n ) { let len = 0 ; let i = 0 ; while ( i < n ) { let end = getEndingIndex ( str , n , i ) ; len = Math . max ( end - i + 1 , len ) ; i = end + 1 ; } return len ; } let str = " " ; let n = str . length ; document . write ( largestSubStr ( str , n ) ) ;
Sum of integers upto N with given unit digit ( Set 2 ) | Function to return the required sum ; Decrement N ; Driver code
function getSum ( n , d ) { if ( n < d ) return 0 ; while ( n % 10 != d ) n -- ; k = parseInt ( n / 10 ) ; return ( k + 1 ) * d + ( k * 10 + 10 * k * k ) / 2 ; } let n = 30 ; let d = 3 ; document . write ( getSum ( n , d ) ) ;