codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given string is Even | Function to check if the string str is palindromic or not ; Pointers to iterate the string from both ends ; If characters are found to be distinct ; Return true if the string is palindromic ; Function to generate string from characters at odd indices ; Function to generate string from characters at even indices ; Functions to checks if string is Even - Odd Palindrome or not ; Generate odd indexed string ; Generate even indexed string ; Check for Palindrome ; Driver code
function isPalindrome ( str ) { var l = 0 ; var h = str . length - 1 ; while ( h > l ) { if ( str . charAt ( l ++ ) != str . charAt ( h -- ) ) return false ; } return true ; } function makeOddString ( str ) { var odd = " " ; for ( var i = 1 ; i < str . length ; i += 2 ) { odd += str . charAt ( i ) ; } return odd ; } function makeevenString ( str ) { var even = " " ; for ( var i = 0 ; i < str . length ; i += 2 ) { even += str . charAt ( i ) ; } return even ; } function checkevenOddPalindrome ( str ) { var odd = makeOddString ( str ) ; var even = makeevenString ( str ) ; if ( isPalindrome ( odd ) && isPalindrome ( even ) ) document . write ( " " ) ; else document . write ( " " ) ; } var str = " " ; checkevenOddPalindrome ( str ) ;
Check given string is oddly palindrome or not | Function to check if the string str is palindromic or not ; Iterate the string str from left and right pointers ; Keep comparing characters while they are same ; If they are not same then return false ; Return true if the string is palindromic ; Function to make string using odd indices of string str ; Functions checks if characters at odd index of the string forms palindrome or not ; Make odd indexed string ; Check for Palindrome ; Given string ; Function Call
function isPalindrome ( str ) { var l = 0 ; var h = str . length - 1 ; while ( h > l ) { if ( str [ l ++ ] != str [ h -- ] ) { return false ; } } return true ; } function makeOddString ( str ) { var odd = " " ; for ( var i = 1 ; i < str . length ; i += 2 ) { odd += str [ i ] ; } return odd ; } function checkOddlyPalindrome ( str ) { var odd = makeOddString ( str ) ; if ( isPalindrome ( odd ) ) document . write ( " " ) ; else document . write ( " " ) ; } var str = " " ; checkOddlyPalindrome ( str ) ;
Count of binary strings of given length consisting of at least one 1 | Function to return the count of Strings ; Calculate Math . pow ( 2 , n ) ; Return Math . pow ( 2 , n ) - 1 ; Driver Code
function count_Strings ( n ) { var x = 1 ; for ( i = 1 ; i < n ; i ++ ) { x = ( 1 << x ) ; } return x - 1 ; } var n = 3 ; document . write ( count_Strings ( n ) ) ;
String formed with middle character of every right substring followed by left sequentially | Function to decrypt and prlet the new String ; If the whole String has been traversed ; To calculate middle index of the String ; Prlet the character at middle index ; Recursively call for right - subString ; Recursive call for left - subString ; Driver Code
function decrypt ( Str , Start , End ) { if ( Start > End ) { return ; } let mid = ( Start + End ) >> 1 ; document . write ( Str [ mid ] ) ; decrypt ( Str , mid + 1 , End ) ; decrypt ( Str , Start , mid - 1 ) ; } let N = 4 ; let Str = " " ; decrypt ( Str , 0 , N - 1 ) ; document . write ( " " ) ; N = 6 ; Str = " " ; decrypt ( Str , 0 , N - 1 ) ;
Check whether the binary equivalent of a number ends with given string or not | Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "111" or not ; To store the binary number ; Count used to store exponent value ; Driver code
function isSuffix ( s1 , s2 ) { var n1 = s1 . length ; var n2 = s2 . length ; if ( n1 > n2 ) return false ; for ( var i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } function CheckBinaryEquivalent ( N , str ) { var B_Number = 0 ; var cnt = 0 ; while ( N != 0 ) { var rem = N % 2 ; var c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N = parseInt ( N / 2 ) ; cnt ++ ; } var bin = B_Number . toString ( ) ; return isSuffix ( str , bin ) ; } var N = 23 ; var str = " " ; if ( CheckBinaryEquivalent ( N , str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of substrings consisting only of vowels | Function to check if a character is vowel or not ; Function to check whether String contains only vowel ; Check if the character is not vowel then invalid ; Function to Count all subStrings in a String which contains only vowels ; Generate all subString of s ; If temp contains only vowels ; Increment the count ; Driver code
function isvowel ( ch ) { return ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) ; } function isvalid ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! isvowel ( s [ i ] ) ) return false ; } return true ; } function CountTotal ( s ) { let ans = 0 ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let temp = " " ; for ( let j = i ; j < n ; j ++ ) { temp += s [ j ] ; if ( isvalid ( temp ) ) ans += 1 ; } } return ans ; } let s = " " ; document . write ( ( CountTotal ( s ) ) ) ;
Longest substring of vowels with no two adjacent alphabets same | Function to check a character is vowel or not ; Function to check a subString is valid or not ; If size is 1 then check only first character ; If 0 'th character is  not vowel then invalidlen ; If two adjacent characters are same or i 'th char is  not vowel then invalid ; Function to find length of longest subString consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; For current subString ; Check if subString is valid ; Size of subString is ( j - i + 1 ) ; Driver Code
function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function isValid ( s ) { let n = s . length ; if ( n == 1 ) return ( isVowel ( s [ 0 ] ) ) ; if ( isVowel ( s [ 0 ] ) == false ) return false ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] || ! isVowel ( s [ i ] ) ) return false ; } return true ; } function findMaxLen ( s ) { let maxLen = 0 ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let temp = " " ; for ( let j = i ; j < n ; j ++ ) { temp = temp + s [ j ] ; if ( isValid ( temp ) ) maxLen = Math . max ( maxLen , ( j - i + 1 ) ) ; } } return maxLen ; } let Str = " " ; document . write ( findMaxLen ( Str ) ) ;
Check if the number is balanced | Function to check whether N is Balanced Number or not ; Calculating the Leftsum and rightSum simultaneously ; Typecasting each character to integer and adding the digit to respective sums ; Driver Code ; Function call
function BalancedNumber ( s ) { let Leftsum = 0 ; let Rightsum = 0 ; for ( let i = 0 ; i < s . length / 2 ; i ++ ) { Leftsum += ( s [ i ] - ' ' ) ; Rightsum += ( s [ s . length - 1 - i ] - ' ' ) ; } if ( Leftsum == Rightsum ) document . write ( " " ) ; else document . write ( " " ) ; } let s = " " ; BalancedNumber ( s ) ;
Count of N size strings consisting of at least one vowel and one consonant | Javascript program to count all possible Strings of length N consisting of atleast one vowel and one consonant ; Function to return base ^ exponent ; Function to count all possible Strings ; All possible Strings of length N ; Vowels only ; Consonants only ; Return the final result ; Driver code
var mod = parseInt ( 1e9 + 7 ) ; function expo ( base , exponent ) { var ans = 1 ; while ( exponent != 0 ) { if ( ( exponent & 1 ) == 1 ) { ans = ans * base ; ans = ans % mod ; } base = base * base ; base %= mod ; exponent >>= 1 ; } return ans % mod ; } function findCount ( N ) { var ans = ( expo ( 26 , N ) - expo ( 5 , N ) - expo ( 21 , N ) ) % mod ; ans += mod ; ans %= mod ; return ans ; } var N = 3 ; document . write ( findCount ( N ) ) ;
Sum of decomposition values of all suffixes of an Array | Function to find the decomposition values of the array ; Stack ; Variable to maintain min value in stack ; Loop to iterate over the array ; Condition to check if the stack is empty ; Condition to check if the top of the stack is greater than the current element ; Loop to pop the element out ; The size of the stack is the max no of subarrays for suffix till index i from the right ; Driver Code
function decompose ( S ) { let s = [ ] ; let N = S . length ; let ans = 0 ; let nix = Number . MAX_VALUE ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( s . length == 0 ) { s . push ( S [ i ] ) ; nix = S [ i ] ; } else { if ( S [ i ] < s [ s . length - 1 ] ) { s . push ( S [ i ] ) ; nix = Math . min ( nix , S [ i ] ) ; } else { let val = S [ i ] ; while ( s . length != 0 && val >= s [ s . length - 1 ] ) { s . pop ( ) ; } nix = Math . min ( nix , S [ i ] ) ; s . push ( nix ) ; } } ans += s . length ; } return ans ; } let S = [ ] ; S . push ( 9 ) ; S . push ( 6 ) ; S . push ( 9 ) ; S . push ( 35 ) ; document . write ( decompose ( S ) ) ;
Maximum number of set bits count in a K | Function that find Maximum number of set bit appears in a substring of size K . ; Traverse string 1 to k ; Increment count if character is set bit ; Traverse string k + 1 to length of string ; Remove the contribution of the ( i - k ) th character which is no longer in the window ; Add the contribution of the current character ; Update maxCount at for each window of size k ; Return maxCount ; Driver code
function maxSetBitCount ( s , k ) { var maxCount = 0 , n = s . length ; var count = 0 ; for ( var i = 0 ; i < k ; i ++ ) { if ( s [ i ] == ' ' ) count ++ ; } maxCount = count ; for ( var i = k ; i < n ; i ++ ) { if ( s [ i - k ] == ' ' ) count -- ; if ( s [ i ] == ' ' ) count ++ ; maxCount = Math . max ( maxCount , count ) ; } return maxCount ; } var s = " " ; var k = 3 ; document . write ( maxSetBitCount ( s , k ) ) ;
Minimum characters to be deleted from the beginning of two strings to make them equal | Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver Program
function minDel ( s1 , s2 ) { var i = s1 . length ; var j = s2 . length ; while ( i > 0 && j > 0 ) { if ( s1 [ i - 1 ] != s2 [ j - 1 ] ) { break ; } i -- ; j -- ; } return i + j ; } var s1 = " " , s2 = " " ; document . write ( minDel ( s1 , s2 ) ) ;
Count of triplets from the given string with non | Function to count valid triplets ; Store frequencies of a , b and c ; If the current letter is ' a ' ; If the current letter is ' b ' ; If the current letter is ' c ' ; Calculate total no of triplets ; Subtract invalid triplets ; Driver code
function CountValidTriplet ( s , n ) { let count_a = 0 , count_b = 0 , count_c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count_a ++ ; if ( s [ i ] == ' ' ) count_b ++ ; if ( s [ i ] == ' ' ) count_c ++ ; } let Total_triplet = count_a * count_b * count_c ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( 2 * j - i ) < n && s [ j ] != s [ i ] && s [ j * 2 - i ] != s [ j ] && s [ 2 * j - i ] != s [ i ] ) Total_triplet -- ; } } document . write ( Total_triplet ) ; } let s = " " ; let n = s . length ; CountValidTriplet ( s , n ) ;
Check if a given string can be formed using characters of adjacent cells of a Matrix | Function to check if the word exists ; If index exceeds board range ; If the current cell does not contain the required character ; If the cell contains the required character and is the last character of the word required to be matched ; Return true as word is found ; Mark cell visited ; Check adjacent cells for the next character ; Restore cell value ; Driver Code
function checkWord ( board , word , index , row , col ) { if ( row < 0 col < 0 row >= board . length col >= board [ 0 ] . length ) return false ; if ( board [ row ] [ col ] !== word [ index ] ) return false ; else if ( index === word . length - 1 ) return true ; var temp = board [ row ] [ col ] ; board [ row ] [ col ] = " " ; if ( checkWord ( board , word , index + 1 , row + 1 , col ) || checkWord ( board , word , index + 1 , row - 1 , col ) || checkWord ( board , word , index + 1 , row , col + 1 ) || checkWord ( board , word , index + 1 , row , col - 1 ) ) { board [ row ] [ col ] = temp ; return true ; } board [ row ] [ col ] = temp ; return false ; } var board = [ [ " " , " " , " " , " " ] , [ " " , " " , " " , " " ] , [ " " , " " , " " , " " ] , ] ; var word = " " ; var f = 0 ; for ( var i = 0 ; i < board . length ; i ++ ) { for ( var j = 0 ; j < board [ 0 ] . length ; j ++ ) { if ( board [ i ] [ j ] === word [ 0 ] && checkWord ( board , word , 0 , i , j ) ) { document . write ( " " ) ; f = 1 ; } } if ( f === 1 ) { i = board . length + 1 ; } } if ( f === 0 ) { document . write ( " " ) ; }
Split the number N by maximizing the count of subparts divisible by K | Function to count the subparts ; Total subStr till now ; If it can be divided , then this subString is one of the possible answer ; Convert String to long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver Code
function count ( N , X , subStr , index , n ) { if ( index == n ) return 0 ; let a = subStr + N [ index ] ; let b = 0 ; if ( parseInt ( a ) % X == 0 ) b = 1 ; let m1 = count ( N , X , a , index + 1 , n ) ; let m2 = b + count ( N , X , " " , index + 1 , n ) ; return Math . max ( m1 , m2 ) ; } let N = " " ; let K = 3 ; let l = N . length ; document . write ( count ( N , K , " " , 0 , l ) + " " ) ;
Check if a number ends with another number or not | Function to check if B is a suffix of A or not ; Convert numbers into strings ; Find the lengths of strings s1 and s2 ; Base case ; Traverse the strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
function checkSuffix ( A , B ) { var s1 = A . toString ( ) ; var s2 = B . toString ( ) ; var n1 = s1 . length ; var n2 = s2 . length ; if ( n1 < n2 ) { return false ; } for ( var i = 0 ; i < n2 ; i ++ ) { if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) { return false ; } } return true ; } var A = 12345 , B = 45 ; var result = checkSuffix ( A , B ) ; if ( result ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if a number ends with another number or not | Function to check if B is a suffix of A or not ; Convert numbers leto Strings ; Check if s2 is a suffix of s1 or not ; If result is true , prlet " Yes " ; Given numbers ; Function Call
function checkSuffix ( A , B ) { let s1 = A . toString ( ) ; let s2 = B . toString ( ) ; let result ; result = s1 . endsWith ( s2 ) ; if ( result ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let A = 12345 , B = 45 ; checkSuffix ( A , B ) ;
Binary string with given frequencies of sums of consecutive pairs of characters | A Function that generates and returns the binary string ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code
function build_binary_str ( p , q , r ) { let ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " " ; } else { if ( p ) { ans = new Array ( p + 1 , ' ' ) ; } else { ans = String ( r + 1 , ' ' ) ; } } } else { for ( let i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += ' ' ; } else { ans += ' ' ; } } ans = ans . substr ( 0 , 1 ) + Strings ( p , ' ' ) + ans . substr ( 1 ) ; ans = Strings ( r , ' ' ) + ans ; } return ans ; } function Strings ( p , c ) { let ans = " " ; for ( let i = 0 ; i < p ; i ++ ) ans += c ; return ans ; } let p = 1 , q = 2 , r = 2 ; document . write ( build_binary_str ( p , q , r ) ) ;
Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
function check ( s ) { var ok = true ; for ( var i = 0 ; i + 1 < s . length ; ++ i ) ok &= ( Math . abs ( s [ i ] - s [ i + 1 ] ) != 1 ) ; return ok ; } function monotonousString ( s ) { var odd = " " , even = " " ; for ( var i = 0 ; i < s . length ; ++ i ) { if ( s [ i ] % 2 == 0 ) odd += s [ i ] ; else even += s [ i ] ; } odd = odd . split ( ' ' ) . sort ( ) . join ( ' ' ) ; even = even . split ( ' ' ) . sort ( ) . join ( ' ' ) ; if ( check ( odd + even ) ) return " " ; else if ( check ( even + odd ) ) return " " ; return " " ; } var str = " " ; var ans ; ans = monotonousString ( str ) ; document . write ( ans ) ;
Length of the smallest substring which contains all vowels | Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current subString ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid subString ; Driver code
function get_index ( ch ) { if ( ch == ' ' ) return 0 ; else if ( ch == ' ' ) return 1 ; else if ( ch == ' ' ) return 2 ; else if ( ch == ' ' ) return 3 ; else if ( ch == ' ' ) return 4 ; else return - 1 ; } function findMinLength ( s ) { let n = s . length ; let ans = n + 1 ; let start = 0 ; let count = new Array ( 5 ) ; for ( let i = 0 ; i < 5 ; i ++ ) { count [ i ] = 0 ; } for ( let x = 0 ; x < n ; x ++ ) { let idx = get_index ( s [ x ] ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } let idx_start = get_index ( s [ start ] ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s [ start ] ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } let s = " " ; document . write ( findMinLength ( s ) ) ;
Number of strings which starts and ends with same character after rotations | Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Given string ; Function Call
function countStrings ( s ) { let cnt = 0 ; for ( let i = 1 ; s [ i ] ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { cnt ++ ; } } return cnt ; } let str = " " ; document . write ( countStrings ( str ) ) ;
Check if frequency of each character is equal to its position in English Alphabet | Javascript program for the above approach ; Initialise frequency array ; Traverse the String ; Update the frequency ; Check for valid String ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Given String str
function checkValidString ( str ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < str . length ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] !== 0 ) { if ( freq [ i ] !== i + 1 ) { return false ; } } } return true ; } var str = " " ; if ( checkValidString ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Print all the non | Function to print all the non - repeating words from the two given sentences ; Concatenate the two Strings into one ; Iterating over the whole concatenated String ; Searching for the word in A . If while searching , we reach the end of the String , then the word is not present in the String ; Initialise word for the next iteration ; Driver code
function removeRepeating ( s1 , s2 ) { let s3 = s1 + " " + s2 + " " ; let words = " " ; let i = 0 ; let temp = s3 . split ( " " ) for ( let x = 0 ; x < temp . length ; x ++ ) { if ( temp [ x ] == ' ' ) { if ( ! s1 . includes ( words ) || ! s2 . includes ( words ) ) document . write ( words ) ; words = " " ; } else { words = words + temp [ x ] ; } } } let s1 = " " ; let s2 = " " ; removeRepeating ( s1 , s2 ) ;
Construct the Cypher string based on the given conditions | Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to Math . sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0' ; Function to make the Cypher String ; Resultant String ; Make String for the number N ; Check for semiPrime ; Traverse to make Cypher String ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher String ; If index is odd then current character is changed ; Else add the current character ; If the resultant String is " " then print - 1 ; Else print the resultant String ; Given Number ; Function Call
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function isPossibleSum ( N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) { return true ; } else { return false ; } } function checkSemiprime ( num ) { let cnt = 0 ; for ( let i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num = Math . floor ( num / i ) ; ++ cnt ; } } if ( num > 1 ) { ++ cnt ; } return cnt == 2 ; } function makeCypherString ( N ) { let semiPrime = " " ; let sumOfPrime = " " ; let str = ( N ) . toString ( ) ; if ( checkSemiprime ( N ) ) { for ( let i = 0 ; i < str . length ; i ++ ) { if ( i % 2 == 1 ) { semiPrime += str [ i ] ; } else { semiPrime += String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 65 ) ; } } } if ( isPossibleSum ( N ) ) { for ( let i = 0 ; i < str . length ; i ++ ) { if ( i % 2 == 1 ) { sumOfPrime += String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 65 ) ; } else { sumOfPrime += str [ i ] ; } } } if ( semiPrime + sumOfPrime == " " ) { document . write ( " " ) ; } else { document . write ( semiPrime + sumOfPrime ) ; } } let N = 1011243 ; makeCypherString ( N ) ;
Maximum splits in binary string such that each substring is divisible by given odd number | Function to calculate maximum splits possible ; Driver code
function rearrange ( str , K ) { var n = str . length ; var s = 0 , sum = 0 , count = 0 ; for ( var i = n - 1 ; i >= 0 ; i -- ) { var a = str . charAt ( i ) - ' ' ; sum += a * Math . pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) document . write ( " " ) ; else document . write ( count ) ; } var str = " " ; var K = 5 ; rearrange ( str , K ) ;
Make the string lexicographically smallest and non palindromic by replacing exactly one character | Function to find the required string ; length of the string ; Iterate till half of the string ; replacing a non ' a ' char with ' a ' ; Check if there is no ' a ' in string we replace last char of string by ' b ' ; If the input is a single character ; Driver code
function findStr ( S ) { var n = S . length ; for ( var i = 0 ; i < n / 2 ; ++ i ) { if ( S [ i ] != ' ' ) { S [ i ] = ' ' ; return S . join ( " " ) ; ; } } S [ n - 1 ] = ' ' ; return n < 2 ? " " : S . join ( " " ) ; ; } var str = " " ; document . write ( findStr ( Array . from ( str ) ) + " " ) ; var str1 = " " ; document . write ( findStr ( Array . from ( str1 ) ) ) ;