codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of Bitwise AND of same | Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driven Program ; Function call
function calcScore ( p , arr , N ) { var ans = 0 ; for ( var i = 0 ; i < N ; i ++ ) { ans += ( p [ i ] & arr [ i ] ) ; } return ans ; } function getMaxUtil ( p , arr , ans , chosen , N ) { if ( p . length == N ) { ans = Math . max ( ans , calcScore ( p , arr , N ) ) ; return ans ; } for ( var i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) { continue ; } chosen [ i ] = true ; p . push ( i ) ; ans = getMaxUtil ( p , arr , ans , chosen , N ) ; chosen [ i ] = false ; p . pop ( ) ; } return ans ; } function getMax ( arr , N ) { var ans = 0 ; var chosen = Array ( N ) . fill ( false ) ; var p = [ ] ; var res = getMaxUtil ( p , arr , ans , chosen , N ) ; document . write ( res ) ; } var arr = [ 4 , 2 , 3 , 6 ] ; var N = arr . length ; getMax ( arr , N ) ;
Sum of array elements whose count of set bits are unique | Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array And Update the value of ans ; If frequency is 1 ; Driver Code
function setBitCount ( n ) { let ans = 0 ; while ( n != 0 ) { ans += n & 1 ; n >>= 1 ; } return ans ; } function getSum ( arr , n ) { let mp = new Map ( ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let key = setBitCount ( arr [ i ] ) ; if ( mp . has ( key ) ) mp . set ( key , mp . get ( key ) + 1 ) ; else mp . set ( key , 1 ) ; } for ( let i = 0 ; i < n ; i ++ ) { let key = setBitCount ( arr [ i ] ) ; if ( mp . has ( key ) && mp . get ( key ) == 1 ) ans += arr [ i ] ; } document . write ( ans ) ; } let arr = [ 8 , 3 , 7 , 5 , 3 ] ; let n = arr . length ; getSum ( arr , n ) ;
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing a pair ; Stores prefix sum array ; Copy array elements ; Traverse the array ; Stores sums of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no such pair exists , print - 1 ; Given array ; Size of the array
function findSplit ( arr , N ) { let sum = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { sum [ i ] = arr [ i ] ; } for ( let i = 1 ; i < N ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; } for ( let l = 1 ; l <= N - 4 ; l ++ ) { for ( let r = l + 2 ; r <= N - 2 ; r ++ ) { let lsum = 0 , rsum = 0 , msum = 0 ; lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == rsum && rsum == msum ) { document . write ( l + " " + r + " " ) ; return ; } } } document . write ( - 1 + " " ) ; } let arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; let N = arr . length ; findSplit ( arr , N ) ;
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Given array ; Size of the array
function findSplit ( arr , N ) { let l = 1 , r = N - 2 ; let lsum , msum , rsum ; let sum = new Array ( N ) ; sum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { sum [ i ] = sum [ i - 1 ] + arr [ i ] ; } while ( l < r ) { lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == msum && msum == rsum ) { document . write ( l + " " + r ) ; return ; } if ( lsum < rsum ) l ++ ; else if ( lsum > rsum ) r -- ; else { l ++ ; r -- ; } } document . write ( - 1 ) ; } let arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; let N = arr . length ; findSplit ( arr , N ) ;
Extract Leaves of a Binary Tree in a Doubly Linked List | A binay tree node ; Binary Tree class ; The main function that links the list list to be traversed ; Utility function for printing tree in In - Order . ; Prints the DLL in both forward and reverse directions . ; Driver program to test above functions
class Node { constructor ( val ) { this . data = val ; this . left = null ; this . right = null ; } } var root ; var head ; var prev ; function extractLeafList ( root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) { if ( head == null ) { head = root ; prev = root ; } else { prev . right = root ; root . left = prev ; prev = root ; } return null ; } root . left = extractLeafList ( root . left ) ; root . right = extractLeafList ( root . right ) ; return root ; } function inorder ( node ) { if ( node == null ) return ; inorder ( node . left ) ; document . write ( node . data + " " ) ; inorder ( node . right ) ; } function printDLL ( head ) { var last = null ; while ( head != null ) { document . write ( head . data + " " ) ; last = head ; head = head . right ; } } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . left . left . left = new Node ( 7 ) ; root . left . left . right = new Node ( 8 ) ; root . right . right . left = new Node ( 9 ) ; root . right . right . right = new Node ( 10 ) ; document . write ( " " ) ; inorder ( root ) ; extractLeafList ( root ) ; document . write ( " " ) ; document . write ( " " ) ; printDLL ( head ) ; document . write ( " " ) ; document . write ( " " ) ; inorder ( root ) ;
Minimum initial vertices to traverse whole matrix with given conditions | Javascript program to find minimum initial vertices to reach whole matrix . ; ( n , m ) is current source cell from which we need to do DFS . N and M are total no . of rows and columns . ; Marking the vertex as visited ; If below neighbor is valid and has value less than or equal to current cell 's value ; If right neighbor is valid and has value less than or equal to current cell 's value ; If above neighbor is valid and has value less than or equal to current cell 's value ; If left neighbor is valid and has value less than or equal to current cell 's value ; Storing the cell value and cell indices in a vector . ; Sorting the newly created array according to cell values ; Create a visited array for DFS and initialize it as false . ; Applying dfs for each vertex with highest value ; If the given vertex is not visited then include it in the set ; driver code
var MAX = 100 ; function dfs ( n , m , visit , adj , N , M ) { visit [ n ] [ m ] = 1 ; if ( n + 1 < N && adj [ n ] [ m ] >= adj [ n + 1 ] [ m ] && ! visit [ n + 1 ] [ m ] ) dfs ( n + 1 , m , visit , adj , N , M ) ; if ( m + 1 < M && adj [ n ] [ m ] >= adj [ n ] [ m + 1 ] && ! visit [ n ] [ m + 1 ] ) dfs ( n , m + 1 , visit , adj , N , M ) ; if ( n - 1 >= 0 && adj [ n ] [ m ] >= adj [ n - 1 ] [ m ] && ! visit [ n - 1 ] [ m ] ) dfs ( n - 1 , m , visit , adj , N , M ) ; if ( m - 1 >= 0 && adj [ n ] [ m ] >= adj [ n ] [ m - 1 ] && ! visit [ n ] [ m - 1 ] ) dfs ( n , m - 1 , visit , adj , N , M ) ; } function printMinSources ( adj , N , M ) { var x = [ ] ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < M ; j ++ ) x . push ( [ adj [ i ] [ j ] , [ i , j ] ] ) ; x = x . sort ( ) ; var visit = new Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { visit [ i ] = [ ] ; for ( var j = 0 ; j < MAX ; j ++ ) { visit [ i ] . push ( false ) ; } } for ( var i = x . length - 1 ; i >= 0 ; i -- ) { if ( ! visit [ x [ i ] [ 1 ] [ 0 ] ] [ x [ i ] [ 1 ] [ 1 ] ] ) { document . write ( x [ i ] [ 1 ] [ 0 ] + " " + x [ i ] [ 1 ] [ 1 ] + " " ) ; dfs ( x [ i ] [ 1 ] [ 0 ] , x [ i ] [ 1 ] [ 1 ] , visit , adj , N , M ) ; } } } var N = 2 var M = 2 var adj = [ [ 3 , 3 ] , [ 1 , 1 ] ] printMinSources ( adj , N , M )
Min steps to empty an Array by removing a pair each time with sum at most K | Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Given array arr [ ] ; Given target value ; Function call
function countMinSteps ( arr , target , n ) { arr = arr . sort ( function ( a , b ) { return a - b ; } ) ; var minimumSteps = 0 ; var i = 0 , j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i ++ ; j -- ; } else { j -- ; } minimumSteps ++ ; } return minimumSteps ; } var arr = [ 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 ] ; var target = 11 ; var size = arr . length ; document . write ( countMinSteps ( arr , target , size ) ) ;
Sort the strings based on the numbers of matchsticks required to represent them | Stick [ ] stores the count of sticks required to represent the alphabets ; Number [ ] stores the count of sticks required to represent the numerals ; Function that return the count of sticks required to represent the given string str ; Loop to iterate over every character of the string ; Add the count of sticks required to represent the current character ; Function to sort the array according to the number of sticks required to represent it ; Vector to store the number of sticks required with respective strings ; Inserting number of sticks with respective strings ; Sort the vector , ; Print the sorted vector
var sticks = [ 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 ] ; var number = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; function countSticks ( str ) { var cnt = 0 ; var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) { var ch = str [ i ] ; if ( ch >= ' ' && ch <= ' ' ) { cnt += sticks [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } else { cnt += number [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } } return cnt ; } function sortArr ( arr , int ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] = [ countSticks ( arr [ i ] ) , arr [ i ] ] ; } vp . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } var arr = [ " " , " " , " " ] ; var n = arr . length ; sortArr ( arr , n ) ;
Find if there exists a direction for ranges such that no two range intersect | Javascript implementation of the approach ; Structure to hold details of each interval ; Function that returns true if the assignment of directions is possible ; Sort the intervals based on velocity ; Test the condition for all intervals with same velocity ; If for any velocity , 3 or more intervals share a common point return false ; Driver code
let MAX = 100001 ; class interval { constructor ( ) { this . l = this . r = this . v = 0 ; } } function isPossible ( range , N ) { let test = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { test [ i ] = new interval ( ) ; test [ i ] . l = range [ i ] [ 0 ] ; test [ i ] . r = range [ i ] [ 1 ] ; test [ i ] . v = range [ i ] [ 2 ] ; } test . sort ( function ( a , b ) { return ( a . v < b . v ? 1 : 0 ) ; } ) ; for ( let i = 0 ; i < N ; i ++ ) { let count = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) count [ i ] = 0 ; let current_velocity = test [ i ] . v ; let j = i ; while ( j < N && test [ j ] . v == current_velocity ) { for ( let k = test [ j ] . l ; k <= test [ j ] . r ; k ++ ) { count [ k ] ++ ; if ( count [ k ] >= 3 ) return false ; } j ++ ; } i = j - 1 ; } return true ; } let range = [ [ 1 , 2 , 3 ] , [ 2 , 5 , 1 ] , [ 3 , 10 , 1 ] , [ 4 , 4 , 1 ] , [ 5 , 7 , 10 ] ] let n = range . length ; if ( isPossible ( range , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the kth smallest number with sum of digits as m | Javascript implementation of the approach ; Recursively moving to add all the numbers upto a limit with sum of digits as m ; Max number of digits allowed in a number for this implementation ; Function to return the kth number with sum of digits as m ; The kth smallest number is found ; Driver code
let N = 2005 ; let ans = new Set ( ) ; function dfs ( num , left , ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . add ( num ) ; for ( let i = 0 ; i <= Math . min ( left , 9 ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } function getKthNum ( m , k ) { dfs ( 0 , m , 0 ) ; let ct = 0 ; for ( let it of ans . values ( ) ) { ct ++ ; if ( ct == k ) { return it ; } } return - 1 ; } let m = 5 , k = 3 ; document . write ( getKthNum ( m , k ) ) ;
Remove minimum elements from the array such that 2 * min becomes more than max | Function to calculate upper bound ; Function to remove minimum elements from the array such that 2 * min becomes more than max ; Sort the array ; To store the required answer ; Traverse from left to right ; Update the answer ; Return the required answer ; Driver code ; Function call
function upperBound ( array , value ) { var low = 0 ; var high = array . length ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } function Removal ( v , n ) { v . sort ( ( a , b ) => a - b ) ; var ans = 2147483648 ; var k = 0 ; for ( const i of v ) { var j = upperBound ( v , 2 * i ) ; ans = Math . min ( ans , n - ( j - k ) ) ; k ++ ; } return ans ; } var a = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] ; var n = a . length ; document . write ( Removal ( a , n ) ) ;
Sort the character array based on ASCII % N | This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; pivot ; Index of smaller element ; If current element is smaller than or equal to pivot Instead of values , ASCII % m values are compared ; Increment index of smaller element ; swap ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print the given array ; Driver code ; Sort the given array ; Print the sorted array
function partition ( arr , low , high , mod ) { var pivot = arr [ high ] ; var i = ( low - 1 ) ; var piv = pivot % mod ; for ( var j = low ; j <= high - 1 ; j ++ ) { var a = arr [ j ] % mod ; if ( a <= piv ) { i ++ ; var t = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = t ; } } var t = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = t ; return ( i + 1 ) ; } function quickSort ( arr , low , high , mod ) { if ( low < high ) { var pi = partition ( arr , low , high , mod ) ; quickSort ( arr , low , pi - 1 , mod ) ; quickSort ( arr , pi + 1 , high , mod ) ; } } function printArray ( arr , size ) { for ( var i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; var n = arr . length ; var mod = 8 ; quickSort ( arr , 0 , n - 1 , mod ) ; printArray ( arr , n ) ;
Iterative selection sort for linked list | Structure of a node of linked list ; Utility function to create a new Linked List Node ; Function to sort a linked list using selection sort algorithm by swapping the next pointers ; While b is not the last node ; While d is pointing to a valid node ; If d appears immediately after b ; Case 1 : b is the head of the linked list ; Move d before b ; Swap b and d pointers ; Update the head ; Skip to the next element as it is already in order ; Case 2 : b is not the head of the linked list ; Move d before b ; Swap b and d pointers ; Skip to the next element as it is already in order ; If b and d have some non - zero number of nodes in between them ; Case 3 : b is the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update the head ; Case 4 : b is not the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update c and skip to the next element as it is already in order ; Function to print the list ; Driver Code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( val ) { var temp = new Node ( ) ; temp . data = val ; temp . next = null ; return temp ; } function selectionSort ( head ) { var a , b , c , d , r ; a = b = head ; while ( b . next != null ) { c = d = b . next ; while ( d != null ) { if ( b . data > d . data ) { if ( b . next == d ) { if ( b == head ) { b . next = d . next ; d . next = b ; r = b ; b = d ; d = r ; c = d ; head = b ; d = d . next ; } else { b . next = d . next ; d . next = b ; a . next = d ; r = b ; b = d ; d = r ; c = d ; d = d . next ; } } else { if ( b == head ) { r = b . next ; b . next = d . next ; d . next = r ; c . next = b ; r = b ; b = d ; d = r ; c = d ; d = d . next ; head = b ; } else { r = b . next ; b . next = d . next ; d . next = r ; c . next = b ; a . next = d ; r = b ; b = d ; d = r ; c = d ; d = d . next ; } } } else { c = d ; d = d . next ; } } a = b ; b = b . next ; } return head ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = newNode ( 5 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 3 ) ; head = selectionSort ( head ) ; printList ( head ) ;
Find original numbers from gcd ( ) every pair | Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; Reverse array ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function findNumbers ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; reverse ( arr ) ; let freq = new Array ( arr [ 0 ] + 1 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } let size = parseInt ( Math . sqrt ( n ) , 10 ) ; let brr = new Array ( size ) ; brr . fill ( 0 ) ; let x , l = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] ] > 0 && l < size ) { brr [ l ] = arr [ i ] ; freq [ brr [ l ] ] -- ; l ++ ; for ( let j = 0 ; j < l ; j ++ ) { if ( i != j ) { x = __gcd ( arr [ i ] , brr [ j ] ) ; freq [ x ] -= 2 ; } } } } printArr ( brr , size ) ; } function reverse ( input ) { let last = input . length - 1 ; let middle = parseInt ( input . length / 2 , 10 ) ; for ( let i = 0 ; i <= middle ; i ++ ) { let temp = input [ i ] ; input [ i ] = input [ last - i ] ; input [ last - i ] = temp ; } } function __gcd ( a , b ) { if ( b == 0 ) { return a ; } return __gcd ( b , a % b ) ; } let arr = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 ] ; let n = arr . length ; findNumbers ( arr , n ) ;
Bitwise AND of N binary strings | Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ▁ ' 0 's in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Utility reverse function ; Swap values of left and right ; Driver code
function strBitwiseAND ( arr , n ) { let res = " " ; let smallest_size = Number . MAX_VALUE ; let largest_size = Number . MIN_VALUE ; let temp = " " ; for ( let i = 0 ; i < n ; i ++ ) { temp += arr [ i ] ; arr [ i ] = reverse ( temp ) ; smallest_size = Math . min ( smallest_size , arr [ i ] . length ) ; largest_size = Math . max ( largest_size , arr [ i ] . length ) ; } for ( let i = 0 ; i < smallest_size ; i ++ ) { let all_ones = true ; for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] == ' ' ) { all_ones = false ; break ; } } res += ( all_ones ? ' ' : ' ' ) ; } for ( let i = 0 ; i < largest_size - smallest_size ; i ++ ) res += ' ' ; let temp1 = " " ; temp1 += res ; res = reverse ( temp1 ) ; let temparray1 = res ; let Temparray = " " ; for ( let i = 6 ; i < temparray1 . length ; i ++ ) { Temparray = Temparray + temparray1 [ i ] ; } return Temparray ; } function reverse ( input ) { let temparray = input . split ( ' ' ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return temparray . join ( " " ) ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( strBitwiseAND ( arr , n ) ) ;
Rearrange Odd and Even values in Alternate Fashion in Ascending Order | Javascript implementation of the above approach ; Sort the array ; var v1 = [ ] ; to insert even values var v2 = [ ] ; to insert odd values ; Set flag to true if first element is even ; Start rearranging array ; If first element is even ; Else , first element is Odd ; Print the rearranged array ; Driver code
function AlternateRearrange ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) v1 . push ( arr [ i ] ) ; else v2 . push ( arr [ i ] ) ; var index = 0 , i = 0 , j = 0 ; var flag = false ; if ( arr [ 0 ] % 2 == 0 ) flag = true ; while ( index < n ) { if ( flag == true ) { arr [ index ++ ] = v1 [ i ++ ] ; flag = ! flag ; } else { arr [ index ++ ] = v2 [ j ++ ] ; flag = ! flag ; } } for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ 9 , 8 , 13 , 2 , 19 , 14 ] ; var n = arr . length ; AlternateRearrange ( arr , n ) ;
Convert given array to Arithmetic Progression by adding an element | Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code
function getNumToAdd ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; var d = arr [ 1 ] - arr [ 0 ] ; var numToAdd = - 1 ; var numAdded = false ; for ( var i = 2 ; i < n ; i ++ ) { var diff = arr [ i ] - arr [ i - 1 ] ; if ( diff != d ) { if ( numAdded ) return - 1 ; if ( diff == 2 * d ) { numToAdd = arr [ i ] - d ; numAdded = true ; } else return - 1 ; } } if ( numToAdd == - 1 ) return ( arr [ n - 1 ] + d ) ; return numToAdd ; } var arr = [ 1 , 3 , 5 , 7 , 11 , 13 , 15 ] ; var n = arr . length ; document . write ( getNumToAdd ( arr , n ) ) ;