codeparrot/xlcost-text-to-code huggingface dataset:
Find missing element in a sorted array of consecutive numbers | Function to return the missing element ; Check if middle element is consistent ; No inconsistency till middle elements When missing element is just after the middle element ; Move right ; Inconsistency found When missing element is just before the middle element ; Move left ; No missing element found ; Driver code
function findMissing ( arr , n ) { let l = 0 , h = n - 1 ; let mid ; while ( h > l ) { mid = l + Math . floor ( ( h - l ) / 2 ) ; if ( arr [ mid ] - mid == arr [ 0 ] ) { if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) return arr [ mid ] + 1 ; else { l = mid + 1 ; } } else { if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) return arr [ mid ] - 1 ; else { h = mid - 1 ; } } } return - 1 ; } let arr = [ - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 ] ; let n = arr . length ; document . write ( findMissing ( arr , n ) ) ;
Find maximum sum taking every Kth element in the array | Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Find maximum from all sequences ; Sum of the sequence starting from index i ; Update maximum ; Driver code
function maxSum ( arr , n , K ) { var maximum = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { var sumk = 0 ; for ( var j = i ; j < n ; j += K ) sumk = sumk + arr [ j ] ; maximum = Math . max ( maximum , sumk ) ; } return maximum ; } var arr = [ 3 , 6 , 4 , 7 , 2 ] ; var n = arr . length ; var K = 2 ; document . write ( maxSum ( arr , n , K ) ) ;
Find the number of elements greater than k in a sorted array | Function to return the count of elements from the array which are greater than k ; Stores the index of the left most element from the array which is greater than k ; Finds number of elements greater than k ; If mid element is greater than k update leftGreater and r ; If mid element is less than or equal to k update l ; Return the count of elements greater than k ; Driver code
function countGreater ( arr , n , k ) { var l = 0 ; var r = n - 1 ; var leftGreater = n ; while ( l <= r ) { var m = l + parseInt ( ( r - l ) / 2 ) ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } var arr = [ 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 ] ; var n = arr . length ; var k = 7 ; document . write ( countGreater ( arr , n , k ) ) ;
Count the number of operations required to reduce the given number | Javascript implementation of the approach ; To store the normalized value of all the operations ; Minimum possible value for a series of operations ; If k can be reduced with first ( i + 1 ) operations ; Impossible to reduce k ; Number of times all the operations can be performed on k without reducing it to <= 0 ; Perform operations ; Final check ; Driver code
function operations ( op , n , k ) { let i , count = 0 ; let nVal = 0 ; let min = Number . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { nVal += op [ i ] ; min = Math . min ( min , nVal ) ; if ( ( k + nVal ) <= 0 ) return ( i + 1 ) ; } if ( nVal >= 0 ) return - 1 ; let times = Math . floor ( ( k - Math . abs ( min ) ) / Math . abs ( nVal ) ) ; k = ( k - ( times * Math . abs ( nVal ) ) ) ; count = ( times * n ) ; while ( k > 0 ) { for ( i = 0 ; i < n ; i ++ ) { k = k + op [ i ] ; count ++ ; if ( k <= 0 ) break ; } } return count ; } let op = [ - 60 , 65 , - 1 , 14 , - 25 ] ; let n = op . length ; let k = 100000 ; document . write ( operations ( op , n , k ) ) ;
Uniform Binary Search | Javascript implementation of above approach ; lookup table ; Create the lookup table for an array of length n ; Power and count variable ; Multiply by 2 ; Initialize the lookup table ; Binary search ; mid point of the array ; count ; If the value is found ; If value is less than the mid value ; If value is greater than the mid value ; Driver code ; Create the lookup table ; Print the position of the array
let MAX_SIZE = 1000 ; let lookup_table = new Array ( MAX_SIZE ) ; lookup_table . fill ( 0 ) ; function create_table ( n ) { let pow = 1 ; let co = 0 ; while ( true ) { pow <<= 1 ; lookup_table [ co ] = parseInt ( ( n + ( pow >> 1 ) ) / pow , 10 ) ; if ( lookup_table [ co ++ ] == 0 ) { break ; } } } function binary ( arr , v ) { let index = lookup_table [ 0 ] - 1 ; let co = 0 ; while ( lookup_table [ co ] != 0 ) { if ( v == arr [ index ] ) return index ; else if ( v < arr [ index ] ) { index -= lookup_table [ ++ co ] ; return index ; } else { index += lookup_table [ ++ co ] ; return index ; } } return index ; } let arr = [ 1 , 3 , 5 , 6 , 7 , 8 , 9 ] ; let n = arr . length ; create_table ( n ) ; document . write ( " " + binary ( arr , 3 ) ) ;
Find the smallest number X such that X ! contains at least Y trailing zeros . | Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code
function countFactor ( P , X ) { if ( X < P ) return 0 ; return ( parseInt ( X / P ) + countFactor ( P , parseInt ( X / P ) ) ) ; } function findSmallestX ( Y ) { let low = 0 , high = 5 * Y ; let N = 0 ; while ( low <= high ) { let mid = parseInt ( ( high + low ) / 2 ) ; if ( countFactor ( 5 , mid ) < Y ) { low = mid + 1 ; } else { N = mid ; high = mid - 1 ; } } return N ; } let Y = 10 ; document . write ( findSmallestX ( Y ) ) ;
Find maximum N such that the sum of square of first N natural numbers is not more than X | Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Driver Code
function squareSum ( N ) { var sum = parseInt ( ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ) ; return sum ; } function findMaxN ( X ) { var low = 1 , high = 100000 ; var N = 0 ; while ( low <= high ) { var mid = ( high + low ) / 2 ; if ( squareSum ( mid ) <= X ) { N = parseInt ( mid ) ; low = mid + 1 ; } else high = mid - 1 ; } return N ; } var X = 25 ; document . write ( findMaxN ( X ) ) ;
Search an element in given N ranges | Function to return the index of the range in which K lies and uses linear search ; Iterate and find the element ; If K lies in the current range ; K doesn 't lie in any of the given ranges ; Driver code
function findNumber ( a , n , K ) { for ( var i = 0 ; i < n ; i ++ ) { if ( K >= a [ i ] [ 0 ] && K <= a [ i ] [ 1 ] ) return i ; } return - 1 ; } var a = [ [ 1 , 3 ] , [ 4 , 7 ] , [ 8 , 11 ] ] ; var n = a . length ; var k = 6 ; var index = findNumber ( a , n , k ) ; if ( index != - 1 ) document . write ( index ) ; else document . write ( - 1 ) ;
Search an element in given N ranges | JavaScript implementation of the approach ; Function to return the index of the range in which K lies and uses binary search ; Binary search ; Find the mid element ; If element is found ; Check in first half ; Check in second half ; Not found ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function findNumber ( a , n , K ) { var low = 0 , high = n - 1 ; while ( low <= high ) { var mid = ( low + high ) >> 1 ; if ( K >= a [ mid ] . first && K <= a [ mid ] . second ) return mid ; else if ( K < a [ mid ] . first ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } var a = [ new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) ] ; var n = a . length ; var k = 6 ; var index = findNumber ( a , n , k ) ; if ( index != - 1 ) document . write ( index ) ; else document . write ( - 1 ) ;
Two equal sum segment range queries | Function to find the required prefix sum ; Function to hash all the values of prefix sum array in an unordered map ; Function to check if a range can be divided into two equal parts ; To store the value of sum of entire range ; If value of sum is odd ; To store p_arr [ l - 1 ] ; If the value exists in the map ; Driver code ; prefix - sum array ; Map to store the values of prefix - sum ; Perform queries
function prefixSum ( p_arr , arr , n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } function hashPrefixSum ( p_arr , n , q ) { for ( var i = 0 ; i < n ; i ++ ) q . add ( p_arr [ i ] ) ; } function canDivide ( p_arr , n , q , l , r ) { var sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { document . write ( " " ) ; return ; } var beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . has ( ( beg + sum / 2 ) ) ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 1 , 2 , 3 ] ; var n = arr . length ; var p_arr = Array ( n ) ; prefixSum ( p_arr , arr , n ) ; var q = new Set ( ) ; hashPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ;
Cost to make a string Panagram | Set 2 | Function to return the cost to make str a Panagram ; Count the occurrences of each lowercase character ; To store the total gain ; If some character is missing , it has to be added at twice the cost ; If some character appears more than once , all of its occurrences except 1 can be traded for some gain ; If gain is more than the cost ; Return the total cost if gain < 0 ; Driver code
function costToPanagram ( str , cost ) { var i , n = str . length ; var occurrences = Array ( 26 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) occurrences [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var gain = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( occurrences [ i ] == 0 ) gain -= ( 2 * cost [ i ] ) ; else if ( occurrences [ i ] > 1 ) gain += ( cost [ i ] * ( occurrences [ i ] - 1 ) ) ; } if ( gain >= 0 ) return 0 ; return ( gain * - 1 ) ; } var cost = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ; var str = " " ; document . write ( costToPanagram ( str , cost ) ) ;
Weighted sum of the characters of a string in an array | Set 2 | Function to return the required string score ; create a hash map of strings in str ; Store every string in the map along with its position in the array ; If given string is not present in str [ ] ; Multiply sum of alphabets with position ; Driver Code
function strScore ( str , s , n ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) m . set ( str [ i ] , i + 1 ) ; if ( ! m . has ( s ) ) return 0 ; let score = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) score += s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ; score = score * m . get ( s ) ; return score ; } let str = [ " " , " " , " " ] ; let s = " " ; let n = str . length ; document . write ( strScore ( str , s , n ) ) ;
Number of subarrays have bitwise OR >= K | Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code
function countSubArrays ( arr , n , K ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let bitwise_or = 0 ; for ( let k = i ; k <= j ; k ++ ) { bitwise_or = bitwise_or | arr [ k ] ; } if ( bitwise_or >= K ) count ++ ; } } return count ; } let arr = [ 3 , 4 , 5 ] ; let n = arr . length ; let k = 6 ; document . write ( countSubArrays ( arr , n , k ) ) ;
Number of subarrays have bitwise OR >= K | Javascript implementation of the approach ; Function to build the segment tree ; Function to return the bitwise OR of segment [ L . . R ] ; Function to return the count of required sub - arrays ; Build segment tree ; Query segment tree for bitwise OR of sub - array [ i . . j ] ; Driver code
let N = 100002 ; let tree = new Array ( 4 * N ) ; function build ( arr , node , start , end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } let mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } function query ( node , start , end , l , r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } let mid = ( start + end ) >> 1 ; let q1 = query ( 2 * node , start , mid , l , r ) ; let q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } function countSubArrays ( arr , n , K ) { build ( arr , 1 , 0 , n - 1 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let bitwise_or = query ( 1 , 0 , n - 1 , i , j ) ; if ( bitwise_or >= K ) count ++ ; } } return count ; } let arr = [ 3 , 4 , 5 ] ; let n = arr . length ; let k = 6 ; document . write ( countSubArrays ( arr , n , k ) ) ;
Number of subarrays have bitwise OR >= K | JavaScript implementation of the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree .
let N = 100002 ; let tree = new Array ( 4 * N ) ; function build ( arr , node , start , end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } let mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } function query ( node , start , end , l , r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } let mid = ( start + end ) >> 1 ; let q1 = query ( 2 * node , start , mid , l , r ) ; let q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } function countSubArrays ( arr , n , K ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let low = i , high = n - 1 , index = Number . MAX_VALUE ; while ( low <= high ) { let mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = Math . min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != Number . MAX_VALUE ) { count += n - index ; } } return count ; } let arr = [ 3 , 4 , 5 ] ; let n = arr . length ; build ( arr , 1 , 0 , n - 1 ) ; let k = 6 ; document . write ( countSubArrays ( arr , n , k ) ) ;
Occurrences of a pattern in binary representation of a number | Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of pattern_int and all_ones ; If the pattern occurs in the last digits of n ; Right shift n by 1 bit ; Driver Code
function countPattern ( n , pat ) { let pattern_int = 0 ; let power_two = 1 ; let all_ones = 0 ; for ( let i = pat . length - 1 ; i >= 0 ; i -- ) { let current_bit = pat . charAt ( i ) - ' ' ; pattern_int += ( power_two * current_bit ) ; all_ones = all_ones + power_two ; power_two = power_two * 2 ; } let count = 0 ; while ( n != 0 && n >= pattern_int ) { if ( ( n & all_ones ) == pattern_int ) { count ++ ; } n = n >> 1 ; } return count ; } let n = 500 ; let pat = " " ; document . write ( countPattern ( n , pat ) ) ;
Remove exactly one element from the array such that max | function to calculate max - min ; Driver code
function max_min ( a , n ) { a . sort ( ) ; return Math . min ( a [ n - 2 ] - a [ 0 ] , a [ n - 1 ] - a [ 1 ] ) ; } let a = [ 1 , 3 , 3 , 7 ] ; let n = a . length ; document . write ( max_min ( a , n ) ) ;
Count numbers < = N whose difference with the count of primes upto them is > = K | Javascript implementation of the above approach ; primeUpto [ i ] denotes count of prime numbers upto i ; Function to compute all prime numbers and update primeUpto array ; 0 and 1 are not primes ; If i is prime ; Set all multiples of i as non - prime ; Compute primeUpto array ; Function to return the count of valid numbers ; Compute primeUpto array ; Check if the number is valid , try to reduce it ; ans is the minimum valid number ; Driver Code
var MAX = 1000001 ; var primeUpto = Array ( MAX ) . fill ( 0 ) ; function SieveOfEratosthenes ( ) { var isPrime = Array ( MAX ) . fill ( 1 ) ; isPrime [ 0 ] = isPrime [ 1 ] = 0 ; for ( var i = 2 ; i * i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( var j = i * 2 ; j < MAX ; j += i ) isPrime [ j ] = 0 ; } } for ( var i = 1 ; i < MAX ; i ++ ) { primeUpto [ i ] = primeUpto [ i - 1 ] ; if ( isPrime [ i ] ) primeUpto [ i ] ++ ; } } function countOfNumbers ( N , K ) { SieveOfEratosthenes ( ) ; var low = 1 , high = N , ans = 0 ; while ( low <= high ) { var mid = ( low + high ) >> 1 ; if ( mid - primeUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ( ans ? N - ans + 1 : 0 ) ; } var N = 10 , K = 3 ; document . write ( countOfNumbers ( N , K ) ) ;
Find the element whose multiplication with | Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
function minIndex ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return - 1 ; } var arr = [ 1 , 3 , - 5 , 3 , 4 ] ; var n = arr . length ; document . write ( minIndex ( arr , n ) ) ;
Floor of every element in same array | Prints greater elements on left side of every element ; Create a sorted copy of arr [ ] ; Traverse through arr [ ] and do binary search for every element . ; Floor of first element is - 1 if there is only one occurrence of it . ; Find the first element that is greater than or or equal to given element ; If next element is also same , then there are multiple occurrences , so print it ; Otherwise print previous element ; Driver program to test insertion sort
function printPrevGreater ( arr , n ) { let v = [ ... arr ] v . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == v [ 0 ] ) { ( arr [ i ] == v [ 1 ] ) ? document . write ( arr [ i ] ) : document . write ( - 1 ) ; document . write ( " " ) ; continue ; } if ( v . includes ( arr [ i ] ) ) it = v [ v . indexOf ( arr [ i ] ) ] else it = v [ n - 1 ] if ( it != v [ n - 1 ] && ( v [ v . indexOf ( it ) + 1 ] == arr [ i ] ) ) document . write ( arr [ i ] + " " ) ; else document . write ( v [ v . indexOf ( it ) - 1 ] + " " ) ; } } function lower_bound ( arr , val ) { } let arr = [ 6 , 11 , 7 , 8 , 20 , 12 ] ; let n = arr . length ; printPrevGreater ( arr , n ) ;
Find smallest and largest element from square matrix diagonals | Function to find smallest and largest element from principal and secondary diagonal ; take length of matrix ; declare and initialism variables with appropriate value ; take new smallest value ; take new largest value ; Condition for secondary diagonal is mat [ n - 1 - i ] [ i ] take new smallest value ; take new largest value ; Declare and initialize 5 X5 matrix
function diagonalsMinMax ( mat ) { let n = mat . length ; if ( n == 0 ) return ; let principalMin = mat [ 0 ] [ 0 ] , principalMax = mat [ 0 ] [ 0 ] ; let secondaryMin = mat [ n - 1 ] [ 0 ] , secondaryMax = mat [ n - 1 ] [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( mat [ i ] [ i ] < principalMin ) { principalMin = mat [ i ] [ i ] ; } if ( mat [ i ] [ i ] > principalMax ) { principalMax = mat [ i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] < secondaryMin ) { secondaryMin = mat [ n - 1 - i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] > secondaryMax ) { secondaryMax = mat [ n - 1 - i ] [ i ] ; } } document . write ( " " + principalMin + " " ) ; document . write ( " " + principalMax + " " ) ; document . write ( " " + secondaryMin + " " ) ; document . write ( " " + secondaryMax + " " ) ; } let matrix = [ [ 1 , 2 , 3 , 4 , - 10 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 1 , 2 , 11 , 3 , 4 ] , [ 5 , 6 , 70 , 5 , 8 ] , [ 4 , 9 , 7 , 1 , - 5 ] ] ; diagonalsMinMax ( matrix ) ;
Check if array can be sorted with one swap | A linear Javascript program to check if array becomes sorted after one swap ; Find counts and positions of elements that are out of order . ; If there are more than two elements are out of order . ; If all elements are sorted already ; Cases like { 1 , 5 , 3 , 4 , 2 } We swap 5 and 2. ; Cases like { 1 , 2 , 4 , 3 , 5 } ; Now check if array becomes sorted for cases like { 4 , 1 , 2 , 3 } ; Driver Program to test above function
function checkSorted ( n , arr ) { var first = 0 , second = 0 ; var count = 0 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { count ++ ; if ( first == 0 ) first = i ; else second = i ; } } if ( count > 2 ) return false ; if ( count == 0 ) return true ; if ( count == 2 ) [ arr [ first - 1 ] , arr [ second ] ] = [ arr [ second ] , arr [ first - 1 ] ] ; else if ( count == 1 ) [ arr [ first - 1 ] , arr [ first ] ] = [ arr [ first ] , arr [ first - 1 ] ] ; for ( var i = 1 ; i < n ; i ++ ) if ( arr [ i ] < arr [ i - 1 ] ) return false ; return true ; } var arr = [ 1 , 4 , 3 , 2 ] ; var n = arr . length ; if ( checkSorted ( n , arr ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the intersection of two Matrices | Javascript program to find intersection of two matrices ; Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code
var N = 4 var M = 4 function printIntersection ( A , B ) { for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] == B [ i ] [ j ] ) document . write ( A [ i ] [ j ] + " " ) ; else document . write ( " " ) ; } document . write ( " " ) ; } } var A = [ [ 2 , 4 , 6 , 8 ] , [ 1 , 3 , 5 , 7 ] , [ 8 , 6 , 4 , 2 ] , [ 7 , 5 , 3 , 1 ] ] ; var B = [ [ 2 , 3 , 6 , 8 ] , [ 1 , 3 , 5 , 2 ] , [ 8 , 1 , 4 , 2 ] , [ 3 , 5 , 4 , 1 ] ] ; printIntersection ( A , B ) ;
Count Triplets such that one of the numbers can be written as sum of the other two | Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code
function countWays ( arr , n ) { let max_val = 0 ; for ( let i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , arr [ i ] ) ; let freq = new Array ( max_val + 1 ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( let i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( let i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( let i = 1 ; i <= max_val ; i ++ ) { for ( let j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; document . write ( countWays ( arr , n ) ) ;
Longest subarray with elements having equal modulo K | function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code
function LongestSubarray ( arr , n , k ) { let count = 1 ; let max_length = 1 ; let prev_mod = arr [ 0 ] % k ; for ( let i = 1 ; i < n ; i ++ ) { let curr_mod = arr [ i ] % k ; if ( curr_mod == prev_mod ) { count ++ ; } else { max_length = Math . max ( max_length , count ) ; count = 1 ; prev_mod = curr_mod ; } } return Math . max ( max_length , count ) ; } let arr = [ 4 , 9 , 7 , 18 , 29 , 11 ] ; let n = arr . length ; let k = 11 ; document . write ( LongestSubarray ( arr , n , k ) ) ;