codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth smallest number having exactly 4 divisors | Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Given Input ; Function Call
function nthNumber ( n ) { let divs = new Array ( 1000000 ) ; for ( var i = 0 ; i < divs . length ; i ++ ) { divs [ i ] = 0 ; } let vis = new Array ( 1000000 ) ; for ( var i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = 0 ; } let cnt = 0 ; for ( let i = 2 ; cnt < n ; i ++ ) { if ( divs [ i ] == 0 ) { for ( let j = 2 * i ; j < 1000000 ; j += i ) { if ( vis [ j ] ) { continue ; } vis [ j ] = true ; let currNum = j ; let count = 0 ; while ( currNum % i == 0 ) { divs [ j ] ++ ; currNum = Math . floor ( currNum / i ) ; count ++ ; } if ( currNum == 1 && count == 3 && divs [ j ] == 3 ) { cnt ++ ; } else if ( currNum != 1 && divs [ currNum ] == 0 && count == 1 && divs [ j ] == 1 ) { cnt ++ ; } if ( cnt == n ) { return j ; } } } } return - 1 ; } let N = 24 ; document . write ( nthNumber ( N ) ) ;
Maximum number formed from the digits of given three numbers | Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Given Input ; Function call
function findkey ( A , B , C ) { let ans = 0 ; let cur = 1 ; while ( A > 0 ) { let a = A % 10 ; let b = B % 10 ; let c = C % 10 ; A = Math . floor ( A / 10 ) ; B = Math . floor ( B / 10 ) ; C = Math . floor ( C / 10 ) ; let m = Math . max ( a , Math . max ( c , b ) ) ; ans += cur * m ; cur = cur * 10 ; } return ans ; } let A = 3521 , B = 2452 , C = 1352 ; document . write ( findkey ( A , B , C ) ) ;
Count of distinct GCDs among all the non | Function to calculate gcd ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; calculate largest number in A and mapping A to Mp ; iterate over all possible values of GCD ; variable to check current GCD ; iterate over all multiples of i ; If j is present in A ; calculate gcd of all encountered multiples of i ; current GCD is possible ; return answer ; Input ; Function calling
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function distinctGCDs ( arr , N ) { let M = - 1 , ans = 0 ; var Mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { M = Math . max ( M , arr [ i ] ) ; Mp . set ( arr [ i ] , 1 ) ; } for ( let i = 1 ; i <= M ; i ++ ) { let currGcd = 0 ; for ( let j = i ; j <= M ; j += i ) { if ( Mp . has ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } let arr = [ 3 , 11 , 14 , 6 , 12 ] ; let N = arr . length ; document . write ( distinctGCDs ( arr , N ) + ' ' ) ;
Egg Dropping Puzzle | DP | Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver code
function eggDrop ( n , k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; let min = Number . MAX_VALUE ; let x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } let n = 2 , k = 10 ; document . write ( " " + " " + n + " " + k + " " + eggDrop ( n , k ) ) ;
K | Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; Given Input ; Function call
function kthDigitFromLast ( n , k ) { if ( k <= 0 ) { document . write ( - 1 ) ; return ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { document . write ( - 1 ) ; } else { document . write ( parseInt ( n % 10 ) ) ; } } var n = 2354 ; var k = 2 ; kthDigitFromLast ( n , k ) ;
Edit Distance | DP | A Space efficient Dynamic Programming based Javascript program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second String is empty then we remove all characters ; Start filling the DP This loop run for every character in second String ; This loop compares the char from second String with first String characters ; if first String is empty then we have to perform add character operation to get second String ; if character from both String is same then we do not perform any operation . here i % 2 is for bound the row number . ; if character from both String is not same then we take the minimum from three specified operation ; after complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver program
function EditDistDP ( str1 , str2 ) { let len1 = str1 . length ; let len2 = str2 . length ; let DP = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { DP [ i ] = new Array ( len1 + 1 ) ; for ( let j = 0 ; j < len1 + 1 ; j ++ ) DP [ i ] [ j ] = 0 ; } for ( let i = 0 ; i <= len1 ; i ++ ) DP [ 0 ] [ i ] = i ; for ( let i = 1 ; i <= len2 ; i ++ ) { for ( let j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 ] [ j ] = i ; else if ( str1 [ j - 1 ] == str2 [ i - 1 ] ) { DP [ i % 2 ] [ j ] = DP [ ( i - 1 ) % 2 ] [ j - 1 ] ; } else { DP [ i % 2 ] [ j ] = 1 + Math . min ( DP [ ( i - 1 ) % 2 ] [ j ] , Math . min ( DP [ i % 2 ] [ j - 1 ] , DP [ ( i - 1 ) % 2 ] [ j - 1 ] ) ) ; } } } document . write ( DP [ len2 % 2 ] [ len1 ] + " " ) ; } let str1 = " " ; let str2 = " " ; EditDistDP ( str1 , str2 ) ;
Sum of Bitwise XOR of elements of an array with all elements of another array | Function to calculate sum of Bitwise XOR of elements of arr [ ] with k ; Initialize sum to be zero ; Iterate over each set bit ; Stores contribution of i - th bet to the sum ; If the i - th bit is set ; Stores count of elements whose i - th bit is not set ; Update value ; Update value ; Add value to sum ; Move to the next power of two ; Stores the count of elements whose i - th bit is set ; Initialize count to 0 for all positions ; Traverse the array ; Iterate over each bit ; If the i - th bit is set ; Increase count ; Driver code
function xorSumOfArray ( arr , n , k , count ) { var sum = 0 ; var p = 1 ; for ( var i = 0 ; i < 31 ; i ++ ) { var val = 0 ; if ( ( k & ( 1 << i ) ) != 0 ) { var not_set = n - count [ i ] ; val = ( ( not_set ) * p ) ; } else { val = ( count [ i ] * p ) ; } sum += val ; p = ( p * 2 ) ; } return sum ; } function sumOfXors ( arr , n , queries , q ) { var count = new Array ( 32 ) ; count . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < 31 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) count [ j ] ++ ; } } for ( var i = 0 ; i < q ; i ++ ) { var k = queries [ i ] ; document . write ( xorSumOfArray ( arr , n , k , count ) + " " ) ; } } var arr = [ 5 , 2 , 3 ] ; var queries = [ 3 , 8 , 7 ] ; var n = arr . length ; var q = queries . length ; sumOfXors ( arr , n , queries , q ) ;
Minimum time required to schedule K processes | Function to execute k processes that can be gained in minimum amount of time ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
function executeProcesses ( A , N , K ) { let pq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { pq . push ( A [ i ] ) ; } let ans = 0 ; pq . sort ( function ( a , b ) { return a - b ; } ) ; while ( pq . length > 0 && K > 0 ) { let top = pq . pop ( ) ; ans ++ ; K -= top ; top = Math . floor ( top / 2 ) ; pq . push ( top ) ; pq . sort ( function ( a , b ) { return a - b ; } ) ; } document . write ( ans ) } let A = [ 3 , 1 , 7 , 4 , 2 ] ; let K = 15 ; let N = A . length ; executeProcesses ( A , N , K ) ;
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1 | JavaScript program for the above approach ; Utility function to find the matrix having the maximum height ; Stores index pairs for bfs ; Stores info about the visited cells ; Traverse the matrix ; Breadth First Search ; x & y are the row & column of current cell ; Check all the 4 adjacent cells and marking them as visited if not visited yet also marking their height as 1 + height of cell ( x , y ) ; Function to find the matrix having the maximum height ; Stores output matrix ; Calling the helper function ; Print the final output matrix ; Given matrix ; Function call to find the matrix having the maximum height
var M = 3 ; var N = 3 ; function findHeightMatrixUtil ( mat , height ) { var q = [ ] ; var vis = Array . from ( Array ( M ) , ( ) => Array ( N ) . fill ( 0 ) ) ; for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { q . push ( [ i , j ] ) ; height [ i ] [ j ] = 0 ; vis [ i ] [ j ] = 1 ; } } } while ( q . length != 0 ) { var k = q [ 0 ] ; q . shift ( ) ; var x = k [ 0 ] , y = k [ 1 ] ; if ( x > 0 && vis [ x - 1 ] [ y ] == 0 ) { height [ x - 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x - 1 ] [ y ] = 1 ; q . push ( [ x - 1 , y ] ) ; } if ( y > 0 && vis [ x ] [ y - 1 ] == 0 ) { height [ x ] [ y - 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y - 1 ] = 1 ; q . push ( [ x , y - 1 ] ) ; } if ( x < M - 1 && vis [ x + 1 ] [ y ] == 0 ) { height [ x + 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x + 1 ] [ y ] = 1 ; q . push ( [ x + 1 , y ] ) ; } if ( y < N - 1 && vis [ x ] [ y + 1 ] == 0 ) { height [ x ] [ y + 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y + 1 ] = 1 ; q . push ( [ x , y + 1 ] ) ; } } return height ; } function findHeightMatrix ( mat ) { var height = Array . from ( Array ( M ) , ( ) => Array ( N ) . fill ( 0 ) ) ; height = findHeightMatrixUtil ( mat , height ) ; for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) document . write ( height [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var mat = [ [ 0 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 0 ] ] ; findHeightMatrix ( mat ) ;
Median of all nodes from a given range in a Binary Search Tree ( BST ) | Tree Node structure ; Function to create a new BST node ; Function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Return the node pointer ; Function to find all the nodes that lies over the range [ node1 , node2 ] ; If the tree is empty , return ; Traverse for the left subtree ; If a second node is found , then update the flag as false ; Traverse the right subtree ; Function to find the median of all the values in the given BST that lies over the range [ node1 , node2 ] ; Stores all the nodes in the range [ node1 , node2 ] ; Store the size of the array ; Print the median of array based on the size of array ; Given BST
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } let interNodes = [ ] ; function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } function insertNode ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insertNode ( node . left , key ) ; else if ( key > node . key ) node . right = insertNode ( node . right , key ) ; return node ; } function getIntermediateNodes ( root , node1 , node2 ) { if ( root == null ) return ; getIntermediateNodes ( root . left , node1 , node2 ) ; if ( root . key <= node2 && root . key >= node1 ) { interNodes . push ( root . key ) ; } getIntermediateNodes ( root . right , node1 , node2 ) ; } function findMedian ( root , node1 , node2 ) { getIntermediateNodes ( root , node1 , node2 ) ; let nSize = interNodes . length ; return ( nSize % 2 == 1 ) ? interNodes [ parseInt ( nSize / 2 , 10 ) ] : ( interNodes [ parseInt ( ( nSize - 1 ) / 2 , 10 ) ] + interNodes [ nSize / 2 ] ) / 2 ; } let root = null ; root = insertNode ( root , 8 ) ; insertNode ( root , 3 ) ; insertNode ( root , 1 ) ; insertNode ( root , 6 ) ; insertNode ( root , 4 ) ; insertNode ( root , 11 ) ; insertNode ( root , 15 ) ; document . write ( findMedian ( root , 3 , 11 ) ) ;
Count number of pairs ( i , j ) up to N that can be made equal on multiplying with a pair from the range [ 1 , N / 2 ] | Function to compute totient of all numbers smaller than or equal to N ; Iterate over the range [ 2 , N ] ; If phi [ p ] is not computed already , then p is prime ; Phi of a prime number p is ( p - 1 ) ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Function to count the pairs ( i , j ) from the range [ 1 , N ] , satisfying the given condition ; Stores the counts of first and second type of pairs respectively ; Count of first type of pairs ; Stores the phi or totient values ; Calculate the Phi values ; Iterate over the range [ N / 2 + 1 , N ] ; Update the value of cnt_type2 ; Print the total count ; Driver Code
function computeTotient ( N , phi ) { for ( let p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( let i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } function countPairs ( N ) { let cnt_type1 = 0 , cnt_type2 = 0 ; let half_N = N / 2 ; cnt_type1 = ( half_N * ( half_N - 1 ) ) / 2 ; let phi = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 1 ; i <= N ; i ++ ) { phi [ i ] = i ; } computeTotient ( N , phi ) ; for ( let i = ( N / 2 ) + 1 ; i <= N ; i ++ ) cnt_type2 += ( i - phi [ i ] - 1 ) ; document . write ( cnt_type1 + cnt_type2 ) ; } let N = 6 ; countPairs ( N ) ;
Sum of subsets nearest to K possible from two given arrays | Stores the sum closest to K ; Stores the minimum absolute difference ; Function to choose the elements from the array B [ ] ; If absolute difference is less then minimum value ; Update the minimum value ; Update the value of ans ; If absolute difference between curr and K is equal to minimum ; Update the value of ans ; If i is greater than M - 1 ; Includes the element B [ i ] once ; Includes the element B [ i ] twice ; Excludes the element B [ i ] ; Function to find a subset sum whose sum is closest to K ; Traverse the array A [ ] ; Function Call ; Return the ans ; Input ; Function Call
let ans = Number . MAX_SAFE_INTEGER let mini = Number . MAX_SAFE_INTEGER function findClosestTarget ( i , curr , B , M , K ) { if ( Math . abs ( curr - K ) < mini ) { mini = Math . abs ( curr - K ) ; ans = curr ; } if ( Math . abs ( curr - K ) == mini ) { ans = Math . min ( ans , curr ) ; } if ( i >= M ) return ; findClosestTarget ( i + 1 , curr + B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr + 2 * B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr , B , M , K ) ; } function findClosest ( A , B , N , M , K ) { for ( let i = 0 ; i < N ; i ++ ) { findClosestTarget ( 0 , A [ i ] , B , M , K ) ; } return ans ; } let A = [ 2 , 3 ] ; let B = [ 4 , 5 , 30 ] ; let N = A . length ; let M = B . length ; let K = 18 ; document . write ( findClosest ( A , B , N , M , K ) ) ;
Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | Function to check if there is a node in tree from where all other nodes are accessible or not ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Given number of nodes ; Given Directed Tree
function findNode ( mp , n ) { var a = new Array ( n ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = mp [ i + 1 ] ; } var count0 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= parseInt ( n / 2 ) ) { document . write ( " " ) ; } else document . write ( " " ) ; } var N = 3 ; var mp = new Map ( ) ; mp . set ( 1 , 0 ) ; mp . set ( 2 , 2 ) ; mp . set ( 3 , 0 ) ; mp [ 1 ] = 0 ; mp [ 2 ] = 2 ; mp [ 3 ] = 0 ; findNode ( mp , N ) ;
Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code
function convert ( p ) { let ans = 0 ; for ( let i of p ) { ans = ( ans << 1 ) | i ; } return ans ; } function count ( num ) { let ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } function checkGoodMatrix ( mat ) { let P = [ ] , S = [ ] , MR = [ ] , MC = [ ] ; for ( let i = 0 ; i < mat . length ; i ++ ) { for ( let j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == j ) P . push ( mat [ i ] [ j ] ) ; if ( i + j == mat . length - 1 ) S . push ( mat [ i ] [ j ] ) ; if ( i == Math . floor ( ( mat . length - 1 ) / 2 ) ) MR . push ( mat [ i ] [ j ] ) ; if ( j == Math . floor ( ( mat . length - 1 ) / 2 ) ) MC . push ( mat [ i ] [ j ] ) ; } } S . reverse ( ) ; let P0 = convert ( P ) ; let S0 = convert ( S ) ; let MR0 = convert ( MR ) ; let MC0 = convert ( MC ) ; let setBitsPS = count ( ( P0 & S0 ) ) ; let setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) document . write ( " " ) ; else document . write ( " " ) ; } let mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] ; checkGoodMatrix ( mat ) ;
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver Code ; Function Call
function assignValues ( Edges , n ) { var tree = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < n - 1 ; i ++ ) { var u = Edges [ i ] [ 0 ] ; var v = Edges [ i ] [ 1 ] ; tree [ u ] . push ( v ) ; tree [ v ] . push ( u ) ; } var visited = Array ( n + 1 ) . fill ( false ) ; var answer = Array ( n + 1 ) ; var K = 1 ; var q = [ ] ; q . push ( 1 ) ; answer [ 1 ] = K ; while ( q . length != 0 ) { var node = q [ 0 ] ; q . shift ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; tree [ node ] . forEach ( child => { if ( ! visited [ child ] ) { q . push ( child ) ; answer [ child ] = K ; } } ) ; } for ( var i = 1 ; i <= n ; i ++ ) { document . write ( answer [ i ] + " " ) ; } } var N = 11 ; var Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 2 , 6 ] , [ 2 , 10 ] , [ 10 , 11 ] , [ 3 , 7 ] , [ 4 , 8 ] , [ 5 , 9 ] ] ; assignValues ( Edges , N ) ;
Minimum prime numbers required to be subtracted to make all array elements equal | Javascript program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code
var limit = 100000 ; var prime = Array ( limit + 1 ) . fill ( true ) ; function sieve ( ) { for ( p = 2 ; p * p <= limit ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * p ; i <= limit ; i += p ) prime [ i ] = false ; } } } function findOperations ( arr , n ) { sieve ( ) ; var minm = Number . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { minm = Math . min ( minm , arr [ i ] ) ; } var val = minm ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minm + 1 ) { val = minm - 2 ; break ; } } var cnt = 0 ; for ( i = 0 ; i < n ; i ++ ) { var D = arr [ i ] - val ; if ( D == 0 ) { continue ; } else if ( prime [ D ] == true ) { cnt += 1 ; } else if ( D % 2 == 0 ) { cnt += 2 ; } else { if ( prime [ D - 2 ] == true ) { cnt += 2 ; } else { cnt += 3 ; } } } return cnt ; } var arr = [ 7 , 10 , 4 , 5 ] ; var N = 4 ; document . write ( findOperations ( arr , N ) ) ;
Print all unique digits present in concatenation of all array elements in the order of their occurrence | Function to print long unique elements ; Reverse the list ; Traverse the list ; Function which check for all unique digits ; Stores the final number ; Stores the count of unique digits ; Converting string to long longer to remove leading zeros ; Stores count of digits ; Iterate over the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Converting string to long longer again ; Iterate over the digits of N ; Retrieve the last digit of N ; If the value of this digit is not visited ; If its frequency is 1 ( unique ) ; Mark the digit visited ; Remove the last digit of N ; Passing this list to print long the reversed list ; Function to concatenate array elements ; Stores the concatenated number ; Traverse the array ; Convert to equivalent string ; Concatenate the string ; Passing string to checkUnique function ; Driver Code ; Function call to print long unique digits present in the concatenation of array elements
function printUnique ( lis ) { lis . reverse ( ) ; for ( var i = 0 ; i < lis . length ; i ++ ) { document . write ( lis [ i ] + " " ) } } function checkUnique ( st ) { var lis = [ ] ; var res = 0 ; var N = parseInt ( st ) ; var cnt = Array ( 10 ) . fill ( 0 ) ; var cnt1 = Array ( 10 ) . fill ( 0 ) ; while ( N > 0 ) { var rem = N % 10 ; cnt [ rem ] += 1 ; N = parseInt ( N / 10 ) ; } N = parseInt ( st ) ; while ( N > 0 ) { var rem = N % 10 ; if ( cnt1 [ rem ] == 0 ) { if ( cnt [ rem ] == 1 ) lis . push ( rem ) ; } cnt1 [ rem ] = 1 ; N = parseInt ( N / 10 ) ; } printUnique ( lis ) ; } function combineArray ( lis ) { var st = " " ; for ( var i = 0 ; i < lis . length ; i ++ ) { var ee = ( lis [ i ] . toString ( ) ) ; st = st + ee ; } checkUnique ( st ) ; } var arr = [ 122 , 474 , 612 , 932 ] ; combineArray ( arr ) ;