codeparrot/xlcost-text-to-code huggingface dataset:
Count of available non | Function to find the pivot index ; Function to implement Quick Sort ; Pivot is pointing to pivot index before which every element is smaller and after pivot , every element is greater ; Sort the array before pivot element ; Sort the array after pivot element ; Function to count the available intervals from the given range of numbers ; If range starts after 0 then an interval is available from 0 to start [ 0 ] ; When a new interval starts ; Since index variable i is being incremented , the current active interval will also get incremented ; When the current interval ends ; Since index variable j is being decremented , the currect active interval will also get decremented ; When start and end both are same there is no change in currActive ; If the end of interval is before the range so interval is available at the end ; Driver Code ; Sort the start array ; Sort the end array ; Calling the function
function partition ( arr , l , h ) { let pivot = arr [ l ] ; let i = l + 1 ; let j = h ; while ( i <= j ) { while ( i <= h && arr [ i ] < pivot ) { i ++ ; } while ( j > l && arr [ j ] > pivot ) { j -- ; } if ( i < j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } else i ++ ; } arr [ l ] = arr [ j ] ; arr [ j ] = pivot ; return j ; } function sortArray ( arr , l , h ) { if ( l >= h ) return ; let pivot = partition ( arr , l , h ) ; sortArray ( arr , l , pivot - 1 ) ; sortArray ( arr , pivot + 1 , h ) ; } function findMaxIntervals ( start , end , n , R ) { let ans = 0 ; let prev = 0 ; let currActive = 0 ; let i = 0 ; let j = 0 ; if ( start [ 0 ] > 0 ) ans ++ ; while ( i < n && j < n ) { if ( start [ i ] < end [ j ] ) { i ++ ; currActive ++ ; } else if ( start [ i ] > end [ j ] ) { j ++ ; currActive -- ; } else { i ++ ; j ++ ; } if ( currActive == 0 ) { ans ++ ; } } if ( end [ n - 1 ] < R ) ans ++ ; return ans ; } let R , N ; R = 10 ; N = 3 ; let start = [ 2 , 5 , 8 ] ; let end = [ 3 , 9 , 10 ] ; sortArray ( start , 0 , N - 1 ) ; sortArray ( end , 0 , N - 1 ) ; document . write ( findMaxIntervals ( start , end , N , R ) ) ;
Maximum items that can be bought from the cost Array based on given conditions | Function to find the maximum number of items that can be bought from the given cost array ; Sort the given array ; Variables to store the prefix sum , answer and the counter variables ; Initializing the first element of the prefix array ; If we can buy at least one item ; Iterating through the first K items and finding the prefix sum ; Check the number of items that can be bought ; Finding the prefix sum for the remaining elements ; Check the number of iteams that can be bought ; Driver code
function number ( a , n , p , k ) { a . sort ( ) ; var pre = Array ( n ) . fill ( 0 ) , val , i , j , ans = 0 ; pre [ 0 ] = a [ 0 ] ; if ( pre [ 0 ] <= p ) ans = 1 ; for ( i = 1 ; i < k - 1 ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i ] ; if ( pre [ i ] <= p ) ans = i + 1 ; } pre [ k - 1 ] = a [ k - 1 ] ; for ( i = k - 1 ; i < n ; i ++ ) { if ( i >= k ) { pre [ i ] += pre [ i - k ] + a [ i ] ; } if ( pre [ i ] <= p ) ans = i + 1 ; } return ans ; } var n = 5 ; var arr = [ 2 , 4 , 3 , 5 , 7 ] ; var p = 11 ; var k = 2 ; document . write ( number ( arr , n , p , k ) ) ;
Sort all special primes in their relative positions | Function for the Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to sort the special primes in their relative positions ; Maximum element from the array ; prime [ i ] will be true if i is a prime ; To store the special primes from the array ; If current element is a special prime ; Add it to the List and set arr [ i ] to - 1 ; Sort the special primes ; Position of a special prime ; Driver Code
function sieveOfEratosthenes ( prime , n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function sortSpecialPrimes ( arr , n ) { let maxVal = Math . max ( ... arr ) ; let prime = Array . from ( { length : maxVal + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; sieveOfEratosthenes ( prime , maxVal ) ; let list = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] && prime [ arr [ i ] - 2 ] ) { list . push ( arr [ i ] ) ; arr [ i ] = - 1 ; } } list . sort ( ( a , b ) => a - b ) ; let j = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) document . write ( list [ j ++ ] + " " ) ; else document . write ( arr [ i ] + " " ) ; } } let arr = [ 31 , 5 , 2 , 1 , 7 ] ; let n = arr . length ; sortSpecialPrimes ( arr , n ) ;
Find the Maximum possible Sum for the given conditions | Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; Print profit ; Driver code
function maxProfit ( arr ) { arr . sort ( ) ; arr . reverse ( ) ; let ans = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } let arr = [ 6 , 6 , 6 ] ; document . write ( maxProfit ( arr ) ) ;
Check if the array can be sorted only if the elements on given positions can be swapped | Function to check if the array can be sorted only if the elements on the given positions can be swapped ; Creating an array for marking the positions ; Iterating through the array and mark the positions ; Iterating through the given array ; If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos ; Sorting the required segment ; Checking if the vector is sorted or not ; Print yes if it is sorted ; Driver code
function check_vector ( A , n , p ) { var pos = A . length ; for ( var i = 0 ; i < p . length ; i ++ ) { pos [ p [ i ] - 1 ] = 1 ; } var flag = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( pos [ i ] == 0 ) continue ; var j = i ; while ( j < n && pos [ j ] ) ++ j ; A = Array . prototype . concat ( A . slice ( 0 , i ) , A . slice ( i + 1 , j + 1 ) . sort ( ( a , b ) => a - b ) , A . slice ( j + 2 ) . sort ( ( a , b ) => a - b ) ) ; i = j ; } for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var A = [ 3 , 2 , 1 ] ; var p = [ 1 , 2 ] ; check_vector ( A , A . length , p ) ;
Sort elements of an array in increasing order of absolute difference of adjacent elements | Function to sort the elements of the array by difference ; Sorting the array ; Array to store the elements of the array ; Iterating over the length of the array to include each middle element of array ; Appending the middle element of the array ; Driver Code
function sortDiff ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let out = [ ] ; while ( n > 0 ) { out . push ( arr [ Math . floor ( n / 2 ) ] ) ; arr . splice ( Math . floor ( n / 2 ) , 1 ) ; n = n - 1 ; } for ( let i = 0 ; i < out . length ; i ++ ) document . write ( out [ i ] + " " ) ; } let arr = [ 8 , 1 , 2 , 3 , 0 ] ; let n = 5 ; sortDiff ( arr , n ) ;
Minimum cost of choosing the array element | Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of arr [ ] ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Prlet the pref [ ] for the result ; Driver Code
function minimumCost ( arr , N , M ) { arr . sort ( ( a , b ) => a - b ) ; let pref = Array . from ( { length : N } , ( _ , i ) => 0 ) ; pref [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { pref [ i ] = arr [ i ] + pref [ i - 1 ] ; } for ( let i = M ; i < N ; i ++ ) { pref [ i ] += pref [ i - M ] ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( pref [ i ] + " " ) ; } } let arr = [ 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 ] ; let M = 2 ; let N = arr . length ; minimumCost ( arr , N , M ) ;
Sort an Array alphabetically when each number is converted into words | Variable to store the word form of units digit and up to twenty ; Variable to store the word form of tens digit ; Function to convert a two digit number to the word by using the above defined arrays ; If n is more than 19 , divide it ; If n is non - zero ; Function to print a given number in words ; Stores the word representation of the given number n ; Handles digits at ten millions and hundred millions places ; Handles digits at hundred thousands and one millions places ; Handles digits at thousands and tens thousands places ; Handles digit at hundreds places ; Call the above function to convert the number into words ; Function to sort the array according to the albhabetical order ; Vector to store the number in words with respective elements ; Inserting number in words with respective elements in vector pair ; Sort the vector , this will sort the pair according to the alphabetical order . ; Print the sorted vector content ; Driver Code
var one = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var ten = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; function numToWords ( n , s ) { var str = " " ; if ( n > 19 ) str += ten [ Math . floor ( n / 10 ) ] + one [ n % 10 ] ; else str += one [ n ] ; if ( n ) str += s ; return str ; } function convertToWords ( n ) { var out ; out += numToWords ( Math . floor ( n / 10000000 ) , " " ) ; out += numToWords ( ( Math . floor ( n / 100000 ) % 100 ) , " " ) ; out += numToWords ( ( Math . floor ( n / 1000 ) % 100 ) , " " ) ; out += numToWords ( ( Math . floor ( n / 100 ) % 10 ) , " " ) ; if ( n > 100 && n % 100 ) out += " " ; out += numToWords ( ( n % 100 ) , " " ) ; return out ; } function sortArr ( arr , n ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] = [ convertToWords ( arr [ i ] ) , arr [ i ] ] ; } vp . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } var arr = [ 12 , 10 , 102 , 31 , 15 ] ; var n = arr . length ; sortArr ( arr , n ) ;
Find sum of all unique elements in the array for K queries | Function to find the sum of unique elements after Q Query ; Updating the array after processing each query ; Making it to 0 - indexing ; Iterating over the array to get the final array ; Variable to store the sum ; Hash to maintain perviously occured elements ; Loop to find the maximum sum ; Driver code
function uniqueSum ( A , R , N , M ) { for ( let i = 0 ; i < M ; ++ i ) { let l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( let i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } let ans = 0 ; let s = new Set ( ) ; for ( let i = 0 ; i < N ; ++ i ) { if ( ! s . has ( A [ i ] ) ) ans += A [ i ] ; s . add ( A [ i ] ) ; } return ans ; } let A = [ 0 , 0 , 0 , 0 , 0 , 0 ] ; let R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] , [ 3 , 3 ] ] ; let N = A . length ; let M = R . length ; document . write ( uniqueSum ( A , R , N , M ) ) ;
Find the Kth pair in ordered list of all possible sorted pairs of the Array | Function to find the k - th pair ; Sorting the array ; Iterating through the array ; Finding the number of same elements ; Checking if N * T is less than the remaining K . If it is , then arr [ i ] is the first element in the required pair ; Printing the K - th pair ; Driver code
function kthpair ( n , k , arr ) { let i , t = 0 ; arr . sort ( ) ; -- k ; for ( i = 0 ; i < n ; i += t ) { for ( t = 1 ; arr [ i ] == arr [ i + t ] ; ++ t ) ; if ( t * n > k ) break ; k = k - t * n ; } document . write ( arr [ i ] + " " + arr [ k / t ] ) ; } let n = 3 , k = 2 ; let arr = [ 3 , 1 , 5 ] ; kthpair ( n , k , arr ) ;
Minimum characters to be replaced to make frequency of all characters same | Function to find the minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code
function minOperations ( s ) { var freq = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } freq . sort ( ( a , b ) => b - a ) ; var answer = n ; for ( var i = 1 ; i <= 26 ; i ++ ) { if ( n % i === 0 ) { var x = n / i ; var y = 0 ; for ( var j = 0 ; j < i ; j ++ ) { y += Math . min ( freq [ j ] , x ) ; } answer = Math . min ( answer , n - y ) ; } } return answer ; } var s = " " ; document . write ( minOperations ( s ) ) ;
Check if it is possible to sort an array with conditional swapping of elements at distance K | Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code
function fun ( arr , n , k ) { let v = [ ] ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = i ; j < n ; j += k ) { v . push ( arr [ j ] ) ; } v . sort ( ) ; let x = 0 ; for ( let j = i ; j < n ; j += k ) { arr [ j ] = v [ x ] ; x ++ ; } v = [ ] ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } let arr = [ 4 , 2 , 3 , 7 , 6 ] ; let K = 2 ; let n = arr . length ; if ( fun ( arr , n , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort an Array based on the absolute difference of adjacent elements | Function that arrange the array such that all absolute difference between adjacent element are sorted ; To store the resultant array ; Sorting the given array in ascending order ; Variable to represent left and right ends of the given array ; Traversing the answer array in reverse order and arrange the array elements from arr [ ] in reverse order ; Inserting elements in zig - zag manner ; Displaying the resultant array ; Driver Code ; Function Call
function sortedAdjacentDifferences ( arr , n ) { let ans = new Array ( n ) ; arr . sort ( ) ; let l = 0 , r = n - 1 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( i % 2 ) { ans [ i ] = arr [ l ] ; l ++ ; } else { ans [ i ] = arr [ r ] ; r -- ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let arr = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ] ; let n = arr . length ; sortedAdjacentDifferences ( arr , n ) ;
Minimum steps to convert an Array into permutation of numbers from 1 to N | Function to find minimum number of steps to convert a given sequence into a permutation ; Sort the given array ; To store the required minimum number of operations ; Find the operations on each step ; Return the answer ; Driver code ; Function call
function get_permutation ( arr , n ) { arr . sort ( ) ; var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { result += Math . abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } var arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] ; var n = arr . length ; document . write ( get_permutation ( arr , n ) ) ;
Minimum increment or decrement required to sort the array | Top | Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then arr [ ] is sorted ; If dp [ N ] [ maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N ] [ maxE ] ; Return the final result ; Driver Code ; Find the minimum and maximum element from the arr [ ] ; Function Call
let dp = new Array ( ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < 1000 ; j ++ ) { temp . push ( [ ] ) } dp . push ( temp ) } function minimumIncDec ( arr , N , maxE , minE ) { if ( N == 0 ) { return 0 ; } if ( ! dp [ N ] [ maxE ] ) return dp [ N ] [ maxE ] ; let ans = Number . MAX_SAFE_INTEGER ; for ( let k = minE ; k <= maxE ; k ++ ) { let x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = Math . min ( ans , x + Math . abs ( arr [ N - 1 ] - k ) ) ; } dp [ N ] [ maxE ] = ans ; return dp [ N ] [ maxE ] ; } let arr = [ 5 , 4 , 3 , 2 , 1 ] ; let N = arr . length ; let minE = arr . sort ( ( a , b ) => a - b ) [ 0 ] ; let maxE = arr . sort ( ( a , b ) => b - a ) [ 0 ] ; document . write ( minimumIncDec ( arr , N , maxE , minE ) ) ;
Sort the major diagonal of the matrix | Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
function sortDiagonal ( a , M , N ) { for ( var i = 0 ; i < M ; i ++ ) { var sm = a [ i ] [ i ] ; var pos = i ; for ( var j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } temp = a [ i ] [ i ] a [ i ] [ i ] = a [ pos ] [ pos ] a [ pos ] [ pos ] = temp } for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) document . write ( a [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var a = [ [ 4 , 2 ] , [ 3 , 1 ] ] ; sortDiagonal ( a , 2 , 2 ) ;
Minimum cost to make an Array a permutation of first N natural numbers | Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call
function make_permutation ( arr , n ) { arr . sort ( ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += Math . abs ( i + 1 - arr [ i ] ) ; return ans ; } let arr = [ 5 , 3 , 8 , 1 , 1 ] ; let n = arr . length ; document . write ( make_permutation ( arr , n ) ) ;
Find maximum sum from top to bottom row with no adjacent diagonal elements | Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver Code ; Function to find maximum path
function maxSum ( V , n , m ) { let ans = 0 ; for ( let i = n - 2 ; i >= 0 ; -- i ) { let aux = new Array ( ) ; for ( let j = 0 ; j < m ; ++ j ) { aux . push ( [ V [ i + 1 ] [ j ] , j ] ) ; } aux . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; aux . reverse ( ) ; for ( let j = 0 ; j < m ; ++ j ) { for ( let k = 0 ; k < m ; ++ k ) { if ( aux [ k ] [ 1 ] - j == 0 || Math . abs ( aux [ k ] [ 1 ] - j ) > 1 ) { V [ i ] [ j ] += aux [ k ] [ 0 ] ; break ; } } } } for ( let i = 0 ; i < m ; ++ i ) { ans = Math . max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } let V = [ [ 1 , 2 , 3 , 4 ] , [ 8 , 7 , 6 , 5 ] , [ 10 , 11 , 12 , 13 ] ] ; let n = V . length ; let m = V [ 0 ] . length ; document . write ( maxSum ( V , n , m ) ) ;
Count numbers whose maximum sum of distinct digit | Function to find the digit - sum of a number ; Loop to iterate the number digit - wise to find digit - sum ; Variable to store last digit ; Function to find the count of number ; Vector to store the Sum of Digits ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; Count variable to store the Count ; Finding the Count of Numbers ; Driver Code ; Function Call
function SumofDigits ( digit ) { let sum = 0 ; while ( digit != 0 ) { let rem = digit % 10 ; sum += rem ; digit = Math . floor ( digit / 10 ) ; } return sum ; } function findCountofNumbers ( arr , n , M ) { let SumDigits = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { let s = SumofDigits ( arr [ i ] ) ; SumDigits . push ( s ) ; } SumDigits . sort ( function ( a , b ) { return a - b ; } ) ; let ip = Array . from ( new Set ( SumDigits ) ) ; let count = 0 ; let sum = 0 ; for ( let i = 0 ; i < SumDigits . length ; i ++ ) { if ( sum > M ) break ; sum += SumDigits [ i ] ; if ( sum <= M ) count ++ ; } return count ; } let arr = [ 1 , 45 , 16 , 17 , 219 , 32 , 22 ] ; let M = 10 ; let n = arr . length ; document . write ( findCountofNumbers ( arr , n , M ) ) ;
Minimum sum of product of elements of pairs of the given array | Function to find the minimum product ; Sort the array using STL sort ( ) function ; Initialise product to 1 ; Find product of sum of all pairs ; Return the product ; Driver code ; Function call to find product
function minimumProduct ( arr , n ) { arr . sort ( ) ; let product = 1 ; for ( let i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } let arr = [ 1 , 6 , 3 , 1 , 7 , 8 ] ; let n = arr . length ; document . write ( minimumProduct ( arr , n ) + " " ) ;
Check if elements of array can be arranged in AP , GP or HP | Returns true if arr [ 0. . n - 1 ] can form AP ; Base Case ; Sort array ; After sorting , difference between consecutive elements must be same . ; Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form GP ; Base Case ; Sort array ; After sorting , common ratio between consecutive elements must be same . ; Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form HP ; Base Case ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . ; Driver 's Code ; Function to check AP ; Function to check GP ; Function to check HP
function checkIsAP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( function ( a , b ) { return a - b ; } ) ; var d = arr [ 1 ] - arr [ 0 ] ; for ( var i = 2 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != d ) { return false ; } } return true ; } function checkIsGP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( ) ; var r = arr [ 1 ] / arr [ 0 ] ; for ( var i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } function checkIsHP ( arr , n ) { if ( n == 1 ) { return true ; } var rec = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } if ( checkIsAP ( rec , n ) ) return true ; else return false ; } var arr = [ 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 ] ; var n = arr . length ; var flag = 0 ; if ( checkIsAP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } if ( checkIsGP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } if ( checkIsHP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( flag == 0 ) { document . write ( " " ) ; }
Sort the Array by reversing the numbers in it | Function to return the reverse of n ; Function to sort the array according to the reverse of elements ; Vector to store the reverse with respective elements ; Inserting reverse with elements in the vector pair ; Sort the vector , this will sort the pair according to the reverse of elements ; Print the sorted vector content ; Driver code
function reversDigits ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = Math . floor ( num / 10 ) ; } return rev_num ; } function sortArr ( arr , n ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] = [ ] ; } for ( var i = 0 ; i < n ; i ++ ) { var pair = [ ] ; pair . push ( reversDigits ( arr [ i ] ) ) ; pair . push ( arr [ i ] ) ; vp [ i ] = pair ; } vp = vp . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] ; } ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( vp [ i ] [ 1 ] + " " ) ; } } var arr = [ 12 , 10 , 102 , 31 , 15 ] ; var n = arr . length ; sortArr ( arr , n ) ;