codeparrot/xlcost-text-to-code huggingface dataset:
Generate a sequence such that float division of array elements is maximized | Function to place the parenthesis such that the result is maximized ; Store the required string ; Add the first integer to string ; If the size of array is 1 ; If the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; If size of array is exceeds two , print 1 st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator ' / ' ; Add parenthesis at the end ; Print the final expression ; Driver Code
function generateSequence ( arr , n ) { var ans ; ans = ( arr [ 0 ] . toString ( ) ) ; if ( n == 1 ) document . write ( ans ) ; else if ( n == 2 ) { document . write ( ans + " " + ( arr [ 1 ] . toString ( ) ) ) ; } else { ans += " " + ( arr [ 1 ] . toString ( ) ) ; for ( var i = 2 ; i < n ; i ++ ) { ans += " " + ( arr [ i ] . toString ( ) ) ; } ans += " " ; document . write ( ans ) ; } } var arr = [ 1000 , 100 , 10 , 2 ] ; var N = arr . length ; generateSequence ( arr , N ) ;
Count of pairs having even and odd LCM from an array | Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr ; Update the count of pairs with odd LCM ; Prvar the count of required pairs ; Driver Code
function LCMPairs ( arr , N ) { var total_pairs = ( N * ( N - 1 ) ) / 2 ; var odd = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; document . write ( " " + ( total_pairs - odd ) + " " + odd ) ; } var arr = [ 3 , 6 , 5 , 4 ] ; var N = arr . length ; LCMPairs ( arr , N ) ;
Value required to be added to N to obtain the sum of first M multiples of K | Function to print the value to be added to N to obtain sum of first M multiples of K ; Store the sum of the first M multiples of K ; Store the value to be added to obtain N ; Input
function printNumber ( N , K , M ) { var sum = K * ( ( M * ( M + 1 ) ) / 2 ) ; return sum - N ; } var N = 17 ; var K = 3 ; var M = 4 ; document . write ( printNumber ( N , K , M ) ) ;
Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver Code
function countEvenOdd ( L , R ) { let range = R - L + 1 ; let even = parseInt ( range / 4 ) * 2 ; if ( ( L & 1 ) && ( range % 4 == 3 ) ) { even ++ ; } else if ( ! ( L & 1 ) && ( range % 4 ) ) { even ++ ; } document . write ( " " + even + " " + ( range - even ) ) ; } let L = 2 , R = 7 ; countEvenOdd ( L , R ) ;
Minimize swaps of same | Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
function countSwaps ( S , T ) { var value = [ ... Array ( 26 ) ] ; for ( var i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; var N = S . length ; var sum1 = 0 ; var sum2 = 0 ; var flag = false ; for ( var i = 0 ; i < N ; i ++ ) { sum1 += value [ S [ i ] - " " ] ; sum2 += value [ T [ i ] - " " ] ; if ( ( value [ S [ i ] - " " ] % 2 === 0 && value [ T [ i ] - " " ] % 2 === 1 ) || ( value [ S [ i ] - " " ] % 2 === 1 && value [ T [ i ] - " " ] % 2 === 0 ) ) flag = false ; } if ( sum1 % 2 === 1 && sum2 % 2 === 1 ) document . write ( " " ) ; else if ( sum1 % 2 === 0 && sum2 % 2 === 0 ) { if ( flag ) document . write ( " " ) ; else document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; var T = " " ; countSwaps ( S , T ) ;
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Print the count obtained ; Driver Code
function isPrime ( N ) { if ( N <= 1 ) return false ; for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } function countPrimeTuples ( N ) { let count = 0 ; for ( let i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } document . write ( count ) ; } let N = 6 ; countPrimeTuples ( N ) ;
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Store the required answer ; Check for all possible pairs ; If the condition satisfy then increment count by 1 ; Print the answer ; Driver Code ; Function Call
function countPairs ( A , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) if ( ( A [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) { count ++ ; } } document . write ( count ) ; } var A = [ 1 , 4 , 7 ] ; var N = A . length ; countPairs ( A , N ) ;
Count maximum number of consumable candies | Function to find the count of maximum consumable candies ; Store the count of total candies ; Stores the count of maximum consumable candies ; Checks if it is safe to counsume all candies ; Traverse the array arr [ ] ; If A [ i ] + M is greater than B [ i ] ; Mark all_safe as false ; Update ans ; Update ans ; Increment total by A [ i ] ; If all_safe is true ; Otherwise , ; Driver Code ; Function call to find maximum consumable candies
function maximumCandy ( candies , safety , N , M ) { let total = 0 ; let ans = Number . MAX_VALUE ; let all_safe = true ; for ( let i = 0 ; i < N ; i ++ ) { if ( candies [ i ] + M > safety [ i ] ) { all_safe = false ; ans = Math . min ( ans , safety [ i ] ) ; } else { ans = Math . min ( ans , candies [ i ] + M ) ; } total += candies [ i ] ; } if ( all_safe ) return total ; else return ans ; } let A = [ 4 , 5 , 2 , 3 ] ; let B = [ 8 , 13 , 6 , 4 ] ; let M = 5 ; let N = A . length ; document . write ( maximumCandy ( A , B , N , M ) ) ;
Check if two arrays can be made equal by swapping pairs of one of the arrays | Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Given arrays ; Size of the array
function checkArrays ( arr1 , arr2 , N ) { let count = 0 ; let flag = true ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = false ; break ; } } } } if ( ( flag && ( count == 0 ) ) == true ) document . write ( " " ) ; else document . write ( " " ) ; } let arr1 = [ 0 , 0 , 1 , 1 ] ; let arr2 = [ 1 , 1 , 0 , 0 ] ; let N = arr1 . length ; checkArrays ( arr1 , arr2 , N ) ;
Generate an N | Function to construct an array with sum of each subarray divisible by K ; Traverse a loop from 1 to N ; Print i - th multiple of K ; Driver Code
function construct_Array ( N , K ) { for ( let i = 1 ; i <= N ; i ++ ) { document . write ( K * i + " " ) ; } } let N = 3 , K = 3 ; construct_Array ( N , K ) ;
Generate an N | Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
function Print ( N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { document . write ( - 1 ) ; return ; } var CurEven = 2 , CurOdd = 1 ; var SumOdd = 0 , SumEven = 0 ; for ( var i = 0 ; i < ( N / 2 ) ; i ++ ) { document . write ( CurEven + " " ) ; SumEven += CurEven ; CurEven += 2 ; } for ( var i = 0 ; i < N / 2 - 1 ; i ++ ) { document . write ( CurOdd + " " ) ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; document . write ( CurOdd ) ; } var N = 12 ; Print ( N ) ;
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | Function to print the last remaining array element after after performing given operations ; If size of the array is 1 ; Check for the condition ; If condition is not satisfied ; Driver Code ; Function Call
function canArrayBeReduced ( arr , N ) { if ( N == 1 ) { document . write ( arr [ 0 ] ) ; return ; } if ( arr [ 0 ] < arr [ N - 1 ] ) { document . write ( arr [ N - 1 ] ) ; } else document . write ( " " ) ; } let arr = [ 6 , 5 , 2 , 4 , 1 , 3 , 7 ] ; let N = arr . length ; canArrayBeReduced ( arr , N ) ;
Minimum prefix increments required to make all elements of an array multiples of another array | Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Input arrays A [ ] and B [ ] ; Length of arrays
function MinimumMoves ( A , B , N ) { let totalOperations = 0 ; let carry = 0 ; let K = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let nearestMultiple = ( Math . ceil ( ( A [ i ] + carry ) / ( B [ i ] ) ) * B [ i ] ) ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } let A = [ 3 , 4 , 5 , 2 , 5 , 5 , 9 ] ; let B = [ 1 , 1 , 9 , 6 , 3 , 8 , 7 ] ; let N = A . length ; document . write ( MinimumMoves ( A , B , N ) + " " ) ;
Minimize deviation of an array by given operations | Function to find the minimum deviation of the array A [ ] ; Store all array elements in sorted order ; Odd number are transformed using 2 nd operation ; ( Maximum - Minimum ) ; Check if the size of set is > 0 and the maximum element is divisible by 2 ; Maximum element of the set ; Erase the maximum element ; Using operation 1 ; ( Maximum - Minimum ) ; Print the Minimum Deviation Obtained ; Driver Code ; Function Call to find Minimum Deviation of A [ ]
function minimumDeviation ( A , N ) { var s = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) s . add ( A [ i ] ) ; else s . add ( 2 * A [ i ] ) ; } var tmp = [ ... s ] . sort ( ( a , b ) => a - b ) ; var diff = tmp [ tmp . length - 1 ] - tmp [ 0 ] ; while ( s . size && tmp [ tmp . length - 1 ] % 2 == 0 ) { var maxEl = tmp [ tmp . length - 1 ] ; s . delete ( maxEl ) ; s . add ( parseInt ( maxEl / 2 ) ) ; tmp = [ ... s ] . sort ( ( a , b ) => a - b ) ; diff = Math . min ( diff , tmp [ tmp . length - 1 ] - tmp [ 0 ] ) ; } document . write ( diff ) ; } var A = [ 4 , 1 , 5 , 20 , 3 ] ; var N = A . length ; minimumDeviation ( A , N ) ;
Maximize given integer by swapping pairs of unequal bits | Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Given "Input ; Function call to find the Maximum Possible Number
function findMaxNum ( num ) { var binaryNumber = Number ( num ) . toString ( 2 ) ; ; var maxBinaryNumber = " " ; var count0 = 0 , count1 = 0 ; var N = binaryNumber . length ; for ( i = 0 ; i < N ; i ++ ) { if ( binaryNumber . charAt ( i ) == ' ' ) { count1 ++ ; } else { count0 ++ ; } } for ( i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += ' ' ; } for ( i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += ' ' ; } return parseInt ( maxBinaryNumber , 2 ) ; } var N = 11 ; document . write ( findMaxNum ( N ) ) ;
Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Input string ; Function call
function minOperations ( N ) { var even = 0 ; var odd = 0 ; for ( var it = 0 ; it < N . length ; it ++ ) { if ( ( N [ it ] - ' ' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { document . write ( " " + " " ) ; } else { var ans = 0 ; for ( var it = 0 ; it < N . length ; it ++ ) { if ( ( N [ it ] - ' ' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 ) { ans ++ ; } document . write ( ans ) ; } } var N = " " ; minOperations ( N ) ;
Find the player with least 0 s after emptying a Binary String by removing non | Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
function FindwinnerOfGame ( S ) { let cntZero = 0 ; let cntConOne = 0 ; let nimSum = 0 ; let N = S . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { document . write ( " " ) ; } else if ( nimSum != 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S = " " ; FindwinnerOfGame ( S ) ;
Place first N natural numbers at indices not equal to their values in an array | Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code
function generatepermutation ( N ) { var answer = [ ] ; answer . push ( N ) ; console . log ( answer ) ; for ( var i = 1 ; i < N ; i ++ ) { console . log ( answer ) ; answer . push ( i ) ; } for ( var i in answer ) document . write ( answer [ i ] + " " ) ; } var N = 4 ; generatepermutation ( N ) ;
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Print the answer ; Given N , K & M
function maxValueAtIndexK ( N , K , M ) { let S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; let X = ( M + S1 + S2 ) / N ; document . write ( X ) ; } let N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ;
Farthest cell from a given cell in a Matrix | Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Print the answer ; Driver Code
function farthestCellDistance ( N , M , R , C ) { let d1 = N + M - R - C ; let d2 = R + C - 2 ; let d3 = N - R + C - 1 ; let d4 = M - C + R - 1 ; let maxDistance = Math . max ( d1 , Math . max ( d2 , Math . max ( d3 , d4 ) ) ) ; document . write ( maxDistance ) ; } let N = 15 , M = 12 , R = 1 , C = 6 ; farthestCellDistance ( N , M , R , C ) ;
Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Print the array ; Given array ; Size of the array
function maxSum ( arr , N ) { let ans = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } let arr = [ 1 , 2 , 7 , 1 , 8 ] ; let N = arr . length ; maxSum ( arr , N ) ;
Generate an N | Function to conan array with given conditions ; Traverse the array arr ; Stores closest power of 2 less than or equal to arr [ i ] ; Stores R into brr [ i ] ; Print array elements ; Given array ; Size of the array ; Function Call
function constructArray ( arr , N ) { var brr = Array ( N ) . fill ( 0 ) ; for ( i = 0 ; i < N ; i ++ ) { var K = parseInt ( ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ) ; var R = parseInt ( Math . pow ( 2 , K ) ) ; brr [ i ] = R ; } for ( i = 0 ; i < N ; i ++ ) { document . write ( brr [ i ] + " " ) ; } } var arr = [ 11 , 5 , 7 , 3 , 2 ] ; var N = arr . length ; constructArray ( arr , N ) ;
Find the element at specified index in a Spiral Matrix | Function to the find element at ( i , j ) index ; Driver Code ; Function Call
function findInGrid ( i , j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } let i = 3 , j = 4 ; document . write ( findInGrid ( i , j ) ) ;
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements | Function to find minimum count of operations required to convert all array elements to zero br replacing subsequence of equal elements to 0 ; Store distinct elements present in the array ; Traverse the array ; If arr [ i ] is already present in the Set or arr [ i ] is equal to 0 ; Otherwise , increment ans by 1 and insert current element ; Given array ; Size of the given array
function minOpsToTurnArrToZero ( arr , N ) { var st = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( st . has ( arr [ i ] ) arr [ i ] == 0 ) { continue ; } else { st . add ( arr [ i ] ) ; } } document . write ( st . size ) } var arr = [ 3 , 7 , 3 ] ; var N = arr . length ; minOpsToTurnArrToZero ( arr , N ) ;
Split an array into equal length subsequences consisting of equal elements only | Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Store the GCD of frequencies of all array elements ; Traverse the map ; Update GCD ; If the GCD is greater than 1 , print YES otherwise print NO ; Given array ; Store the size of the array
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function splitArray ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var G = 0 ; mp . forEach ( ( value , key ) => { G = gcd ( G , value ) ; } ) ; if ( G > 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] ; var n = arr . length ; splitArray ( arr , n ) ;
Minimize count of given operations required to be performed to make all array elements equal to 1 | Function to check if all array elements are equal or not ; Traverse the array ; If all array elements are not equal ; Function to find minimum count of operation to make all the array elements equal to 1 ; Stores largest element of the array ; Check if a number is a power of 2 or not ; If Max is a power of 2 and all array elements are equal ; Driver Code
function CheckAllEqual ( arr , N ) { for ( i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return false ; } } return true ; } function minCntOperations ( arr , N ) { var Max = Math . max . apply ( Math , arr ) ; var isPower2 ; if ( parseInt ( ( Math . ceil ( ( Math . log ( N ) / Math . log ( N ) ) ) ) ) == parseInt ( ( Math . floor ( ( ( Math . log ( N ) / Math . log ( 2 ) ) ) ) ) ) ) { isPower2 = true ; } else { isPower2 = false ; } if ( isPower2 && CheckAllEqual ( arr , N ) ) { return parseInt ( ( Math . log ( Max ) / Math . log ( 2 ) ) ) ; } else { return parseInt ( Math . ceil ( Math . log ( Max ) / Math . log ( 2 ) ) ) + 1 ; } } var arr = [ 2 , 4 ] ; var N = arr . length ; document . write ( minCntOperations ( arr , N ) ) ;
Largest number not exceeding N that does not contain any of the digits of S | Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver Code
function greatestReducedNumber ( num , s ) { let vis_s = [ false , false , false , false , false , false , false , false , false , false ] ; for ( let i = 0 ; i < s . length ; i ++ ) { vis_s [ Number ( s [ i ] ) - 48 ] = true ; } let n = num . length ; let inn = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis_s [ Number ( num [ i ] ) - ' ' ] ) { inn = i ; break ; } } if ( inn == - 1 ) { return num ; } for ( let dig = String ( num [ inn ] ) ; dig >= ' ' ; dig -- ) { if ( vis_s [ Number ( dig ) - ' ' ] == 0 ) { num [ inn ] = dig ; break ; } } let LargestDig = ' ' ; for ( let dig = ' ' ; dig >= ' ' ; dig -- ) { if ( vis_s [ dig - ' ' ] == false ) { LargestDig = dig ; break ; } } for ( let i = inn + 1 ; i < n ; i ++ ) { num [ i ] = LargestDig ; } let Count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( num [ i ] == ' ' ) Count ++ ; else break ; } num = Number ( num ) . toString ( ) ; if ( num . length == 0 ) return " " ; return num ; } let N = " " ; let S = " " ; document . write ( greatestReducedNumber ( N , S ) ) ;
Generate an array of minimum sum whose XOR of same | Function to generate an array whose XOR with same - indexed elements of the given array is always a prime ; Traverse the array ; If current array element is 2 ; Print its XOR with 3 ; Otherwise ; Print its XOR with 2 ; Given array ; Size of the array ; Prints the required array
function minXOR ( Arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { if ( Arr [ i ] == 2 ) { document . write ( ( Arr [ i ] ^ 3 ) + " " ) ; } else { document . write ( ( Arr [ i ] ^ 2 ) + " " ) ; } } } let Arr = [ 5 , 4 , 7 , 6 ] ; let N = Arr . length ; minXOR ( Arr , N ) ;