codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of subsequences required to convert one string to another using Greedy Algorithm | Function to find the minimum number of subsequences required to convert one String to another S2 == A and S1 == B ; At least 1 subsequence is required Even in best case , when A is same as B ; size of B ; size of A ; Create an 2D array next [ ] [ ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] ; Array Initialization with infinite ; Loop to Store the values of index ; If the value of next [ i ] [ j ] is infinite then update it with next [ i ] [ j + 1 ] ; Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining String of A using next subsequence ; Loop to iterate over the String A ; Condition to check if the character is not present in the String B ; Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' ] [ pos ] ; Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero ; Driver Code
function findMinimumSubsequences ( A , B ) { let numberOfSubsequences = 1 ; let sizeOfB = B . length ; let sizeOfA = A . length ; let inf = 1000000 ; let next = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { next [ i ] = new Array ( sizeOfB ) ; for ( let j = 0 ; j < sizeOfB ; j ++ ) { next [ i ] [ j ] = inf ; } } for ( let i = 0 ; i < sizeOfB ; i ++ ) { next [ B [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ i ] = i ; } for ( let i = 0 ; i < 26 ; i ++ ) { for ( let j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i ] [ j ] == inf ) { next [ i ] [ j ] = next [ i ] [ j + 1 ] ; } } } let pos = 0 ; let i = 0 ; while ( i < sizeOfA ) { if ( pos == 0 && next [ A [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ pos ] == inf ) { numberOfSubsequences = - 1 ; break ; } else if ( pos < sizeOfB && next [ A [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ pos ] < inf ) { let nextIndex = next [ A [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ pos ] + 1 ; pos = nextIndex ; i ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } let A = " " ; let B = " " ; document . write ( findMinimumSubsequences ( A , B ) ) ;
Vertical and Horizontal retrieval ( MRT ) on Tapes | Javascript program to print Horizontal filling ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; vector is used because n number of records can insert in one tape with size constraint ; Null vector obtained to use fresh vector ' v ' ; initialize variables to 0 for each iteration ; sum is used for checking whether i 'th tape is full or not ; check sum less than size of tape ; increment in j for next record ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
function horizontalFill ( records , tape , nt ) { let sum = 0 ; let Retrieval_Time = 0 ; let Mrt ; let current = 0 ; let v = [ ] ; for ( let i = 0 ; i < nt ; i ++ ) { v = [ ] ; Retrieval_Time = 0 ; sum = 0 ; document . write ( " " + ( i + 1 ) + " " ) ; sum += records [ current ] ; while ( sum <= tape [ i ] ) { document . write ( records [ current ] + " " ) ; v . push ( records [ current ] ) ; current ++ ; sum += records [ current ] ; } document . write ( " " ) ; for ( let j = 0 ; j < v . length ; j ++ ) { Retrieval_Time += v [ j ] * ( v . length - j ) ; } Mrt = Retrieval_Time / v . length ; if ( i == 0 ) { document . write ( " " + Mrt . toFixed ( 4 ) + " " ) ; } else { document . write ( " " + Mrt + " " ) ; } } } let records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ] ; let tape = [ 25 , 80 , 160 ] ; let n = records . length ; let m = tape . length ; records . sort ( function ( a , b ) { return a - b } ) ; horizontalFill ( records , tape , m ) ;
Circular Convolution using Matrix Method | JavaScript program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program
let MAX_SIZE = 10 ; function convolution ( x , h , n , m ) { let row_vec = new Array ( MAX_SIZE ) ; row_vec . fill ( 0 ) ; let col_vec = new Array ( MAX_SIZE ) ; col_vec . fill ( 0 ) ; let out = new Array ( MAX_SIZE ) ; out . fill ( 0 ) ; let circular_shift_mat = new Array ( MAX_SIZE ) ; circular_shift_mat . fill ( 0 ) ; for ( let i = 0 ; i < MAX_SIZE ; i ++ ) { circular_shift_mat [ i ] = new Array ( MAX_SIZE ) ; for ( let j = 0 ; j < MAX_SIZE ; j ++ ) { circular_shift_mat [ i ] [ j ] = 0 ; } } let maxSize = n > m ? n : m ; for ( let i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( let i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } let k = 0 , d = 0 ; for ( let i = 0 ; i < maxSize ; i ++ ) { let curIndex = k - d ; for ( let j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( let i = 0 ; i < maxSize ; i ++ ) { for ( let j = 0 ; j < maxSize ; j ++ ) { out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; } document . write ( out [ i ] + " " ) ; } } let x = [ 5 , 7 , 3 , 2 ] ; let n = x . length ; let h = [ 1 , 5 ] ; let m = h . length ; convolution ( x , h , n , m ) ;
Maximize the number of palindromic Strings | Javascript program for the above approach ; To check if there is any string of odd length ; If there is at least 1 string of odd length . ; If all the strings are of even length . ; Count of 0 's in all  the strings ; Count of 1 's in  all the strings ; If z is even and o is even then ans will be N . ; Otherwise ans will be N - 1. ; Driver code
function max_palindrome ( s , n ) { let flag = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] . length % 2 != 0 ) { flag = 1 ; } } if ( flag == 1 ) { return n ; } let z = 0 ; let o = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < s [ i ] . length ; j ++ ) { if ( s [ i ] [ j ] == ' ' ) z += 1 ; else o += 1 ; } } if ( o % 2 == 0 && z % 2 == 0 ) { return n ; } else { return n - 1 ; } } let n = 3 ; let s = [ " " , " " , " " ] ; document . write ( max_palindrome ( s , n ) ) ;
Minimum possible travel cost among N cities | Function to return the minimum cost to travel from the first city to the last ; To store the total cost ; Start from the first city ; If found any city with cost less than that of the previous boarded bus then change the bus ; Calculate the cost to travel from the currently boarded bus till the current city ; Update the currently boarded bus ; Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city ; Driver code
function minCost ( cost , n ) { var totalCost = 0 ; var boardingBus = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( cost [ boardingBus ] > cost [ i ] ) { totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) ; boardingBus = i ; } } totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) ; return totalCost ; } var cost = [ 4 , 7 , 8 , 3 , 4 ] ; var n = cost . length ; document . write ( minCost ( cost , n ) ) ;
Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | Function to return the minimum flips required such that the submatrix from mat [ i ] [ j ] to mat [ i + 1 ] [ j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code
function minFlipsSub ( mat , i , j ) { var cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] . charAt ( j ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] . charAt ( j + 1 ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j + 1 ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; return Math . min ( cnt0 , cnt1 ) ; } function minFlips ( mat , r , c ) { var res = Number . MAX_VALUE ; for ( i = 0 ; i < r - 1 ; i ++ ) { for ( j = 0 ; j < c - 1 ; j ++ ) { res = Math . min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } var mat = [ " " , " " , " " ] ; var r = mat . length ; var c = mat [ 0 ] . length ; document . write ( minFlips ( mat , r , c ) ) ;
Count set bits in the Kth number after segregating even and odd from N natural numbers | Function to return the kth element of the Odd - Even sequence of length n ; Finding the index from where the even numbers will be stored ; Return the kth element ; Function to return the count of set bits in the kth number of the odd even sequence of length n ; Required kth number ; Return the count of set bits ; Driver code
function findK ( n , k ) { let pos ; if ( n % 2 == 0 ) { pos = parseInt ( n / 2 , 10 ) ; } else { pos = parseInt ( n / 2 , 10 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else { return ( ( k - pos ) * 2 ) ; } } function countSetBits ( n , k ) { let kth = findK ( n , k ) ; let count = 0 ; while ( kth > 0 ) { count += kth & 1 ; kth >>= 1 ; } return count ; } let n = 18 , k = 12 ; document . write ( countSetBits ( n , k ) ) ;
Minimum cost to convert str1 to str2 with the given operations | Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
function minCost ( str1 , str2 , n ) { let cost = 0 ; let array = str1 . split ( ' ' ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { let temp = array [ i ] ; array [ i ] = array [ i + 1 ] ; array [ i + 1 ] = temp ; cost ++ ; } else { cost ++ ; } } } return cost ; } let str1 = " " , str2 = " " ; let n = str1 . length ; document . write ( minCost ( str1 , str2 , n ) ) ;
Partition first N natural number into two sets such that their sum is not coprime | Function to find the required sets ; Impossible case ; Sum of first n - 1 natural numbers ; Driver code
function find_set ( n ) { if ( n <= 2 ) { document . write ( " " ) ; return ; } let sum1 = parseInt ( ( n * ( n - 1 ) ) / 2 , 10 ) ; let sum2 = n ; document . write ( sum1 + " " + sum2 + " " ) ; } let n = 8 ; find_set ( n ) ;
Count common elements in two arrays containing multiples of N and M | Recursive function to find gcd using euclidean algorithm ; Function to find lcm of two numbers using gcd ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( n , m ) { return ( n * m ) / gcd ( n , m ) ; } var n = 2 , m = 3 , k = 5 ; document . write ( parseInt ( k / lcm ( n , m ) ) ) ;
Check whether a subsequence exists with sum equal to k if arr [ i ] > 2 * arr [ i | Function to check whether sum of any set of the array element is equal to k or not ; Traverse the array from end to start ; if k is greater than arr [ i ] then subtract it from k ; If there is any subsequence whose sum is equal to k ; Driver code
function CheckForSequence ( arr , n , k ) { for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( k >= arr [ i ] ) k -= arr [ i ] ; } if ( k != 0 ) return false ; else return true ; } var A = [ 1 , 3 , 7 , 15 , 31 ] ; var n = A . length ; document . write ( ( CheckForSequence ( A , n , 18 ) ? " " : " " ) + " " ) ;
Maximum possible sub | Function to return the maximum sub - array sum after at most x swaps ; To store the required answer ; For all possible intervals ; Keep current ans as zero ; To store the integers which are not part of the sub - array currently under consideration ; To store elements which are part of the sub - array currently under consideration ; Create two sets ; Swap at most X elements ; Remove the minimum of the taken elements ; Add maximum of the discarded elements ; Update the answer ; Return the maximized sub - array sum ; Driver code .
function SubarraySum ( a , n , x ) { let ans = - 10000 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let curans = 0 ; let pq = [ ] ; let pq2 = [ ] ; for ( let k = 0 ; k < n ; k ++ ) { if ( k >= i && k <= j ) { curans += a [ k ] ; pq2 . push ( a [ k ] ) ; } else pq . push ( a [ k ] ) ; } pq . sort ( ) ; pq . reverse ( ) ; pq2 . sort ( ) ; ans = Math . max ( ans , curans ) ; for ( let k = 1 ; k <= x ; k ++ ) { if ( pq . length == 0 pq2 . length == 0 pq2 [ 0 ] >= pq [ 0 ] ) break ; curans -= pq2 [ 0 ] ; pq2 . shift ( ) ; curans += pq [ 0 ] ; pq . shift ( ) ; ans = Math . max ( ans , curans ) ; } } } return ans ; } let a = [ 5 , - 1 , 2 , 3 , 4 , - 2 , 5 ] ; let x = 2 ; let n = a . length ; document . write ( SubarraySum ( a , n , x ) ) ;
Generate an array of size K which satisfies the given conditions | Function to generate and print the required array ; Initializing the array ; Finding r ( from above approach ) ; If r < 0 ; Finding ceiling and floor values ; Fill the array with ceiling values ; Fill the array with floor values ; Add 1 , 2 , 3 , ... with corresponding values ; There is no solution for below cases ; Modify A [ 1 ] and A [ k - 1 ] to get the required array ; Driver Code
function generateArray ( n , k ) { var array = Array ( k ) . fill ( 0 ) ; var remaining = n - parseInt ( k * ( k + 1 ) / 2 ) ; if ( remaining < 0 ) document . write ( " " ) ; var right_most = remaining % k ; var high = parseInt ( Math . ceil ( remaining / ( k * 1.0 ) ) ) ; var low = parseInt ( Math . floor ( remaining / ( k * 1.0 ) ) ) ; for ( i = k - right_most ; i < k ; i ++ ) array [ i ] = high ; for ( i = 0 ; i < ( k - right_most ) ; i ++ ) array [ i ] = low ; for ( i = 0 ; i < k ; i ++ ) array [ i ] += i + 1 ; if ( k - 1 != remaining k == 1 ) { for ( var u = 0 ; u < array . length ; u ++ ) document . write ( array [ u ] + " " ) ; } else if ( k == 2 k == 3 ) document . write ( " " ) ; else { array [ 1 ] -= 1 ; array [ k - 1 ] += 1 ; for ( var f = 0 ; f < array . length ; f ++ ) document . write ( array [ f ] + " " ) ; } } var n = 26 , k = 6 ; generateArray ( n , k ) ;
Maximize the given number by replacing a segment of digits with the alternate digits given | Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code
function get_maximum ( s , a ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) < a [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ) { let j = i ; while ( j < n && ( s [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) <= a [ s [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ) ) { s [ j ] = String . fromCharCode ( ' ' . charCodeAt ( ) + a [ s [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ) ; j ++ ; } return s . join ( " " ) ; } } return s . join ( " " ) ; } let s = " " ; let a = [ 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ] ; document . write ( get_maximum ( s . split ( ' ' ) , a ) ) ;
Number of times the largest perfect square number can be subtracted from N | Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code
function countSteps ( n ) { let steps = 0 ; while ( n ) { let largest = Math . floor ( Math . sqrt ( n ) ) ; n -= ( largest * largest ) ; steps ++ ; } return steps ; } let n = 85 ; document . write ( countSteps ( n ) ) ;
Maximum array sum that can be obtained after exactly k changes | Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code
function sumArr ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function maxSum ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b } ) ; let i = 0 ; while ( i < n && k > 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; i ++ ; } if ( k % 2 == 1 ) { let min = 0 ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ min ] > arr [ i ] ) min = i ; arr [ min ] *= - 1 ; } return sumArr ( arr , n ) ; } let arr = [ - 5 , 4 , 1 , 3 , 2 ] ; let n = arr . length ; let k = 4 ; document . write ( maxSum ( arr , n , k ) ) ;
Spanning Tree With Maximum Degree ( Using Kruskal 's Algorithm) | par and rank will store the parent and rank of particular node in the Union Find Algorithm ; Find function of Union Find Algorithm ; Union function of Union Find Algorithm ; Function to find the required spanning tree ; Initialising parent of a node by itself ; Variable to store the node with maximum degree ; Finding the node with maximum degree ; Union of all edges incident on vertex with maximum degree ; Carrying out normal Kruskal Algorithm ; Number of nodes ; Number of edges ; ArrayList to store the graph ; Array to store the degree of each node in the graph ; Add edges and update degrees
let par , rank ; function find ( x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } function union ( u , v ) { let x = find ( u ) ; let y = find ( v ) ; if ( x == y ) return ; if ( rank [ x ] > rank [ y ] ) par [ y ] = x ; else if ( rank [ x ] < rank [ y ] ) par [ x ] = y ; else { par [ x ] = y ; rank [ y ] ++ ; } } function findSpanningTree ( deg , n , m , g ) { par = new Array ( n + 1 ) ; rank = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) par [ i ] = i ; let max = 1 ; for ( let i = 2 ; i <= n ; i ++ ) if ( deg [ i ] > deg [ max ] ) max = i ; for ( let v = 0 ; v < g [ max ] . length ; v ++ ) { document . write ( max + " " + g [ max ] [ v ] + " " ) ; union ( max , g [ max ] [ v ] ) ; } for ( let u = 1 ; u <= n ; u ++ ) { for ( let v = 0 ; v < g [ u ] . length ; v ++ ) { let x = find ( u ) ; let y = find ( g [ u ] [ v ] ) ; if ( x == y ) continue ; union ( x , y ) ; document . write ( u + " " + g [ u ] [ v ] + " " ) ; } } } let n = 5 ; let m = 5 ; let g = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) g [ i ] = [ ] ; let deg = new Array ( n + 1 ) ; deg . fill ( 0 ) ; g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; deg [ 1 ] ++ ; deg [ 2 ] ++ ; g [ 1 ] . push ( 5 ) ; g [ 5 ] . push ( 1 ) ; deg [ 1 ] ++ ; deg [ 5 ] ++ ; g [ 2 ] . push ( 3 ) ; g [ 3 ] . push ( 2 ) ; deg [ 2 ] ++ ; deg [ 3 ] ++ ; g [ 5 ] . push ( 3 ) ; g [ 3 ] . push ( 5 ) ; deg [ 3 ] ++ ; deg [ 5 ] ++ ; g [ 3 ] . push ( 4 ) ; g [ 4 ] . push ( 3 ) ; deg [ 3 ] ++ ; deg [ 4 ] ++ ; findSpanningTree ( deg , n , m , g ) ;
Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver code
function Maxsum ( c1 , c2 , c3 , c4 ) { let sum = 0 ; let two34 = Math . min ( c2 , Math . min ( c3 , c4 ) ) ; sum = two34 * 234 ; c2 -= two34 ; sum += Math . min ( c2 , c1 ) * 12 ; return sum ; } let c1 = 5 , c2 = 2 , c3 = 3 , c4 = 4 ; document . write ( Maxsum ( c1 , c2 , c3 , c4 ) ) ;
Replace all elements by difference of sums of positive and negative numbers after that element | Function to print the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; calculate difference of both sums ; if i - th element is positive , add it to positive sum ; if i - th element is negative , add it to negative sum ; replace i - th elements with absolute difference ; Driver Code
function printArray ( N , arr ) { for ( var i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function replacedArray ( N , arr ) { var pos_sum , neg_sum , i , j , diff ; pos_sum = 0 ; neg_sum = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; if ( arr [ i ] > 0 ) pos_sum += arr [ i ] ; else neg_sum += arr [ i ] ; arr [ i ] = Math . abs ( diff ) ; } } var N = 5 ; var arr = [ 1 , - 1 , 2 , 3 , - 2 ] ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; var arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ;
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) ; pairs from 1 to n * ( a / n ) and n * ( b / n ) to b ; pairs from n * ( a / n ) to a and 1 to n * ( b / n ) ; pairs from n * ( a / n ) to a and n * ( b / n ) to b ; Return answer ; Driver code
function findCountOfPairs ( a , b , n ) { let ans = 0 ; ans += n * parseInt ( a / n , 10 ) * parseInt ( b / n , 10 ) ans += parseInt ( a / n , 10 ) * parseInt ( b % n , 10 ) ; ans += parseInt ( a % n , 10 ) * parseInt ( b / n , 10 ) ; ans += parseInt ( ( ( a % n ) + ( b % n ) ) / n , 10 ) ; return ans ; } let a = 5 , b = 13 , n = 3 ; document . write ( findCountOfPairs ( a , b , n ) ) ;
Generate array with minimum sum which can be deleted in P steps | Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
function findArray ( N , P ) { let ans = parseInt ( ( P * ( P + 1 ) ) / 2 , 10 ) + ( N - P ) ; let arr = new Array ( N + 1 ) ; for ( let i = 1 ; i <= P ; i ++ ) { arr [ i ] = i ; } for ( let i = P + 1 ; i <= N ; i ++ ) { arr [ i ] = 1 ; } document . write ( " " + ans + " " ) ; document . write ( " " + " " ) ; for ( let i = 1 ; i <= N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let N = 5 , P = 3 ; findArray ( N , P ) ;
Find Intersection of all Intervals | Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
function findIntersection ( intervals , N ) { let l = intervals [ 0 ] [ 0 ] ; let r = intervals [ 0 ] [ 1 ] ; for ( let i = 1 ; i < N ; i ++ ) { if ( intervals [ i ] [ 0 ] > r intervals [ i ] [ 1 ] < l ) { document . write ( - 1 + " " ) ; return ; } else { l = Math . max ( l , intervals [ i ] [ 0 ] ) ; r = Math . min ( r , intervals [ i ] [ 1 ] ) ; } } document . write ( " " + l + " " + r + " " + " " ) ; } let intervals = [ [ 1 , 6 ] , [ 2 , 8 ] , [ 3 , 10 ] , [ 5 , 8 ] ] ; let N = intervals . length ; findIntersection ( intervals , N ) ;
Maximum size of sub | Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Prvar the required answer
function cmp ( a , b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } function maxSubarraySize ( arr , n ) { var ans = 1 ; var anchor = 0 ; for ( i = 1 ; i < n ; i ++ ) { var c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } var arr = [ 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ] ; var n = arr . length ; document . write ( maxSubarraySize ( arr , n ) ) ;