codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given array contains duplicate elements within k distance from each other | Javascript program to Check if a given array contains duplicate elements within k distance from each other ; Creates an empty hashset ; Traverse the input array ; If already present n hash , then we found a duplicate within k distance ; Add this item to hashset ; Remove the k + 1 distant item ; Driver method to test above method
function checkDuplicatesWithinK ( arr , n , k ) { let myset = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr . includes ( arr [ i ] ) ) { return true ; } myset . add ( arr [ i ] ) ; if ( i >= k ) { index = array . indexOf ( arr [ i - k ] ) ; array . splice ( index , 1 ) ; } } return false ; } let arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] ; let n = arr . length ; if ( checkDuplicatesWithinK ( arr , n , 3 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Most frequent element in an array | JavaScript program to find the most frequent element in an array ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver Code
function mostFrequent ( arr , n ) { arr . sort ( ) ; let max_count = 1 , res = arr [ 0 ] ; let curr_count = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } let arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] ; let n = arr . length ; document . write ( mostFrequent ( arr , n ) ) ;
Most frequent element in an array | Javascript program to find the most frequent element in an array . ; Insert all elements in hash . ; find the max frequency ; driver program
function mostFrequent ( arr , n ) { var hash = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( hash . has ( arr [ i ] ) ) hash . set ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) else hash . set ( arr [ i ] , 1 ) } var max_count = 0 , res = - 1 ; hash . forEach ( ( value , key ) => { if ( max_count < value ) { res = key ; max_count = value ; } } ) ; return res ; } var arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] ; var n = arr . length ; document . write ( mostFrequent ( arr , n ) ) ;
Smallest subarray with all occurrences of a most frequent element | JavaScript implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver program
function smallestSubsegment ( a , n ) { let left = new Map ( ) ; let count = new Map ( ) ; let mx = 0 ; let mn = - 1 , strindex = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ; if ( count . get ( x ) == null ) { left . set ( x , i ) ; count . set ( x , 1 ) ; } else count . set ( x , count . get ( x ) + 1 ) ; if ( count . get ( x ) > mx ) { mx = count . get ( x ) ; mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } else if ( ( count . get ( x ) == mx ) && ( i - left . get ( x ) + 1 < mn ) ) { mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } } for ( let i = strindex ; i < strindex + mn ; i ++ ) document . write ( a [ i ] + " " ) ; } let A = [ 1 , 2 , 2 , 2 , 1 ] ; let n = A . length ; smallestSubsegment ( A , n ) ;
Given an array of pairs , find all symmetric pairs in it | Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; Look for second element of this pair in hash ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver method
function findSymPairs ( arr ) { let hM = new Map ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { let first = arr [ i ] [ 0 ] ; let sec = arr [ i ] [ 1 ] ; let val = hM . get ( sec ) ; if ( val != null && val == first ) document . write ( " " + sec + " " + first + " " ) ; else hM . set ( first , sec ) ; } } let arr = new Array ( 5 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { arr [ i ] [ j ] = 0 ; } } arr [ 0 ] [ 0 ] = 11 ; arr [ 0 ] [ 1 ] = 20 ; arr [ 1 ] [ 0 ] = 30 ; arr [ 1 ] [ 1 ] = 40 ; arr [ 2 ] [ 0 ] = 5 ; arr [ 2 ] [ 1 ] = 10 ; arr [ 3 ] [ 0 ] = 40 ; arr [ 3 ] [ 1 ] = 30 ; arr [ 4 ] [ 0 ] = 10 ; arr [ 4 ] [ 1 ] = 5 ; findSymPairs ( arr ) ;
Group multiple occurrence of array elements ordered by first occurrence | A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , prlet it and all subsequent occurrences ; Driver program
function groupElements ( arr , n ) { let visited = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } for ( let i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { document . write ( arr [ i ] + " " ) ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { document . write ( arr [ i ] + " " ) ; visited [ j ] = true ; } } } } } let arr = [ 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 ] ; let n = arr . length ; groupElements ( arr , n ) ;
How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver program to test above function
function aredisjoint ( set1 , set2 ) { for ( let i = 0 ; i < set1 . length ; i ++ ) { for ( let j = 0 ; j < set2 . length ; j ++ ) { if ( set1 [ i ] == set2 [ j ] ) return false ; } } return true ; } let set1 = [ 12 , 34 , 11 , 9 , 3 ] ; let set2 = [ 7 , 2 , 1 , 5 ] ; result = aredisjoint ( set1 , set2 ) ; if ( result ) document . write ( " " ) ; else document . write ( " " ) ;
How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver code
function aredisjoint ( set1 , set2 ) { let i = 0 , j = 0 ; set1 . sort ( function ( a , b ) { return a - b } ) ; set2 . sort ( function ( a , b ) { return a - b } ) ; while ( i < set1 . length && j < set2 . length ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set1 [ i ] > set2 [ j ] ) j ++ ; else return false ; } return true ; } let set1 = [ 12 , 34 , 11 , 9 , 3 ] ; let set2 = [ 7 , 2 , 1 , 5 ] ; result = aredisjoint ( set1 , set2 ) ; if ( result ) document . write ( " " ) ; else document . write ( " " ) ;
Find elements which are present in first array and not in second | Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver Code
function findMissing ( a , b , n , m ) { for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) document . write ( a [ i ] + " " ) ; } } let a = [ 1 , 2 , 6 , 3 , 4 , 5 ] ; let b = [ 2 , 4 , 3 , 1 , 0 ] ; let n = a . length ; let m = b . length ; findMissing ( a , b , n , m ) ;
Find elements which are present in first array and not in second | Function for finding elements which are there in a [ ] but not in b [ ] . ; Store all elements of second array in a hash table ; Print all elements of first array that are not present in hash table ; Driver code
function findMissing ( a , b , n , m ) { let s = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) s . add ( b [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) if ( ! s . has ( a [ i ] ) ) document . write ( a [ i ] + " " ) ; } let a = [ 1 , 2 , 6 , 3 , 4 , 5 ] ; let b = [ 2 , 4 , 3 , 1 , 0 ] ; let n = a . length ; let m = b . length ; findMissing ( a , b , n , m ) ;
Check if two arrays are equal or not | Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver Code
function areEqual ( arr1 , arr2 ) { let n = arr1 . length ; let m = arr2 . length ; if ( n != m ) return false ; arr1 . sort ( ) ; arr2 . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } let arr1 = [ 3 , 5 , 2 , 5 , 2 ] ; let arr2 = [ 2 , 3 , 5 , 5 , 2 ] ; if ( areEqual ( arr1 , arr2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Pair with given sum and maximum shortest distance from end | JavaScript code to find maximum shortest distance from endpoints ; Store counts of all elements . ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; Driver Code
function makePermutation ( a , n ) { let count = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( count . has ( a [ i ] ) ) { count . set ( a [ i ] , count . get ( a [ i ] ) + 1 ) ; } else { count . set ( a [ i ] , 1 ) ; } } } function find_maximum ( a , n , k ) { let b = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ; let d = Math . min ( 1 + i , n - i ) ; if ( ! b . has ( x ) ) b . set ( x , d ) ; else { b . set ( x , Math . min ( d , b . get ( x ) ) ) ; } } let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ; if ( x != k - x && b . has ( k - x ) ) ans = Math . min ( Math . max ( b . get ( x ) , b . get ( k - x ) ) , ans ) ; } return ans ; } let a = [ 3 , 5 , 8 , 6 , 7 ] ; let K = 11 ; let n = a . length ; document . write ( find_maximum ( a , n , K ) ) ;
Pair with given product | Set 1 ( Find if any pair exists ) | Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver code
function isProduct ( arr , n , x ) { for ( var i = 0 ; i < n - 1 ; i ++ ) for ( var j = i + 1 ; i < n ; i ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } var arr = [ 10 , 20 , 9 , 40 ] ; var x = 400 ; var n = arr . length ; isProduct ( arr , n , x ) ? document . write ( " " ) : document . write ( " " ) ; x = 190 ; isProduct ( arr , n , x ) ? document . write ( " " ) : document . write ( " " ) ;
Pair with given product | Set 1 ( Find if any pair exists ) | Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element leto it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; Driver program
function isProduct ( arr , n , x ) { let hset = new Set ( ) ; if ( n < 2 ) return false ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( hset . has ( x / arr [ i ] ) ) return true ; hset . add ( arr [ i ] ) ; } } return false ; } let arr = [ 10 , 20 , 9 , 40 ] ; let x = 400 ; let n = arr . length ; if ( isProduct ( arr , arr . length , x ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; x = 190 ; if ( isProduct ( arr , arr . length , x ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Find pair with greatest product in array | Function to find greatest number ; Driver code
function findGreatest ( arr , n ) { let result = - 1 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n - 1 ; j ++ ) for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . max ( result , arr [ i ] ) ; return result ; } let arr = [ 30 , 10 , 9 , 3 , 35 ] ; let n = arr . length ; document . write ( findGreatest ( arr , n ) ) ;
Find pair with greatest product in array | Function to find greatest number ; Store occurrences of all elements in hash array ; m [ arr [ i ] ] ++ ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Driver code
function findGreatest ( arr , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m [ arr [ i ] ] + 1 ) ; } else { m . set ( arr [ i ] , m . get ( arr [ i ] ) ) ; } } arr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = n - 1 ; i > 1 ; i -- ) { for ( let j = 0 ; j < i && arr [ j ] <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { let result = Math . floor ( arr [ i ] / arr [ j ] ) ; if ( result != arr [ j ] && m [ result ] == null m [ result ] > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m [ result ] > 1 ) { return arr [ i ] ; } } } } return - 1 ; } let arr = [ 17 , 2 , 1 , 15 , 30 ] ; let n = arr . length ; document . write ( findGreatest ( arr , n ) ) ;
Remove minimum number of elements such that no common element exist in both array | To find no elements to remove so no common element exist ; To store count of array element ; Count elements of a ; Count elements of b ; Traverse through all common element , and pick minimum occurrence from two arrays ; To return count of minimum elements ; Driver program to test above function
function minRemove ( a , b , n , m ) { let countA = new Map ( ) ; let countB = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( countA . has ( a [ i ] ) ) countA . set ( a [ i ] , countA . get ( a [ i ] ) + 1 ) ; else countA . set ( a [ i ] , 1 ) ; } for ( let i = 0 ; i < m ; i ++ ) { if ( countB . has ( b [ i ] ) ) countB . set ( b [ i ] , countB . get ( b [ i ] ) + 1 ) ; else countB . set ( b [ i ] , 1 ) ; } let res = 0 ; for ( let x of countA . keys ( ) ) if ( countB . has ( x ) ) res += Math . min ( countB . get ( x ) , countA . get ( x ) ) ; return res ; } let a = [ 1 , 2 , 3 , 4 ] ; let b = [ 2 , 3 , 4 , 5 , 8 ] ; let n = a . length ; let m = b . length ; document . write ( minRemove ( a , b , n , m ) ) ;
Count items common to both the lists but with different prices | function to count items common to both the lists but with different prices ; for each item of ' list1' check if it is in ' list2' but with a different price ; required count of items ; Driver program to test above
function countItems ( list1 , m , list2 , n ) { var count = 0 ; for ( var i = 0 ; i < m ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) if ( list1 [ i ] [ 0 ] === list2 [ j ] [ 0 ] && ( list1 [ i ] [ 1 ] != list2 [ j ] [ 1 ] ) ) count ++ ; return count ; } var list1 = [ [ " " , 60 ] , [ " " , 20 ] , [ " " , 50 ] , [ " " , 30 ] ] ; var list2 = [ [ " " , 20 ] , [ " " , 15 ] , [ " " , 40 ] , [ " " , 60 ] ] ; var m = list1 . length ; var n = list2 . length ; document . write ( " " + countItems ( list1 , m , list2 , n ) ) ;
Check whether a binary tree is a full binary tree or not | Tree node structure ; this function checks if a binary tree is full or not ; if empty tree ; if leaf node ; if both left and right subtrees are not null the are full ; if none work ; Driver program
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } var root ; function isFullTree ( node ) { if ( node == null ) return true ; if ( node . left == null && node . right == null ) return true ; if ( ( node . left != null ) && ( node . right != null ) ) return ( isFullTree ( node . left ) && isFullTree ( node . right ) ) ; return false ; } root = new Node ( 10 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . right = new Node ( 40 ) ; root . left . left = new Node ( 50 ) ; root . right . left = new Node ( 60 ) ; root . left . left . left = new Node ( 80 ) ; root . right . right = new Node ( 70 ) ; root . left . left . right = new Node ( 90 ) ; root . left . right . left = new Node ( 80 ) ; root . left . right . right = new Node ( 90 ) ; root . right . left . left = new Node ( 80 ) ; root . right . left . right = new Node ( 90 ) ; root . right . right . left = new Node ( 80 ) ; root . right . right . right = new Node ( 90 ) ; if ( isFullTree ( root ) ) document . write ( " " ) ; else document . write ( " " ) ;
Change the array into a permutation of numbers from 1 to n | JavaScript program to make a permutation of numbers from 1 to n using minimum changes . ; Store counts of all elements . ; Find next missing element to put in place of current element . ; Replace with next missing and insert the missing element in hash . ; Driver Code
function makePermutation ( a , n ) { var count = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( count . has ( a [ i ] ) ) count . set ( a [ i ] , count . get ( a [ i ] ) + 1 ) else count . set ( a [ i ] , 1 ) } var next_missing = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( count . get ( a [ i ] ) != 1 a [ i ] > n a [ i ] < 1 ) { count . set ( a [ i ] , count . get ( a [ i ] ) - 1 ) ; while ( count . has ( next_missing ) ) next_missing ++ ; a [ i ] = next_missing ; count . set ( next_missing , 1 ) ; } } } var A = [ 2 , 2 , 3 , 3 ] ; var n = A . length ; makePermutation ( A , n ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ;
Count pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver function to test the above function
function getPairsCount ( arr , n , sum ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) count ++ ; return count ; } let arr = [ 1 , 5 , 7 , - 1 , 5 ] ; let n = arr . length ; let sum = 6 ; document . write ( " " + getPairsCount ( arr , n , sum ) ) ;
Analysis of Algorithms | Big | Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Given Input ; Function Call
function linearSearch ( a , n , key ) { for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } var arr = [ 2 , 3 , 4 , 10 , 40 ] ; var x = 10 ; var n = arr . length ; if ( linearSearch ( arr , n , x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; generating pairs from both the arrays ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver Code
function countPairs ( arr1 , arr2 , m , n , x ) { let count = 0 ; for ( let i = 0 ; i < m ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 2 , 3 , 5 , 8 ] ; let m = arr1 . length ; let n = arr2 . length ; let x = 10 ; document . write ( " " + countPairs ( arr1 , arr2 , m , n , x ) ) ;
Count pairs from two sorted arrays whose sum is equal to a given value x | function to search ' value ' in the given array ' arr [ ] ' it uses binary search technique as ' arr [ ] ' is sorted ; value found ; value not found ; function to count all pairs from both the sorted arrays whose sum is equal to a given value1 ; for each arr1 [ i ] ; check if the ' value ' is present in ' arr2 [ ] ' ; required count of pairs ; Driver Code
function isPresent ( arr , low , high , value ) { while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } function countPairs ( arr1 , arr2 , m , n , x ) { let count = 0 ; for ( let i = 0 ; i < m ; i ++ ) { let value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 2 , 3 , 5 , 8 ] ; let m = arr1 . length ; let n = arr2 . length ; let x = 10 ; document . write ( " " + countPairs ( arr1 , arr2 , m , n , x ) ) ;
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; insert all the elements of 1 st array in the hash table ( unordered_set ' us ' ) ; for each element of 'arr2[] ; find ( x - arr2 [ j ] ) in ' us ' ; required count of pairs ; Driver Code
function countPairs ( arr1 , arr2 , m , n , x ) { let count = 0 ; let us = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) us . add ( arr1 [ i ] ) ; for ( let j = 0 ; j < n ; j ++ ) if ( us . has ( x - arr2 [ j ] ) ) count ++ ; return count ; } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 2 , 3 , 5 , 8 ] ; let m = arr1 . length ; let n = arr2 . length ; let x = 10 ; document . write ( " " + countPairs ( arr1 , arr2 , m , n , x ) ) ;
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if this sum is equal to ' x ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if this sum is less than x , then increment l ; else decrement ' r ' ; required count of pairs ; Driver Code
function countPairs ( arr1 , arr2 , m , n , x ) { let count = 0 ; let l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) { l ++ ; r -- ; count ++ ; } else if ( ( arr1 [ l ] + arr2 [ r ] ) < x ) l ++ ; else r -- ; } return count ; } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 2 , 3 , 5 , 8 ] ; let m = arr1 . length ; let n = arr2 . length ; let x = 10 ; document . write ( " " + countPairs ( arr1 , arr2 , m , n , x ) ) ;
How to learn Pattern printing easily ? | Driver code
var N = 4 , i , j , min ; document . write ( " " + N + " " ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } document . write ( " " ) ; }
Number of subarrays having sum exactly equal to k | Javascript program for the above approach ; Calculate all subarrays ; Calculate required sum ; Check if sum is equal to required sum
let arr = [ 10 , 2 , - 2 , - 20 , 10 ] ; let k = - 10 ; let n = arr . length ; let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let sum = 0 ; for ( let j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( sum == k ) res ++ ; } } document . write ( res ) ;
Number of subarrays having sum exactly equal to k | Function to find number of subarrays with sum exactly equal to k . ; HashMap to store number of subarrays starting from index zero having particular value of sum . ; Sum of elements so far . ; Add current element to sum so far . ; If currsum is equal to desired sum , then a new subarray is found . So increase count of subarrays . ; currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount . ; Add currsum value to count of different values of sum . ; Driver Code
function findSubarraySum ( arr , n , sum ) { let prevSum = new Map ( ) ; let res = 0 ; let currsum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == sum ) res ++ ; if ( prevSum . has ( currsum - sum ) ) res += prevSum . get ( currsum - sum ) ; let count = prevSum . get ( currsum ) ; if ( count == null ) prevSum . set ( currsum , 1 ) ; else prevSum . set ( currsum , count + 1 ) ; } return res ; } let arr = [ 10 , 2 , - 2 , - 20 , 10 ] ; let sum = - 10 ; let n = arr . length ; document . write ( findSubarraySum ( arr , n , sum ) ) ;