codeparrot/xlcost-text-to-code huggingface dataset:
Count pair of strings whose concatenation has every vowel | Function to return the count of all concatenated string with each vowel at least once ; Creating a hash array with initial value as 0 ; Traversing through each string and getting hash value for each of them ; Initializing the weight of each string ; Find the hash value for each string ; Increasing the count of the hash value ; Getting all possible pairs of indexes in hash array ; Check if the pair which has hash value 31 and multiplying the count of string and add it strCount ; Corner case , for strings which independently has all the vowels ; Return thre final count ; Given array of strings ; Function call
function good_pairs ( str , N ) { let arr = new Array ( 32 ) ; arr . fill ( 0 ) ; let strCount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let Weight = 0 ; for ( let j = 0 ; j < str [ i ] . length ; j ++ ) { switch ( str [ i ] [ j ] ) { case ' ' : Weight = Weight | 1 ; break ; case ' ' : Weight = Weight | 2 ; break ; case ' ' : Weight = Weight | 4 ; break ; case ' ' : Weight = Weight | 8 ; break ; case ' ' : Weight = Weight | 16 ; break ; } } arr [ Weight ] ++ ; } for ( let i = 0 ; i < 32 ; i ++ ) { for ( let j = i + 1 ; j < 32 ; j ++ ) { if ( ( i j ) == 31 ) strCount += arr [ i ] * arr [ j ] ; } } strCount += parseInt ( ( arr [ 31 ] * ( arr [ 31 ] - 1 ) ) / 2 , 10 ) ; return strCount ; } let str = [ " " , " " ] ; let N = str . length ; document . write ( good_pairs ( str , N ) ) ;
Color a grid such that all same color cells are connected either horizontally or vertically | Javascript Program to Color a grid such that all same color cells are connected either horizontally or vertically ; Current color ; final grid ; if even row ; traverse from left to right ; if color has been exhausted , move to the next color ; color the grid at this position ; reduce the color count ; traverse from right to left for odd rows ; print the grid ; Driver code
function solve ( arr , r , c ) { var idx = 1 ; var dp = new Array ( r ) ; var i , j ; for ( i = 0 ; i < r ; i ++ ) dp [ i ] = new Array ( c ) ; for ( i = 0 ; i < r ; i ++ ) { if ( i % 2 == 0 ) { for ( j = 0 ; j < c ; j ++ ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i ] [ j ] = idx ; arr [ idx - 1 ] -- ; } } else { for ( j = c - 1 ; j >= 0 ; j -- ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i ] [ j ] = idx ; arr [ idx - 1 ] -- ; } } } for ( i = 0 ; i < r ; ++ i ) { for ( j = 0 ; j < c ; ++ j ) { document . write ( dp [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } var r = 3 , c = 5 ; var n = 5 ; var arr = [ 1 , 2 , 3 , 4 , 5 ] ; solve ( arr , r , c ) ;
Count of 0 s to be flipped to make any two adjacent 1 s at least K 0 s apart | Function to find the count of 0 s to be flipped ; Loop traversal to mark K adjacent positions to the right of already existing 1 s . ; Loop traversal to mark K adjacent positions to the left of already existing 1 s . ; Loop to count the maximum number of 0 s that will be replaced by 1 s ; Driver code
function count ( k , s ) { var ar = Array ( s . length ) . fill ( 0 ) ; var end = 0 ; var i , j ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) { for ( j = i ; j < s . length && j <= i + k ; j ++ ) { ar [ j ] = - 1 ; end = j ; } i = end ; } } end = 0 ; for ( i = s . length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { for ( j = i ; j >= 0 && j >= i - k ; j -- ) { ar [ j ] = - 1 ; end = j ; } i = end ; } } var ans = 0 ; end = 0 ; var g ; for ( j = 0 ; j < s . length ; j ++ ) { if ( ar [ j ] == 0 ) { ans ++ ; for ( g = j ; g <= j + k && g < s . length ; g ++ ) { ar [ g ] = - 1 ; end = g ; } j = end - 1 ; } } return ans ; } var K = 2 ; var s = " " ; document . write ( count ( K , s ) ) ;
Length of longest connected 1 â €™ s in a Binary Grid | JavaScript program to implement the above approach ; Keeps a track of directions that is up , down , left , right ; Function to perform the dfs traversal ; Mark the current node as visited ; Increment length from this node ; Update the diameter length ; Move to next cell in x - direction ; Move to next cell in y - direction ; Check if cell is invalid then continue ; Perform DFS on new cell ; Decrement the length ; Function to find the maximum length of connected 1 s in the given grid ; Increment the id ; Traverse the grid [ ] ; Find start polet of start dfs call ; DFS Traversal from cell ( x , y ) ; Prlet the maximum length ; Given grid [ ] [ ] ; Function Call
let row = 6 ; let col = 7 ; let vis = new Array ( row + 1 ) ; for ( var i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = new Array ( 2 ) ; } let id = 0 ; let diameter = 0 , length = 0 ; let x = 0 , y = 0 ; let dx = [ - 1 , 1 , 0 , 0 ] ; let dy = [ 0 , 0 , - 1 , 1 ] ; function dfs ( a , b , lis ) { vis [ a ] [ b ] = id ; length ++ ; if ( length > diameter ) { x = a ; y = b ; diameter = length ; } for ( let j = 0 ; j < 4 ; j ++ ) { let cx = a + dx [ j ] ; let cy = b + dy [ j ] ; if ( cx < 0 cy < 0 cx >= row cy >= col lis [ cx ] [ cy ] == 0 vis [ cx ] [ cy ] > 0 ) { continue ; } dfs ( cx , cy , lis ) ; } vis [ a ] [ b ] = 0 ; length -- ; } function findMaximumLength ( lis ) { id ++ ; length = 0 ; diameter = 0 ; for ( let i = 0 ; i < row ; i ++ ) { for ( let j = 0 ; j < col ; j ++ ) { if ( lis [ i ] [ j ] != 0 ) { dfs ( i , j , lis ) ; i = row ; break ; } } } id ++ ; length = 0 ; diameter = 0 ; dfs ( x , y , lis ) ; document . write ( diameter ) ; } let grid = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ] ; findMaximumLength ( grid ) ;
Last two digits of powers of 7 | Function to find the last two digits of 7 ^ N ; Case 4 ; Case 3 ; Case 2 ; Case 1 ; Given number ; Function Call
function get_last_two_digit ( N ) { if ( N % 4 == 0 ) return " " ; else if ( N % 4 == 1 ) return " " ; else if ( N % 4 == 2 ) return " " ; return " " ; } var N = 12 ; document . write ( get_last_two_digit ( N ) ) ;
Subsequence with maximum pairwise absolute difference and minimum size | Function to find the subsequence with maximum absolute difference ; To store the resultant subsequence ; First element should be included in the subsequence ; Traverse the given array arr [ ] ; If current element is greater than the previous element ; If the current element is not the local maxima then continue ; Else push it in subsequence ; If the current element is less then the previous element ; If the current element is not the local minima then continue ; Else push it in subsequence ; Last element should also be included in subsequence ; Print the element ; Given array ; Function Call
function getSubsequence ( ar ) { let N = ar . length ; let ans = [ ] ; ans . push ( ar [ 0 ] ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { if ( ar [ i ] > ar [ i - 1 ] ) { if ( i < N - 1 && ar [ i ] <= ar [ i + 1 ] ) { continue ; } else { ans . push ( ar [ i ] ) ; } } else { if ( i < N - 1 && ar [ i + 1 ] < ar [ i ] ) { continue ; } else { ans . push ( ar [ i ] ) ; } } } ans . push ( ar [ N - 1 ] ) ; for ( let it = 0 ; it < ans . length ; it ++ ) document . write ( ans [ it ] + " " ) ; } let arr = [ 1 , 2 , 4 , 3 , 5 ] ; getSubsequence ( arr ) ;
Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
function Minsteps ( n ) { var ans = 0 ; while ( n > 1 ) { if ( n % 5 == 0 ) { ans ++ ; n = n / 5 ; continue ; } else if ( n % 4 == 0 ) { ans ++ ; n = n / 4 ; continue ; } else if ( n % 3 == 0 ) { ans ++ ; n = n / 3 ; continue ; } else if ( n % 2 == 0 ) { ans ++ ; n = n / 2 ; continue ; } return - 1 ; } return ans ; } var n = 10 ; document . write ( Minsteps ( n ) ) ;
Find the maximum difference after applying the given operations two times on a number | Function that returns the maximum difference after two given operations on a number ; Initialize the strings to make operations ; Store length of the string ; Make the maximum number using the first operation ; Find the first digit which is not equal to '9' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '9' ; Break after the operation is completed ; Check if first digit is equal to '1' ; Find the first digit which is greater than '1' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '0' ; Break after the operation is completed ; Select the first digit for the given operation ; Replace all the selected ' digit ' with '1' ; Return the difference after converting into integers ; Driver code
function minDifference ( num ) { let maximum = ( num . toString ( ) ) . split ( ' ' ) ; let minimum = ( num . toString ( ) ) . split ( ' ' ) ; let n = maximum . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( maximum [ i ] != ' ' ) { let digit = maximum [ i ] ; for ( let j = i ; j < n ; j ++ ) { if ( maximum [ j ] == digit ) maximum [ j ] = ' ' ; } break ; } } if ( minimum [ 0 ] == ' ' ) { for ( let i = 1 ; i < n ; i ++ ) { if ( minimum [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) > 1 ) { let digit = minimum [ i ] ; for ( let j = i ; j < n ; j ++ ) { if ( digit == minimum [ j ] ) minimum [ j ] = ' ' ; } break ; } } } else { let digit = minimum [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( minimum [ i ] == digit ) minimum [ i ] = ' ' ; } } return parseInt ( maximum . join ( " " ) ) - parseInt ( minimum . join ( " " ) ) ; } let N = 1101157 ; document . write ( minDifference ( N ) ) ;
Check if the square of a number is divisible by K or not | Utility gcd ; Function to return if square of X is divisible by K ; Finding gcd of x and k ; Dividing k by their gcd ; Check for divisibility of X by reduced K ; Driver code
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function checkDivisible ( x , k ) { var g = gcd ( x , k ) ; k /= g ; if ( x % k == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var x = 6 , k = 9 ; checkDivisible ( x , k ) ;
Find the minimum value of the given expression over all pairs of the array | Function to find the minimum value of the expression ; The expression simplifies to finding the minimum xor value pair ; Calculate min xor of consecutive pairs ; Driver code
function MinimumValue ( arr , n ) { arr . sort ( ) ; let minXor = Number . MAX_VALUE ; let val = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { val = arr [ i ] ^ arr [ i + 1 ] ; minXor = Math . min ( minXor , val ) ; } return minXor ; } let N = 6 ; let A = [ 12 , 3 , 14 , 5 , 9 , 8 ] ; document . write ( MinimumValue ( A , N ) ) ;
Find length of longest substring with at most K normal characters | Function to find maximum length of normal substrings ; keeps count of normal characters ; indexes of substring ; maintain length of longest substring with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with substring length ; get position of character ; check if character is normal then decrement count ; initialise the String
function maxNormalSubstring ( P , Q , K , N ) { if ( K == 0 ) return 0 ; var count = 0 ; var left = 0 , right = 0 ; var ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { var pos = P [ right ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( Q [ pos ] == ' ' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { var pos = P [ left ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; left ++ ; if ( Q [ pos ] == ' ' ) count -- ; if ( count < K ) break ; } } return ans ; } var P = " " , Q = " " ; var K = 2 ; var N = P . length ; document . write ( maxNormalSubstring ( P , Q , K , N ) ) ;
Make all the elements of array even with given operations | Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code
function countOperations ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] & 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) return - 1 ; } return count ; } let arr = [ 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; document . write ( countOperations ( arr , n ) ) ;
Count of pairs with difference at most K with no element repeating | Function to count the number of pairs whose difference is atmost K in an array ; Sorting the Array ; Variable to store the count of pairs whose difference is atmost K ; Loop to consider the consecutive pairs of the array ; if Pair found increment the index by 2 ; Driver Code ; Function Call
function countPairs ( arr , k , n ) { arr . sort ( ( a , b ) => a - b ) ; let pair = 0 ; let index = 0 ; while ( index < n - 1 ) { if ( arr [ index + 1 ] - arr [ index ] <= k ) { pair += 1 ; index += 2 ; } else { index += 1 ; } } return pair ; } let arr = [ 1 , 4 , 3 , 7 , 5 ] ; let k = 2 ; let n = arr . length ; let count = countPairs ( arr , k , n ) ; document . write ( count + " " ) ;
Maximum profit by selling N items at two markets | Function to calculate max profit ; Prefix sum array for profitA [ ] ; Suffix sum array for profitB [ ] ; If all the items are sold in market A ; Find the maximum profit when the first i items are sold in market A and the rest of the items are sold in market B for all possible values of i ; If all the items are sold in market B ; Driver code ; Function to calculate max profit
function maxProfit ( profitA , profitB , n ) { let preSum = new Array ( n ) ; preSum [ 0 ] = profitA [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; } let suffSum = new Array ( n ) ; suffSum [ n - 1 ] = profitB [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; } let res = preSum [ n - 1 ] ; for ( let i = 1 ; i < n - 1 ; i ++ ) { res = Math . max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; } res = Math . max ( res , suffSum [ 0 ] ) ; return res ; } let profitA = [ 2 , 3 , 2 ] ; let profitB = [ 10 , 30 , 40 ] ; let n = profitA . length ; document . write ( maxProfit ( profitA , profitB , n ) ) ;
Length of the longest subsequence such that xor of adjacent elements is non | Function to find the length of the longest subsequence such that the XOR of adjacent elements in the subsequence must be non - decreasing ; Computing xor of all the pairs of elements and store them along with the pair ( i , j ) ; Sort all possible xor values ; Initialize the dp array ; Calculating the dp array for each possible position and calculating the max length that ends at a particular index ; Taking maximum of all position ; Driver code
function LongestXorSubsequence ( arr , n ) { let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { v . push ( [ arr [ i ] ^ arr [ j ] , [ i , j ] ] ) ; } } v . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( let i of v ) { dp [ i [ 1 ] [ 1 ] ] = Math . max ( dp [ i [ 1 ] [ 1 ] ] , 1 + dp [ i [ 1 ] [ 0 ] ] ) ; } let ans = 1 ; for ( let i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , dp [ i ] ) ; return ans ; } let arr = [ 2 , 12 , 6 , 7 , 13 , 14 , 8 , 6 ] ; let n = arr . length ; document . write ( LongestXorSubsequence ( arr , n ) ) ;
Minimum operations required to make every element greater than or equal to K | Function to calculate gcd of two numbers ; function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function FindMinOperation ( a , n , k ) { let Q = [ ] ; for ( let i = 0 ; i < n ; i ++ ) Q . push ( a [ i ] ) ; Q . sort ( function ( a , b ) { return a - b } ) ; let ans = 0 ; while ( true ) { if ( Q [ 0 ] >= k ) break ; if ( Q . length < 2 ) return - 1 ; let x = Q [ 0 ] ; Q . shift ( ) ; let y = Q [ 0 ] ; Q . shift ( ) ; let z = parseInt ( ( x * y ) / gcd ( x , y ) , 10 ) ; Q . push ( z ) ; Q . sort ( function ( a , b ) { return a - b } ) ; ans ++ ; } return ans ; } let a = [ 3 , 5 , 7 , 6 , 8 ] ; let k = 8 ; let n = a . length ; document . write ( FindMinOperation ( a , n , k ) ) ;
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | Javascript implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver Code
using System ; using System . Collections ; class GfG { function Check ( a , b , n , m ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] > b [ i ] [ j ] ) { let temp = a [ i ] [ j ] ; a [ i ] [ j ] = b [ i ] [ j ] ; b [ i ] [ j ] = temp ; } for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < m - 1 ; j ++ ) if ( a [ i ] [ j ] >= a [ i ] [ j + 1 ] b [ i ] [ j ] >= b [ i ] [ j + 1 ] ) return " " ; for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] >= a [ i + 1 ] [ j ] b [ i ] [ j ] >= b [ i + 1 ] [ j ] ) return " " ; return " " ; } let n = 2 , m = 2 ; let a = [ [ 2 , 10 ] , [ 11 , 5 ] ] ; let b = [ [ 9 , 4 ] , [ 3 , 12 ] ] ; document . write ( Check ( a , b , n , m ) ) ;
Find the lexicographically smallest string which satisfies the given condition | Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array . Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
function smallestString ( N , A ) { let ch = ' ' ; let S = " " ; if ( N < 1 A [ 0 ] != 1 ) { S = " " ; return S ; } S += ch ; ch = String . fromCharCode ( ch . charCodeAt ( 0 ) + 1 ) ; for ( let i = 1 ; i < N ; i ++ ) { let diff = A [ i ] - A [ i - 1 ] ; if ( diff > 1 diff < 0 A [ i ] > 26 ) { S = " " ; return S ; } else if ( diff == 0 ) S += ' ' ; else { S += ch ; ch = String . fromCharCode ( ch . charCodeAt ( 0 ) + 1 ) ; } } return S ; } let arr = [ 1 , 1 , 2 , 3 , 3 ] ; let n = arr . length ; document . write ( smallestString ( n , arr ) ) ;
Count of alphabets whose ASCII values can be formed with the digits of N | Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't contain  current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
function canBePicked ( digits , num ) { var copyDigits = new Array ( 10 ) ; ; for ( var i = 0 ; i < 10 ; i ++ ) copyDigits [ i ] = digits [ i ] ; while ( num > 0 ) { var digit = num % 10 ; if ( copyDigits [ digit ] == 0 ) return false ; else copyDigits [ digit ] -= 1 ; num = Math . floor ( num / 10 ) ; } return true ; } function countAlphabets ( n ) { var count = 0 ; var digits = new Array ( 10 ) ; digits . fill ( 0 ) ; while ( n > 0 ) { var digit = n % 10 ; digits [ digit ] += 1 ; n = Math . floor ( n / 10 ) ; } for ( var i = 97 ; i <= 122 ; i ++ ) if ( canBePicked ( digits , i ) ) count += 1 ; for ( var i = 65 ; i < 91 ; i ++ ) if ( canBePicked ( digits , i ) ) count += 1 ; return count ; } var n = 1623455078 ; document . write ( countAlphabets ( n ) ) ;
Largest number less than X having at most K set bits | Function to get no of set bits in binary representation of positive integer n ; Function to return the greatest number <= X having at most K set bits . ; Remove rightmost set bits one by one until we count becomes k ; Return the required number ; Driver Code
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function greatestKBits ( X , K ) { let set_bit_count = countSetBits ( X ) ; if ( set_bit_count <= K ) return X ; let diff = set_bit_count - K ; for ( let i = 0 ; i < diff ; i ++ ) X &= ( X - 1 ) ; return X ; } let X = 21 , K = 2 ; document . write ( greatestKBits ( X , K ) ) ;
Find two numbers whose sum and GCD are given | function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function findTwoNumbers ( sum , gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) document . write ( " " + Math . min ( gcd , sum - gcd ) + " " + ( sum - Math . min ( gcd , sum - gcd ) ) ) ; else document . write ( - 1 ) ; } let sum = 8 ; let gcd = 2 ; findTwoNumbers ( sum , gcd ) ;
Find maximum distance between any city and station | Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
function findMaxDistance ( numOfCities , station , n ) { var hasStation = Array ( numOfCities + 1 ) . fill ( false ) ; for ( var city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } var dist = 0 ; var maxDist = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { maxDist = Math . min ( station [ i ] , maxDist ) ; } for ( var city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = Math . max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return Math . max ( maxDist , dist ) ; } var numOfCities = 6 ; var station = [ 3 , 1 ] ; var n = station . length ; document . write ( " " + findMaxDistance ( numOfCities , station , n ) ) ;
Split the number into N parts such that difference between the smallest and the largest part is minimum | Function that prlets the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; driver code
function split ( x , n ) { if ( x < n ) document . write ( " " ) ; else if ( x % n == 0 ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( ( x / n ) + " " ) ; } else { let zp = n - ( x % n ) ; let pp = Math . floor ( x / n ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i >= zp ) document . write ( ( pp + 1 ) + " " ) ; else document . write ( pp + " " ) ; } } } let x = 5 ; let n = 3 ; split ( x , n ) ;
Minimum time to reach a point with + t and | returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; driver code
function cal_minimum_time ( X ) { let t = 0 ; let sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } let n = 6 ; let ans = cal_minimum_time ( n ) ; document . write ( " " + ans ) ;