codeparrot/xlcost-text-to-code huggingface dataset:
Make all elements zero by decreasing any two elements by one at a time | Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
function checkZeroArray ( arr , n ) { let sum = 0 , maximum = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } let arr = [ 1 , 2 , 1 , 2 , 2 ] ; let n = arr . length ; if ( checkZeroArray ( arr , n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Subarray permutation that satisfies the given condition | Function that returns true if the required subarray exists in the given array ; Map to store the positions of each integer in the original permutation ; To store the address of each entry in arr [ n ] but with 1 - based indexing ; To track minimum position sumcur for sum of all positions till this position ; Summing up addresses ; Tracking minimum address encountered till now ; The sum of the addresses if it forms the required subarray ; If current sum of address is equal to val ; Driver code
function subArray ( arr , n , m ) { var i ; var mp = new Map ( ) ; for ( i = 0 ; i < n ; i ++ ) { mp . set ( arr [ i ] , i + 1 ) ; } var sumcur = 0 ; var p = 1000000000 ; var ans = [ ] ; for ( i = 1 ; i <= m ; i ++ ) { sumcur += mp . get ( i ) ; p = Math . min ( p , mp . get ( i ) ) ; var val = p * i - i + parseInt ( ( i * ( i + 1 ) ) / 2 ) ; if ( i == m ) { if ( val == sumcur ) { return true ; } else return false ; } } } var arr = [ 4 , 5 , 1 , 3 , 2 , 6 ] ; var n = arr . length ; var m = 3 ; if ( subArray ( arr , n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of all N digit palindrome numbers | Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
function isPalindrome ( s ) { var left = 0 , right = s . length - 1 ; while ( left <= right ) { if ( s [ left ] != s [ right ] ) { return false ; } left ++ ; right -- ; } return true ; } function getSum ( n ) { var start = Math . pow ( 10 , n - 1 ) ; var end = Math . pow ( 10 , n ) - 1 ; var sum = 0 ; for ( var i = start ; i <= end ; i ++ ) { var s = ( i . toString ( ) ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } var n = 1 ; var ans = getSum ( n ) ; document . write ( ans + " " ) ;
Sum of all N digit palindrome numbers | Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
function getSum ( n ) { var sum = 0 ; if ( n == 1 ) { sum = 45.0 ; } else { sum = ( 99.0 / 2.0 ) * Math . pow ( 10 , n - 1 ) * Math . pow ( 10 , parseInt ( ( n - 1 ) / 2 ) ) ; } return sum ; } var n = 3 ; var ans = getSum ( n ) ; document . write ( ans + " " ) ;
Longest sub | Function to return the length of the largest subarray with maximum possible GCD ; To store the maximum number present in the array ; Finding the maximum element ; To store the final answer ; Two pointer ; Running a loop from j = i ; Condition for incrementing ' j ' ; Updating the answer ; Driver code
function findLength ( arr , n ) { var x = 0 ; for ( var i = 0 ; i < n ; i ++ ) x = Math . max ( x , arr [ i ] ) ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; var j = i ; while ( arr [ j ] == x ) j ++ ; ans = Math . max ( ans , j - i ) ; } return ans ; } var arr = [ 1 , 2 , 2 ] ; var n = arr . length ; document . write ( findLength ( arr , n ) ) ;
Number of subsequences with positive product | Function to return the count of all the subsequences with positive product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; For the empty subsequence ; Driver code
function cntSubSeq ( arr , n ) { let pos_count = 0 ; let neg_count = 0 ; let result ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } let arr = [ 2 , - 3 , - 1 , 4 ] ; let n = arr . length ; document . write ( cntSubSeq ( arr , n ) ) ;
Smallest number dividing minimum number of elements in the array | Set 2 | Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code
function findMin ( arr , n ) { var m = 0 ; for ( var i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; var freq = Array ( m + 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( var i = 1 ; i <= m + 1 ; i ++ ) { var j = i ; var cnt = 0 ; while ( j <= m ) { cnt += freq [ j ] ; j += i ; } if ( ! cnt ) return i ; } return m + 1 ; } var arr = [ 2 , 12 , 6 ] ; var n = arr . length ; document . write ( findMin ( arr , n ) ) ;
Smallest number dividing minimum number of elements in the Array | Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code
function findMin ( arr , n ) { var m = 0 ; for ( var i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; var cnt = Array ( m + 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else cnt [ j ] ++ , cnt [ arr [ i ] / j ] ++ ; } } } for ( var i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } var arr = [ 2 , 12 , 6 ] ; var n = arr . length ; document . write ( findMin ( arr , n ) ) ;
Remove all the prime numbers from the given array | Javascript implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the elements of the array ; Function to remove all the prime numbers ; Generate primes ; Traverse the array ; If the current element is prime ; Shift all the elements on the right of it to the left ; Decrease the loop counter by 1 to check the shifted element ; Decrease the length ; Print the updated array ; Driver code
const sz = 1e5 ; let isPrime = new Array ( sz + 1 ) ; function sieve ( ) { isPrime . fill ( true ) isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( let i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } function printArray ( arr , len ) { for ( let i = 0 ; i < len ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } function removePrimes ( arr , len ) { sieve ( ) ; for ( let i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( let j = i ; j < len ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } let arr = [ 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 ] ; let len = arr . length ; removePrimes ( arr , len ) ;
Remove one element to get minimum OR value | Function to return the minimized OR after removing an element from the array ; Base case ; Prefix and suffix OR array ; Computing prefix / suffix OR arrays ; To store the final answer ; Finding the final answer ; Returning the final answer ; Driver code
function minOR ( arr , n ) { if ( n == 1 ) return 0 ; var pre = Array ( n ) , suf = Array ( n ) ; pre [ 0 ] = arr [ 0 ] , suf [ n - 1 ] = arr [ n - 1 ] ; for ( var i = 1 ; i < n ; i ++ ) pre [ i ] = ( pre [ i - 1 ] arr [ i ] ) ; for ( var i = n - 2 ; i >= 0 ; i -- ) suf [ i ] = ( suf [ i + 1 ] arr [ i ] ) ; var ans = Math . min ( pre [ n - 2 ] , suf [ 1 ] ) ; for ( var i = 1 ; i < n - 1 ; i ++ ) ans = Math . min ( ans , ( pre [ i - 1 ] suf [ i + 1 ] ) ) ; return ans ; } var arr = [ 1 , 2 , 3 ] ; var n = arr . length ; document . write ( minOR ( arr , n ) ) ;
Check if there exists a prime number which gives Y after being repeatedly subtracted from X | Function that returns true if any prime number satisfies the given conditions ; No such prime exists ; Driver code
function isPossible ( x , y ) { if ( ( x - y ) == 1 ) return false ; return true ; } var x = 100 , y = 98 ; if ( isPossible ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find number of square of area Z which can be built in a matrix having blocked regions | Function to calculate the number of square areas of size K * K ; Row array and column array to store the lengths of differences between consecutive rows / columns ; Fill the conrow vector ; Fill the concol vector ; To store the required answer ; Every pair of row size and column size would result in an unblocked region ; Driver code
function subgrids ( N , Z , row , col , r , c ) { var conrow = [ ] ; var concol = [ ] ; var K = Math . sqrt ( Z ) ; conrow . push ( row [ 0 ] - 0 - 1 ) ; conrow . push ( N + 1 - row [ r - 1 ] - 1 ) ; for ( var i = 1 ; i < r ; i ++ ) { conrow . push ( row [ i ] - row [ i - 1 ] - 1 ) ; } concol . push ( col [ 0 ] - 0 - 1 ) ; concol . push ( N + 1 - col - 1 ) ; for ( var i = 1 ; i < c ; i ++ ) { concol . push ( col [ i ] - col [ i - 1 ] - 1 ) ; } var row_size = conrow . length ; var col_size = concol . length ; var answer = 0 ; for ( var i = 0 ; i < row_size ; i ++ ) { for ( var j = 0 ; j < col_size ; j ++ ) { var total = parseInt ( concol [ j ] / K ) * parseInt ( conrow [ i ] / K ) ; answer += ( total ) ; } } return answer ; } var N = 8 , Z = 4 ; var row = [ 4 , 6 ] ; var col = [ 3 , 8 ] ; var r = row . length ; var c = col . length ; document . write ( subgrids ( N , Z , row , col , r , c ) ) ;
Count of numbers whose sum of increasing powers of digits is equal to the number itself | Function to return the count of digits of n ; Function to return the sum of increasing powers of N ; To store the required answer ; Count of digits in n which will be the power of the last digit ; While there are digits left ; Get the last digit ; Add the last digit after raising it to the required power ; Decrement the power for the previous digit ; Remove the last digit ; Function to return the count of integers which satisfy the given conditions ; If current element satisfies the given condition ; Driver code
function countDigits ( n ) { let cnt = 0 ; while ( n > 0 ) { cnt ++ ; n = Math . floor ( n / 10 ) ; } return cnt ; } function digitPowSum ( n ) { let sum = 0 ; let pw = countDigits ( n ) ; while ( n > 0 ) { let d = n % 10 ; sum += Math . pow ( d , pw ) ; pw -- ; n = Math . floor ( n / 10 ) ; } return sum ; } function countNum ( n ) { let count = 0 ; for ( let i = 0 ; i <= n ; i ++ ) { if ( i == digitPowSum ( i ) ) { count ++ ; } } return count ; } let n = 200 ; document . write ( countNum ( n ) ) ;
Print all perfect squares from the given range | Function to print all the perfect squares from the given range ; For every element from the range ; If current element is a perfect square ; Driver code
function perfectSquares ( l , r ) { for ( let i = l ; i <= r ; i ++ ) { if ( Math . sqrt ( i ) == parseInt ( Math . sqrt ( i ) ) ) document . write ( i + " " ) ; } } let l = 2 ; let r = 24 ; perfectSquares ( l , r )
Print all perfect squares from the given range | Function to print all the perfect squares from the given range ; Getting the very first number ; First number 's square ; Next number is at the difference of ; While the perfect squares are from the range ; Print the perfect square ; Get the next perfect square ; Next odd number to be added ; Driver code
function perfectSquares ( l , r ) { let number = Math . ceil ( Math . sqrt ( l ) ) ; let n2 = number * number ; number = ( number * 2 ) + 1 ; while ( ( n2 >= l && n2 <= r ) ) { document . write ( n2 + " " ) ; n2 = n2 + number ; number += 2 ; } } let l = 2 , r = 24 ; perfectSquares ( l , r ) ;
Find the value of N XOR 'ed to itself K times | Function to return n ^ n ^ ... k times ; Find the result ; Driver code
function xorK ( n , k ) { let res = n ; for ( let i = 1 ; i < k ; i ++ ) res = ( res ^ n ) ; return n ; } let n = 123 , k = 3 ; document . write ( xorK ( n , k ) ) ;
Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code
function findRemainders ( n ) { var vc = new Set ( ) ; for ( var i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( parseInt ( n / i ) ) ; for ( var i = parseInt ( n / Math . ceil ( Math . sqrt ( n ) ) ) - 1 ; i >= 0 ; i -- ) vc . add ( i ) ; [ ... vc ] . sort ( ( a , b ) => a - b ) . forEach ( it => { document . write ( it + " " ) ; } ) ; } var n = 5 ; findRemainders ( n ) ;
Count of primes below N which can be expressed as the sum of two primes | javascript implementation of the approach ; Function for Sieve of Eratosthenes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the count of primes less than or equal to n which can be expressed as the sum of two primes ; To store the required count ; If the integer is prime and it can be expressed as the sum of 2 and a prime number ; Driver code
var MAX = 100005 ; var prime = new Array ( MAX ) . fill ( false ) ; function SieveOfEratosthenes ( ) { for ( i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } function countPrimes ( n ) { SieveOfEratosthenes ( ) ; var cnt = 0 ; for ( i = 2 ; i < n ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) cnt ++ ; } return cnt ; } var n = 11 ; document . write ( countPrimes ( n ) ) ;
Find an integer in the given range that satisfies the given conditions | javascript implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code
var MAX = 10 ; function distinctDigits ( x ) { var present = new Array ( MAX ) . fill ( false ) ; while ( x > 0 ) { var digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x = parseInt ( x / 10 ) ; } return true ; } function findK ( l , r ) { var maxExp = Number . MIN_VALUE ; var k = - 1 ; for ( var i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { var exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } var l = 50 , r = 60 ; document . write ( findK ( l , r ) ) ;
Restore a permutation from the given helper array | Function to find the required permutation ; Each element in P ' is like a  cumulative sum in Q ; minval is the minimum value in P ' ; To check if each entry in P is from the range [ 1 , n ] ; Invalid permutation ; If a valid permutation exists ; Print the permutation ; No valid permutation ; Driver code
function findPerm ( Q , n ) { var minval = 0 , qsum = 0 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { qsum += Q [ i ] ; if ( qsum < minval ) minval = qsum ; } var P = Array ( n ) ; P [ 0 ] = 1 - minval ; var permFound = true ; for ( var i = 0 ; i < n - 1 ; i ++ ) { P [ i + 1 ] = P [ i ] + Q [ i ] ; if ( P [ i + 1 ] > n P [ i + 1 ] < 1 ) { permFound = false ; break ; } } if ( permFound ) { for ( var i = 0 ; i < n ; i ++ ) { document . write ( P [ i ] + " " ) ; } } else { document . write ( - 1 ) ; } } var Q = [ - 2 , 1 ] ; var n = 1 + Q . length ; findPerm ( Q , n ) ;
Convert Decimal To Hexa | Function to convert decimal no . to hexadecimal number ; map for decimal to hexa , 0 - 9 are straightforward , alphabets a - f used for 10 to 15. ; string to be returned ; check if num is 0 and directly return "0" ; if num > 0 , use normal technique as discussed in other post ; if num < 0 , we need to use the elaborated trick above , lets see this ; store num in a u_int , size of u_it is greater , it will be positive since msb is 0 ; use the same remainder technique . ; Driver Code
function Hex ( num ) { let m = new Map ( ) ; let digit = ' ' . charCodeAt ( 0 ) ; let c = ' ' . charCodeAt ( 0 ) ; for ( let i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m . set ( i , String . fromCharCode ( digit ) ) ; digit ++ ; } else { m . set ( i , String . fromCharCode ( c ) ) ; c ++ ; } } let res = " " ; if ( num == 0 ) { return " " ; } if ( num > 0 ) { while ( num != 0 ) { res = m . get ( num % 16 ) + res ; num = Math . floor ( num / 16 ) ; } } else { let n = num + Math . pow ( 2 , 32 ) ; while ( n != 0 ) { res = m . get ( n % 16 ) + res ; n = Math . floor ( n / 16 ) ; } } return res ; } let x = 134 , y = - 1 , z = - 234 ; document . write ( " " ) ; document . write ( x + " " + Hex ( x ) + " " ) ; document . write ( y + " " + Hex ( y ) + " " ) ; document . write ( z + " " + Hex ( z ) + " " ) ;
Find the player who will win the Coin game | Function to check the wining player ; As discussed in the above approach ; Driver function
function findWinner ( n ) { if ( ( n - 1 ) % 6 == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var n = 7 ; findWinner ( n ) ;
Minimum inversions required so that no two adjacent elements are same | Function to return the minimum inversions required so that no two adjacent elements are same ; To store the inversions required to make the array { 1 , 0 , 1 , 0 , 1 , 0 , 1 , ... } and { 0 , 1 , 0 , 1 , 0 , 1 , 0 , ... } respectively ; Find all the changes required ; Return the required answer ; Driver code
function min_changes ( a , n ) { let ans_a = 0 , ans_b = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . min ( ans_a , ans_b ) ; } let a = [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 ] ; let n = a . length ; document . write ( min_changes ( a , n ) ) ;
Sum of all the numbers present at given level in Modified Pascal ’ s triangle | Function to calculate sum ; Driver Code
function ans ( n ) { if ( n == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var n = 2 ; ans ( n ) ;
Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code
function isPossible ( w , h , x , y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } let w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimize the number by changing at most K digits | Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
function minNum ( num , k ) { let len = num . length ; if ( len == 0 k == 0 ) { let num_str = num . join ( " " ) ; return num_str ; } if ( len == 1 ) return " " ; if ( num [ 0 ] != ' ' ) { num [ 0 ] = ' ' ; k -- ; } let i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != ' ' ) { num [ i ] = ' ' ; k -- ; } i ++ ; } let num_str = num . join ( " " ) ; return num_str ; } let num = " " ; let k = 3 ; document . write ( minNum ( num . split ( " " ) , k ) ) ;
Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M | Function to calculate the minimum number of steps required total steps taken is divisible by m and only 1 or 2 steps can be taken at a time ; If m > n ans is - 1 ; else discussed above approach ; Driver code
function minsteps ( n , m ) { if ( m > n ) { return - 1 ; } else { return ( ( n + 1 ) / 2 + m - 1 ) / m * m ; } } var n = 17 , m = 4 ; var ans = minsteps ( n , m ) ; document . write ( ans ) ;
Compare numbers represented by Linked Lists | Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not null and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; The constructed linked lists are : a : 5 -> 6 -> 7 b : 2 -> 3 -> 3
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function removeLeadingZeros ( a ) { if ( a != null && a . data == 0 ) return removeLeadingZeros ( a . next ) ; else return a ; } function getSize ( a ) { let sz = 0 ; while ( a != null ) { a = a . next ; sz ++ ; } return sz ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function compare ( a , b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; let lenA = getSize ( a ) ; let lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return - 1 ; } while ( a != null && b != null ) { if ( a . data > b . data ) return 1 ; else if ( a . data < b . data ) return - 1 ; a = a . next ; b = b . next ; } return 0 ; } var a = null ; a = push ( a , 7 ) ; a = push ( a , 6 ) ; a = push ( a , 5 ) ; var b = null ; b = push ( b , 3 ) ; b = push ( b , 3 ) ; b = push ( b , 2 ) ; document . write ( compare ( a , b ) ) ;
Sum of the updated array after performing the given operation | Utility function to return the sum of the array ; Function to return the sum of the modified array ; Find the sum of the subarray arr [ i + 1. . . n - 1 ] ; Subtract the subarray sum ; Return the sum of the modified array ; Driver code
function sumArr ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function sumModArr ( arr , n ) { for ( i = 0 ; i < n - 1 ; i ++ ) { var subSum = 0 ; for ( j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } var arr = [ 40 , 25 , 12 , 10 ] ; var n = arr . length ; document . write ( sumModArr ( arr , n ) ) ;
Find the minimum possible health of the winning player | Function to return the minimum possible health of the last player ; Find the GCD of the array elements ; Driver code
function minHealth ( health , n ) { var gcd = 0 ; for ( var i = 0 ; i < n ; i ++ ) { gcd = __gcd ( gcd , health [ i ] ) ; } return gcd ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var health = [ 5 , 6 , 1 , 2 , 3 , 4 ] ; var n = health . length ; document . write ( minHealth ( health , n ) ) ;