codeparrot/xlcost-text-to-code huggingface dataset:
Largest divisor of a number not divisible by another given number | Function to find the largest number X such that it divides P but is not divisible by Q ; Stores the frequency count of of all Prime Factors ; Increment the frequency of the current prime factor ; If Q is a prime factor ; Stores the desired result ; Iterate through all divisors of Q ; Stores the frequency count of current prime divisor on dividing P ; Count the frequency of the current prime factor ; If cur is less than frequency then P is the final result ; Iterate to get temporary answer ; Update current answer ; Print the desired result ; Given P and Q ; Function Call
function findTheGreatestX ( P , Q ) { var divisiors = new Map ( ) ; for ( var i = 2 ; i * i <= Q ; i ++ ) { while ( Q % i == 0 && Q > 1 ) { Q = parseInt ( Q / i ) ; if ( divisiors . has ( i ) ) divisiors . set ( i , divisiors . get ( i ) + 1 ) else divisiors . set ( i , 1 ) } } if ( Q > 1 ) if ( divisiors . has ( Q ) ) divisiors . set ( Q , divisiors . get ( Q ) + 1 ) else divisiors . set ( Q , 1 ) var ans = 0 ; divisiors . forEach ( ( value , key ) => { var frequency = value ; var temp = P ; var cur = 0 ; while ( temp % key == 0 ) { temp = parseInt ( temp / key ) ; cur ++ ; } if ( cur < frequency ) { ans = P ; } temp = P ; for ( var j = cur ; j >= frequency ; j -- ) { temp = parseInt ( temp / key ) ; } ans = Math . max ( temp , ans ) ; } ) ; document . write ( ans ) ; } var P = 10 , Q = 4 ; findTheGreatestX ( P , Q ) ;
Check if rows of a Matrix can be rearranged to make Bitwise XOR of first column non | Function to check if there is any row where number of unique elements are greater than 1 ; Iterate over the matrix ; Function to check if it is possible to rearrange mat [ ] [ ] such that XOR of its first column is non - zero ; Find bitwise XOR of the first column of mat [ ] [ ] ; If bitwise XOR of the first column of mat [ ] [ ] is non - zero ; Otherwise check rearrangements ; Given Matrix mat [ ] [ ] ; Function Call
function checkRearrangements ( mat , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ 0 ] != mat [ i ] [ j ] ) { return " " ; } } } return " " ; } function nonZeroXor ( mat , N , M ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { res = res ^ mat [ i ] [ 0 ] ; } if ( res != 0 ) return " " ; else return checkRearrangements ( mat , N , M ) ; } let mat = [ [ 1 , 1 , 2 ] , [ 2 , 2 , 2 ] , [ 3 , 3 , 3 ] ] ; let N = mat . length ; let M = mat [ 0 ] . length ; document . write ( nonZeroXor ( mat , N , M ) ) ;
Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array | Javascript Program to implement the above approach ; Function to maximize the value for the given function and the array elements ; Vector array to maintain which bit is set for which integer in the given array by saving index of that integer ; Check if j - th bit is set for i - th integer ; Push the index of that integer in setBit [ j ] ; Find the element having highest significant set bit unset in other elements ; Place that integer at 0 - th index ; Store the maximum AND value ; Return the answer ; Driver Code ; Function call
var size_int = 32 ; function functionMax ( arr , n ) { var setBit = Array . from ( Array ( 32 ) , ( ) => new Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < size_int ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) setBit [ j ] . push ( i ) ; } } for ( var i = size_int - 1 ; i >= 0 ; i -- ) { if ( setBit [ i ] . length == 1 ) { [ arr [ 0 ] , arr [ setBit [ i ] [ 0 ] ] ] = [ arr [ setBit [ i ] [ 0 ] ] , arr [ 0 ] ] ; break ; } } var maxAnd = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { maxAnd = maxAnd & ( ~ arr [ i ] ) ; } return maxAnd ; } var arr = [ 1 , 2 , 4 , 8 , 16 ] ; var n = arr . length ; document . write ( functionMax ( arr , n ) ) ;
Sum of all products of the Binomial Coefficients of two numbers up to K | Function returns nCr i . e . Binomial Coefficient ; Initialize res with 1 ; Since C ( n , r ) = C ( n , n - r ) ; Evaluating expression ; Function to calculate and return the sum of the products ; Initialize sum to 0 ; Traverse from 0 to k ; Driver Code
function nCr ( n , r ) { let res = 1 ; if ( r > n - r ) r = n - r ; for ( let i = 0 ; i < r ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function solve ( n , m , k ) { let sum = 0 ; for ( let i = 0 ; i <= k ; i ++ ) sum += nCr ( n , i ) * nCr ( m , k - i ) ; return sum ; } let n = 3 , m = 2 , k = 2 ; document . write ( solve ( n , m , k ) ) ;
Fast Exponention using Bit Manipulation | Function to return a ^ n ; Stores final answer ; Check if current LSB is set ; Right shift ; Driver Code
function powerOptimised ( a , n ) { let ans = 1 ; while ( n > 0 ) { let last_bit = ( n & 1 ) ; if ( last_bit > 0 ) { ans = ans * a ; } a = a * a ; n = n >> 1 ; } return ans ; } let a = 3 , n = 5 ; document . write ( powerOptimised ( a , n ) ) ;
Find M such that GCD of M and given number N is maximum | Function to find the integer M such that gcd ( N , M ) is maximum ; Initialize a variable ; Find all the divisors of N and return the maximum divisor ; Check if i is divisible by N ; Update max_gcd ; Return the maximum value ; Given Number ; Function Call
function findMaximumGcd ( n ) { let max_gcd = 1 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i > max_gcd ) max_gcd = i ; if ( ( n / i != i ) && ( n / i != n ) && ( ( n / i ) > max_gcd ) ) max_gcd = n / i ; } } return max_gcd ; } let N = 10 ; document . write ( findMaximumGcd ( N ) ) ;
First element of every K sets having consecutive elements with exactly K prime factors less than N | Javascript program for the above approach ; For storing smallest prime factor ; Function construct smallest prime factor array ; Mark smallest prime factor for every number to be itself . ; separately mark spf for every even number as 2 ; Check if i is prime ; Mark SPF for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function for counts total number of prime factors ; Function to print elements of sets of K consecutive elements having K prime factors ; To store the result ; Count number of prime factors of number ; If number has exactly K factors push in result [ ] ; Iterate till we get K consecutive elements in result [ ] ; Count sequence until K ; Print the element if count >= K ; To construct spf [ ] ; Given N and K ; Function Call
let x = 2000021 let v = new Array ( x ) ; function sieve ( ) { v [ 1 ] = 1 ; for ( let i = 2 ; i < x ; i ++ ) v [ i ] = i ; for ( let i = 4 ; i < x ; i += 2 ) v [ i ] = 2 ; for ( let i = 3 ; i * i < x ; i ++ ) { if ( v [ i ] == i ) { for ( let j = i * i ; j < x ; j += i ) { if ( v [ j ] == j ) { v [ j ] = i ; } } } } } function prime_factors ( n ) { let s = new Set ( ) ; while ( n != 1 ) { s . add ( v [ n ] ) ; n = n / v [ n ] ; } return s . size ; } function distinctPrimes ( m , k ) { let result = new Array ( ) ; for ( let i = 14 ; i < m + k ; i ++ ) { let count = prime_factors ( i ) ; if ( count == k ) { result . push ( i ) ; } } let p = result . length ; for ( let index = 0 ; index < p - 1 ; index ++ ) { let element = result [ index ] ; let count = 1 , z = index ; while ( z < p - 1 && count <= k && result [ z ] + 1 == result [ z + 1 ] ) { count ++ ; z ++ ; } if ( count >= k ) document . write ( element + ' ' ) ; } } sieve ( ) ; let N = 1000 , K = 3 ; distinctPrimes ( N , K ) ;
Product of Complex Numbers using three Multiplication Operation | Function to multiply Complex Numbers with just three multiplications ; Find value of prod1 , prod2 and prod3 ; Real Part ; Imaginary Part ; Print the result ; Driver Code ; Given four Numbers ; Function Call
function print_product ( a , b , c , d ) { let prod1 = a * c ; let prod2 = b * d ; let prod3 = ( a + b ) * ( c + d ) ; let real = prod1 - prod2 ; let imag = prod3 - ( prod1 + prod2 ) ; document . write ( real + " " + imag + " " ) ; } let a , b , c , d ; a = 2 ; b = 3 ; c = 4 ; d = 5 ; print_product ( a , b , c , d ) ;
Insolite Numbers | Function to check if a number is an Insolite numbers ; To store sum of squares of digits ; To store product of squares of digits ; extracting digit ; Driver Code ; Function Call
function isInsolite ( n ) { let N = n ; let sum = 0 ; let product = 1 ; while ( n != 0 ) { let r = n % 10 ; sum = sum + r * r ; product = product * r * r ; n = parseInt ( n / 10 ) ; } return ( N % sum == 0 ) && ( N % product == 0 ) ; } let N = 111 ; if ( isInsolite ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Superabundant Numbers | Function to calculate the sum of all divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Function to check if N is a superabundant number ; to check all numbers from 1 to N ; Driver code
function sigma ( n ) { if ( n == 1 ) return 1 ; var result = 0 ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } function isSuperabundant ( N ) { for ( var i = 1 ; i < N ; i ++ ) { var x = sigma ( i ) / i ; var y = sigma ( N ) / ( N * 1.0 ) ; if ( x > y ) return false ; } return true ; } var N = 4 ; isSuperabundant ( N ) ? document . write ( " " ) : document . write ( " " ) ;
D Numbers | Function to find the N - th icosikaipentagon number ; Number should be greater than 3 ; Check every k in range 2 to n - 1 ; Condition for D - Number ; Driver Code
function isDNum ( n ) { if ( n < 4 ) return false ; let numerator = 0 , hcf = 0 ; for ( k = 2 ; k <= n ; k ++ ) { numerator = parseInt ( ( Math . pow ( k , n - 2 ) - k ) ) ; hcf = __gcd ( n , k ) ; } if ( hcf == 1 && ( numerator % n ) != 0 ) return false ; return true ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let n = 15 ; let a = isDNum ( n ) ; if ( a ) document . write ( " " ) ; else document . write ( " " ) ;
Ruth | Function to find prime divisors of all numbers from 1 to N ; if the number is prime ; add this prime to all it 's multiples ; Function to check Ruth - Aaron number ; Driver code
function Sum ( N ) { let SumOfPrimeDivisors = Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 1 ) { for ( let j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } function RuthAaronNumber ( n ) { if ( Sum ( n ) == Sum ( n + 1 ) ) return true ; else return false ; } let N = 714 ; if ( RuthAaronNumber ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximize sum of absolute difference between adjacent elements in Array with sum K | Function for maximising the sum ; Difference is 0 when only one element is present in array ; Difference is K when two elements are present in array ; Otherwise ; Driver Code
function maxAdjacentDifference ( N , K ) { if ( N == 1 ) { return 0 ; } if ( N == 2 ) { return K ; } return 2 * K ; } let N = 6 ; let K = 11 ; document . write ( maxAdjacentDifference ( N , K ) ) ;
Sum of all divisors from 1 to N | Set 2 | JavaScript program for the above approach ; Functions returns sum of numbers from 1 to n ; Functions returns sum of numbers from a + 1 to b ; Function returns total sum of divisors ; Stores total sum ; Finding numbers and its occurence ; Sum of product of each number and its occurence ; Driver Code
let mod = 1000000007 ; function linearSum ( n ) { return ( n * ( n + 1 ) / 2 ) % mod ; } function rangeSum ( b , a ) { return ( linearSum ( b ) - linearSum ( a ) ) % mod ; } function totalSum ( n ) { let result = 0 ; let i = 1 ; while ( true ) { result += rangeSum ( Math . floor ( n / i ) , Math . floor ( n / ( i + 1 ) ) ) * ( i % mod ) % mod ; result %= mod ; if ( i == n ) break ; i = Math . floor ( n / ( n / ( i + 1 ) ) ) ; } return result ; } let N = 4 ; document . write ( totalSum ( N ) + " " ) ; N = 12 ; document . write ( totalSum ( N ) ) ;
Nontrivial undulant Numbers | Function to check if a string is double string or not ; a and b should not be equal ; Condition to check if length is odd make length even ; First half of s ; Second half of s ; Double string if first and last half are equal ; Function to check if N is an Nontrivial undulant number ; Driver Code
function isDouble ( num ) { let s = num . toString ( ) ; let l = s . length ; if ( s [ 0 ] == s . charAt [ 1 ] ) return false ; if ( l % 2 == 1 ) { s = s + s [ 1 ] ; l ++ ; } let s1 = s . substr ( 0 , l / 2 ) ; let s2 = s . substr ( l / 2 ) ; return ( s1 == s2 ) ; } function isNontrivialUndulant ( N ) { return N > 100 && isDouble ( N ) ; } let n = 121 ; if ( isNontrivialUndulant ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Megagon number | Function to find the nth Megagon Number ; Driver Code
function MegagonNum ( n ) { return ( 999998 * n * n - 999996 * n ) / 2 ; } var n = 3 ; document . write ( MegagonNum ( n ) ) ;
Product of all the pairs from the given array | Javascript implementation to find the product of all the pairs from the given array ; Function to return the product of the elements of all possible pairs from the array ; To store the required product ; Nested loop to calculate all possible pairs ; Multiply the product of the elements of the current pair ; Return the final result ; Driver code
mod = 1000000007 function productPairs ( arr , n ) { let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { product *= ( arr [ i ] % mod * arr [ j ] % mod ) % mod ; product = product % mod ; } } return product % mod ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( productPairs ( arr , n ) ) ;
Product of all the pairs from the given array | Javascript implementation to Find the product of all the pairs from the given array ; Function to calculate ( x ^ y ) % 1000000007 ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; Return the final result ; Function to return the product of the elements of all possible pairs from the array ; To store the required product ; Iterate for every element of the array ; Each element appears ( 2 * n ) times ; Driver Code
let mod = 1000000007 ; function power ( x , y ) { let p = 1000000007 ; let res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function productPairs ( arr , n ) { let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { product = ( product % mod * power ( arr [ i ] , ( 2 * n ) ) % mod ) % mod ; } return product % mod ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( productPairs ( arr , n ) ) ;
Construct an Array such that cube sum of all element is a perfect square | Function to create and print the array ; initialise the array of size N ; Print the array ; Driver code
function constructArray ( N ) { let arr = new Array ( N ) ; for ( let i = 1 ; i <= N ; i ++ ) { arr [ i - 1 ] = i ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let N = 6 ; constructArray ( N ) ;
Count of all subsequence whose product is a Composite number | Function to check whether a number is prime or not ; Function to find number of subsequences whose product is a composite number ; Find total non empty subsequence ; Find count of prime number and ones ; Calculate the non empty one subsequence ; Find count of composite subsequence ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( var i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function countSubsequences ( arr , n ) { var totalSubsequence = Math . pow ( 2 , n ) - 1 ; var countPrime = 0 , countOnes = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) countOnes ++ ; else if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } var compositeSubsequence ; var onesSequence = Math . pow ( 2 , countOnes ) - 1 ; compositeSubsequence = totalSubsequence - countPrime - onesSequence - onesSequence * countPrime ; return compositeSubsequence ; } var arr = [ 2 , 1 , 2 ] ; var n = arr . length ; document . write ( countSubsequences ( arr , n ) ) ;
Find K consecutive integers such that their sum is N | Function to check if a number can be expressed as the sum of k consecutive ; Finding the first term of AP ; Checking if first term is an integer ; Loop to prvar the K consecutive integers ; Driver Code
function checksum ( n , k ) { var first_term = ( ( ( 2 * n ) / k + ( 1 - k ) ) / 2.0 ) ; if ( first_term - parseInt ( ( first_term ) ) == 0 ) { for ( i = parseInt ( first_term ) ; i <= first_term + k - 1 ; i ++ ) { document . write ( i + " " ) ; } } else document . write ( " " ) ; } var n = 33 , k = 6 ; checksum ( n , k ) ;
Represent N as sum of K even numbers | Function to prlet the representation ; N must be greater than equal to 2 * K and must be even ; Driver Code
function sumEvenNumbers ( N , K ) { let check = N - 2 * ( K - 1 ) ; if ( check > 0 && check % 2 == 0 ) { for ( let i = 0 ; i < K - 1 ; i ++ ) { document . write ( " " ) ; } document . write ( check ) ; } else { document . write ( " " ) ; } } let N = 8 ; let K = 2 ; sumEvenNumbers ( N , K ) ;
Count of contiguous subarrays possible for every index by including the element at that index | Function to find the number of subarrays including the element at every index of the array ; Creating an array of size N ; The loop is iterated till half the length of the array ; Condition to avoid overwriting the middle element for the array with even length . ; Computing the number of subarrays ; The ith element from the beginning and the ending have the same number of possible subarray ; Function to prlet the vector ; Driver Code
function calculateWays ( n ) { let x = 0 ; let v = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) v [ i ] = 0 ; for ( let i = 0 ; i < n / 2 ; i ++ ) { if ( n % 2 == 0 && i == n / 2 ) break ; x = n * ( i + 1 ) - ( i + 1 ) * i ; v [ i ] = x ; v [ n - i - 1 ] = x ; } return v ; } function prletArray ( v ) { for ( let i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ; } let v ; v = calculateWays ( 4 ) ; prletArray ( v ) ;
Smallest number greater than or equal to X whose sum of digits is divisible by Y | javascript program to find the smallest number greater than or equal to X and divisible by Y ; Function that returns the sum of digits of a number ; Initialize variable to store the sum ; Add the last digit of the number ; Remove the last digit from the number ; Function that returns the smallest number greater than or equal to X and divisible by Y ; Initialize result variable ; Loop through numbers greater than equal to X ; Calculate sum of digits ; Check if sum of digits is divisible by Y ; Driver code
var MAXN = 10000000 ; function sumOfDigits ( n ) { var sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function smallestNum ( X , Y ) { var res = - 1 ; for ( i = X ; i < MAXN ; i ++ ) { var sum_of_digit = sumOfDigits ( i ) ; if ( sum_of_digit % Y == 0 ) { res = i ; break ; } } return res ; } var X = 5923 , Y = 13 ; document . write ( smallestNum ( X , Y ) ) ;
Count the numbers which can convert N to 1 using given operation | Function to count the numbers which can convert N to 1 using the given operation ; Store all the divisors of N ; If i is a divisor ; If i is not equal to N / i ; Iterate through all the divisors of N - 1 and count them in answer ; Check if N - 1 is a divisor or not ; Iterate through all divisors and check for N mod d = 1 or ( N - 1 ) mod d = 0 ; Driver code
function countValues ( N ) { var div = [ ] ; for ( var i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { div . push ( i ) ; if ( N != i * i ) { div . push ( N / i ) ; } } } var answer = 0 ; for ( var i = 1 ; i * i <= N - 1 ; i ++ ) { if ( ( N - 1 ) % i == 0 ) { if ( i * i == N - 1 ) answer ++ ; else answer += 2 ; } } div . forEach ( d => { var K = N ; while ( K % d == 0 ) K /= d ; if ( ( K - 1 ) % d == 0 ) answer ++ ; } ) ; return answer ; } var N = 6 ; document . write ( countValues ( N ) ) ;
Maximum possible prime divisors that can exist in numbers having exactly N divisors | Function to find the maximum possible prime divisors of a number can have with N divisors ; Number of time number divided by 2 ; Divide by other prime numbers ; If the last number of also prime then also include it ; Driver Code ; Function Call
function findMaxPrimeDivisor ( n ) { let max_possible_prime = 0 ; while ( n % 2 == 0 ) { max_possible_prime ++ ; n = Math . floor ( n / 2 ) ; } for ( let i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { max_possible_prime ++ ; n = Math . floor ( n / i ) ; } } if ( n > 2 ) { max_possible_prime ++ ; } document . write ( max_possible_prime + " " ) ; } let n = 4 ; findMaxPrimeDivisor ( n ) ;
Count ways to express a number as sum of exactly two numbers | Function returns the count of ways express a number as sum of two numbers . ; Driver Code
function CountWays ( n ) { let ans = Math . floor ( ( n - 1 ) / 2 ) ; return ans ; } let N = 8 ; document . write ( CountWays ( N ) ) ;
Divide array in two maximum equal length arrays of similar and dissimilar elements | Function to find the max - size to which an array can be divided leto 2 equal parts ; Array to find the frequency of each element of array ; Find the index maximum frequency element present in array arr [ ] ; Find total unique elements present in array arr [ ] ; Find the Max - Size to which an array arr [ ] can be splitted ; Find the first array containing same elements ; Find the second array containing unique elements ; initialise n ; array declaration ; size of array
function Solve ( arr , size , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < size ; i ++ ) v [ arr [ i ] ] ++ ; let max1 = - 1 , mx = - 1 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] > mx ) { mx = v [ i ] ; max1 = i ; } } let cnt = 0 ; for ( let i in v ) { if ( i == 0 ) ++ cnt ; } let diff1 = n + 1 - cnt ; let max_size = Math . max ( Math . min ( v [ max1 ] - 1 , diff1 ) , Math . min ( v [ max1 ] , diff1 - 1 ) ) ; document . write ( " " + max_size + " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < max_size ; i ++ ) { document . write ( max1 + " " ) ; v [ max1 ] -= 1 ; } document . write ( " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { if ( v [ i ] > 0 ) { document . write ( i + " " ) ; max_size -- ; } if ( max_size < 1 ) break ; } document . write ( " " ) ; } let n = 7 ; let arr = [ 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 ] ; let size = arr . length ; Solve ( arr , size , n ) ;