codeparrot/xlcost-text-to-code huggingface dataset:
Find the modified array after performing k operations of given type | Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
function printArray ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function removeMin ( arr , n ) { var i , minVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) minVal = Math . min ( minVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] - minVal ; } function removeFromMax ( arr , n ) { var i , maxVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) maxVal = Math . max ( maxVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = maxVal - arr [ i ] ; } function modifyArray ( arr , n , k ) { if ( k % 2 == 0 ) removeMin ( arr , n ) ; else removeFromMax ( arr , n ) ; printArray ( arr , n ) ; } arr = [ 4 , 8 , 12 , 16 ] var n = arr . length ; var k = 2 ; modifyArray ( arr , n , k ) ;
Minimize the sum of the squares of the sum of elements of each group the array is divided into | Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
function findAnswer ( n , arr ) { arr . sort ( ( a , b ) => a - b ) ; let sum = 0 ; for ( let i = 0 ; i < Math . floor ( n / 2 ) ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } let arr = new Array ( 53 , 28 , 143 , 5 ) ; let n = arr . length ; document . write ( findAnswer ( n , arr ) ) ;
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | Javascript program to merge K sorted arrays ; Function to perform merge operation ; To store the starting point of left and right array ; To store the size of left and right array ; array to temporarily store left and right array ; storing data par left array ; storing data par right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; Code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; To sort left half ; To sort right half ; Merge the left and right half ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
var n = 4 ; function merge ( l , r , output ) { var l_in = l * n , r_in = ( parseInt ( ( l + r ) / 2 ) + 1 ) * n ; var l_c = ( parseInt ( ( l + r ) / 2 ) - l + 1 ) * n ; var r_c = ( r - parseInt ( ( l + r ) / 2 ) ) * n ; var l_arr = Array ( l_c ) , r_arr = Array ( r_c ) ; for ( var i = 0 ; i < l_c ; i ++ ) l_arr [ i ] = output [ l_in + i ] ; for ( var i = 0 ; i < r_c ; i ++ ) r_arr [ i ] = output [ r_in + i ] ; var l_curr = 0 , r_curr = 0 ; var par = l_in ; while ( l_curr + r_curr < l_c + r_c ) { if ( r_curr == r_c || ( l_curr != l_c && l_arr [ l_curr ] < r_arr [ r_curr ] ) ) { output [ par ] = l_arr [ l_curr ] ; l_curr ++ , par ++ ; } else { output [ par ] = r_arr [ r_curr ] ; r_curr ++ , par ++ ; } } } function divide ( l , r , output , arr ) { if ( l == r ) { for ( var i = 0 ; i < n ; i ++ ) output [ l * n + i ] = arr [ l ] [ i ] ; return ; } divide ( l , parseInt ( ( l + r ) / 2 ) , output , arr ) ; divide ( parseInt ( ( l + r ) / 2 ) + 1 , r , output , arr ) ; merge ( l , r , output ) ; } var arr = [ [ 5 , 7 , 15 , 18 ] , [ 1 , 8 , 9 , 17 ] , [ 1 , 4 , 7 , 7 ] ] ; var k = arr . length ; var output = Array ( n * k ) ; divide ( 0 , k - 1 , output , arr ) ; for ( var i = 0 ; i < n * k ; i ++ ) document . write ( output [ i ] + " " ) ;
Make palindromic string non | Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code
function findNonPalinString ( s ) { var freq = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var flag = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( flag == 0 ) document . write ( " " ) ; else { for ( var i = 0 ; i < 26 ; i ++ ) for ( var j = 0 ; j < freq [ i ] ; j ++ ) document . write ( String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ) ; } } var s = " " ; findNonPalinString ( s . split ( ' ' ) ) ;
Minimum operations of given type to make all elements of a matrix equal | Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; If not possible ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; Return minimum operations required ; Driver code
function minOperations ( n , m , k , matrix ) { let arr = new Array ( n * m ) ; arr . fill ( 0 ) ; let mod = matrix [ 0 ] [ 0 ] % k ; for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i ] [ j ] ; if ( matrix [ i ] [ j ] % k != mod ) { return - 1 ; } } } arr . sort ( function ( a , b ) { return a - b } ) ; let median = arr [ parseInt ( ( n * m ) / 2 , 10 ) ] ; let minOperations = 0 ; for ( let i = 0 ; i < n * m ; ++ i ) minOperations += parseInt ( Math . abs ( arr [ i ] - median ) / k , 10 ) ; if ( ( n * m ) % 2 == 0 ) { let median2 = arr [ parseInt ( ( n * m ) / 2 , 10 ) ] ; let minOperations2 = 0 ; for ( let i = 0 ; i < n * m ; ++ i ) minOperations2 += parseInt ( Math . abs ( arr [ i ] - median2 ) / k , 10 ) ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } let matrix = [ [ 2 , 4 , 6 ] , [ 8 , 10 , 12 ] , [ 14 , 16 , 18 ] , [ 20 , 22 , 24 ] ] ; let n = 4 ; let m = 3 ; let k = 2 ; document . write ( minOperations ( n , m , k , matrix ) ) ;
Count distinct elements in an array | JavaScript program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver program to test above function
function countDistinct ( arr , n ) { let res = 1 ; for ( let i = 1 ; i < n ; i ++ ) { let j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] === arr [ j ] ) break ; if ( i === j ) res ++ ; } return res ; } let arr = [ 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ] ; let n = arr . length ; document . write ( countDistinct ( arr , n ) ) ;
Count distinct elements in an array | JavaScript program to count all distinct elements in a given array ; First sort the array so that all occurrences become consecutive ; Traverse the sorted array ; Move the index ahead while there are duplicates ; Driver code
function countDistinct ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } let arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; let n = arr . length ; document . write ( countDistinct ( arr , n ) ) ;
Choose n elements such that their mean is maximum | Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
function printArray ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function printMaxMean ( arr , n ) { var newArr = Array ( n ) . fill ( 0 ) ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } var arr = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ] ; var n = arr . length ; printMaxMean ( arr , n / 2 ) ;
Average of remaining elements after removing K largest and K smallest elements from array | Function to find average ; Base case if 2 * k >= n means all element get removed ; First sort all elements ; Sum of req number ; Find average ; Driver code
function average ( arr , n , k ) { var total = 0 ; if ( 2 * k >= n ) return 0 ; arr . sort ( ) ; var start = k , end = n - k - 1 ; for ( i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } var arr = [ 1 , 2 , 4 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 2 ; document . write ( average ( arr , n , k ) ) ;
Minimum sum after subtracting multiples of k from the elements of the array | function to calculate minimum sum after transformation ; no element can be reduced further ; if all the elements of the array are identical ; check if a [ i ] can be reduced to a [ 0 ] ; one of the elements cannot be reduced to be equal to the other elements ; if k = 1 then all elements can be reduced to 1 ; Driver code
function min_sum ( n , k , a ) { a . sort ( ) ; if ( a [ 0 ] < 0 ) return - 1 ; if ( k == 0 ) { if ( a [ 0 ] == a [ n - 1 ] ) return ( n * a [ 0 ] ) ; else return - 1 ; } else { let f = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let p = a [ i ] - a [ 0 ] ; if ( p % k == 0 ) continue ; else { f = 1 ; break ; } } if ( f > 0 ) return - 1 ; else { if ( k == 1 ) return n ; else return ( n * ( a [ 0 ] % k ) ) ; } } } let arr = [ 2 , 3 , 4 , 5 ] ; let K = 1 ; let N = arr . length ; document . write ( min_sum ( N , K , arr ) ) ;
In | Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] Inplace Implementation ; If the direct merge is already sorted ; Two pointers to maintain start of both arrays to merge ; If element 1 is in right place ; Shift all the elements between element 1 element 2 , right by 1. ; Update all the pointers ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and r ; Sort first and second halves ; Function to print an array ; Driver code
function merge ( arr , start , mid , end ) { let start2 = mid + 1 ; if ( arr [ mid ] <= arr [ start2 ] ) { return ; } while ( start <= mid && start2 <= end ) { if ( arr [ start ] <= arr [ start2 ] ) { start ++ ; } else { let value = arr [ start2 ] ; let index = start2 ; while ( index != start ) { arr [ index ] = arr [ index - 1 ] ; index -- ; } arr [ start ] = value ; start ++ ; mid ++ ; start2 ++ ; } } } function mergeSort ( arr , l , r ) { if ( l < r ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } function printArray ( A , size ) { let i ; for ( i = 0 ; i < size ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; let arr_size = arr . length ; mergeSort ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ;
Minimum Increment / decrement to make array elements equal | Function to return minimum operations need to be make each element of array equal ; Initialize cost to 0 ; Sort the array ; Middle element ; Find Cost ; If n , is even . Take minimum of the Cost obtained by considering both middle elements ; Find cost again ; Take minimum of two cost ; Return total cost ; Driver Code
function minCost ( A , n ) { var cost = 0 ; A . sort ( ) ; var K = A [ parseInt ( n / 2 ) ] ; var i ; for ( i = 0 ; i < n ; ++ i ) cost += Math . abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { var tempCost = 0 ; K = A [ parseInt ( n / 2 ) - 1 ] ; for ( i = 0 ; i < n ; ++ i ) tempCost += Math . abs ( A [ i ] - K ) ; cost = Math . min ( cost , tempCost ) ; } return cost ; } var A = [ 1 , 6 , 7 , 10 ] ; var n = A . length ; document . write ( minCost ( A , n ) ) ;
Print array elements in alternatively increasing and decreasing order | Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; Printing the elements in decreasing order ; Increasing the number of elements to printed in next iteration ; Driver code
function printArray ( arr , n ) { arr . sort ( ) ; let l = 0 , r = n - 1 , flag = 0 , i ; let k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) document . write ( arr [ i ] + " " ) ; flag = 1 ; l = i ; } else { for ( i = r ; i > r - k && i >= l ; i -- ) document . write ( arr [ i ] + " " ) ; flag = 0 ; r = i ; } k ++ ; } } let n = 6 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; printArray ( arr , n ) ;
Count triplets in a sorted doubly linked list whose product is equal to a given value x | structure of node of doubly linked list ; function to count pairs whose product equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if product is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with product ( x / current . data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; start with an empty doubly linked list ; insert values in sorted order
class Node { constructor ( ) { let data ; let next , prev ; } } function countPairs ( first , second , value ) { let count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data * second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data * second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } function countTriplets ( head , x ) { if ( head == null ) return 0 ; let current , first , last ; let count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x / current . data ) ; } return count ; } function insert ( head , data ) { let temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } let head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; let x = 8 ; document . write ( " " + countTriplets ( head , x ) ) ;
Check if the characters of a given string are in alphabetical order | Function that checks whether the string is in alphabetical order or not ; if element at index ' i ' is less than the element at index ' i - 1' then the string is not sorted ; Driver code ; check whether the string is in alphabetical order or not
function isAlphabaticOrder ( s ) { let n = s . length ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] < s [ i - 1 ] ) return false ; } return true ; } let s = " " ; if ( isAlphabaticOrder ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Product of non | Function to find the product of all non - repeated elements in an array ; sort all elements of array ; Driver code
function findProduct ( arr , n ) { arr . sort ( ) ; var prod = 1 * arr [ 0 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { prod = prod * arr [ i + 1 ] ; } } return prod ; } var arr = [ 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 ] ; var n = arr . length ; document . write ( findProduct ( arr , n ) ) ;
Check if it is possible to rearrange rectangles in a non | Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; initialize the number of rectangles ; initialize n rectangles with length and breadth
function rotateRec ( n , L , B ) { var m = Number . MAX_VALUE for ( var i = 0 ; i < n ; i ++ ) { if ( Math . max ( L [ i ] , B [ i ] ) <= m ) m = Math . max ( L [ i ] , B [ i ] ) ; else if ( Math . min ( L [ i ] , B [ i ] ) <= m ) m = Math . min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } var n = 3 ; var L = [ 5 , 5 , 6 ] ; var B = [ 6 , 7 , 8 ] ; if ( rotateRec ( n , L , B ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Find a point such that sum of the Manhattan distances is minimized | Function to print the required points which minimizes the sum of Manhattan distances ; Sorting points in all k dimension ; Output the required k points ; Driver code ; function call to print required points
function minDistance ( n , k , points ) { for ( let i = 0 ; i < k ; i ++ ) ( point [ i ] ) . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < k ; i ++ ) document . write ( point [ i ] [ Math . ceil ( ( n / 2 ) - 1 ) ] + " " ) ; } let n = 4 ; let k = 4 ; let point = [ [ 1 , 5 , 2 , 4 ] , [ 6 , 2 , 0 , 6 ] , [ 9 , 5 , 1 , 3 ] , [ 6 , 7 , 5 , 9 ] ] ; minDistance ( n , k , point ) ;
Sort first k values in ascending order and remaining n | Function to print half of the array in ascending order and the other half in descending order ; Store the k elements in an array ; Store the remaining n - k elements in an array ; Sorting the array from 0 to k - 1 places ; Sorting the array from k to n places ; Storing the values in the final array arr ; Printing the array ; Driver code
function printOrder ( arr , n , k ) { let len1 = k , len2 = n - k ; let arr1 = new Array ( k ) ; let arr2 = new Array ( n - k ) ; for ( let i = 0 ; i < k ; i ++ ) arr1 [ i ] = arr [ i ] ; for ( let i = k ; i < n ; i ++ ) arr2 [ i - k ] = arr [ i ] ; arr1 . sort ( function ( a , b ) { return a - b ; } ) ; arr2 . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i < k ) arr [ i ] = arr1 [ i ] ; else { arr [ i ] = arr2 [ len2 - 1 ] ; len2 -- ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 ] ; let k = 4 ; let n = arr . length ; printOrder ( arr , n , k ) ;
Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in descending order to print the required number ; Print the number of times a digits occurs ; Driver code
function findMaxNum ( arr , n ) { var hash = Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( var i = 9 ; i >= 0 ; i -- ) { for ( var j = 0 ; j < hash [ i ] ; j ++ ) document . write ( i ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 0 ] ; var n = arr . length ; findMaxNum ( arr , n ) ;
Sort a nearly sorted array using STL | Given an array of size n , where every element is k away from its target position , sorts the array in O ( n Log n ) time . ; Sort the array using inbuilt function ; An utility function to print array elements ; Driver code
function sortK ( arr , n , k ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let k = 3 ; let arr = [ 2 , 6 , 3 , 12 , 56 , 8 ] ; let n = arr . length ; sortK ( arr , n , k ) ; document . write ( " " ) ; printArray ( arr , n ) ;
Equally divide into two sets such that one set has maximum distinct elements | Javascript program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Driver code
function distribution ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; var count = 1 ; for ( var i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) count ++ ; return Math . min ( count , parseInt ( n / 2 ) ) ; } var arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] ; var n = arr . length ; document . write ( distribution ( arr , n ) ) ;
Print all the pairs that contains the positive and negative values of an element | Function to print pairs of positive and negative values present in the array ; Store all the positive elements in the unordered_set ; Start traversing the array ; Check if the positive value of current element exists in the set or not ; Print that pair ; Driver code
function printPairs ( arr , n ) { let pairs = new Set ( ) ; let pair_exists = false ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . add ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . has ( - arr [ i ] ) ) { document . write ( arr [ i ] + " " + - arr [ i ] + " " ) ; pair_exists = true ; } } if ( pair_exists == false ) document . write ( " " ) ; } let arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] ; let n = arr . length ; printPairs ( arr , n ) ;
Sort 3 numbers | Javascript program to sort an array of size 3
let arr = [ 10 , 12 , 5 ] ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < 3 ; i ++ ) document . write ( arr [ i ] + " " ) ;
Print triplets with sum less than k | A Simple JavaScript program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver program
function printTriplets ( arr , n , sum ) { 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 ] < sum ) document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ k ] + " " ) ; } } } let arr = [ 5 , 1 , 3 , 4 , 7 ] ; let n = arr . length ; let sum = 12 ; printTriplets ( arr , n , sum ) ;
Count number of triplets in an array having sum in the range [ a , b ] | Function to count triplets ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code
function countTriplets ( arr , n , a , b ) { var ans = 0 ; for ( var i = 0 ; i < n - 2 ; i ++ ) { for ( var j = i + 1 ; j < n - 1 ; j ++ ) { for ( var k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] >= a && arr [ i ] + arr [ j ] + arr [ k ] <= b ) ans ++ ; } } return ans ; } var arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ] ; var n = arr . length ; var a = 8 , b = 16 ; document . write ( countTriplets ( arr , n , a , b ) ) ;