codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | Function that checks if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Driver Code ; function call
function check ( s , m ) { let l = s . length ; let c1 = 0 ; let c2 = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( s [ i ] == ' ' ) { c2 = 0 ; c1 ++ ; } else { c1 = 0 ; c2 ++ ; } if ( c1 == m c2 == m ) return true ; } return false ; } let s = " " ; let m = 2 ; if ( check ( s , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Product of nodes at k | Function to find product of digits of elements at k - th level ; Initialize result ; increasing level number ; decreasing level number ; check if current level is the desired level or not ; required product ; Driver Code
function productAtKthLevel ( tree , k ) { let level = - 1 ; let product = 1 ; let n = tree . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( tree [ i ] == ' ' ) level ++ ; else if ( tree [ i ] == ' ' ) level -- ; else { if ( level == k ) product *= ( tree [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; } } return product ; } let tree = " " ; let k = 2 ; document . write ( productAtKthLevel ( tree , k ) ) ;
Removing row or column wise duplicates from matrix of characters | Function to check duplicates in row and column ; Create an array isPresent and initialize all entries of it as false . The value of isPresent [ i , j ] is going to be true if s [ i , j ] is present in its row or column . ; Checking every row for duplicates of a [ i , j ] ; Checking every column for duplicate characters ; If the character is unique in its row and column ; Driver code ; character array ; Calling function
function findDuplciates ( a , n , m ) { var isPresent = Array ( n ) . fill ( ) . map ( ( ) => Array ( m ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { isPresent [ i ] [ j ] = false ; } } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { for ( var k = 0 ; k < n ; k ++ ) { if ( a [ i ] . charAt ( j ) == a [ k ] . charAt ( j ) && i != k ) { isPresent [ i ] [ j ] = true ; isPresent [ k ] [ j ] = true ; } } for ( k = 0 ; k < m ; k ++ ) { if ( a [ i ] . charAt ( j ) == a [ i ] . charAt ( k ) && j != k ) { isPresent [ i ] [ j ] = true ; isPresent [ i ] [ k ] = true ; } } } } for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < m ; j ++ ) if ( isPresent [ i ] [ j ] == false ) document . write ( a [ i ] . charAt ( j ) ) ; } var n = 2 , m = 2 ; var a = [ " " , " " ] ; findDuplciates ( a , n , m ) ;
Program to check for ISBN | Javascript program to check if a given ISBN isvalid or not . ; length must be 10 ; Computing weighted sum of first 9 digits ; Checking last digit . ; If last digit is ' X ' , add 10 to sum , else add its value . ; Return true if weighted sum of digits is divisible by 11. ; Driver code
function isValidISBN ( isbn ) { let n = isbn . length ; if ( n != 10 ) return false ; let sum = 0 ; for ( let i = 0 ; i < 9 ; i ++ ) { let digit = isbn [ i ] - ' ' ; if ( 0 > digit 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } let last = isbn [ 9 ] ; if ( last != ' ' && ( last < ' ' last > ' ' ) ) return false ; sum += ( ( last == ' ' ) ? 10 : ( last - ' ' ) ) ; return ( sum % 11 == 0 ) ; } let isbn = " " ; if ( isValidISBN ( isbn ) ) document . write ( " " ) ; else document . write ( " " ) ;
Reverse vowels in a given string | utility function to check for vowel ; Function to reverse order of vowels ; Storing the vowels separately ; Placing the vowels in the reverse order in the string ; Driver code
function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function reverseVowel ( str1 ) { let j = 0 ; let str = str1 . split ( ' ' ) ; let vowel = " " ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { j ++ ; vowel += str [ i ] ; } } for ( let i = 0 ; i < str . length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { str [ i ] = vowel [ -- j ] ; } } return str . join ( " " ) ; } let str = " " ; document . write ( reverseVowel ( str ) ) ;
String containing first letter of every word in a given string with spaces | Function to find string which has first character of each word . ; Traverse the string . ; If it is space , set v as true . ; Else check if v is true or not . If true , copy character in output string and set v as false . ; Driver code
function firstLetterWord ( str ) { let result = " " ; let v = true ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { v = true ; } else if ( str [ i ] != ' ' && v == true ) { result += ( str [ i ] ) ; v = false ; } } return result ; } let str = " " ; document . write ( firstLetterWord ( str ) ) ;
Unique paths covering every non | Javascript implementation of the approach ; Function for dfs . i , j == > Current cell indexes vis == > To mark visited cells ans == > Result z == > Current count 0 s visited z_count == > Total 0 s present ; Mark the block as visited ; update the count ; If end block reached ; If path covered all the non - obstacle blocks ; Up ; Down ; Left ; Right ; Unmark the block ( unvisited ) ; Function to return the count of the unique paths ; let z_count = 0 ; Total 0 s present ; Count non - obstacle blocks ; Starting position ; Driver code
let ans = 0 ; function dfs ( i , j , grid , vis , z , z_count ) { let n = grid . length , m = grid [ 0 ] . length ; vis [ i ] [ j ] = true ; if ( grid [ i ] [ j ] == 0 ) z ++ ; if ( grid [ i ] [ j ] == 2 ) { if ( z == z_count ) ans ++ ; vis [ i ] [ j ] = false ; return ; } if ( i >= 1 && ! vis [ i - 1 ] [ j ] && grid [ i - 1 ] [ j ] != - 1 ) dfs ( i - 1 , j , grid , vis , z , z_count ) ; if ( i < n - 1 && ! vis [ i + 1 ] [ j ] && grid [ i + 1 ] [ j ] != - 1 ) dfs ( i + 1 , j , grid , vis , z , z_count ) ; if ( j >= 1 && ! vis [ i ] [ j - 1 ] && grid [ i ] [ j - 1 ] != - 1 ) dfs ( i , j - 1 , grid , vis , z , z_count ) ; if ( j < m - 1 && ! vis [ i ] [ j + 1 ] && grid [ i ] [ j + 1 ] != - 1 ) dfs ( i , j + 1 , grid , vis , z , z_count ) ; vis [ i ] [ j ] = false ; } function uniquePaths ( grid ) { let n = grid . length , m = grid [ 0 ] . length ; let vis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { vis [ i ] = new Array ( m ) ; for ( let j = 0 ; j < m ; j ++ ) { vis [ i ] [ j ] = false ; } } let x = 0 , y = 0 ; for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { if ( grid [ i ] [ j ] == 0 ) z_count ++ ; else if ( grid [ i ] [ j ] == 1 ) { x = i ; y = j ; } } } dfs ( x , y , grid , vis , 0 , z_count ) ; return ans ; } let grid = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 2 , - 1 ] ] ; document . write ( uniquePaths ( grid ) ) ;
Count of unordered pairs ( x , y ) of Array which satisfy given equation | Return the number of unordered pairs satisfying the conditions ; ans stores the number of unordered pairs ; Making each value of array to positive ; Sort the array ; For each index calculating the right boundary for the unordered pairs ; Return the final result ; Driver code
function numPairs ( a , n ) { let ans , i , index ; ans = 0 ; for ( i = 0 ; i < n ; i ++ ) a [ i ] = Math . abs ( a [ i ] ) ; a . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) { index = 2 ; ans += index - i - 1 ; } return ans ; } let a = [ 3 , 6 ] ; let n = a . length ; document . write ( numPairs ( a , n ) ) ;
Area of a Square | Using Side , Diagonal and Perimeter | Function to find the area of a square ; Use above formula ; Given Side of square ; Function call
function areaOfSquare ( S ) { let area = S * S ; return area ; } let S = 5 ; document . write ( areaOfSquare ( S ) ) ;
Maximum points of intersections possible among X circles and Y straight lines | Javascript program to implement the above approach ; Number of circles ; Number of straight lines ; Function Call
function maxPointOfIntersection ( x , y ) { let k = y * ( y - 1 ) / 2 ; k = k + x * ( 2 * y + x - 1 ) ; return k ; } let x = 3 ; let y = 4 ; document . write ( maxPointOfIntersection ( x , y ) ) ;
Icosihenagonal Number | Function to find Icosihenagonal number ; Formula to calculate nth Icosihenagonal number ; Driver Code
function Icosihenagonal_num ( n ) { return ( 19 * n * n - 17 * n ) / 2 ; } let n = 3 ; document . write ( Icosihenagonal_num ( n ) + " " ) ; n = 10 ; document . write ( Icosihenagonal_num ( n ) ) ;
Find the centroid of a non | Javascript implementation of the approach ; For all vertices ; Calculate value of A using shoelace formula ; Calculating coordinates of centroid of polygon ; Coordinate of the vertices
function find_Centroid ( v ) { let ans = new Array ( 2 ) ; ans . fill ( 0 ) ; let n = v . length ; let signedArea = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let x0 = v [ i ] [ 0 ] , y0 = v [ i ] [ 1 ] ; let x1 = v [ ( i + 1 ) % n ] [ 0 ] , y1 = v [ ( i + 1 ) % n ] [ 1 ] ; let A = ( x0 * y1 ) - ( x1 * y0 ) ; signedArea += A ; ans [ 0 ] += ( x0 + x1 ) * A ; ans [ 1 ] += ( y0 + y1 ) * A ; } signedArea *= 0.5 ; ans [ 0 ] = ( ans [ 0 ] ) / ( 6 * signedArea ) ; ans [ 1 ] = ( ans [ 1 ] ) / ( 6 * signedArea ) ; return ans ; } let vp = [ [ 1 , 2 ] , [ 3 , - 4 ] , [ 6 , - 7 ] ] ; let ans = find_Centroid ( vp ) ; document . write ( ans [ 0 ] . toFixed ( 11 ) + " " + ans [ 1 ] ) ;
Program to find the angles of a quadrilateral | Driver code ; according to formula derived above ; print all the angles
var d = 10 ; var a ; a = parseInt ( ( 360 - ( 6 * d ) ) / 4 ) ; document . write ( a + " " + ( a + d ) + " " + ( a + ( 2 * d ) ) + " " + ( a + ( 3 * d ) ) ) ;
Distance between two parallel Planes in 3 | Function to find distance ; Driver Code
function distance ( a1 , b1 , c1 , d1 , a2 , b2 , c2 , d2 ) { let x1 , y1 , z1 , d ; if ( a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2 ) { x1 = y1 = 0 ; z1 = - d1 / c1 ; d = Math . abs ( ( c2 * z1 + d2 ) ) / ( Math . sqrt ( a2 * a2 + b2 * b2 + c2 * c2 ) ) ; document . write ( " " + d ) ; } else document . write ( " " ) ; } let a1 = 1 ; let b1 = 2 ; let c1 = - 1 ; let d1 = 1 ; let a2 = 3 ; let b2 = 6 ; let c2 = - 3 ; let d2 = - 4 ; distance ( a1 , b1 , c1 , d1 , a2 , b2 , c2 , d2 ) ;
Count of ways to form 2 necklace from N beads containing N / 2 beads each | Function to calculate factorial ; Function to count number of ways to make 2 necklace having exactly N / 2 beads if each bead is considered different ; Number of ways to choose N / 2 beads from N beads ; Number of ways to permute N / 2 beads ; Divide ans by 2 to remove repetitions ; Return ans ; Given Input ; Function Call
function factorial ( n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } function numOfNecklace ( N ) { var ans = factorial ( N ) / ( factorial ( N / 2 ) * factorial ( N / 2 ) ) ; ans = ans * factorial ( N / 2 - 1 ) ; ans = ans * factorial ( N / 2 - 1 ) ; ans /= 2 ; return ans ; } var N = 4 ; document . write ( numOfNecklace ( N ) ) ;
Check if a number S can be made divisible by D by repeatedly adding the remainder to S | Function to check if S is divisible by D while changing S to ( S + S % D ) ; V ( 0 ) = S % D ; Stores the encountered values ; V ( i ) = ( V ( i - 1 ) + V ( i - 1 ) % D ) % D ; Check if the value has already been encountered ; Edge Case ; Otherwise , insert it into the hashmap ; Driver Code
function isDivisibleByDivisor ( S , D ) { S %= D ; var hashMap = [ ] ; hashMap . push ( S ) ; for ( var i = 0 ; i <= D ; i ++ ) { S += S % D ; S %= D ; if ( hashMap . includes ( S ) ) { if ( S == 0 ) { return " " ; } return " " ; } else hashMap . push ( S ) ; } return " " ; } var S = 3 , D = 6 ; document . write ( isDivisibleByDivisor ( S , D ) ) ;
Minimum number of given moves required to reach ( 1 , 1 ) from ( X , Y ) | Function to count the number of steps required to convert ( x , y ) to ( 1 , 1 ) ; Store the required result ; Iterate while both x and y are not equal to 0 ; If x is greater than y ; Update count and value of x ; Otherwise ; Update count and value of y ; If both x and y > 1 ; Print the result ; Given X and Y
function minimumSteps ( x , y ) { var cnt = 0 ; while ( x != 0 && y != 0 ) { if ( x > y ) { cnt += x / y ; x %= y ; } else { cnt += y / x ; y %= x ; } } cnt -- ; if ( x > 1 y > 1 ) cnt = - 1 ; document . write ( cnt ) ; } var x = 3 , y = 1 ; minimumSteps ( x , y ) ;
Form minimum number from given sequence | Javascript program to print minimum number that can be formed from a given sequence of Is and Ds ; min_avail represents the minimum number which is still available for inserting in the output vector . pos_of_I keeps track of the most recent index where ' I ' was encountered w . r . t the output vector ; vector to store the output ; cover the base cases ; Traverse rest of the input ; print the number ; Driver code
function printLeast ( arr ) { let min_avail = 1 , pos_of_I = 0 ; let al = [ ] ; if ( arr [ 0 ] == ' ' ) { al . push ( 1 ) ; al . push ( 2 ) ; min_avail = 3 ; pos_of_I = 1 ; } else { al . push ( 2 ) ; al . push ( 1 ) ; min_avail = 3 ; pos_of_I = 0 ; } for ( let i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] == ' ' ) { al . push ( min_avail ) ; min_avail ++ ; pos_of_I = i + 1 ; } else { al . push ( al [ i ] ) ; for ( let j = pos_of_I ; j <= i ; j ++ ) al [ j ] = al [ j ] + 1 ; min_avail ++ ; } } for ( let i = 0 ; i < al . length ; i ++ ) document . write ( al [ i ] + " " ) ; document . write ( " " ) ; } printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ; printLeast ( " " ) ;
Form minimum number from given sequence | Function to decode the given sequence to construct minimum number without repeated digits ; result store output string ; create an empty stack of integers ; run n + 1 times where n is length of input sequence ; push number i + 1 into the stack ; if all characters of the input sequence are processed or current character is ' I ' ( increasing ) ; run till stack is empty ; remove top element from the stack and add it to solution ; main function
function PrintMinNumberForPattern ( seq ) { let result = " " ; let stk = [ ] ; for ( let i = 0 ; i <= seq . length ; i ++ ) { stk . push ( i + 1 ) ; if ( i == seq . length seq [ i ] == ' ' ) { while ( stk . length != 0 ) { result += ( stk [ stk . length - 1 ] ) . toString ( ) ; result += " " ; stk . pop ( ) ; } } } document . write ( result + " " ) ; } PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ; PrintMinNumberForPattern ( " " ) ;
Form minimum number from given sequence | Returns minimum number made from given sequence without repeating digits ; The loop runs for each input character as well as one additional time for assigning rank to each remaining characters ; Driver Code
function getMinNumberForPattern ( seq ) { let n = seq . length ; if ( n >= 9 ) return " " ; let result = new Array ( n + 1 ) ; let count = 1 ; for ( let i = 0 ; i <= n ; i ++ ) { if ( i == n seq [ i ] == ' ' ) { for ( let j = i - 1 ; j >= - 1 ; j -- ) { result [ j + 1 ] = String . fromCharCode ( ' ' . charCodeAt ( ) + count ++ ) ; if ( j >= 0 && seq [ j ] == ' ' ) break ; } } } return result . join ( " " ) ; } let inputs = [ " " , " " , " " , " " , " " , " " , " " ] ; for ( let input = 0 ; input < inputs . length ; input ++ ) { document . write ( getMinNumberForPattern ( inputs [ input ] ) + " " ) ; }
Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver code
function isPrime ( n ) { var i , c = 0 ; for ( i = 1 ; i < n / 2 ; i ++ ) { if ( n % i == 0 ) c ++ ; } if ( c == 1 ) return 1 ; else return 0 ; } function findMinNum ( arr , n ) { var first = 0 , last = 0 , num , rev , i ; var hash = new Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } document . write ( " " ) ; for ( var i = 0 ; i <= 9 ; i ++ ) { for ( var j = 0 ; j < hash [ i ] ; j ++ ) document . write ( i ) ; } document . write ( " " ) ; for ( i = 0 ; i <= 9 ; i ++ ) { if ( hash [ i ] != 0 ) { first = i ; break ; } } for ( i = 9 ; i >= 0 ; i -- ) { if ( hash [ i ] != 0 ) { last = i ; break ; } } num = first * 10 + last ; rev = last * 10 + first ; document . write ( " " ) ; if ( isPrime ( num ) && isPrime ( rev ) ) document . write ( num + " " + rev ) ; else if ( isPrime ( num ) ) document . write ( num ) ; else if ( isPrime ( rev ) ) document . write ( rev ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 4 , 7 , 8 ] ; findMinNum ( arr , 5 ) ;
Finding a Non Transitive Co | Function to return gcd of a and b ; function to check for gcd ; a and b are coprime if their gcd is 1. ; Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Generate and check for all possible triplets between L and R ; if we find any such triplets set flag to true ; flag = True indicates that a pair exists between L and R ; Driver Code ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function coprime ( a , b ) { return ( gcd ( a , b ) == 1 ) ; } function possibleTripletInRange ( L , R ) { let flag = false ; let possibleA = 0 , possibleB = 0 , possibleC = 0 ; for ( let a = L ; a <= R ; a ++ ) { for ( let b = a + 1 ; b <= R ; b ++ ) { for ( let c = b + 1 ; c <= R ; c ++ ) { if ( coprime ( a , b ) && coprime ( b , c ) && ! coprime ( a , c ) ) { flag = true ; possibleA = a ; possibleB = b ; possibleC = c ; break ; } } } } if ( flag == true ) { document . write ( " " + possibleA + " " + possibleB + " " + possibleC + " " + " " + " " + L + " " + R + " " ) ; } else { document . write ( " " + " " + L + " " + R + " " ) ; } } let L , R ; L = 2 ; R = 10 ; possibleTripletInRange ( L , R ) ; L = 23 ; R = 46 ; possibleTripletInRange ( L , R ) ;
Reach A and B by multiplying them with K and K ^ 2 at every step | function to check is it is possible to reach A and B starting from 1 and 1 ; find the cuberoot of the number ; divide the number by cuberoot ; if it is a perfect cuberoot and divides a and b ; Driver code
function possibleToReach ( a , b ) { let c = Math . cbrt ( a * b ) ; let re1 = a / c ; let re2 = b / c ; if ( ( re1 * re1 * re2 == a ) && ( re2 * re2 * re1 == b ) ) return true ; else return false ; } let A = 60 , B = 450 ; if ( possibleToReach ( A , B ) ) document . write ( " " ) ; else document . write ( " " ) ;
Undulating numbers | JavaScript program tto check whether a number is undulating or not ; Considering the definition with restriction that there should be at least 3 digits ; Check if all alternate digits are same or not . ; Driver Code
function isUndulating ( n ) { if ( n . length <= 2 ) return false ; for ( let i = 2 ; i < n . length ; i ++ ) if ( n [ i - 2 ] != n [ i ] ) return false ; return true ; } let n = " " ; if ( isUndulating ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find the sum of a Series ( 1 * 1 ) + ( 2 * 2 ) + ( 3 * 3 ) + ( 4 * 4 ) + ( 5 * 5 ) + ... + ( n * n ) | Function to calculate the following series ; Driver Code
function Series ( n ) { let i ; let sums = 0 ; for ( i = 1 ; i <= n ; i ++ ) sums += ( i * i ) ; return sums ; } let n = 3 ; let res = Series ( n ) ; document . write ( res ) ;
Count numbers with unit digit k in given range | Returns count of numbers with k as last digit . ; Driver function
function counLastDigitK ( low , high , k ) { let mlow = 10 * ( Math . ceil ( low / 10.0 ) ) ; let mhigh = 10 * ( Math . floor ( high / 10.0 ) ) ; let count = ( mhigh - mlow ) / 10 ; if ( high % 10 >= k ) count ++ ; if ( low % 10 <= k && ( low % 10 ) > 0 ) count ++ ; return count ; } let low = 3 , high = 35 , k = 3 ; document . write ( counLastDigitK ( low , high , k ) ) ;
Sum of all numbers divisible by 6 in a given range | function to calculate the sum of all numbers divisible by 6 in range L - R . . ; no of multiples of 6 upto r ; no of multiples of 6 upto l - 1 ; summation of all multiples of 6 upto r ; summation of all multiples of 6 upto l - 1 ; returns the answer ; Driver Code
function sum ( L , R ) { let p = Math . floor ( R / 6 ) ; let q = Math . floor ( ( L - 1 ) / 6 ) ; let sumR = Math . floor ( 3 * ( p * ( p + 1 ) ) ) ; let sumL = Math . floor ( ( q * ( q + 1 ) ) * 3 ) ; return sumR - sumL ; } let L = 1 , R = 20 ; document . write ( sum ( L , R ) ) ;
Largest smaller number possible using only one swap operation | Returns largest possible number with one swap such that the number is smaller than str . It is assumed that there are leading 0 s . ; Traverse from right until we find a digit which is greater than its next digit . For example , in 34125 , our index is 4. ; We can also use binary search here as digits after index are sorted in increasing order . Find the biggest digit in the right of arr [ index ] which is smaller than arr [ index ] ; If index is - 1 i . e . digits are in increasing order . ; Swap both values ; Driver code
function prevNum ( str ) { let len = str . length ; let index = - 1 ; for ( let i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { index = i ; break ; } } let smallGreatDgt = - 1 ; for ( let i = len - 1 ; i > index ; i -- ) { if ( str [ i ] < str [ index ] ) { if ( smallGreatDgt == - 1 ) { smallGreatDgt = i ; } else if ( str [ i ] >= str [ smallGreatDgt ] ) { smallGreatDgt = i ; } } } if ( index == - 1 ) { return " " ; } if ( smallGreatDgt != - 1 ) { str = swap ( str , index , smallGreatDgt ) ; return str ; } return " " ; } function swap ( str , i , j ) { let ch = str . split ( ' ' ) ; let temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return ch . join ( " " ) ; } let str = " " ; document . write ( prevNum ( str ) ) ;
Sgn value of a polynomial | Returns value of poly [ 0 ] x ( n - 1 ) + poly [ 1 ] x ( n - 2 ) + . . + poly [ n - 1 ] ; Initialize result ; Evaluate value of polynomial using Horner 's method ; Returns sign value of polynomial ; Let us evaluate value of 2 x3 - 6 x2 + 2 x - 1 for x = 3
function horner ( poly , n , x ) { var result = poly [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) result = result * x + poly [ i ] ; return result ; } function findSign ( poly , n , x ) { var result = horner ( poly , n , x ) ; if ( result > 0 ) return 1 ; else if ( result < 0 ) return - 1 ; return 0 ; } var poly = [ 2 , - 6 , 2 , - 1 ] ; var x = 3 ; var n = poly . length ; document . write ( " " + findSign ( poly , n , x ) ) ;
Insert minimum number in array so that sum of array becomes prime | Javascript program to find minimum number to insert in array so their sum is prime ; Array to store primes ; function to calculate primes using sieve of eratosthenes ; Find prime number greater than a number ; To return prime number greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; call sieveOfEratostheneses to calculate primes ; To find sum of array elements ; To find prime number greater then sum ; Return difference of sum and num ; driver program
let MAX = 100005 ; let isPrime = new Array ( MAX ) . fill ( 0 ) ; function sieveOfEratostheneses ( ) { isPrime [ 1 ] = true ; for ( let i = 2 ; i * i < MAX ; i ++ ) { if ( ! isPrime [ i ] ) { for ( let j = 2 * i ; j < MAX ; j += i ) isPrime [ j ] = true ; } } } function findPrime ( n ) { let num = n + 1 ; while ( num > 0 ) { if ( ! isPrime [ num ] ) return num ; num = num + 1 ; } return 0 ; } function minNumber ( arr , n ) { sieveOfEratostheneses ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( ! isPrime [ sum ] ) return 0 ; let num = findPrime ( sum ) ; return num - sum ; } let arr = [ 2 , 4 , 6 , 8 , 12 ] ; let n = arr . length ; document . write ( minNumber ( arr , n ) ) ;