codeparrot/xlcost-text-to-code huggingface dataset:
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver Code ; Query 1 ; Query 2 ; Query 3
function update ( res , L , R , K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } function display ( arr , res , n ) { let i ; for ( i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( i = 0 ; i < n ; i ++ ) { document . write ( ( arr [ i ] ^ res [ i ] ) + " " ) ; } document . write ( " " ) ; } let arr = [ 2 , 4 , 6 , 8 , 10 ] ; let N = arr . length ; let res = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ;
Shortest subarray to be removed to make all Array elements unique | Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver Code
function check ( a , n , k ) { let m = new Map ( ) ; let extra = 0 ; for ( let i = k ; i < n ; i ++ ) m . set ( a [ i ] , m . get ( a [ i ] ) == null ? 1 : m . get ( a [ i ] ) + 1 ) ; for ( let x of m . values ( ) ) extra += x - 1 ; if ( extra == 0 ) return true ; for ( let i = 1 ; i + k - 1 < n ; i ++ ) { if ( m . get ( a [ i + k - 1 ] ) > 1 ) extra -- ; m . set ( a [ i + k - 1 ] , m . get ( a [ i + k - 1 ] ) - 1 ) ; m . set ( a [ i - 1 ] , m . get ( a [ i - 1 ] ) + 1 ) ; if ( m . get ( a [ i - 1 ] ) > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } function minlength ( a , n ) { let lo = 0 , hi = n + 1 ; let ans = 0 ; while ( lo < hi ) { let mid = Math . floor ( ( lo + hi ) / 2 ) ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } let a = [ 1 , 2 , 1 , 2 , 3 ] ; let n = a . length ; document . write ( minlength ( a , n ) ) ;
Find N distinct integers with zero sum | Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; print a extra 0 if N is odd ; Driver code
function findNumbers ( N ) { for ( var i = 1 ; i <= N / 2 ; i ++ ) { document . write ( i + " " + - i + " " ) ; } if ( N % 2 == 1 ) document . write ( 0 ) ; } var N = 10 ; findNumbers ( N ) ;
Program to calculate Electricity Bill | Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
function calculateBill ( units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } var units = 250 ; document . write ( calculateBill ( units ) ) ;
Find all divisors of first N natural numbers | Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
function factors ( n ) { let i , j ; document . write ( " " ) ; for ( i = 2 ; i <= n ; i ++ ) { document . write ( i + " " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { document . write ( j + " " ) ; if ( parseInt ( i / j ) != j ) document . write ( parseInt ( i / j ) + " " ) ; } } document . write ( " " ) ; } } let n = 5 ; factors ( n ) ;
Find all divisors of first N natural numbers | Javascript implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
var MAX = 100000 ; var divisor = Array . from ( Array ( MAX + 1 ) , ( ) => Array ( 0 ) ) ; function sieve ( ) { for ( var i = 1 ; i <= MAX ; ++ i ) { for ( var j = i ; j <= MAX ; j += i ) divisor [ j ] . push ( i ) ; } } function findNFactors ( n ) { for ( var i = 1 ; i <= n ; i ++ ) { document . write ( i + " " ) ; for ( var j = 0 ; j < divisor [ i ] . length ; j ++ ) { document . write ( divisor [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } var n = 5 ; sieve ( ) ; findNFactors ( n ) ;
Maximum number of prime factors a number can have with exactly x factors | Function to count number of prime factors of x ; Count variable is incremented for every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
function countPrimeFactors ( n ) { if ( n == 1 ) return 0 ; let cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = parseInt ( n / 2 ) ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = parseInt ( n / i ) ; } } if ( n > 2 ) cnt ++ ; return cnt ; } let x = 8 ; let prime_factor_cnt = countPrimeFactors ( x ) ; document . write ( prime_factor_cnt ) ;
Count of N digit palindromic numbers divisible by 9 | Function to find the count of N digits palindromic numbers which are divisible by 9 ; if N is odd ; if N is even ; Driver Code
function countPalindromic ( n ) { var count ; if ( n % 2 == 1 ) { count = Math . pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = Math . pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } var n = 3 ; document . write ( countPalindromic ( n ) ) ;
Count of sub | Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver Code ; Function call
function countSubArrayWithOddProduct ( A , N ) { var count = 0 ; var last = - 1 ; var K = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } var arr = [ 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 ] ; var n = arr . length ; document . write ( countSubArrayWithOddProduct ( arr , n ) ) ;
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | Javascript program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver Code
function CgpaCalc ( marks , n ) { let grade = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let cgpa , sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { grade [ i ] = ( marks [ i ] / 10 ) ; } for ( let i = 0 ; i < n ; i ++ ) { sum += grade [ i ] ; } cgpa = sum / n ; return cgpa ; } let n = 5 ; let marks = [ 90 , 80 , 70 , 80 , 90 ] ; let cgpa = CgpaCalc ( marks , n ) ; document . write ( " " + cgpa + " " ) ; document . write ( " " + ( cgpa * 9.5 ) . toFixed ( 2 ) ) ;
Find the subarray of size K with minimum XOR | Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; let k = 3 ; Subarray size ; Function Call
function findMinXORSubarray ( arr , n , k ) { if ( n < k ) return ; let res_index = 0 ; let curr_xor = 0 ; for ( let i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; let min_xor = curr_xor ; for ( let i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } document . write ( min_xor + " " ) ; } let arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] ; let n = arr . length ; findMinXORSubarray ( arr , n , k ) ;
Greatest number that can be formed from a pair in a given Array | Function to find the greatest number formed from the pairs ; first append Y at the end of X ; then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
function getNumber ( a , b ) { var X = a . toString ( ) ; var Y = b . toString ( ) ; var XY = X + Y ; var YX = Y + X ; return XY > YX ? XY : YX ; } function printMaxPair ( arr , n ) { var largest = - 100000000 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) { var number = parseInt ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = Math . max ( largest , number ) ; } document . write ( largest ) ; } var a = [ 23 , 14 , 16 , 25 , 3 , 9 ] ; var n = a . length ; printMaxPair ( a , n ) ;
Check whether two numbers are in golden ratio | Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver Code ; Function Call
function checkGoldenRatio ( a , b ) { if ( a <= b ) { let temp = a ; a = b ; b = temp ; } let ratio1 = ( a / b ) . toFixed ( 3 ) ; let ratio2 = ( ( a + b ) / a ) . toFixed ( 3 ) ; if ( ( ratio1 == ratio2 ) && ratio1 == " " ) { document . write ( " " ) ; return true ; } else { document . write ( " " ) ; return false ; } } let a = 0.618 ; let b = 1 ; checkGoldenRatio ( a , b ) ;
Find the minimum number to be added to N to make it a power of K | Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
function minNum ( n , k ) { var x = parseInt ( Math . log ( n ) / Math . log ( k ) ) + 1 ; var mn = Math . pow ( k , x ) - n ; return mn ; } var n = 20 , k = 5 ; document . write ( minNum ( n , k ) ) ;
Previous perfect square and cube number smaller than number N | Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
function previousPerfectSquare ( N ) { let prevN = Math . floor ( Math . sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } function previousPerfectCube ( N ) { let prevN = Math . floor ( Math . cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } let n = 30 ; document . write ( previousPerfectSquare ( n ) + " " ) ; document . write ( previousPerfectCube ( n ) + " " ) ;
Find the count of even odd pairs in a given Array | Function to count the pairs in array of the form ( even , odd ) ; variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
function findCount ( arr , n ) { let res = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } let a = [ 5 , 4 , 1 , 2 , 3 ] ; let n = a . length ; document . write ( findCount ( a , n ) ) ;
Find the count of even odd pairs in a given Array | Function to count the pairs in array of the form ( even , odd ) ; check if number is even or not ; Driver code
function findCount ( arr , n ) { let count = 0 , ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count ++ ; else ans = ans + count ; } return ans ; } let a = [ 5 , 4 , 1 , 2 , 3 ] ; let n = a . length ; document . write ( findCount ( a , n ) ) ;
Product of all non repeating Subarrays of an Array | Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver Code
function product ( arr ) { let occurrence = Math . pow ( 2 , arr . length - 1 ) ; let product = 1 ; for ( let i = 0 ; i < arr . length ; i ++ ) { product *= Math . pow ( arr [ i ] , occurrence ) ; } return product ; } let arr = [ 10 , 3 , 7 ] ; document . write ( product ( arr ) ) ;
Sum of all N | Javascript program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
let MOD = 1000000007 ; function power ( a , b ) { if ( b == 0 ) return 1 ; let temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } function palindromicSum ( N ) { let sum = 0 , res , ways ; if ( N == 1 ) { document . write ( " " + " " ) ; return ; } if ( N == 2 ) { document . write ( " " + " " ) ; return ; } ways = Math . floor ( N / 2 ) ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } document . write ( sum + " " ) ; } let N = 3 ; palindromicSum ( N ) ;
Maximize array sum by concatenating corresponding elements of given two arrays | Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
function joinNumbers ( numA , numB ) { var revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = parseInt ( numB / 10 ) ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = parseInt ( revB / 10 ) ; } return numA ; } function findMaxSum ( A , B , n ) { var maxArr = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; ++ i ) { var X = joinNumbers ( A [ i ] , B [ i ] ) ; var Y = joinNumbers ( B [ i ] , A [ i ] ) ; var mx = Math . max ( X , Y ) ; maxArr [ i ] = mx ; } var maxAns = 0 ; for ( var i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } var N = 5 ; var A = [ 11 , 23 , 38 , 43 , 59 ] ; var B = [ 36 , 24 , 17 , 40 , 56 ] ; document . write ( findMaxSum ( A , B , N ) ) ;
Split N natural numbers into two sets having GCD of their sums greater than 1 | Function to create and print the two sets ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code
function createSets ( n ) { if ( n <= 2 ) { document . write ( " " ) ; return ; } else { let x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; document . write ( x + " " ) ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; document . write ( i + " " ) ; } } return ; } let N = 7 ; createSets ( N ) ;
Count the factors of K present in the given Array | Function to find the count of factors of K present in array ; Loop to consider every element of array ; Driver Code ; Function Call
function calcCount ( arr , n , k ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 ) count ++ ; } return count ; } var arr = [ 1 , 2 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 6 ; document . write ( calcCount ( arr , n , k ) ) ;
Length of largest subarray whose all elements are Perfect Number | Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to return the length of the largest sub - array of an array every element of whose is a perfect number ; Check if arr [ i ] is a perfect number ; Driver Code
function isPerfect ( n ) { let sum = 1 ; let i ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } function contiguousPerfectNumber ( arr , n ) { let current_length = 0 ; let max_length = 0 ; let i ; for ( i = 0 ; i < n ; i ++ ) { if ( isPerfect ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } let arr = [ 1 , 7 , 36 , 4 , 6 , 28 , 4 ] ; let n = arr . length ; document . write ( contiguousPerfectNumber ( arr , n ) ) ;
Length of largest subarray whose all elements Powerful number | Function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver Code
function isPowerful ( n ) { while ( n % 2 == 0 ) { let power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( let factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { let power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } function contiguousPowerfulNumber ( arr , n ) { let current_length = 0 ; let max_length = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } let arr = [ 1 , 7 , 36 , 4 , 6 , 28 , 4 ] ; let n = arr . length ; document . write ( contiguousPowerfulNumber ( arr , n ) ) ;
Maximize sum of K corner elements in Array | Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the final answer ; Function to find the maximized sum ; Driver Code
function maxSum ( arr , K , start , end , max_sum ) { if ( K == 0 ) return max_sum ; let max_sum_start = max_sum + arr [ start ] ; let max_sum_end = max_sum + arr [ end ] ; let ans = Math . max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } function maximizeSum ( arr , K , n ) { let max_sum = 0 ; let start = 0 ; let end = n - 1 ; document . write ( maxSum ( arr , K , start , end , max_sum ) ) ; } let arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] ; let K = 3 ; let n = arr . length ; maximizeSum ( arr , K , n ) ;
Maximize sum of K corner elements in Array | Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the final result ; Driver code
function maxPointCount ( arr , k , size ) { let curr_points = 0 ; let max_points = 0 ; for ( let i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; let j = size - 1 ; for ( let i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = Math . max ( curr_points , max_points ) ; j -- ; } return max_points ; } let arr = [ 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 ] ; let K = 3 ; let n = arr . length ; document . write ( maxPointCount ( arr , K , n ) ) ;
Find the total count of numbers up to N digits in a given base B | Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver code
function count ( N , B ) { var sum = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { sum += ( B - 1 ) * Math . pow ( B , i - 1 ) ; } return sum ; } var N = 2 , B = 10 ; document . write ( count ( N , B ) ) ;
Find smallest perfect square number A such that N + A is also a perfect square number | JavaScript code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
function SmallestPerfectSquare ( N ) { let X = 1e9 ; let ans ; for ( let i = 1 ; i < Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { let a = i ; let b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = Math . min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = - 1 ; return ans ; } let N = 3 ; document . write ( SmallestPerfectSquare ( N ) ) ;
Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the final result ; Driver code
function minCost ( arr , n , circumference ) { arr . sort ( ( a , b ) => a - b ) var arr2 = Array ( 2 * n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } var res = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) res = Math . min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } var arr = [ 19 , 3 , 6 ] ; var n = arr . length ; var circumference = 20 ; document . write ( minCost ( arr , n , circumference ) ) ;
Unique element in an array where all elements occur K times except one | Set 2 | Function to find single occurrence element ; By shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; Taking modulo of p with k ; Generate result ; Loop for negative numbers ; Check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; Driver code ; Function call
function findunique ( a , k ) { var res = 0 ; for ( var i = 0 ; i < 32 ; i ++ ) { var p = 0 ; for ( var j = 0 ; j < a . length ; j ++ ) { p += ( Math . abs ( a [ j ] ) & ( 1 << i ) ) != 0 ? 1 : 0 ; } p %= k ; res += Math . pow ( 2 , i ) * p ; } var c = 0 ; for ( var i = 0 ; i < a . length ; i ++ ) if ( a [ i ] == res ) { c = 1 ; break ; } return c == 1 ? res : - res ; } var a = [ 12 , 12 , 2 , 2 , 3 ] ; var k = 2 ; document . write ( findunique ( a , k ) + " " ) ;