codeparrot/xlcost-text-to-code huggingface dataset:
Find the largest element in an array generated using the given conditions | Function to generate the required array and to find and return the maximum array element ; Stores the array ; Base case ; Iterate over the indices ; If current index is even ; Otherwise ; Function to find and return the maximum array element ; If n is 0 ; If n is 1 ; Generates the required array ; Return maximum element of Arr ; Driver Code
function findArray ( n ) { let Arr = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { Arr [ i ] = Arr [ i / 2 ] ; } else { Arr [ i ] = Arr [ ( i - 1 ) / 2 ] + Arr [ ( i - 1 ) / 2 + 1 ] ; } } return Arr ; } function maxElement ( n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; let Arr = findArray ( n ) ; let ans = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Arr [ i ] ) ; } return ans ; } let N = 7 ; document . write ( maxElement ( N ) ) ;
Construct a graph using N vertices whose shortest distance between K pair of vertices is 2 | Javascript program to implement the above approach ; Function to construct the simple and connected graph such that the distance between exactly K pairs of vertices is 2 ; Stores maximum possible count of edges in a graph ; Base Case ; Stores edges of a graph ; Connect all vertices of pairs ( i , j ) ; Print first ( ( N - 1 ) + Max - K ) elements of edges [ ] ; Driver Code
class pair { constructor ( first , second ) { this [ 0 ] = first ; this [ 1 ] = second ; } } function constGraphWithCon ( N , K ) { var Max = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; if ( K > Max ) { document . write ( - 1 + " " ) ; return ; } var ans = [ ] ; for ( var i = 1 ; i < N ; i ++ ) { for ( var j = i + 1 ; j <= N ; j ++ ) { ans . push ( [ i , j ] ) ; } } for ( var i = 0 ; i < ( N - 1 ) + Max - K ; i ++ ) { document . write ( ans [ i ] [ 0 ] + " " + ans [ i ] [ 1 ] + " " ) ; } } var N = 5 , K = 3 ; constGraphWithCon ( N , K ) ;
Minimum steps required to visit all corners of an N * M grid | Function to find the minimum count of steps required to visit all the corners of the grid ; Stores corner of the grid ; Stores minimum count of steps required to visit the first corner of the grid ; Checking for leftmost upper corner ; Checking for leftmost down corner ; Checking for rightmost upper corner ; Checking for rightmost down corner ; Stores minimum count of steps required to visit remaining three corners of the grid ; Driver Code
function min_steps_required ( n , m , r , c ) { var i , j ; var corner_steps_req = Number . MAX_VALUE ; i = 1 ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = 1 ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; var minimum_steps = Math . min ( 2 * ( n - 1 ) + m - 1 , 2 * ( m - 1 ) + n - 1 ) ; return minimum_steps + corner_steps_req ; } var n = 3 ; var m = 2 ; var r = 1 ; var c = 1 ; document . write ( min_steps_required ( n , m , r , c ) ) ;
Minimum replacements required to have at most K distinct elements in the array | Function to find minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Store the frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Store frequency of each distinct element of the array ; Traverse the map ; Stores key of the map ; Insert mp [ i ] into Freq [ ] ; Sort Freq [ ] in descending order ; Stores size of Freq [ ] ; If len is less than or equal to K ; Stores minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Iterate over the range [ K , len ] ; Update cntMin ; Driver Code
function min_elements ( arr , N , K ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } } let Freq = [ ] ; for ( let it of mp ) { let i = it [ 0 ] ; Freq . push ( mp . get ( i ) ) ; } Freq . sort ( ( a , b ) => b - a ) ; let len = Freq . length ; if ( len <= K ) { return 0 ; } let cntMin = 0 ; for ( let i = K ; i < len ; i ++ ) { cntMin += Freq [ i ] ; } return cntMin ; } let arr = [ 5 , 1 , 3 , 2 , 4 , 1 , 1 , 2 , 3 , 4 ] ; let N = arr . length ; let K = 3 ; document . write ( min_elements ( arr , N , K ) ) ;
Check if sum of the given array can be reduced to 0 by reducing array elements by K | Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array arr
function sumzero ( arr , N , K ) { var sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 0 ) document . write ( " " ) ; else if ( sum > 0 ) { if ( sum % K == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } else document . write ( " " ) ; return 0 ; } var K , N ; var arr1 = [ 1 , - 6 , 2 , 2 ] ; K = 1 ; N = arr1 . length ; sumzero ( arr1 , N , K ) ;
Counts 1 s that can be obtained in an Array by performing given operations | Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code
function cntOnesArrWithGivenOp ( arr , N ) { let cntOnes = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } let arr = [ 0 , 0 , 0 , 0 , 0 ] ; let N = arr . length ; document . write ( cntOnesArrWithGivenOp ( arr , N ) ) ;
Counts 1 s that can be obtained in an Array by performing given operations | Function to count total number of 1 s in array by performing the given operations ; Stores count of 1 s in the array by performing the operations ; Update cntOnes ; Driver code
function cntOnesArrWithGivenOp ( arr , N ) { var cntOnes = 0 ; cntOnes = parseInt ( Math . sqrt ( N ) ) ; return cntOnes ; } var arr = [ 0 , 0 , 0 , 0 , 0 ] ; var N = arr . length ; document . write ( cntOnesArrWithGivenOp ( arr , N ) ) ;
Count Knights that can attack a given pawn in an N * N board | Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights [ ] [ ] array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver Code ; Stores total count of knights
function cntKnightsAttackPawn ( knights , pawn , M ) { let cntKnights = 0 ; for ( let i = 0 ; i < M ; i ++ ) { let X = Math . abs ( knights [ i ] [ 0 ] - pawn [ 0 ] ) ; let Y = Math . abs ( knights [ i ] [ 1 ] - pawn [ 1 ] ) ; if ( ( X == 1 && Y == 2 ) || ( X == 2 && Y == 1 ) ) { cntKnights ++ ; } } return cntKnights ; } let knights = [ [ 0 , 4 ] , [ 4 , 5 ] , [ 1 , 4 ] , [ 3 , 1 ] ] ; let pawn = [ 2 , 3 ] ; let M = knights . length ; document . write ( cntKnightsAttackPawn ( knights , pawn , M ) ) ;
Maximum possible sum of K even multiples of 5 in a given range | Function to find the maximum sum of K even multiples of 5 in the range [ L , R ] ; Store the total number of even multiples of 5 in the range [ L , R ] ; Check if K > N ; If true , print - 1 and return ; Otherwise , divide R by 10 ; Store the sum using the formula ; Print the sum ; Given L , R and K ; Function Call
function maxksum ( L , R , K ) { let N = ( R / 10 - L / 10 ) + 1 ; if ( K > N ) { document . write ( " " ) ; return ; } R = R / 10 ; let X = R - K ; let sum = 10 * ( ( R * ( R + 1 ) ) / 2 - ( X * ( X + 1 ) ) / 2 ) ; document . write ( sum ) ; } let L = 16 , R = 60 , K = 4 ; maxksum ( L , R , K ) ;
Minimum swaps of same | Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , prlet 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , prlet - 1 ; Driver Code ; Function Call
function minimumSwaps ( arr1 , arr2 , n ) { let sumArr1 = 0 , sumArr2 = 0 ; for ( let i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { document . write ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { let flag = - 1 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } document . write ( flag ) ; return ; } document . write ( - 1 ) ; } let arr1 = [ 11 , 14 , 20 , 2 ] ; let arr2 = [ 5 , 9 , 6 , 3 ] ; let N = arr1 . length ; minimumSwaps ( arr1 , arr2 , N ) ;
Count of seats booked on each of the given N flights | Function to find the total of the seats booked in each of the flights ; Stores the resultant sequence ; Traverse the array ; Store the first booked flight ; Store the last booked flight ; Store the total number of seats booked in flights [ l , r ] ; Add K to the flight L ; Subtract K from flight number R + 1 ; Find the prefix sum of the array ; Print the total number of seats booked in each flight ; Given list of bookings ; Function Call
function corpFlightBookings ( Bookings , N ) { let res = new Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < Bookings . length ; i ++ ) { let l = Bookings [ i ] [ 0 ] ; let r = Bookings [ i ] [ 1 ] ; let K = Bookings [ i ] [ 2 ] ; res [ l - 1 ] = res [ l - 1 ] + K ; if ( r <= res . length - 1 ) res [ r ] = ( - K ) + res [ r ] ; } for ( let i = 1 ; i < res . length ; i ++ ) res [ i ] = res [ i ] + res [ i - 1 ] ; for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } let bookings = [ [ 1 , 3 , 100 ] , [ 2 , 6 , 100 ] , [ 3 , 4 , 100 ] ] ; let N = 6 ; corpFlightBookings ( bookings , N ) ;
Bitwise XOR of all odd numbers from a given range | Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call
function findXOR ( n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } } function findOddXOR ( n ) { if ( n % 2 == 0 ) document . write ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ; else document . write ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ; } let N = 11 ; findOddXOR ( N ) ;
Smallest number greater than or equal to N which is divisible by its non | Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call
function findSmallestNumber ( n ) { for ( i = n ; i <= ( n + 2520 ) ; ++ i ) { var possible = 1 ; var temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { var digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp = parseInt ( temp / 10 ) ; } if ( possible == 1 ) { document . write ( i ) ; return ; } } } var N = 31 ; findSmallestNumber ( N ) ;
Print path from a node to root of given Complete Binary Tree | Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver code
function path_to_root ( node ) { while ( node >= 1 ) { document . write ( node + " " ) ; node = parseInt ( node / 2 , 10 ) ; } } let N = 7 ; path_to_root ( N ) ;
Smallest number whose sum of digits is N and every digit occurring at most K times | Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call
function findSmallestNumberPossible ( N , K ) { if ( N > 45 * K ) { document . write ( " " ) ; return ; } var res = " " ; var count = 0 ; for ( var i = 9 ; i >= 1 ; ) { if ( count === K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res += String . fromCharCode ( " " . charCodeAt ( 0 ) + i ) ; } else { res += String . fromCharCode ( " " . charCodeAt ( 0 ) + N ) ; N = 0 ; break ; } count ++ ; } var ans = " " ; for ( var i = res . length - 1 ; i >= 0 ; i -- ) { ans += res [ i ] + " " ; } document . write ( ans ) ; } var N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ;
Minimum sum of values subtracted from array elements to make all array elements equal | Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
function minValue ( arr , n ) { var minimum = Math . min . apply ( Math , arr ) ; var sum = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } var arr = [ 1 , 2 , 3 ] ; var N = arr . length ; document . write ( minValue ( arr , N ) ) ;
Smallest positive number made up of non | Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call
function result ( n ) { if ( n > 45 ) { document . write ( - 1 ) ; } var res = " " ; var digit = 9 ; while ( n > digit ) { res = String . fromCharCode ( 48 + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = String . fromCharCode ( 48 + n ) + res ; } document . write ( res ) ; } var N = 19 ; result ( N ) ;
Count pair of integers having even sum | Function to count even pairs ; Stores count of pairs having even sum ; Stores count of even numbers up to N ; Stores count of odd numbers up to N ; Stores count of even numbers up to M ; Stores count of odd numbers up to M ; Return the count ; Driver Code
function countEvenPairs ( N , M ) { let count = 0 ; nEven = parseInt ( Math . floor ( N / 2 ) ) ; nOdd = parseInt ( Math . ceil ( N / 2 ) ) ; mEven = parseInt ( Math . floor ( M / 2 ) ) ; mOdd = parseInt ( Math . ceil ( M / 2 ) ) ; count = nEven * mEven + nOdd * mOdd ; return count ; } let N = 4 ; let M = 6 ; document . write ( countEvenPairs ( N , M ) ) ;
Generate an N | Function to generate a String of length N having longest palindromic subString of length K ; Fill first K characters with ' a ' ; Stores a non - palindromic sequence to be repeated for N - k slots ; Print N - k remaining characters ; Given N and K
function String_palindrome ( N , K ) { for ( let i = 0 ; i < K ; i ++ ) document . write ( " " ) ; let s = " " ; for ( let i = 0 ; i < N - K ; i ++ ) document . write ( s [ i % 3 ] ) ; } let N = 5 , K = 3 ; String_palindrome ( N , K ) ;
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | Function to find the last removed character from the string ; Stores length of the string ; Base Case : ; If the second last character is '0' ; If the second last character is '1' ; Driver Code
function lastRemovedCharacter ( str ) { var n = str . length ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == " " ) { return " " - str [ n - 1 ] + " " ; } else return str [ n - 1 ] ; } var str = " " ; document . write ( lastRemovedCharacter ( str ) ) ;
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Print a ; Print b ; Print c ; Driver Code
function findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) { let a , b , c ; let aSUMb ; let aSUMc ; let bSUMc ; aSUMb = aXORb + aANDb * 2 ; aSUMc = aXORc + aANDc * 2 ; bSUMc = bXORc + bANDc * 2 ; a = Math . floor ( ( aSUMb - bSUMc + aSUMc ) / 2 ) ; b = aSUMb - a ; c = aSUMc - a ; document . write ( " " + a ) ; document . write ( " " + b ) ; document . write ( " " + c ) ; } let aXORb = 30 , aANDb = 0 , aXORc = 20 , aANDc = 10 , bXORc = 10 , bANDc = 20 ; findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ;
Find N distinct numbers whose Bitwise XOR is equal to K | Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Prlet the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
function findArray ( N , K ) { if ( N == 1 ) { document . write ( K + " " ) ; return ; } if ( N == 2 ) { document . write ( 0 + " " + K ) ; return ; } let P = N - 2 ; let Q = N - 1 ; let VAL = 0 ; for ( let i = 1 ; i <= ( N - 3 ) ; i ++ ) { document . write ( i + " " ) ; VAL ^= i ; } if ( VAL == K ) { document . write ( P + " " + Q + " " + ( P ^ Q ) ) ; } else { document . write ( 0 + " " + P + " " + ( P ^ K ^ VAL ) ) ; } } let N = 4 , X = 6 ; findArray ( N , X ) ;
Flip consecutive set bits starting from LSB of a given number | Function to find the number after converting 1 s from end to 0 s ; Count of 1 s ; AND operation of N and 1 ; Left shift N by count ; Driver Code ; Function Call
function findNumber ( N ) { let count = 0 ; while ( ( N & 1 ) == 1 ) { N = N >> 1 ; count ++ ; } return N << count ; } let N = 39 ; document . write ( findNumber ( N ) ) ;
Flip consecutive set bits starting from LSB of a given number | Function to find the number after converting 1 s from end to 0 s ; Return the logical AND of N and ( N + 1 ) ; Driver Code ; Function Call
function findNumber ( N ) { return N & ( N + 1 ) ; } let N = 39 ; document . write ( findNumber ( N ) ) ;
Count of substrings of a string containing another given string as a substring | Function to store all substrings of S ; Stores the substrings of S ; Pick start point in outer loop and lengths of different strings for a given starting point ; Return the array containing substrings of S ; Function to check if a string is present in another string ; Check if target is in the string str or not ; Function to count the substring of S containing T in it as substring ; Store all substrings of S in the array v [ ] ; Store required count of substrings ; Iterate through all the substrings of S ; If string T is present in the current substring , then increment the ans ; Print the answer ; Driver code ; Function Call
function subString ( s , n ) { var v = [ ] ; var i , len ; for ( i = 0 ; i < n ; i ++ ) { for ( len = 1 ; len <= n - i ; len ++ ) { var find = s . substr ( i , len ) ; v . push ( find ) ; } } return v ; } function IsPresent ( str , target ) { if ( str . includes ( target ) ) { return 1 ; } return - 1 ; } function countSubstrings ( S , T ) { var v = subString ( S , S . length ) ; var ans = 0 ; var i ; for ( i = 0 ; i < v . length ; i ++ ) { if ( IsPresent ( v [ i ] , T ) != - 1 ) { ans ++ ; } } document . write ( ans ) ; } var S = " " ; var T = " " ; countSubstrings ( S , T ) ;
Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver code ; Function Call
function minOperation ( X , Y , n ) { let C = 0 ; let count = 0 ; for ( let i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( let i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; document . write ( C ) ; } let X = [ 1 , 0 , 0 , 0 , 0 , 1 ] ; let Y = [ 1 , 1 , 0 , 1 , 1 , 1 ] ; let N = X . length ; minOperation ( X , Y , N ) ;
Minimum subarray reversals required to make given binary array alternating | Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver code
function minimumcntOperationReq ( arr , N ) { let cntOp = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } let arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ] ; let N = arr . length ; document . write ( Math . floor ( minimumcntOperationReq ( arr , N ) ) ) ;
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver code ; Function Call
function isSequenceValid ( B , N ) { for ( let i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let N = 4 ; let arr = [ 1 , 2 , 3 , 2 ] ; isSequenceValid ( arr , N ) ;
Construct an array of first N natural numbers such that every adjacent pair is coprime | Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver code
function ConstArrayAdjacentCoprime ( N ) { for ( let i = 1 ; i <= N ; i ++ ) { document . write ( i + " " ) ; } } let N = 6 ; ConstArrayAdjacentCoprime ( N ) ;
Minimum cost to convert one given string to another using swap , insert or delete operations | Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
function minimumCost ( a , b ) { var fre1 = Array ( 256 ) . fill ( 0 ) , fre2 = Array ( 256 ) . fill ( 0 ) ; a . split ( ' ' ) . forEach ( c => { fre1 [ c . charCodeAt ( 0 ) ] ++ ; } ) ; b . split ( ' ' ) . forEach ( c => { fre2 [ c . charCodeAt ( 0 ) ] ++ ; } ) ; var mincost = 0 ; for ( var i = 0 ; i < 256 ; i ++ ) { mincost += Math . abs ( fre1 [ i ] - fre2 [ i ] ) ; } document . write ( mincost ) ; } var A = " " , B = " " ; minimumCost ( A , B ) ;