codeparrot/xlcost-text-to-code huggingface dataset:
Maximum possible difference of two subsets of an array | function for maximum subset diff ; if frequency of any element is two make both equal to zero ; Driver program
function maxDiff ( arr , n ) { let SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { let isSingleOccurance = true ; for ( let j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } let arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;
Count of all possible Paths in a Tree such that Node X does not appear before Node Y | Maximum number of nodes ; Vector to store the tree ; Function to perform DFS Traversal ; Mark the node as visited ; Initialize the subtree size of each node as 1 ; If the node is same as A ; Mark check_subtree [ node ] as true ; Otherwise ; Iterate over the adjacent nodes ; If the adjacent node is not visited ; Update the size of the subtree of current node ; Check if the subtree of current node contains node A ; Return size of subtree of node ; Function to add edges to the tree ; Function to calculate the number of possible paths ; Stores the size of subtree of each node ; Stores which nodes are visited ; Stores if the subtree of a node contains node A ; DFS Call ; Stores the difference between total number of nodes and subtree size of an immediate child of Y lies between the path from A to B ; Iterate over the adjacent nodes B ; If the node is in the path from A to B ; Calculate the difference ; Return the final answer ; Driver Code ; Insert Edges
let NN = 3e5 ; let G = new Array ( NN + 1 ) ; function dfs ( node , A , subtree_size , visited , check_subtree ) { visited [ node ] = 1 ; subtree_size [ node ] = 1 ; if ( node == A ) { check_subtree [ node ] = 1 ; } else check_subtree [ node ] = 0 ; for ( let v = 0 ; v < G [ node ] . length ; v ++ ) { if ( visited [ G [ node ] [ v ] ] == 0 ) { subtree_size [ node ] += dfs ( G [ node ] [ v ] , A , subtree_size , visited , check_subtree ) ; check_subtree [ node ] = check_subtree [ node ] | check_subtree [ G [ node ] [ v ] ] ; } } return subtree_size [ node ] ; } function addedge ( node1 , node2 ) { G [ node1 ] . push ( node2 ) ; G [ node2 ] . push ( node1 ) ; } function numberOfPairs ( N , B , A ) { let subtree_size = new Array ( N + 1 ) ; subtree_size . fill ( 0 ) ; let visited = new Array ( N + 1 ) ; visited . fill ( 0 ) ; let check_subtree = new Array ( N + 1 ) ; check_subtree . fill ( 0 ) ; dfs ( B , A , subtree_size , visited , check_subtree ) ; let difference = 0 ; for ( let v = 0 ; v < G [ B ] . length ; v ++ ) { if ( check_subtree [ G [ B ] [ v ] ] > 0 ) { difference = N - subtree_size [ G [ B ] [ v ] ] ; break ; } } return ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ; } let N = 9 ; let X = 5 , Y = 3 ; for ( let i = 0 ; i < G . length ; i ++ ) G [ i ] = [ ] ; addedge ( 0 , 2 ) ; addedge ( 1 , 2 ) ; addedge ( 2 , 3 ) ; addedge ( 3 , 4 ) ; addedge ( 4 , 6 ) ; addedge ( 4 , 5 ) ; addedge ( 5 , 7 ) ; addedge ( 5 , 8 ) ; document . write ( numberOfPairs ( N , Y , X ) ) ;
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
function equalXORandOR ( n ) { var m = 0 ; var MSB = parseInt ( Math . log ( n ) ) ; for ( i = 0 ; i <= MSB ; i ++ ) { if ( ( n & ( 1 << i ) ) <= 0 ) { m += ( 1 << i ) ; } } return m ; } var n = 14 ; document . write ( equalXORandOR ( n ) ) ;
Maximum possible difference of two subsets of an array | function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; driver program
function maxDiff ( arr , n ) { var result = 0 ; arr . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return result ; } var arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] ; var n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;
Count of Palindromic Strings possible by swapping of a pair of Characters |  ; Function to return the count of possible palindromic strings ; Stores the frequencies of each character ; Stores the length of the string ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
/ *	JavaScript program to implement the above approach function findNewString ( s ) { var ans = 0 ; var freq = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { ans += freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ; freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] += 1 ; } return ans ; } var s = " " ; document . write ( findNewString ( s ) ) ;
Maximum possible difference of two subsets of an array | Function for maximum subset diff ; Construct hash for positive elements ; Calculate subset sum for positive elements ; Construct hash for negative elements ; Calculate subset sum for negative elements ; Driver code
function maxDiff ( arr , n ) { let hashPositive = new Map ( ) ; let hashNegative = new Map ( ) ; let SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . has ( arr [ i ] ) ) { hashPositive . set ( arr [ i ] , hashPositive . get ( arr [ i ] ) + 1 ) ; } else { hashPositive . set ( arr [ i ] , 1 ) ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . has ( arr [ i ] ) ) { if ( hashPositive . get ( arr [ i ] ) == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . has ( Math . abs ( arr [ i ] ) ) ) { hashNegative . set ( Math . abs ( arr [ i ] ) , hashNegative . get ( Math . abs ( arr [ i ] ) ) + 1 ) ; } else { hashNegative . set ( Math . abs ( arr [ i ] ) , 1 ) ; } } } for ( let i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . has ( Math . abs ( arr [ i ] ) ) ) { if ( hashNegative . get ( Math . abs ( arr [ i ] ) ) == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } let arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] ; let n = arr . length ; document . write ( " " + maxDiff ( arr , n ) ) ;
Change the given string according to the given conditions | Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Given string str ; Function Call
function proofreadSpell ( str ) { var result = [ ] ; str . split ( ' ' ) . forEach ( c => { result . push ( c ) ; var n = result . length ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . pop ( ) ; } } n = result . length ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . pop ( ) ; } } } ) ; var resultStr = " " ; result . forEach ( c => { resultStr += c ; } ) ; return resultStr ; } var str = " " ; document . write ( proofreadSpell ( str ) ) ;
Smallest subarray with k distinct numbers | Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code
function minRange ( arr , n , k ) { let l = 0 , r = n ; for ( let i = 0 ; i < n ; i ++ ) { let s = new Set ( ) ; let j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) document . write ( " " ) ; else document . write ( l + " " + r ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; let k = 3 ; minRange ( arr , n , k ) ;
Smallest subarray with k distinct numbers | Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; Increment right side . ; If number of distinct elements less than k . ; If distinct elements are equal to k and length is less than previous length . ; If number of distinct elements less than k , then break . ; If distinct elements equals to k then try to increment left side . ; Increment left side . ; It is same as explained in above loop . ; Driver code
function minRange ( arr , n , k ) { let l = 0 , r = n ; let j = - 1 ; let hm = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size < k ) { if ( hm . has ( arr [ j ] ) ) hm . set ( arr [ j ] , hm . get ( arr [ j ] ) + 1 ) ; else hm . set ( arr [ j ] , 1 ) ; } if ( hm . size == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size < k ) break ; while ( hm . size == k ) { if ( hm . has ( arr [ i ] ) && hm . get ( arr [ i ] ) == 1 ) hm . delete ( arr [ i ] ) ; else if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) - 1 ) ; i ++ ; if ( hm . size == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . has ( arr [ i ] ) && hm . get ( arr [ i ] ) == 1 ) hm . delete ( arr [ i ] ) ; else if ( hm . has ( arr [ i ] ) ) hm . set ( arr [ i ] , hm . get ( arr [ i ] ) - 1 ) ; } if ( l == 0 && r == n ) document . write ( " " ) ; else document . write ( l + " " + r ) ; } let arr = [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 ] ; let n = arr . length ; let k = 3 ; minRange ( arr , n , k ) ;
Sum of f ( a [ i ] , a [ j ] ) over all pairs in an array of n integers | Function to calculate the sum ; map to keep a count of occurrences ; Traverse in the list from start to end number of times a [ i ] can be in a pair and to get the difference we subtract pre_sum . ; if the ( a [ i ] - 1 ) is present then subtract that value as f ( a [ i ] , a [ i ] - 1 ) = 0 ; if the ( a [ i ] + 1 ) is present then add that value as f ( a [ i ] , a [ i ] - 1 ) = 0 here we add as a [ i ] - ( a [ i ] - 1 ) < 0 which would have been added as negative sum , so we add to remove this pair from the sum value ; keeping a counter for every element ; Driver code
function sum ( a , n ) { var cnt = new Map ( ) ; var ans = 0 , pre_sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . has ( a [ i ] - 1 ) ) ans -= cnt . get ( a [ i ] - 1 ) ; if ( cnt . has ( a [ i ] + 1 ) ) ans += cnt . get ( a [ i ] + 1 ) ; if ( cnt . has ( a [ i ] ) ) cnt . set ( a [ i ] , cnt . get ( a [ i ] ) + 1 ) else cnt . set ( a [ i ] , 1 ) } return ans ; } var a = [ 1 , 2 , 3 , 1 , 3 ] ; var n = a . length ; document . write ( sum ( a , n ) ) ;
Minimum distance between the maximum and minimum element of a given Array | Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code
function minDistance ( a , n ) { let max = - 1 , min = Number . MAX_VALUE ; let min_index = - 1 , max_index = - 1 ; let min_dist = n + 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . min ( min_dist , Math . abs ( min_index - max_index ) ) ; } return min_dist ; } let n = 12 ; let a = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 ] ; document . write ( minDistance ( a , n ) ) ;
Kth diagonal from the top left of a given matrix | Function returns required diagonal ; Initialize values to print upper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonal ; Print its contents ; Driver Code
function printDiagonal ( K , N , M ) { let startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { document . write ( M [ startrow ] [ startcol ] + " " ) ; } } let N = 3 , K = 4 ; let M = [ [ 4 , 7 , 8 ] , [ 9 , 2 , 3 ] , [ 0 , 4 , 1 ] ] ; printDiagonal ( K , N , M ) ;
Find parent of given node in a Binary Tree with given postorder traversal | Function to find the parent of the given node ; Condition to check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Finding the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver Code
function findParent ( height , node ) { let start = 1 ; let end = Math . pow ( 2 , height ) - 1 ; if ( end == node ) return - 1 ; while ( node >= 1 ) { end = end - 1 ; let mid = start + parseInt ( ( end - start ) / 2 , 10 ) ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } return - 1 ; } let height = 4 ; let node = 6 ; let k = findParent ( height , node ) ; document . write ( k ) ;
Replace every element with the smallest of the others | Javascript code for the above approach . ; There should be atleast two elements ; If current element is smaller than firstSmallest then update both firstSmallest and secondSmallest ; If arr [ i ] is in between firstSmallest and secondSmallest then update secondSmallest ; Replace every element by smallest of all other elements ; Print the modified array . ; Driver code
function ReplaceElements ( arr , n ) { if ( n < 2 ) { document . write ( " " ) ; return ; } let firstSmallest = Number . MAX_VALUE ; let secondSmallest = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < firstSmallest ) { secondSmallest = firstSmallest ; firstSmallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest && arr [ i ] != firstSmallest ) secondSmallest = arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != firstSmallest ) arr [ i ] = firstSmallest ; else arr [ i ] = secondSmallest ; } for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 4 , 2 , 1 , 3 ] ; let n = arr . length ; ReplaceElements ( arr , n ) ;
Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table ' um ' ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver program to test above
function countSubarrWithEqualZeroAndOne ( arr , n ) { var um = new Map ( ) ; var curr_sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; if ( um . has ( curr_sum ) ) um . set ( curr_sum , um . get ( curr_sum ) + 1 ) ; else um . set ( curr_sum , 1 ) } var count = 0 ; um . forEach ( ( value , key ) => { if ( value > 1 ) count += ( ( value * ( value - 1 ) ) / 2 ) ; } ) ; if ( um . has ( 0 ) ) count += um . get ( 0 ) ; return count ; } var arr = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ; var n = arr . length ; document . write ( " " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ;
Longest subarray having count of 1 s one more than count of 0 s | function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider '0' as ' - 1' ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver program to test above
function lenOfLongSubarr ( arr , n ) { var um = new Map ( ) ; var sum = 0 , maxLen = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . has ( sum ) ) um . set ( sum , i ) ; if ( um . has ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } var arr = [ 0 , 1 , 1 , 0 , 0 , 1 ] ; var n = arr . length ; document . write ( " " + lenOfLongSubarr ( arr , n ) ) ;
Maximum sum subarray having sum less than or equal to given sum using Set | Function to maximum required sum < K ; Hash to lookup for value ( cum_sum - K ) ; Getting cummulative sum from [ 0 to i ] ; Lookup for upperbound of ( cSum - K ) in hash ; Check if upper_bound of ( cSum - K ) exists then update max sum ; Insert cummulative value in hash ; Return maximum sum lesser than K ; Initialise the array ; Initialise the value of K ; Size of array
function maxSubarraySum ( arr , N , K ) { let cum_set = new Set ( ) ; cum_set . add ( 0 ) ; let max_sum = Number . MIN_SAFE_INTEGER ; let cSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; let al = [ ] ; let end = 0 ; for ( let it of cum_set ) { end = it ; al . push ( it ) ; } al . sort ( ( a , b ) => a - b ) ; let sit = lower_bound ( al , cSum - K ) ; if ( sit != end ) max_sum = Math . max ( max_sum , cSum - sit ) ; cum_set . add ( cSum ) ; } return max_sum ; } let lower_bound = ( al , x ) => al . filter ( ( item ) => item > x ) [ 0 ] let arr = [ 5 , - 2 , 6 , 3 , - 5 ] ; let K = 15 ; let N = arr . length ; document . write ( maxSubarraySum ( arr , N , K ) ) ;
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
function printAllAPTriplets ( arr , n ) { const s = new Set ( ) for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let diff = arr [ j ] - arr [ i ] ; if ( s . has ( arr [ i ] - diff ) ) document . write ( arr [ i ] - diff + " " + arr [ i ] + " " + arr [ j ] + " " ) ; } s . add ( arr [ i ] ) ; } } let arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] ; let n = arr . length ; printAllAPTriplets ( arr , n ) ;
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
function printAllAPTriplets ( arr , n ) { for ( let i = 1 ; i < n - 1 ; i ++ ) { for ( let j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { document . write ( arr [ j ] + " " + arr [ i ] + " " + arr [ k ] + " " ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } let arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] ; let n = arr . length ; printAllAPTriplets ( arr , n ) ;
All unique triplets that sum up to a given value | Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call
function findTriplets ( a , n , sum ) { let i ; a . sort ( function ( a , b ) { return a - b } ) ; let flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { let start = i + 1 ; let end = n - 1 ; let target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { document . write ( " " + a [ i ] + " " + a [ start ] + " " + a [ end ] + " " ) ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { document . write ( " " ) ; } } let a = [ 12 , 3 , 6 , 1 , 6 , 9 ] ; let n = a . length ; let sum = 24 ; a . sort ( ) ; findTriplets ( a , n , sum ) ;
Find the position of the given row in a 2 | Javascript implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code
let m = 6 , n = 4 ; function linearCheck ( ar , arr ) { for ( let i = 0 ; i < m ; i ++ ) { let matched = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } let mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; let row = [ 10 , 9 , 22 , 23 ] ; document . write ( linearCheck ( mat , row ) ) ;
Count number of triplets with product equal to given number |  ; Consider all triplets and count if their product is equal to m ; Driver method
/ *function countTriplets(arr,n,m) { let count = 0 ; for ( let i = 0 ; i < n - 2 ; i ++ ) for ( let j = i + 1 ; j < n - 1 ; j ++ ) for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) count ++ ; return count ; } let arr = [ 1 , 4 , 6 , 2 , 3 , 8 ] ; let m = 24 ; document . write ( countTriplets ( arr , arr . length , m ) ) ;
Count of index pairs with equal elements in an array | Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driver code
function countPairs ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } let arr = [ 1 , 1 , 2 ] ; let n = arr . length ; document . write ( countPairs ( arr , n ) ) ;