codeparrot/xlcost-text-to-code huggingface dataset:
Maximum elements that can be removed from front of two arrays such that their sum is at most K | Function to find the maximum number of items that can be removed from both the arrays ; Stores the maximum item count ; Stores the prefix sum of the cost of items ; Insert the item cost 0 at the front of the arrays ; Build the prefix sum for the array A [ ] ; Update the value of A [ i ] ; Build the prefix sum for the array B [ ] ; Update the value of B [ i ] ; Iterate through each item of the array A [ ] ; If A [ i ] exceeds K ; Store the remaining amount after taking top i elements from the array A ; Store the number of items possible to take from the array B [ ] ; Store low and high bounds for binary search ; Binary search to find number of item that can be taken from stack B in rem amount ; Calculate the mid value ; Update the value of j and lo ; Update the value of the hi ; Store the maximum of total item count ; Print the result ; Driver Code
function maxItems ( n , m , a , b , K ) { var count = 0 ; var A = new Array ( n + 1 ) ; var B = new Array ( m + 1 ) ; A [ 0 ] = 0 ; B [ 0 ] = 0 ; var i ; for ( i = 1 ; i <= n ; i ++ ) { A [ i ] = a [ i - 1 ] + A [ i - 1 ] ; } for ( i = 1 ; i <= m ; i ++ ) { B [ i ] = b [ i - 1 ] + B [ i - 1 ] ; } for ( i = 0 ; i <= n ; i ++ ) { if ( A [ i ] > K ) break ; var rem = K - A [ i ] ; var j = 0 ; var lo = 0 , hi = m ; while ( lo <= hi ) { var mid = parseInt ( ( lo + hi ) / 2 ) ; if ( B [ mid ] <= rem ) { j = mid ; lo = mid + 1 ; } else { hi = mid - 1 ; } } count = Math . max ( j + i , count ) ; } document . write ( count ) ; } var n = 4 , m = 5 , K = 7 ; var A = [ 2 , 4 , 7 , 3 ] ; var B = [ 1 , 9 , 3 , 4 , 5 ] ; maxItems ( n , m , A , B , K ) ;
Numbers of pairs from an array whose average is also present in the array | Function to count the total count of pairs having sum S ; Store the total count of all elements in map mp ; Initialize value to 0 , if key not found ; Stores the total count of total pairs ; Iterate through each element and increment the count ; If the value ( S - arr [ i ] ) exists in the map hm ; Update the twice count ; Return the half of twice_count ; Function to count of pairs having whose average exists in the array ; Stores the total count of pairs ; Use set to remove duplicates ; Insert all the element in the set S ; For every sum find the getCountPairs ; Return the total count of pairs ; Driver Code
function getCountPairs ( arr , N , S ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 0 ) ; mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } let twice_count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . get ( S - arr [ i ] ) != null ) { twice_count += mp . get ( S - arr [ i ] ) ; } if ( S - arr [ i ] == arr [ i ] ) twice_count -- ; } return Math . floor ( twice_count / 2 ) ; } function countPairs ( arr , N ) { let count = 0 ; let S = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) S . add ( arr [ i ] ) ; for ( let ele of S . values ( ) ) { let sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } let N = 6 ; let arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] ; document . write ( countPairs ( arr , N ) ) ;
Number which is co | Function to check whether the given number N is prime or not ; Base Case ; If N has more than one factor , then return false ; Otherwise , return true ; Function to find X which is co - prime with the integers from the range [ L , R ] ; Store the resultant number ; Check for prime integers greater than R ; If the current number is prime , then update coPrime and break out of loop ; Prvar the resultant number ; Driver Code
function isPrime ( N ) { if ( N == 1 ) return false ; for ( var i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } function findCoPrime ( L , R ) { var coPrime ; for ( var i = R + 1 ; ; i ++ ) { if ( isPrime ( i ) ) { coPrime = i ; break ; } } return coPrime ; } var L = 16 , R = 17 ; document . write ( findCoPrime ( L , R ) ) ;
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Javascript program to for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traversing over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver code
var arr = [ 1 , 9 , 3 , 6 , 5 ] ; function maximumValue ( arr , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { ans = Math . max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } document . write ( ans ) ; } n = arr . length maximumValue ( arr , n ) ;
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Prvar the result ; Driver Code
function maximumValue ( arr , n ) { var maxvalue = arr [ 0 ] ; var result = 0 ; for ( i = 1 ; i < n ; i ++ ) { result = Math . max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = Math . max ( maxvalue , arr [ i ] + i ) ; } document . write ( result ) ; } var arr = [ 1 , 9 , 3 , 6 , 5 ] ; var N = arr . length ; maximumValue ( arr , N ) ;
Minimum sum of absolute differences of pairs in a triplet from three arrays | Lower_bound function ; Function to find the value closest to K in the array A [ ] ; Initialize close value as the end element ; Find lower bound of the array ; If lower_bound is found ; If lower_bound is not the first array element ; If * ( it - 1 ) is closer to k ; Return closest value of k ; Function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; Sort the vectors arr and crr ; Initialize minimum as LET_MAX ; Traverse the array brr [ ] ; Stores the element closest to val from the array arr [ ] ; Stores the element closest to val from the array crr [ ] ; If sum of differences is minimum ; Update the minimum ; Print the minimum absolute difference possible ; Driver code ; Function Call
function lower_bound ( arr , key ) { let low = 0 ; let high = arr . length - 1 ; while ( low < high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( arr [ mid ] >= key ) { high = mid ; } else { low = mid + 1 ; } } return low ; } function closestValue ( A , k ) { let close = A [ A . length - 1 ] ; let it = lower_bound ( A , k ) ; if ( it != A . length ) { close = A [ it ] ; if ( it != 0 ) { if ( ( k - A [ it - 1 ] ) < ( close - k ) ) { close = A [ it - 1 ] ; } } } return close ; } function minPossible ( arr , brr , crr ) { arr . sort ( ) ; crr . sort ( ) ; let minimum = Number . MAX_VALUE ; for ( let val in brr ) { let arr_close = closestValue ( arr , val ) ; let crr_close = closestValue ( crr , val ) ; if ( Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) < minimum ) minimum = Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) ; } document . write ( minimum ) ; } let a = [ 1 , 8 , 5 ] ; let b = [ 2 , 9 ] ; let c = [ 5 , 4 ] ; minPossible ( a , b , c ) ;
Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code
function minimum_swaps ( arr , n ) { var count = 0 ; var max_el = Math . max ( ... arr ) ; var min_el = Math . min ( ... arr ) ; if ( min_el === max_el ) return 0 ; var index_max = - 1 ; var index_min = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] === max_el && index_max === - 1 ) { index_max = i ; } if ( arr [ i ] === min_el ) { index_min = i ; } } count += index_max ; count += n - 1 - index_min ; if ( index_min < index_max ) count -= 1 ; return count ; } var arr = [ 2 , 4 , 1 , 6 , 5 ] ; var N = arr . length ; document . write ( minimum_swaps ( arr , N ) ) ;
Maximum difference between node and its ancestor in a Directed Acyclic Graph ( DAG ) | Javascript program for the above approach ; Function to perform DFS Traversal on the given graph ; Update the value of ans ; Update the currentMin and currentMax ; Traverse the adjacency list of the node src ; Recursively call for the child node ; Function to calculate maximum absolute difference between a node and its ancestor ; Stores the adjacency list of graph ; Create Adjacency list ; Add a directed edge ; Perform DFS Traversal ; Print the maximum absolute difference ; Driver Code
var ans = 0 ; function DFS ( src , Adj , arr , currentMin , currentMax ) { ans = Math . max ( ans , Math . max ( Math . abs ( currentMax - arr [ src - 1 ] ) , Math . abs ( currentMin - arr [ src - 1 ] ) ) ) ; currentMin = Math . min ( currentMin , arr [ src - 1 ] ) ; currentMax = Math . min ( currentMax , arr [ src - 1 ] ) ; Adj [ src ] . forEach ( child => { DFS ( child , Adj , arr , currentMin , currentMax ) ; } ) ; } function getMaximumDifference ( Edges , arr , N , M ) { var Adj = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < M ; i ++ ) { var u = Edges [ i ] [ 0 ] ; var v = Edges [ i ] [ 1 ] ; Adj [ u ] . push ( v ) ; } DFS ( 1 , Adj , arr , arr [ 0 ] , arr [ 0 ] ) ; document . write ( ans ) ; } var N = 5 , M = 4 ; var Edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 1 , 3 ] ] ; var arr = [ 13 , 8 , 3 , 15 , 18 ] ; getMaximumDifference ( Edges , arr , N , M ) ;
Minimum cost path from source node to destination node via K intermediate nodes | Function to find the minimum cost path from the source vertex to destination vertex via K intermediate vertices ; Initialize the adjacency list ; Generate the adjacency list ; Initialize the minimum priority queue ; Stores the minimum cost to travel between vertices via K intermediate nodes ; Push the starting vertex , cost to reach and the number of remaining vertices ; Pop the top element of the stack ; If destination is reached ; Return the cost ; If all stops are exhausted ; Find the neighbour with minimum cost ; Pruning ; Update cost ; Update priority queue ; If no path exists ; Driver Code ; Function Call to find the path from src to dist via k nodes having least sum of weights
function leastWeightedSumPath ( n , edges , src , dst , K ) { var graph = new Map ( ) ; for ( var edge of edges ) { if ( ! graph . has ( edge [ 0 ] ) ) graph . set ( edge [ 0 ] , new Array ( ) ) var tmp = graph . get ( edge [ 0 ] ) ; tmp . push ( [ edge [ 1 ] , edge [ 2 ] ] ) ; graph . set ( edge [ 0 ] , tmp ) ; } var pq = [ ] ; var costs = Array . from ( Array ( n + 1 ) , ( ) => Array ( K + 2 ) . fill ( 1000000000 ) ) ; costs [ src ] [ K + 1 ] = 0 ; pq . push ( [ 0 , src , K + 1 ] ) ; while ( pq . length != 0 ) { var top = pq [ pq . length - 1 ] ; pq . pop ( ) ; if ( top [ 1 ] == dst ) return top [ 0 ] ; if ( top [ 2 ] == 0 ) continue ; if ( graph . has ( top [ 1 ] ) ) { for ( var neighbor of graph . get ( top [ 1 ] ) ) { if ( costs [ neighbor [ 0 ] ] [ top [ 2 ] - 1 ] < neighbor [ 1 ] + top [ 0 ] ) { continue ; } costs [ neighbor [ 0 ] ] [ top [ 2 ] - 1 ] = neighbor [ 1 ] + top [ 0 ] ; pq . push ( [ neighbor [ 1 ] + top [ 0 ] , neighbor [ 0 ] , top [ 2 ] - 1 ] ) ; } } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return b [ 1 ] - a [ 1 ] return b [ 0 ] - a [ 0 ] ; } ) ; } return - 1 ; } var n = 3 , src = 0 , dst = 2 , k = 1 ; var edges = [ [ 0 , 1 , 100 ] , [ 1 , 2 , 100 ] , [ 0 , 2 , 500 ] ] ; document . write ( leastWeightedSumPath ( n , edges , src , dst , k ) ) ;
Check if a given string is a comment or not | Function to check if the given string is a comment or not ; If two continuous slashes preceeds the comment var regex = new RegExp ( " . * " , ) ; var rex = regex . test ( line ) ; if ( rex ) { document . write ( " It ▁ is ▁ a ▁ single - line ▁ comment " ) ; return ; } var regexMul = new RegExp ( "/\*.*?\ ; Given string ; Function call to check whether then given string is a comment or not
function isComment ( line ) { " " It is a multi - line comment " " It is not a comment " " GeeksForGeeks GeeksForGeeks "
Print digits for each array element that does not divide any digit of that element | Function to find digits for each array element that doesn 't divide any digit  of the that element ; Traverse the array arr ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code
function indivisibleDigits ( arr , N ) { for ( i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; for ( j = 2 ; j < 10 ; j ++ ) { var temp = arr [ i ] ; var flag = true ; while ( temp > 0 ) { if ( ( temp % 10 ) != 0 && ( temp % 10 ) % j == 0 ) { flag = false ; break ; } temp = parseInt ( temp / 10 ) ; } if ( flag ) { document . write ( j + " " ) ; } } document . write ( " " ) ; } } var arr = [ 4162 , 1152 , 99842 ] ; var N = arr . length ; indivisibleDigits ( arr , N ) ;
Length of longest non | Function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; Base case ; Sort the array in ascending order ; Stores the maximum length ; Traverse the array ; If difference between current pair of adjacent elements is 1 or 0 ; Extend the current sequence Update len and max_len ; Otherwise , start a new subsequence ; Print the maximum length ; Given array ; Size of the array ; Function call to find the longest subsequence
function longestSequence ( arr , N ) { if ( N == 0 ) { document . write ( 0 ) ; return ; } arr . sort ( ) ; var maxLen = 1 ; var len = 1 ; var i ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i - 1 ] + 1 ) { len ++ ; maxLen = Math . max ( maxLen , len ) ; } else { len = 1 ; } } document . write ( maxLen ) ; } var arr = [ 8 , 5 , 4 , 8 , 4 ] ; var N = arr . length ; longestSequence ( arr , N ) ;
Queries to find the minimum array sum possible by removing elements from either end | Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Print the minimum of the two as the answer ; Driver Code ; Function Call
function minSum ( arr , N , Q , M ) { for ( var i = 0 ; i < M ; i ++ ) { var val = Q [ i ] ; var front = 0 , rear = 0 ; for ( var j = 0 ; j < N ; j ++ ) { front += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } for ( var j = N - 1 ; j >= 0 ; j -- ) { rear += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } document . write ( Math . min ( front , rear ) + " " ) ; } } var arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ] ; var N = arr . length ; var Q = [ 7 , 6 ] ; var M = Q . length ; minSum ( arr , N , Q , M ) ;
Frequency of lexicographically Kth smallest character in the a string | Function to find the frequency of the lexicographically Kth smallest character ; Convert the string to array of characters ; Sort the array in ascending order ; Store the Kth character ; Store the frequency of the K - th character ; Count the frequency of the K - th character ; Print the count ; Driver Code
function KthCharacter ( S , N , K ) { var strarray = S . split ( ' ' ) ; strarray . sort ( ) ; var ch = strarray [ K - 1 ] ; var count = 0 ; strarray . forEach ( c => { if ( c == ch ) count ++ ; } ) ; document . write ( count ) ; } var S = " " ; var N = S . length ; var K = 3 ; KthCharacter ( S , N , K ) ;
Longest substring with no pair of adjacent characters are adjacent English alphabets | Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the substring T ; Stores the first character of string S ; Traverse the string ; If the absolute difference is 1 ; Update the length of substring T ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Given string ; Function call to find the longest substring satisfying given condition
function findSubstring ( S ) { var T = " " ; var ans = " " ; var l = 0 ; T += S [ 0 ] ; for ( var i = 1 ; i < S . length ; i ++ ) { if ( Math . abs ( S [ i ] . charCodeAt ( 0 ) - S [ i - 1 ] . charCodeAt ( 0 ) ) === 1 ) { l = T . length ; if ( l > ans . length ) { ans = T ; } T = " " ; T += S [ i ] ; } else { T += S [ i ] ; } } l = T . length ; if ( l > ans . length ) { ans = T ; } document . write ( ans ) ; } var S = " " ; findSubstring ( S ) ;
Minimize length of an array by removing similar subarrays from both ends | Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Input ; Function call to find the minimized length of the array
function findMinLength ( arr , N ) { let front = 0 , back = N - 1 ; while ( front < back ) { let x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } document . write ( back - front + 1 ) ; document . write ( " " ) ; } let arr = [ 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 ] ; let N = arr . length ; findMinLength ( arr , N ) ;
Queries to find the maximum and minimum array elements excluding elements from a given range | Function to find the maximum and minimum array elements up to the i - th index ; Traverse the array ; Compare current value with maximum and minimum values up to previous index ; Function to find the maximum and minimum array elements from i - th index ; Traverse the array in reverse ; Compare current value with maximum and minimum values in the next index ; Function to find the maximum and minimum array elements for each query ; If no index remains after excluding the elements in a given range ; Find maximum and minimum from from the range [ R + 1 , N - 1 ] ; Find maximum and minimum from from the range [ 0 , N - 1 ] ; Find maximum and minimum values from the ranges [ 0 , L - 1 ] and [ R + 1 , N - 1 ] ; Print the maximum and minimum value ; Function to perform queries to find the minimum and maximum array elements excluding elements from a given range ; Size of the array ; Size of query array ; prefix [ i ] [ 0 ] : Stores the maximum prefix [ i ] [ 1 ] : Stores the minimum value ; suffix [ i ] [ 0 ] : Stores the maximum suffix [ i ] [ 1 ] : Stores the minimum value ; Function calls to store maximum and minimum values for respective ranges ; Given array
function prefixArr ( arr , prefix , N ) { for ( var i = 0 ; i < N ; i ++ ) { if ( i == 0 ) { prefix [ i ] [ 0 ] = arr [ i ] ; prefix [ i ] [ 1 ] = arr [ i ] ; } else { prefix [ i ] [ 0 ] = Math . max ( prefix [ i - 1 ] [ 0 ] , arr [ i ] ) ; prefix [ i ] [ 1 ] = Math . min ( prefix [ i - 1 ] [ 1 ] , arr [ i ] ) ; } } } function suffixArr ( arr , suffix , N ) { for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { suffix [ i ] [ 0 ] = arr [ i ] ; suffix [ i ] [ 1 ] = arr [ i ] ; } else { suffix [ i ] [ 0 ] = Math . max ( suffix [ i + 1 ] [ 0 ] , arr [ i ] ) ; suffix [ i ] [ 1 ] = Math . min ( suffix [ i + 1 ] [ 1 ] , arr [ i ] ) ; } } } function maxAndmin ( prefix , suffix , N , L , R ) { var maximum , minimum ; if ( L == 0 && R == N - 1 ) { document . write ( " " + " " ) ; return ; } else if ( L == 0 ) { maximum = suffix [ R + 1 ] [ 0 ] ; minimum = suffix [ R + 1 ] [ 1 ] ; } else if ( R == N - 1 ) { maximum = prefix [ L - 1 ] [ 0 ] ; minimum = prefix [ R - 1 ] [ 1 ] ; } else { maximum = Math . max ( prefix [ L - 1 ] [ 0 ] , suffix [ R + 1 ] [ 0 ] ) ; minimum = Math . min ( prefix [ L - 1 ] [ 1 ] , suffix [ R + 1 ] [ 1 ] ) ; } document . write ( maximum + " " + minimum + " " ) ; } function MinMaxQueries ( a , Q ) { var N = arr . length ; var q = queries . length ; var prefix = Array . from ( Array ( N ) , ( ) => Array ( 2 ) ) ; var suffix = Array . from ( Array ( N ) , ( ) => Array ( 2 ) ) ; prefixArr ( arr , prefix , N ) ; suffixArr ( arr , suffix , N ) ; for ( var i = 0 ; i < q ; i ++ ) { var L = queries [ i ] [ 0 ] ; var R = queries [ i ] [ 1 ] ; maxAndmin ( prefix , suffix , N , L , R ) ; } } var arr = [ 2 , 3 , 1 , 8 , 3 , 5 , 7 , 4 ] ; var queries = [ [ 4 , 6 ] , [ 0 , 4 ] , [ 3 , 7 ] , [ 2 , 5 ] ] ; MinMaxQueries ( arr , queries ) ;
Check if two binary strings can be made equal by swapping pairs of unequal characters | Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0' ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver code
function check ( s1 , s2 ) { let s1_0 = 0 , s2_0 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == ' ' ) { s1_0 ++ ; } if ( s2 [ i ] == ' ' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { document . write ( " " ) ; return ; } else { let Count1 = 0 , Count2 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == ' ' ) { Count1 ++ ; } if ( s2 [ i ] == ' ' ) { Count2 ++ ; } if ( Count1 < Count2 ) { document . write ( " " + " " ) ; return ; } } document . write ( " " + " " ) ; } } let s1 = " " ; let s2 = " " ; check ( s1 , s2 ) ; s1 = " " ; s2 = " " ; check ( s1 , s2 ) ;
Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then print not possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps  possible ; Swaps 1 with 0 in the string str1 ; Print not possible ; Given Strings ; Function Call
function isBinaryStringsEqual ( list1 , list2 ) { var str1 = list1 . split ( " " ) ; var str2 = list2 . split ( " " ) ; var str1Zeros = 0 , str1Ones = 0 ; var str2Zeros = 0 , str2Ones = 0 ; var flag = 0 ; var curStr1Ones = 0 ; for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str1 [ i ] === " " ) { str1Ones ++ ; } else if ( str1 [ i ] === " " ) { str1Zeros ++ ; } if ( str2 [ i ] === " " ) { str2Ones ++ ; } else if ( str2 [ i ] === " " ) { str2Zeros ++ ; } } if ( str1Zeros !== str2Zeros && str1Ones !== str2Ones ) { document . write ( " " ) ; } else { for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str1 [ i ] !== str2 [ i ] ) { if ( str1 [ i ] === " " && curStr1Ones > 0 ) { str1 [ i ] = " " ; curStr1Ones -- ; } if ( str1 [ i ] === " " && curStr1Ones === 0 ) { flag ++ ; break ; } if ( str1 [ i ] === " " && str2 [ i ] === " " ) { str1 [ i ] = " " ; curStr1Ones ++ ; } } } if ( flag === 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } var str1 = " " ; var str2 = " " ; isBinaryStringsEqual ( str1 , str2 ) ;