"A Product Array Puzzle | Set 3 | Function to form product array with O ( n ) time and O ( 1 ) space ; Stores the product of array ; Stores the count of zeros ; Traverse the array ; If arr [ i ] is not zero ; If arr [ i ] is zero then increment count of z by 1 ; Stores the absolute value of the product ; If Z is equal to 1 ; If arr [ i ] is not zero ; Else ; If count of 0 s at least 2 ; Assign arr [ i ] = 0 ; Store absolute value of arr [ i ] ; Find the value of a / b ; If arr [ i ] and product both are less than zero ; If arr [ i ] and product both are greater than zero ; Else ; Traverse the array arr [ ] ; Driver Code ; Function Call"
How can the above be solved in JS?

function productExceptSelf ( arr , N ) { let product = 1 ; let z = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) product *= arr [ i ] ; if ( arr [ i ] == 0 ) z += 1 ; } let a = Math . abs ( product ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( z == 1 ) { if ( arr [ i ] != 0 ) arr [ i ] = 0 ; else arr [ i ] = product ; continue ; } else if ( z > 1 ) { arr [ i ] = 0 ; continue ; } let b = Math . abs ( arr [ i ] ) ; let curr = Math . round ( Math . exp ( Math . log ( a ) - Math . log ( b ) ) ) ; if ( arr [ i ] < 0 && product < 0 ) arr [ i ] = curr ; else if ( arr [ i ] > 0 && product > 0 ) arr [ i ] = curr ; else arr [ i ] = - 1 * curr ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 10 , 3 , 5 , 6 , 2 ] ; let N = arr . length ; productExceptSelf ( arr , N ) ;


"Absolute difference between the count of odd and even factors of N | Function to find the smallest prime factor of all the numbers using Sieve Of Eratosthenes ; Stores whether any number is prime or not ; Initialize smallest factor as 2 for all the even numbers ; Iterate over the range [ 3 , N ] ; If i is prime ; Iterate all multiples of i ; i is the smallest prime factor of i * j ; Function to find the absolute difference between the count of odd and even factors of N ; Stores the smallest prime factor of i ; Fill values in s [ ] using sieve of eratosthenes ; Stores the total number of factors and the total number of odd and even factors ; Store the current prime factor of the number N ; Store the power of current prime factor ; Loop while N is greater than 1 ; If N also has smallest prime factor as curr , then increment cnt by 1 ; Update only total number of factors if curr is 2 ; Update total number of factors and total number of odd factors ; Update current prime factor as s [ N ] and count as 1 ; Calculate the number of even factors ; Prlet the difference ; Driver Code"
How can the above be solved in JS?

function sieveOfEratosthenes ( N , s ) { let prime = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( let i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( let j = i ; j * i <= N ; j += 2 ) { if ( ! prime [ i * j ] ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } function findDifference ( N ) { let s = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; sieveOfEratosthenes ( N , s ) ; let total = 1 , odd = 1 , even = 0 ; let curr = s [ N ] ; let cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } if ( curr == 2 ) { total = total * ( cnt + 1 ) ; } else { total = total * ( cnt + 1 ) ; odd = odd * ( cnt + 1 ) ; } curr = s [ N ] ; cnt = 1 ; } even = total - odd ; document . write ( Math . abs ( even - odd ) ) ; } let N = 12 ; findDifference ( N ) ;


"Add two numbers without using arithmetic operators | Javascript Program to add two numbers without using arithmetic operator ; Iterate till there is no carry ; carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; driver code"
How can the above be solved in JS?

function Add ( x , y ) { while ( y != 0 ) { let carry = x & y ; x = x ^ y ; y = carry << 1 ; } return x ; } document . write ( Add ( 15 , 32 ) ) ;


"Add two numbers without using arithmetic operators |"
How can the above be solved in JS?

function Add ( x , y ) { if ( y == 0 ) return x ; else return Add ( x ^ y , ( x & y ) << 1 ) ; }


"Angle between 3 given vertices in a n | Function that checks whether given angle can be created using any 3 sides ; Initialize x and y ; Calculate the number of vertices between i and j , j and k ; Calculate the angle subtended at the circumference ; Angle subtended at j can be found using the fact that the sum of angles of a triangle is equal to 180 degrees ; Driver code"
How can the above be solved in JS?

function calculate_angle ( n , i , j , k ) { var x , y ; if ( i < j ) x = j - i ; else x = j + n - i ; if ( j < k ) y = k - j ; else y = k + n - j ; var ang1 = ( 180 * x ) / n ; var ang2 = ( 180 * y ) / n ; var ans = 180 - ang1 - ang2 ; return ans ; } var n = 5 ; var a1 = 1 ; var a2 = 2 ; var a3 = 5 ; document . write ( parseInt ( calculate_angle ( n , a1 , a2 , a3 ) ) ) ;


"Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given | javascript program to find the angle subtended by the chord to the centre of the circle when the angle subtended by another equal chord of a congruent circle is given ; Driver code"
How can the above be solved in JS?

function anglequichord ( z ) { document . write ( " " + z + " " ) ; } var z = 48 ; anglequichord ( z ) ;


"Anti | Iterative function to reverse digits of num ; Return the reversed num ; Function to calculate sum of reverse all proper divisors ; Final result of summation of divisors ; Find all divisors of num ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Function to check if N is anti - perfect or not ; Given Number N ; Function Call"
How can the above be solved in JS?

function rev ( num ) { var rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = Math . floor ( num / 10 ) ; } return rev_num ; } function divSum ( num ) { var result = 0 ; for ( var i = 2 ; i <= Math . floor ( Math . sqrt ( num ) ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += rev ( i ) ; else result += ( rev ( i ) + rev ( num / i ) ) ; } } result += 1 ; return result ; } function isAntiPerfect ( n ) { return divSum ( n ) == n ; } var N = 244 ; if ( isAntiPerfect ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Apothem of a n | Function to find the apothem of a regular polygon ; Side and side length cannot be negative ; Degree converted to radians ; Driver code"
How can the above be solved in JS?

function polyapothem ( n , a ) { if ( a < 0 && n < 0 ) return - 1 ; return ( a / ( 2 * Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ) ; } var a = 9 , n = 6 ; document . write ( polyapothem ( n , a ) . toFixed ( 5 ) ) ;


"Arc length from given Angle | function to calculate arc length ; Driver Code"
How can the above be solved in JS?

function arcLength ( diameter , angle ) { let pi = 22.0 / 7.0 ; let arc ; if ( angle >= 360 ) { document . write ( " " + " " ) ; return 0 ; } else { arc = ( pi * diameter ) * ( angle / 360.0 ) ; return arc ; } } let diameter = 25.0 ; let angle = 45.0 ; let arc_len = arcLength ( diameter , angle ) ; document . write ( arc_len ) ;


"Area of Equilateral triangle inscribed in a Circle of radius R | Function to find the area of equilateral triangle inscribed in a circle of radius R ; Base and Height of equilateral triangle ; Area using Base and Height ; Driver code"
How can the above be solved in JS?

function area ( R ) { var base = 1.732 * R ; var height = ( 1.5 ) * R ; var area = 0.5 * base * height ; return area ; } var R = 7 ; document . write ( area ( R ) ) ;


"Area of a Circular Sector | Javascript program to find Area of a Sector ; Calculating area of the sector ; Driver code"
How can the above be solved in JS?

function SectorArea ( radius , angle ) { if ( angle >= 360 ) document . write ( " " ) ; else { let sector = ( ( 22 * radius * radius ) / 7 ) * ( angle / 360 ) ; document . write ( sector ) ; } } let radius = 9 ; let angle = 60 ; SectorArea ( radius , angle ) ;


"Area of a Hexagon | function for calculating area of the hexagon . ; Length of a side"
How can the above be solved in JS?

function hexagonArea ( s ) { return ( ( 3 * Math . sqrt ( 3 ) * ( s * s ) ) / 2 ) ; } let s = 4 ; document . write ( " " + hexagonArea ( s ) ) ;


"Area of a Regular Pentagram | Javascript implementation of the approach ; Function to return the area of triangle BCD ; Using Golden ratio ; Calculate area of triangle BCD ; Return area of all 5 triangle are same ; Function to return the area of regular pentagon ; Calculate the area of regular pentagon using above formula ; Return area of regular pentagon ; Function to return the area of pentagram ; Area of a pentagram is equal to the area of regular pentagon and five times the area of Triangle ; Driver code"
How can the above be solved in JS?

var PI = 3.14159 function areaOfTriangle ( d ) { var c = 1.618 * d ; var s = ( d + c + c ) / 2 ; var area = Math . sqrt ( s * ( s - c ) * ( s - c ) * ( s - d ) ) ; return 5 * area ; } function areaOfRegPentagon ( d ) { var cal = 4 * Math . tan ( PI / 5 ) ; var area = ( 5 * d * d ) / cal ; return area ; } function areaOfPentagram ( d ) { return areaOfRegPentagon ( d ) + areaOfTriangle ( d ) ; } var d = 5 ; document . write ( areaOfPentagram ( d ) . toFixed ( 3 ) ) ;


"Area of a Square | Using Side , Diagonal and Perimeter | Function to find the area of a square ; Use above formula ; Given Side of square ; Function call"
How can the above be solved in JS?

function areaOfSquare ( S ) { let area = S * S ; return area ; } let S = 5 ; document . write ( areaOfSquare ( S ) ) ;


"Area of a n | Function to find the area of a regular polygon ; Side and radius cannot be negative ; Area degree converted to radians ; Driver code"
How can the above be solved in JS?

function polyarea ( n , r ) { if ( r < 0 && n < 0 ) return - 1 ; var A = ( ( r * r * n ) * Math . sin ( ( 360 / n ) * 3.14159 / 180 ) ) / 2 ; return A ; } var r = 9 , n = 6 ; document . write ( polyarea ( n , r ) . toFixed ( 5 ) ) ;


"Area of a n | Function to find the area of a regular polygon ; Side and side length cannot be negative ; Area degree converted to radians ; Driver code"
How can the above be solved in JS?

function polyarea ( n , a ) { if ( a < 0 && n < 0 ) return - 1 ; var A = ( a * a * n ) / ( 4 * Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ; return A ; } var a = 9 , n = 6 ; document . write ( polyarea ( n , a ) . toFixed ( 5 ) ) ;


"Area of a square inscribed in a circle which is inscribed in a hexagon | Function to find the area of the square ; side of hexagon cannot be negative ; area of the square ; Driver code"
How can the above be solved in JS?

function area ( a ) { if ( a < 0 ) return - 1 ; var area = Math . pow ( ( a * Math . sqrt ( 3 ) ) / ( Math . sqrt ( 2 ) ) , 2 ) ; return area ; } var a = 5 ; document . write ( area ( a ) . toFixed ( 5 ) ) ;


"Area of a square inscribed in a circle which is inscribed in an equilateral triangle | Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code"
How can the above be solved in JS?

function area ( a ) { if ( a < 0 ) return - 1 ; var area = Math . sqrt ( a ) / 6 ; return area ; } var a = 10 ; document . write ( area ( a ) . toFixed ( 6 ) ) ;


"Area of circle inscribed in a Isosceles Trapezoid | Function to find area of circle inscribed in a trapezoid having non - parallel sides m , n ; Radius of circle by the formula i . e . root ( m * n ) / 2 area of circle = ( 3.141 ) * ( R * * 2 ) ; Driver Code"
How can the above be solved in JS?

function area_of_circle ( m , n ) { var square_of_radius = ( m * n ) / 4 ; var area = ( 3.141 * square_of_radius ) ; return area ; } var n = 10 ; var m = 30 ; document . write ( area_of_circle ( m , n ) ) ;


"Area of largest Circle that can be inscribed in a SemiCircle | Function to find the area of the circle ; Radius cannot be negative ; Area of the largest circle ; Driver code"
How can the above be solved in JS?

function circlearea ( R ) { if ( R < 0 ) return - 1 ; var a = 3.14 * R * R / 4 ; return a ; } var R = 2 ; document . write ( circlearea ( R ) ) ;


"Area of the biggest possible rhombus that can be inscribed in a rectangle | Function to find the area of the biggest rhombus ; the length and breadth cannot be negative ; area of the rhombus ; Driver code"
How can the above be solved in JS?

function rhombusarea ( l , b ) { if ( l < 0 b < 0 ) return - 1 ; return ( l * b ) / 2 ; } var l = 16 , b = 6 ; document . write ( rhombusarea ( l , b ) ) ;


"Area of the largest Rectangle without a given point | Function to find the maximum area such that it does not contains any hole ; Area for all the possible positions of the cut ; Find the maximum area among the above rectangles ; Driver Code ; Function call"
How can the above be solved in JS?

function maximumArea ( l , b , x , y ) { var left = x * b ; var right = ( l - x - 1 ) * b ; var above = l * y ; var below = ( b - y - 1 ) * l ; document . write ( Math . max ( Math . max ( left , right ) , Math . max ( above , below ) ) ) ; } var L = 8 , B = 8 ; var X = 0 , Y = 0 ; maximumArea ( L , B , X , Y ) ;


"Arrange consonants and vowels nodes in a linked list | A linked list node ; Function to add new node to the List ; utility function to print linked list ; utility function for checking vowel ; function to arrange consonants and vowels nodes ; for keep track of vowel ; list is empty ; We need to discover the first vowel in the list . It is going to be the returned head , and also the initial latestVowel . ; first element is a vowel . It will also be the new head and the initial latestVowel ; ; First element is not a vowel . Iterate through the list until we find a vowel . Note that curr points to the element * before * the element with the vowel . ; This is an edge case where there are only consonants in the list . ; Set the initial latestVowel and the new head to the vowel item that we found . Relink the chain of consonants after that vowel item : old_head_consonant -> consonant1 -> consonant2 -> vowel -> rest_of_list becomes vowel -> old_head_consonant -> consonant1 -> consonant2 -> rest_of_list ; Now traverse the list . Curr is always the item * before * the one we are checking , so that we can use it to re - link . ; The next discovered item is a vowel ; If it comes directly after the previous vowel , we don 't need to  move items around, just mark the  new latestVowel and advance curr. ; But if it comes after an intervening chain of consonants , we need to chain the newly discovered vowel right after the old vowel . Curr is not changed as after the re - linking it will have a new next , that has not been checked yet , and we always keep curr at one before the next to check . ; Chain in new vowel ; Advance latestVowel ; Remove found vowel from previous place ; Re - link chain of consonants after latestVowel ; No vowel in the next element , advance curr . ; Driver code"
How can the above be solved in JS?

class Node { constructor ( key ) { this . data = key ; this . next = null ; } } function printlist ( head ) { if ( head == null ) { document . write ( " " ) ; return ; } while ( head != null ) { document . write ( head . data + " " ) ; if ( head . next != null ) document . write ( " " ) ; head = head . next ; } document . write ( " " ) ; } function isVowel ( x ) { return ( x == ' ' x == ' ' x == ' ' x == ' ' x == ' ' ) ; } function arrange ( head ) { let newHead = head ; let latestVowel ; let curr = head ; if ( head == null ) return null ; if ( isVowel ( head . data ) == true ) latestVowel = head ; else { while ( curr . next != null && ! isVowel ( curr . next . data ) ) curr = curr . next ; if ( curr . next == null ) return head ; latestVowel = newHead = curr . next ; curr . next = curr . next . next ; latestVowel . next = head ; } while ( curr != null && curr . next != null ) { if ( isVowel ( curr . next . data ) == true ) { if ( curr == latestVowel ) { latestVowel = curr = curr . next ; } else { let temp = latestVowel . next ; latestVowel . next = curr . next ; latestVowel = latestVowel . next ; curr . next = curr . next . next ; latestVowel . next = temp ; } } else { curr = curr . next ; } } return newHead ; } let head = new Node ( ' ' ) ; head . next = new Node ( ' ' ) ; head . next . next = new Node ( ' ' ) ; head . next . next . next = new Node ( ' ' ) ; head . next . next . next . next = new Node ( ' ' ) ; head . next . next . next . next . next = new Node ( ' ' ) ; head . next . next . next . next . next . next = new Node ( ' ' ) ; head . next . next . next . next . next . next . next = new Node ( ' ' ) ; document . write ( " " ) ; printlist ( head ) ; head = arrange ( head ) ; document . write ( " " ) ; printlist ( head ) ;


"Arrange given numbers to form the smallest number | Utility function to print the contents of an array ; A comparison function that return true if ' AB ' is smaller than ' BA ' when we concatenate two numbers ' A ' and ' B ' For example , it will return true if we pass 12 and 24 as arguments . This function will be used by sort ( ) function ; Convert first number to string format ; Convert second number to string format ; Check if ' AB ' is smaller or ' BA ' and return bool value since comparison operator ' < = ' returns true or false ; Function to print the arrangement with the smallest value ; If we pass the name of the comparison function it will sort the array according to the compare function Sort using compare function which is defined above ; Print the sorted array ; Driver code"
How can the above be solved in JS?

function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] ) ; } function compare ( num1 , num2 ) { let A = num1 . toString ( ) ; let B = num2 . toString ( ) ; return ( A + B ) . localeCompare ( B + A ) ; } function printSmallest ( N , arr ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( compare ( arr [ i ] , arr [ j ] ) > 0 ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } printArr ( arr , N ) ; } let arr = [ 5 , 6 , 2 , 9 , 21 , 1 ] ; let N = arr . length ; printSmallest ( N , arr ) ;


"Automorphic Number | Function to check Automorphic number ; Store the square ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ ▁ match ▁ with ▁ its ▁ square ' s digits from last ; Reduce N and square ; Driver code"
How can the above be solved in JS?

function isAutomorphic ( N ) { let sq = N * N ; while ( N > 0 ) { if ( N % 10 != sq % 10 ) return - 1 ; N /= 10 ; sq /= 10 ; } return 1 ; } let N = 5 ; let geeks = isAutomorphic ( N ) ? " " : " " ; document . write ( geeks ) ;


"Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function"
How can the above be solved in JS?

function averageEven ( n ) { if ( n % 2 != 0 ) { document . write ( " " ) ; return - 1 ; } let sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } let n = 16 ; document . write ( averageEven ( n ) ) ;


"Average of even numbers till a given even number | Function to calculate the average of even numbers ; driver function"
How can the above be solved in JS?

function averageEven ( n ) { if ( n % 2 != 0 ) { document . write ( " " ) ; return - 1 ; } return ( n + 2 ) / 2 ; } let n = 16 ; document . write ( averageEven ( n ) ) ;


"Average of first n odd naturals numbers | Return the average of sum of first n odd numbers ; Driver Code"
How can the above be solved in JS?

function avg_of_odd_num ( n ) { return n ; } var n = 8 ; document . write ( avg_of_odd_num ( n ) ) ;


"Average of first n odd naturals numbers | Returns the Avg of first n odd numbers ; sum of first n odd number ; Average of first n odd numbers ; Driver Code"
How can the above be solved in JS?

function avg_of_odd_num ( n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ( 2 * i + 1 ) ; return sum / n ; } let n = 20 ; document . write ( avg_of_odd_num ( n ) ) ;


"Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; count odd numbers ; store the sum of odd numbers ; driver function"
How can the above be solved in JS?

function averageOdd ( n ) { if ( n % 2 == 0 ) { document . write ( " " ) ; return - 1 ; } let sum = 0 , count = 0 ; while ( n >= 1 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } let n = 15 ; document . write ( averageOdd ( n ) ) ;


"Average of odd numbers till a given odd number | Function to calculate the average of odd numbers ; driver function"
How can the above be solved in JS?

function averageOdd ( n ) { if ( n % 2 == 0 ) { document . write ( " " ) ; return - 1 ; } return ( n + 1 ) / 2 ; } let n = 15 ; document . write ( averageOdd ( n ) ) ;


"Biggest Square that can be inscribed within an Equilateral triangle | Function to find the side of the square ; the side cannot be negative ; side of the square ; Driver code"
How can the above be solved in JS?

function square ( a ) { if ( a < 0 ) return - 1 ; var x = 0.464 * a ; return x ; } var a = 5 ; document . write ( square ( a ) . toFixed ( 2 ) ) ;


"Binary Insertion Sort | iterative implementation ; Function to sort an array a [ ] of size ' n ' ; find location where selected should be inseretd ; Move all elements after location to create space ; Driver Code"
How can the above be solved in JS?

function binarySearch ( a , item , low , high ) { while ( low <= high ) { var mid = low + ( high - low ) / 2 ; if ( item == a [ mid ] ) return mid + 1 ; else if ( item > a [ mid ] ) low = mid + 1 ; else high = mid - 1 ; } return low ; } function insertionSort ( a , n ) { var i , loc , j , k , selected ; for ( i = 1 ; i < n ; ++ i ) { j = i - 1 ; selected = a [ i ] ; loc = binarySearch ( a , selected , 0 , j ) ; while ( j >= loc ) { a [ j + 1 ] = a [ j ] ; j -- ; } a [ j + 1 ] = selected ; } } var a = [ 37 , 23 , 0 , 17 , 12 , 72 , 31 , 46 , 100 , 88 , 54 ] ; var n = a . length , i ; insertionSort ( a , n ) ; document . write ( " " + " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ;


"Bitwise OR of Bitwise AND of all subarrays of an array | Function to find the Bitwise OR of Bitwise AND of all consecutive subsets of the array ; Stores the required result ; Traverse the given array ; Print the result ; Driver Code"
How can the above be solved in JS?

function findbitwiseOR ( a , n ) { var res = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; document . write ( res ) ; } var A = [ 1 , 2 , 3 ] ; var N = A . length ; findbitwiseOR ( A , N ) ;


"Bitwise OR of Bitwise AND of all subarrays of an array | Function to find the Bitwise OR of Bitwise AND of all subarrays ; Stores the required result ; Generate all the subarrays ; Store the current element ; Find the Bitwise OR ; Update the result ; Print the result ; Driver Code"
How can the above be solved in JS?

function findbitwiseOR ( a , n ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let curr_sub_array = a [ i ] ; res = res | curr_sub_array ; for ( let j = i ; j < n ; j ++ ) { curr_sub_array = curr_sub_array & a [ j ] ; res = res | curr_sub_array ; } } document . write ( res ) ; } let A = [ 1 , 2 , 3 ] ; let N = A . length ; findbitwiseOR ( A , N ) ;


"Bitwise OR of bitwise AND of all possible non | Function to find the Bitwise OR of Bitwise AND of all possible subarrays after performing the every query ; Traversing each pair of the query ; Stores the Bitwise OR ; Updating the array ; Find the Bitwise OR of new updated array ; Print the ans ; Driver Code"
How can the above be solved in JS?

function performQuery ( arr , Q ) { for ( let i = 0 ; i < Q . length ; i ++ ) { let or = 0 ; let x = Q [ i ] [ 0 ] ; arr [ x - 1 ] = Q [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { or = or | arr [ j ] ; } document . write ( or + " " ) ; } } let arr = [ 1 , 2 , 3 ] ; let Q = [ [ 1 , 4 ] , [ 3 , 0 ] ] ; performQuery ( arr , Q ) ;


"Calculate 7 n / 8 without using division and multiplication operators | JavaScript program to evaluate ceil ( 7 n / 8 ) without using * and / ; Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < ' ; Driver program to test above function"
How can the above be solved in JS?

function multiplyBySevenByEight ( n ) { return ( n - ( n >> 3 ) ) ; } let n = 9 ; document . write ( multiplyBySevenByEight ( n ) ) ;


"Calculate 7 n / 8 without using division and multiplication operators | Javascript program to evaluate 7 n / 8 without using * and / ; Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n * Step 2 ) Divide result by 8 ; Driver code"
How can the above be solved in JS?

function multiplyBySevenByEight ( n ) { return ( ( n << 3 ) - n ) >> 3 ; } var n = 15 ; document . write ( multiplyBySevenByEight ( n ) ) ;


"Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values | Function to calculate Bitwise OR from given bitwise XOR and bitwise AND values ; Driver Code"
How can the above be solved in JS?

function findBitwiseORGivenXORAND ( X , Y ) { return X + Y ; } let X = 5 , Y = 2 ; document . write ( findBitwiseORGivenXORAND ( X , Y ) ) ;


"Calculate Median from given values of Mean and Mode | Function to find the median of a group of data with given mean and mode ; Calculate the median ; Print the median ; Driver Code"
How can the above be solved in JS?

function findMedian ( Mean , Mode ) { var Median = ( 2 * Mean + Mode ) / 3.0 ; document . write ( Median ) ; } var mode = 6 , mean = 3 ; findMedian ( mean , mode ) ;


"Calculate area of a cyclic quadrilateral with given side lengths | Function to find the area of cyclic quadrilateral ; Stores the value of half of the perimeter ; Stores area of cyclic quadrilateral ; Return the resultant area ; Driver Code"
How can the above be solved in JS?

function calculateArea ( A , B , C , D ) { let S = ( A + B + C + D ) / 2 let area = Math . sqrt ( ( S - A ) * ( S - B ) * ( S - C ) * ( S - D ) ) return area ; } let A = 10 ; let B = 15 ; let C = 20 ; let D = 25 ; document . write ( calculateArea ( A , B , C , D ) . toFixed ( 3 ) )


"Calculate nCr using Pascal 's Triangle | Initialize the matrix with 0 ; 0 C0 = 1 ; Set every nCr = 1 where r = 0 ; Value for the current cell of Pascal 's triangle ; Function to return the value of nCr ; Return nCr ; Build the Pascal 's triangle"
How can the above be solved in JS?

let l = new Array ( 1001 ) . fill ( 0 ) . map ( ( ) => new Array ( 1001 ) . fill ( 0 ) ) ; function initialize ( ) { l [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i < 1001 ; i ++ ) { l [ i ] [ 0 ] = 1 ; for ( let j = 1 ; j < i + 1 ; j ++ ) { l [ i ] [ j ] = ( l [ i - 1 ] [ j - 1 ] + l [ i - 1 ] [ j ] ) ; } } } function nCr ( n , r ) { return l [ n ] [ r ] ; } initialize ( ) ; let n = 8 ; let r = 3 ; document . write ( nCr ( n , r ) ) ;


"Calculate ratio of area of a triangle inscribed in an Ellipse and the triangle formed by corresponding points on auxiliary circle | Function to calculate ratio of a triangle inscribed in an ellipse to the triangle on the auxiliary circle ; Stores the ratio of the semi - major to semi - minor axes ; Print the ratio ; Driver Code"
How can the above be solved in JS?

function triangleArea ( a , b ) { ratio = b / a document . write ( ratio ) } var a = 1 var b = 2 triangleArea ( a , b )


"Calculate the value of 2 raised to the power of twice the binary representation of N | javascript program to implement the above approach ; Function to find the value of power ( X , Y ) in O ( log Y ) ; Stores power ( X , Y ) ; Update X ; Base Case ; Calculate power ( X , Y ) ; If Y is an odd number ; Update res ; Update Y ; Update X ; Function to calculate ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Stores binary representation of n ; Stores power of 10 ; Calculate the binary representation of n ; If n is an odd number ; Update X ; Update pow_10 ; Update n ; Double the value of X ; Stores the value of ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Driver code"
How can the above be solved in JS?

M = 1000000007 ; function power ( X , Y ) { var res = 1 ; X = X % M ; if ( X == 0 ) return 0 ; while ( Y > 0 ) { if ( ( Y & 1 ) != 0 ) { res = ( res * X ) % M ; } Y = Y >> 1 ; X = ( X * X ) % M ; } return res ; } function findValue ( n ) { var X = 0 ; var pow_10 = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) { X += pow_10 ; } pow_10 *= 10 ; n /= 2 ; } X = ( X * 2 ) % M ; var res = power ( 2 , X ) ; return res ; } var n = 2 ; document . write ( findValue ( n ) ) ;


"Central polygonal numbers | Function to find N - th term in the series ; Driver code"
How can the above be solved in JS?

function findNthTerm ( n ) { document . write ( n * n - n + 1 ) ; } N = 4 ; findNthTerm ( N ) ;


"Character pairs from two strings with even sum | Function to return the total number of valid pairs ; Count total number of even and odd ascii values for string s1 ; Count total number of even and odd ascii values for string s2 ; Return total valid pairs ; Driver code"
How can the above be solved in JS?

function totalPairs ( s1 , s2 ) { var a1 = 0 , b1 = 0 ; for ( var i = 0 ; i < s1 . length ; i ++ ) { if ( ( s1 [ i ] . charCodeAt ( 0 ) ) % 2 != 0 ) a1 ++ ; else b1 ++ ; } var a2 = 0 , b2 = 0 ; for ( var i = 0 ; i < s2 . length ; i ++ ) { if ( ( s2 [ i ] . charCodeAt ( 0 ) ) % 2 != 0 ) a2 ++ ; else b2 ++ ; } return ( ( a1 * a2 ) + ( b1 * b2 ) ) ; } var s1 = " " , s2 = " " ; document . write ( totalPairs ( s1 , s2 ) ) ;


"Check divisibility of binary string by 2 ^ k | Function to check whether given binary number is evenly divisible by 2 ^ k or not ; Count of number of 0 from last ; If count = k , number is evenly divisible , so returns true else false ; First example ; Second example"
How can the above be solved in JS?

function isDivisible ( str , k ) { let n = str . length ; let c = 0 ; for ( let i = 0 ; i < k ; i ++ ) if ( str [ n - i - 1 ] == ' ' ) c ++ ; return ( c == k ) ; } let str1 = " " ; let k = 2 ; if ( isDivisible ( str1 , k ) == true ) document . write ( " " + " " ) ; else document . write ( " " ) ; let str2 = " " ; k = 2 ; if ( isDivisible ( str2 , k ) == true ) document . write ( " " ) ; else document . write ( " " ) ;


"Check duplicates in a stream of strings | Function to insert the names and check whether they appear for the first time ; To store the names of the employees ; If current name is appearing for the first time ; Driver code"
How can the above be solved in JS?

function insertNames ( arr , n ) { let set = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! set . has ( arr [ i ] ) ) { document . write ( " " + " " ) ; set . add ( arr [ i ] ) ; } else { document . write ( " " + " " ) ; } } } let arr = [ " " , " " , " " ] ; let n = arr . length ; insertNames ( arr , n ) ;


"Check for Majority Element in a sorted array |  ;"
How can the above be solved in JS?

function isMajorityElement ( arr , n , key ) { if ( arr [ parseInt ( n / 2 , 10 ) ] == key ) return true ; else return false ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 10 ] ; let n = arr . length ; let x = 3 ; if ( isMajorityElement ( arr , n , x ) ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + " " + parseInt ( n / 2 , 10 ) + " " ) ;


"Check for Majority Element in a sorted array | If x is present in arr [ low ... high ] then returns the index of first occurrence of x , otherwise returns - 1 ; Check if arr [ mid ] is the first occurrence of x . arr [ mid ] is first occurrence if x is one of the following is true : ( i ) mid == 0 and arr [ mid ] = = x ( ii ) arr [ mid - 1 ] < x and arr [ mid ] == x ; This function returns true if the x is present more than n / 2 times in arr [ ] of size n ; Find the index of first occurrence of x in arr [ ] ; If element is not present at all , return false ; check if the element is present more than n / 2 times ; Driver code"
How can the above be solved in JS?

function _binarySearch ( arr , low , high , x ) { if ( high >= low ) { let mid = parseInt ( ( low + high ) / 2 , 10 ) ; if ( ( mid == 0 x > arr [ mid - 1 ] ) && ( arr [ mid ] == x ) ) return mid ; else if ( x > arr [ mid ] ) return _binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return _binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } function isMajority ( arr , n , x ) { let i = _binarySearch ( arr , 0 , n - 1 , x ) ; if ( i == - 1 ) return false ; if ( ( ( i + parseInt ( n / 2 , 10 ) ) <= ( n - 1 ) ) && arr [ i + parseInt ( n / 2 , 10 ) ] == x ) return true ; else return false ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 10 ] ; let n = arr . length ; let x = 3 ; if ( isMajority ( arr , n , x ) == true ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ;


"Check for Majority Element in a sorted array | Javascript Program to check for majority element in a sorted array ; Get last index according to n ( even or odd ) ; Search for first occurrence of x in arr [ ] ; Check if x is present and is present more than n / 2 times ; Driver code"
How can the above be solved in JS?

function isMajority ( arr , n , x ) { let i , last_index = 0 ; last_index = ( n % 2 == 0 ) ? parseInt ( n / 2 , 10 ) : parseInt ( n / 2 , 10 ) + 1 ; for ( i = 0 ; i < last_index ; i ++ ) { if ( arr [ i ] == x && arr [ i + parseInt ( n / 2 , 10 ) ] == x ) return true ; } return false ; } let arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 ] ; let n = arr . length ; let x = 4 ; if ( isMajority ( arr , n , x ) == true ) document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ; else document . write ( x + " " + parseInt ( n / 2 , 10 ) + " " ) ;


"Check for an array element that is co | Javascript implementation of the approach ; Stores smallest prime factor for every number ; Hash to store prime factors count ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to store the prime factors after dividing by the smallest prime factor at every step ; Storing the count of prime factors in hash ; Function that returns true if there are no common prime factors between x and other numbers of the array ; Checking whether it common prime factor with other numbers ; Function that returns true if there is an element in the array which is coprime with all the other elements of the array ; Using sieve for generating prime factors ; Checking the common prime factors with other numbers ; Driver code"
How can the above be solved in JS?

let MAXN = 1000001 ; let spf = new Array ( MAXN ) ; let hash1 = new Array ( MAXN ) ; function sieve ( ) { spf [ 1 ] = 1 ; for ( let i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function getFactorization ( x ) { let temp ; while ( x != 1 ) { temp = spf [ x ] ; if ( x % temp == 0 ) { hash1 [ spf [ x ] ] ++ ; x = x / spf [ x ] ; } while ( x % temp == 0 ) x = x / temp ; } } function check ( x ) { let temp ; while ( x != 1 ) { temp = spf [ x ] ; if ( x % temp == 0 && hash1 [ temp ] > 1 ) return false ; while ( x % temp == 0 ) x = x / temp ; } return true ; } function hasValidNum ( arr , n ) { sieve ( ) ; for ( let i = 0 ; i < n ; i ++ ) getFactorization ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) if ( check ( arr [ i ] ) ) return true ; return false ; } let arr = [ 2 , 8 , 4 , 10 , 6 , 7 ] ; let n = arr . length ; if ( hasValidNum ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if Decimal representation of an Octal number is divisible by 7 | Function to check Divisibility ; Sum of all individual digits ; Condition ; Octal number"
How can the above be solved in JS?

function check ( n ) { let sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n = Math . floor ( n / 10 ) ; } if ( sum % 7 == 0 ) return 1 ; else return 0 ; } let n = 25 ; ( check ( n ) == 1 ) ? document . write ( " " ) : document . write ( " " ) ;


"Check if K palindromic strings can be formed from a given string | function to check whether the string is K palindrome or not ; map to frequency of character ; Check when k is given as same as length of string ; storing the frequency of every character in map ; if K is greater than size of string then return false ; check that number of character having the odd frequency ; if k is less than number of odd frequency character then it is again false other wise true ; Driver code"
How can the above be solved in JS?

function can_Construct ( S , K ) { var m = new Map ( ) ; var i = 0 , j = 0 , p = 0 ; if ( S . length == K ) { return true ; } for ( i = 0 ; i < S . length ; i ++ ) { if ( m . has ( S [ i ] ) ) m . set ( S [ i ] , m . get ( S [ i ] ) + 1 ) else m . set ( S [ i ] , 1 ) } if ( K > S . length ) { return false ; } else { m . forEach ( ( value , key ) => { if ( value % 2 != 0 ) { p = p + 1 ; } } ) ; } if ( K < p ) { return false ; } return true ; } var S = " " ; var K = 4 ; if ( can_Construct ( S , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if N can be obtained from 1 by repetitively multiplying by 10 or 20 | Function to check if N can be obtained or not ; Count and remove trailing zeroes ; Check if remaining N is a power of 2 ; To check the condition to prlong YES or NO ; Driver code"
How can the above be solved in JS?

function Is_possible ( N ) { let C = 0 ; let D = 0 ; while ( N % 10 == 0 ) { N = N / 10 ; C += 1 ; } if ( Math . pow ( 2 , ( Math . log ( N ) / ( Math . log ( 2 ) ) ) ) == N ) { D = ( Math . log ( N ) / ( Math . log ( 2 ) ) ) ; if ( C >= D ) document . write ( " " ) ; else document . write ( " " ) ; } else document . write ( " " ) ; } let N = 2000000000000 ; Is_possible ( N ) ;


"Check if N is Strong Prime | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a strong prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a strong prime ; Driver code"
How can the above be solved in JS?

function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isStrongPrime ( n ) { if ( ! isPrime ( n ) n == 2 ) return false ; let previous_prime = n - 1 ; let next_prime = n + 1 ; while ( ! isPrime ( next_prime ) ) next_prime ++ ; while ( ! isPrime ( previous_prime ) ) previous_prime -- ; let mean = parseInt ( ( previous_prime + next_prime ) / 2 ) ; if ( n > mean ) return true ; else return false ; } let n = 11 ; if ( isStrongPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if N is a Factorial Prime | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a factorial prime ; If n is not prime then return false ; Calculate factorial ; If n is a factorial prime ; n is not a factorial prime ; Driver code"
How can the above be solved in JS?

function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isFactorialPrime ( n ) { if ( ! isPrime ( n ) ) return false ; let fact = 1 ; let i = 1 ; while ( fact <= n + 1 ) { fact = fact * i ; if ( n + 1 == fact n - 1 == fact ) return true ; i ++ ; } return false ; } let n = 23 ; if ( isFactorialPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a String contains any index with more than K active characters | Function to check if any index contains more than K active characters ; Store the last occurrence of each character in the map . ; Stores the active characters ; Insert the character ; If the size of set exceeds K ; Remove the character from set if i is the last index of the current character ; Driver Code"
How can the above be solved in JS?

function checkString ( s , K ) { var n = s . length ; var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( s [ i ] ) ) { mp . set ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else mp . set ( s [ i ] , 1 ) ; } var cnt = 0 , f = 0 ; var st = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { st . add ( s [ i ] ) ; if ( st . size > K ) { f = 1 ; break ; } if ( mp . get ( s [ i ] ) == i ) st . delete ( s [ i ] ) ; } return ( f == 1 ? " " : " " ) ; } var s = " " ; var k = 2 ; document . write ( checkString ( s , k ) ) ;


"Check if a circle lies inside another circle or not | javascript program to check if one circle lies inside another circle or not . ; Driver code"
How can the above be solved in JS?


"Check if a given number is factorial of any number | Function to check if the given number is a factorial of any number ; Driver Code"
How can the above be solved in JS?

function isFactorial ( n ) { for ( var i = 1 ; ; i ++ ) { if ( n % i == 0 ) { n = parseInt ( n / i ) ; } else { break ; } } if ( n == 1 ) { return true ; } else { return false ; } } var n = 24 ; var ans = isFactorial ( n ) ; if ( ans == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a given string can be converted to another by given possible swaps | Javascript program to implement the above approach ; Stores length of str1 ; Stores length of str2 ; Stores distinct characters of str1 ; Stores distinct characters of str2 ; Stores frequency of each character of str1 ; Traverse the string str1 ; Update frequency of str1 [ i ] ; Traverse the string str1 ; Insert str1 [ i ] into st1 ; Traverse the string str2 ; Insert str1 [ i ] into st1 ; If distinct characters in str1 and str2 are not same ; Stores frequency of each character of str2 ; Traverse the string str2 ; Update frequency of str2 [ i ] ; Sort hash1 [ ] array ; Sort hash2 [ ] array ; Traverse hash1 [ ] and hash2 [ ] ; If hash1 [ i ] not equal to hash2 [ i ] ; Driver Code"
How can the above be solved in JS?

function checkStr1CanConStr2 ( str1 , str2 ) { var N = str1 . length ; var M = str2 . length ; var st1 = new Set ( ) ; var st2 = new Set ( ) ; var hash1 = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash1 [ str1 [ i ] . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < N ; i ++ ) { st1 . add ( str1 [ i ] ) ; } for ( var i = 0 ; i < M ; i ++ ) { st2 . add ( str2 [ i ] ) ; } if ( st1 . size != st2 . size ) { return false ; } var hash2 = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < M ; i ++ ) { hash2 [ str2 [ i ] . charCodeAt ( 0 ) ] ++ ; } hash1 . sort ( ( a , b ) => a - b ) ; hash2 . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < 256 ; i ++ ) { if ( hash1 [ i ] != hash2 [ i ] ) { return false ; } } return true ; } var str1 = " " ; var str2 = " " ; if ( checkStr1CanConStr2 ( str1 , str2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a given value can be reached from another value in a Circular Queue by K | Function to return GCD of two numbers a and b ; Base Case ; Recursively Find the GCD ; Function to check of B can be reaced from A with a jump of K elements in the circular queue ; Find GCD of N and K ; If A - B is divisible by gcd then print Yes ; Otherwise ; Driver Code ; Function Call"
How can the above be solved in JS?

function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function canReach ( N , A , B , K ) { var gcd = GCD ( N , K ) ; if ( Math . abs ( A - B ) % gcd == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var N = 5 , A = 2 , B = 1 , K = 2 ; canReach ( N , A , B , K ) ;


"Check if a line at 45 degree can divide the plane into two equal weight parts | Checking if a plane can be divide by a line at 45 degrees such that weight sum is equal ; Rotating each point by 45 degrees and calculating prefix sum . Also , finding maximum and minimum x coordinates ; storing weight sum upto x - y point ; Finding prefix sum ; Line passes through i , so it neither falls left nor right . ; Driven code"
How can the above be solved in JS?

function is_partition_possible ( n , x , y , w ) { let weight_at_x = new Map ( ) ; let max_x = - 2e3 , min_x = 2e3 ; for ( let i = 0 ; i < n ; i ++ ) { let new_x = x [ i ] - y [ i ] ; max_x = Math . max ( max_x , new_x ) ; min_x = Math . min ( min_x , new_x ) ; if ( weight_at_x . has ( new_x ) ) { weight_at_x . set ( new_x , weight_at_x . get ( new_x ) + w [ i ] ) ; } else { weight_at_x . set ( new_x , w [ i ] ) ; } } let sum_till = [ ] ; sum_till . push ( 0 ) ; for ( let s = min_x ; s <= max_x ; s ++ ) { if ( weight_at_x . get ( s ) == null ) sum_till . push ( sum_till [ sum_till . length - 1 ] ) ; else sum_till . push ( sum_till [ sum_till . length - 1 ] + weight_at_x . get ( s ) ) ; } let total_sum = sum_till [ sum_till . length - 1 ] ; let partition_possible = 0 ; for ( let i = 1 ; i < sum_till . length ; i ++ ) { if ( sum_till [ i ] == total_sum - sum_till [ i ] ) partition_possible = 1 ; if ( sum_till [ i - 1 ] == total_sum - sum_till [ i ] ) partition_possible = 1 ; } document . write ( partition_possible == 1 ? " " : " " ) ; } let n = 3 ; let x = [ - 1 , - 2 , 1 ] ; let y = [ 1 , 1 , - 1 ] ; let w = [ 3 , 1 , 4 ] ; is_partition_possible ( n , x , y , w ) ;


"Check if a line touches or intersects a circle | JavaScript program to check if a line touches or intersects or outside a circle . ; Finding the distance of line from center . ; Checking if the distance is less than , greater than or equal to radius . ; Driver Code"
How can the above be solved in JS?

function checkCollision ( a , b , c , x , y , radius ) { let dist = ( Math . abs ( a * x + b * y + c ) ) / Math . sqrt ( a * a + b * b ) ; if ( radius == dist ) document . write ( " " ) ; else if ( radius > dist ) document . write ( " " ) ; else document . write ( " " ) ; } let radius = 5 ; let x = 0 , y = 0 ; let a = 3 , b = 4 , c = 25 ; checkCollision ( a , b , c , x , y , radius ) ;


"Check if a number S can be made divisible by D by repeatedly adding the remainder to S | Function to check if S is divisible by D while changing S to ( S + S % D ) ; V ( 0 ) = S % D ; Stores the encountered values ; V ( i ) = ( V ( i - 1 ) + V ( i - 1 ) % D ) % D ; Check if the value has already been encountered ; Edge Case ; Otherwise , insert it into the hashmap ; Driver Code"
How can the above be solved in JS?

function isDivisibleByDivisor ( S , D ) { S %= D ; var hashMap = [ ] ; hashMap . push ( S ) ; for ( var i = 0 ; i <= D ; i ++ ) { S += S % D ; S %= D ; if ( hashMap . includes ( S ) ) { if ( S == 0 ) { return " " ; } return " " ; } else hashMap . push ( S ) ; } return " " ; } var S = 3 , D = 6 ; document . write ( isDivisibleByDivisor ( S , D ) ) ;


"Check if a number can be represented as sum of K positive integers out of which at least K | Function to count all prime factors of a given number ; Count the number of 2 s that divides n ; Since n is odd at this point , skip one element ; While i divides n , count i and divide n ; If n is a prime number greater than 2 ; Function to find the sum of first n nearly prime numbers ; Store the required sum ; Add this number if it is satisfies the condition ; Increment count of nearly prime numbers ; Function to check if N can be represented as sum of K different positive integers out of which at least K - 1 of them are nearly prime ; Store the sum of first K - 1 nearly prime numbers ; If sum is greater than or equal to n ; Otherwise , prvar Yes ; Driver Code"
How can the above be solved in JS?

function countPrimeFactors ( n ) { var count = 0 ; while ( n % 2 == 0 ) { n = parseInt ( n / 2 ) ; count ++ ; } for ( i = 3 ; i <= parseInt ( Math . sqrt ( n ) ) ; i = i + 2 ) { while ( n % i == 0 ) { n = parseInt ( n / i ) ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } function findSum ( n ) { var sum = 0 ; for ( i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == 2 ) { sum += num ; i ++ ; } } return sum ; } function check ( n , k ) { var s = findSum ( k - 1 ) ; if ( s >= n ) document . write ( " " ) ; else document . write ( " " ) ; } var n = 100 , k = 6 ; check ( n , k ) ;


"Check if a number is an Unusual Number or not | Utility function to find largest prime factor of a number ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; n >>= 1 ; equivalent to n /= 2 ; n must be odd at this point , thus skip the even numbers and iterate only for odd integers ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check Unusual number ; Get the largest Prime Factor of the number ; Check if largest prime factor is greater than sqrt ( n ) ; Driver Code"
How can the above be solved in JS?

function largestPrimeFactor ( n ) { var max = - 1 ; while ( n % 2 == 0 ) { max = 2 ; } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { max = i ; n = n / i ; } } if ( n > 2 ) max = n ; return max ; } function checkUnusual ( n ) { var factor = largestPrimeFactor ( n ) ; if ( factor > Math . sqrt ( n ) ) { return true ; } else { return false ; } } var n = 14 ; if ( checkUnusual ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a number is divisible by 17 using bitwise operators | function to check recursively if the number is divisible by 17 or not ; if n = 0 or n = 17 then yes ; if n is less then 17 , not divisible by 17 ; reducing the number by floor ( n / 16 ) - n % 16 ; driver code to check the above function"
How can the above be solved in JS?

function isDivisibleby17 ( n ) { if ( n == 0 n == 17 ) return true ; if ( n < 17 ) return false ; return isDivisibleby17 ( Math . floor ( n >> 4 ) - Math . floor ( n & 15 ) ) ; } let n = 35 ; if ( isDivisibleby17 ( n ) ) document . write ( n + " " ) ; else document . write ( n + " " ) ;


"Check if a number is divisible by 8 using bitwise operators | function to check number is div by 8 or not using bitwise operator ; Driver code"
How can the above be solved in JS?

function Div_by_8 ( n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } var n = 16 ; if ( Div_by_8 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a number is prime in Flipped Upside Down , Mirror Flipped and Mirror Flipped Upside Down | Function to check if N contains digits 0 , 1 , 2 , 5 , 8 only ; Extract digits of N ; Return false if any of these digits are present ; Function to check if N is prime or not ; Check for all factors ; Function to check if n is prime in all the desired forms ; Driver Code"
How can the above be solved in JS?

function checkDigits ( n ) { do { var r = n % 10 ; if ( r == 3 r == 4 r == 6 r == 7 r == 9 ) return false ; n = parseInt ( n / 10 ) ; } while ( n != 0 ) ; return true ; } function isPrime ( n ) { if ( n <= 1 ) return false ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function isAllPrime ( n ) { return isPrime ( n ) && checkDigits ( n ) ; } var N = 101 ; if ( isAllPrime ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a point lies inside a rectangle | Set | function to find if given point lies inside a given rectangle or not . ; bottom - left and top - right corners of rectangle ; given point ; function call"
How can the above be solved in JS?

function FindPoint ( x1 , y1 , x2 , y2 , x , y ) { if ( x > x1 && x < x2 && y > y1 && y < y2 ) return true ; return false ; } let x1 = 0 , y1 = 0 , x2 = 10 , y2 = 8 ; let x = 1 , y = 5 ; if ( FindPoint ( x1 , y1 , x2 , y2 , x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a sequence of path visits any coordinate twice or not | Function to check if the man crosses previous visited coordinate or not ; Stores the count of crossed vertex ; Stores ( x , y ) coordinates ; The coordinates for the origin ; Iterate over the String ; Condition to increment X or Y co - ordinates respectively ; Check if ( x , y ) is already visited ; Print the result ; Given String ; Function call"
How can the above be solved in JS?

function isCrossed ( path ) { if ( path . length == 0 ) return ; let ans = false ; let mySet = new Set ( ) ; let x = 0 , y = 0 ; mySet . add ( [ x , y ] ) ; for ( let i = 0 ; i < path . length ; i ++ ) { if ( path [ i ] == ' ' ) mySet . add ( [ x , y ++ ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x , y -- ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x ++ , y ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x -- , y ] ) ; if ( ! mySet . has ( [ x , y ] ) ) { ans = true ; break ; } } if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; } let path = " " ; isCrossed ( path ) ;


"Check if a string can be converted to another by swapping of adjacent characters of given type | Function to check if it is possible to transform start to end ; Check the sequence of A , B in both Strings str1 and str2 ; If both the Strings are not equal ; Traverse the Strings ; Check for indexes of A and B ; Driver Code ; Function call"
How can the above be solved in JS?

function canTransform ( str1 , str2 ) { var s1 = " " ; var s2 = " " ; for ( const c of str1 ) { if ( c !== " " ) { s1 += c ; } } for ( const c of str2 ) { if ( c !== " " ) { s2 += c ; } } if ( s1 !== s2 ) return false ; var i = 0 ; var j = 0 ; var n = str1 . length ; while ( i < n && j < n ) { if ( str1 [ i ] === " " ) { i ++ ; } else if ( str2 [ j ] === " " ) { j ++ ; } else { if ( ( str1 [ i ] === " " && i < j ) || ( str1 [ i ] === " " && i > j ) ) { return false ; } i ++ ; j ++ ; } } return true ; } var str1 = " " ; var str2 = " " ; if ( canTransform ( str1 . split ( " " ) , str2 . split ( " " ) ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a string can be formed from another string using given constraints | Function to check if S2 can be formed of S1 ; Length of Strings ; hash - table to store count ; Store count of each character ; Traverse and check for every character ; If the character of s2 is present in s1 ; If the character of s2 is not present in S1 , then check if previous two ASCII characters are present in S1 ; Driver Code ; Calling function to check"
How can the above be solved in JS?

function check ( S1 , S2 ) { var n1 = S1 . length ; var n2 = S2 . length ; var mp = { } ; for ( var i = 0 ; i < n1 ; i ++ ) { if ( mp . hasOwnProperty ( S1 [ i ] ) ) { mp [ S1 [ i ] ] = mp [ S1 [ i ] ] + 1 ; } else { mp [ S1 [ i ] ] = 1 ; } } for ( var i = 0 ; i < n2 ; i ++ ) { if ( mp . hasOwnProperty ( S2 [ i ] ) ) { mp [ S2 [ i ] ] = mp [ S2 [ i ] ] - 1 ; } else if ( mp . hasOwnProperty ( String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ) && mp . hasOwnProperty ( String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ) ) { mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ] = mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ] - 1 ; mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ] = mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ] - 1 ; } else { return false ; } } return true ; } var S1 = " " ; var S2 = " " ; if ( check ( S1 , S2 ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a string can be repeated to make another string | Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times ; Driver code"
How can the above be solved in JS?

function getCount ( a , b ) { if ( b . length % a . length != 0 ) return - 1 ; var count = parseInt ( b . length / a . length ) ; var str = " " ; for ( i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str == ( b ) ) return count ; return - 1 ; } var a = " " ; var b = " " ; document . write ( getCount ( a , b ) ) ;


"Check if a string can be split into even length palindromic substrings | Function to check string str can be split a string into even length palindromic substrings ; Initialize a stack ; Iterate the string ; If the i - th character is same as that at the top of the stack then pop the top element ; Else push the current charactor into the stack ; If the stack is empty , then even palindromic substrings are possible ; Else not - possible ; Given string ; Function Call"
How can the above be solved in JS?

function check ( s , n ) { var st = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( st . length != 0 && st [ st . length - 1 ] == s [ i ] ) st . pop ( ) ; else st . push ( s [ i ] ) ; } if ( st . length == 0 ) { return true ; } else { return false ; } } var str = " " ; var n = str . length ; if ( check ( str , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a string can be split into two strings with same number of K | Function to print the arrangement of characters ; Stores frequency of characters ; Count the character having frequency K ; Count the character having frequency greater than K and not equal to 2 K ; Case 1 ; Case 2 ; Case 3 ; If all cases fail ; Driver code"
How can the above be solved in JS?

function DivideString ( s , n , k ) { var i , c = 0 , no = 1 ; var c1 = 0 , c2 = 0 ; var fr = new Array ( 26 ) . fill ( 0 ) ; var ans = [ ] ; for ( i = 0 ; i < n ; i ++ ) { fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var ch = " " , ch1 = " " ; for ( i = 0 ; i < 26 ; i ++ ) { if ( fr [ i ] === k ) { c ++ ; } if ( fr [ i ] > k && fr [ i ] !== 2 * k ) { c1 ++ ; ch = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } if ( fr [ i ] === 2 * k ) { c2 ++ ; ch1 = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } } for ( i = 0 ; i < n ; i ++ ) ans . push ( " " ) ; var mp = { } ; if ( c % 2 === 0 c1 > 0 c2 > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k ) { if ( mp . hasOwnProperty ( s [ i ] ) ) { ans [ i ] = " " ; } else { if ( no <= parseInt ( c / 2 ) ) { ans [ i ] = " " ; no ++ ; mp [ s [ i ] ] = 1 ; } } } } if ( c % 2 === 1 && c1 > 0 ) { no = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch && no <= k ) { ans [ i ] = " " ; no ++ ; } } } if ( c % 2 === 1 && c1 === 0 ) { no = 1 ; var flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch1 && no <= k ) { ans [ i ] = " " ; no ++ ; } if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k && flag === 0 && ans [ i ] === " " ) { ans [ i ] = " " ; flag = 1 ; } } } document . write ( ans . join ( " " ) ) ; } else { document . write ( " " ) ; } } var S = " " ; var N = S . length ; var K = 1 ; DivideString ( S , N , K ) ;


"Check if a string can be transformed to another by sorting substrings | Function to check if str1 can be transformed to t by sorting substrings ; Occur [ i ] stores the indices of char ( ' a ' + i ) in string s ; idx [ i ] stores the next available index of char ( ' a ' + i ) in occur [ i ] ; If this char is not available anymore ; Conversion not possible ; If one of the smaller characters is available and occurs before ; Conversion not possible ; Print the answer ; Driver Code"
How can the above be solved in JS?

function canTransform ( s , t ) { var n = s . length ; var occur = Array . from ( Array ( 26 ) , ( ) => new Array ( ) ) ; for ( var x = 0 ; x < n ; x ++ ) { var ch = s [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; occur [ ch ] . push ( x ) ; } var idx = Array ( 26 ) . fill ( 0 ) ; var poss = true ; for ( var x = 0 ; x < n ; x ++ ) { var ch = t [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( idx [ ch ] >= occur [ ch ] . length ) { poss = false ; break ; } for ( var small = 0 ; small < ch ; small ++ ) { if ( idx [ small ] < occur [ small ] . length && occur [ small ] [ idx [ small ] ] < occur [ ch ] [ idx [ ch ] ] ) { poss = false ; break ; } } idx [ ch ] ++ ; } if ( poss ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var s , t ; s = " " ; t = " " ; canTransform ( s , t ) ;


"Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | Function that checks if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Driver Code ; function call"
How can the above be solved in JS?

function check ( s , m ) { let l = s . length ; let c1 = 0 ; let c2 = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( s [ i ] == ' ' ) { c2 = 0 ; c1 ++ ; } else { c1 = 0 ; c2 ++ ; } if ( c1 == m c2 == m ) return true ; } return false ; } let s = " " ; let m = 2 ; if ( check ( s , m ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if all substrings of length K of a Binary String has equal count of 0 s and 1 s | Function to check if the substring of length K has equal 0 and 1 ; Traverse the String ; Check if every K - th character is the same or not ; Traverse subString of length K ; If current character is 0 ; Increment count ; Otherwise ; Decrement count ; Check for equal 0 s and 1 s ; Driver Code"
How can the above be solved in JS?

function check ( s , k ) { let n = s . length ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = i ; j < n ; j += k ) { if ( s [ i ] != s [ j ] ) return false ; } } let c = 0 ; for ( let i = 0 ; i < k ; i ++ ) { if ( s [ i ] == ' ' ) c ++ ; else c -- ; } if ( c == 0 ) return true ; else return false ; } let s = " " ; let k = 2 ; if ( check ( s , k ) ) document . write ( " " + " " ) ; else document . write ( " " ) ;


"Check if an array can be sorted by swapping pairs from indices consisting of unequal elements in another array | Function to check if array , A can be converted into sorted array by swapping ( A [ i ] , A [ j ] ) if B [ i ] not equal to B [ j ] ; Stores if array A is sorted in descending order or not ; Traverse the array A ; If A [ i ] is greater than A [ i + 1 ] ; Update flag ; If array is sorted in ascending order ; count = 2 : Check if 0 s and 1 s both present in the B ; Traverse the array ; If current element is 0 ; Update count ; Traverse the array B ; If current element is 1 ; If both 0 s and 1 s are present in the array ; Input array A ; Input array B ; Function call ; If true , print YES ; Else print NO"
How can the above be solved in JS?

function checkifSorted ( A , B , N ) { var flag = false ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = true ; break ; } } if ( ! flag ) { return true ; } var count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( B [ i ] == 0 ) { count ++ ; break ; } } for ( i = 0 ; i < N ; i ++ ) { if ( B [ i ] == 1 ) { count ++ ; break ; } } if ( count == 2 ) { return true ; } return false ; } var A = [ 3 , 1 , 2 ] ; var B = [ 0 , 1 , 1 ] ; var N = A . length ; var check = checkifSorted ( A , B , N ) ; if ( check ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if an array can be split into 3 subsequences of equal sum or not | JavaScript program for the above approach ; Function to check array can be partition into sum of 3 equal ; Base Case ; If value at particular index is not - 1 then return value at that index which ensure no more further calls ; When element at index j is added to sm1 ; When element at index j is added to sm2 ; When element at index j is added to sm3 ; Update the current state and return that value ; Function to check array can be partition to 3 subsequences of equal sum or not ; Initialise 3 sums to 0 ; Function Call ; Given array arr [ ] ; Function Call"
How can the above be solved in JS?

var dp = new Map ( ) ; function checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) { var s = ( sm1 . toString ( ) ) + " " + ( sm2 . toString ( ) ) + ( j . toString ( ) ) ; if ( j == N ) { if ( sm1 == sm2 && sm2 == sm3 ) return 1 ; else return 0 ; } if ( dp . has ( s ) ) return dp [ s ] ; else { var l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 ) ; var m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 ) ; var r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 ) ; return dp [ s ] = Math . max ( Math . max ( l , m ) , r ) ; } } function checkEqualSum ( arr , N ) { var sum1 , sum2 , sum3 ; sum1 = sum2 = sum3 = 0 ; if ( checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ] ; var N = arr . length ; checkEqualSum ( arr , N ) ;


"Check if an array can be split into 3 subsequences of equal sum or not | Utility function to check array can be partition to 3 subsequences of equal sum or not ; Base Case ; When element at index j is added to sm1 ; When element at index j is added to sm2 ; When element at index j is added to sm3 ; Return maximum value among all above 3 recursive call ; Function to check array can be partition to 3 subsequences of equal sum or not ; Initialise 3 sums to 0 ; Function Call ; Given array arr [ ] ; Function Call"
How can the above be solved in JS?

function checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 , j ) { if ( j == N ) { if ( sm1 == sm2 && sm2 == sm3 ) return 1 ; else return 0 ; } else { let l = checkEqualSumUtil ( arr , N , sm1 + arr [ j ] , sm2 , sm3 , j + 1 ) ; let m = checkEqualSumUtil ( arr , N , sm1 , sm2 + arr [ j ] , sm3 , j + 1 ) ; let r = checkEqualSumUtil ( arr , N , sm1 , sm2 , sm3 + arr [ j ] , j + 1 ) ; return Math . max ( Math . max ( l , m ) , r ) ; } } function checkEqualSum ( arr , N ) { let sum1 , sum2 , sum3 ; sum1 = sum2 = sum3 = 0 ; if ( checkEqualSumUtil ( arr , N , sum1 , sum2 , sum3 , 0 ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ 17 , 34 , 59 , 23 , 17 , 67 , 57 , 2 , 18 , 59 , 1 ] ; let N = arr . length ; checkEqualSum ( arr , N ) ;


"Check if an array is Wave Array | Function to check if array is wave array arr : input array n : size of array ; Check the wave form * If arr [ 1 ] is greater than left and right * Same pattern will be followed by whole * elements , else reverse pattern * will be followed by array elements ; Check for last element ; Check for last element ; Array"
How can the above be solved in JS?

function isWaveArray ( arr , n ) { let result = true ; if ( arr [ 1 ] > arr [ 0 ] && arr [ 1 ] > arr [ 2 ] ) { for ( let i = 1 ; i < n - 1 ; i += 2 ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { result = true ; } else { result = false ; break ; } } if ( result == true && n % 2 == 0 ) { if ( arr [ n - 1 ] <= arr [ n - 2 ] ) { result = false ; } } } else if ( arr [ 1 ] < arr [ 0 ] && arr [ 1 ] < arr [ 2 ] ) { for ( let i = 1 ; i < n - 1 ; i += 2 ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { result = true ; } else { result = false ; break ; } } if ( result == true && n % 2 == 0 ) { if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { result = false ; } } } return result ; } let arr = [ 1 , 3 , 2 , 4 ] ; let n = arr . length ; if ( isWaveArray ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if any K ranges overlap at any point | Function that returns true if any k segments overlap at any point ; Vector to store the starting point and the ending point ; Starting points are marked by - 1 and ending points by + 1 ; Sort the vector by first element ; Stack to store the overlaps ; Get the current element ; If it is the starting point ; Push it in the stack ; It is the ending point ; Pop an element from stack ; If more than k ranges overlap ; Driver code"
How can the above be solved in JS?

function kOverlap ( pairs , k ) { var vec = [ ] ; for ( var i = 0 ; i < pairs . length ; i ++ ) { vec . push ( [ pairs [ i ] [ 0 ] , - 1 ] ) ; vec . push ( [ pairs [ i ] [ 1 ] , + 1 ] ) ; } vec . sort ( ( a , b ) => { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] - b [ 0 ] return a [ 1 ] - b [ 1 ] } ) ; var st = [ ] ; for ( var i = 0 ; i < vec . length ; i ++ ) { var cur = vec [ i ] ; if ( cur [ 1 ] == - 1 ) { st . push ( cur ) ; } else { st . pop ( ) ; } if ( st . length >= k ) { return true ; } } return false ; } var pairs = [ ] ; pairs . push ( [ 1 , 3 ] ) ; pairs . push ( [ 2 , 4 ] ) ; pairs . push ( [ 3 , 5 ] ) ; pairs . push ( [ 7 , 10 ] ) ; var n = pairs . length , k = 3 ; if ( kOverlap ( pairs , k ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if any anagram of a string is palindrome or not | Javascript program to Check if any anagram of a string is palindrome or not ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver program to test to print printDups"
How can the above be solved in JS?

let NO_OF_CHARS = 256 ; function canFormPalindrome ( str ) { let count = new Array ( NO_OF_CHARS ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < str . length ; i ++ ) count [ str [ i ] . charCodeAt ( ) ] ++ ; let odd = 0 ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) != 0 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } document . write ( canFormPalindrome ( " " ) ? " " + " " : " " + " " ) ; document . write ( canFormPalindrome ( " " ) ? " " : " " ) ;


"Check if array contains contiguous integers with duplicates allowed | Function to check whether the array contains a set of contiguous integers ; Storing elements of ' arr [ ] ' in a hash table ' us ' ; as arr [ 0 ] is present in ' us ' ; starting with previous smaller element of arr [ 0 ] ; if ' curr _ ele ' is present in ' us ' ; increment count ; update 'curr_ele" ; starting with next greater element of arr [ 0 ] ; if ' curr _ ele ' is present in ' us ' ; increment count ; update 'curr_ele" ; returns true if array contains a set of contiguous integers else returns false ; Driver program to test above"
How can the above be solved in JS?

function areElementsContiguous ( arr , n ) { var us = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) us . add ( arr [ i ] ) ; var count = 1 ; var curr_ele = arr [ 0 ] - 1 ; while ( us . has ( curr_ele ) ) { count ++ ; curr_ele -- ; } curr_ele = arr [ 0 ] + 1 ; while ( us . has ( curr_ele ) ) { count ++ ; curr_ele ++ ; } return ( count == ( us . size ) ) ; } var arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ] ; var n = arr . length ; if ( areElementsContiguous ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if array contains contiguous integers with duplicates allowed | function to check whether the array contains a set of contiguous integers ; Find maximum and minimum elements . ; There should be at least m elements in aaray to make them contiguous . ; Create a visited array and initialize false . ; Mark elements as true . ; If any element is not marked , all elements are not contiguous . ; Driver program"
How can the above be solved in JS?

function areElementsContiguous ( arr , n ) { let max = Number . MIN_VALUE ; let min = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { max = Math . max ( max , arr [ i ] ) ; min = Math . min ( min , arr [ i ] ) ; } let m = max - min + 1 ; if ( m > n ) return false ; let visited = new Array ( n ) ; visited . fill ( false ) ; for ( let i = 0 ; i < n ; i ++ ) visited [ arr [ i ] - min ] = true ; for ( let i = 0 ; i < m ; i ++ ) if ( visited [ i ] == false ) return false ; return true ; } let arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ] ; let n = arr . length ; if ( areElementsContiguous ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if array contains contiguous integers with duplicates allowed | function to check whether the array contains a set of contiguous integers ; Sort the array ; After sorting , check if current element is either same as previous or is one more . ; Driver program"
How can the above be solved in JS?

function areElementsContiguous ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 1 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] > 1 ) return false ; return true ; } let arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ] ; let n = arr . length ; if ( areElementsContiguous ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if at least half array is reducible to zero by performing some operations | Function to print the desired result after computation ; Driver Code"
How can the above be solved in JS?

function isHalfReducible ( arr , n , m ) { var frequencyHash = Array ( m + 1 ) . fill ( 0 ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { frequencyHash [ arr [ i ] % ( m + 1 ) ] ++ ; } for ( i = 0 ; i <= m ; i ++ ) { if ( frequencyHash [ i ] >= n / 2 ) break ; } if ( i <= m ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 8 , 16 , 32 , 3 , 12 ] ; var n = arr . length ; var m = 7 ; isHalfReducible ( arr , n , m ) ;


"Check if characters of a given string can be used to form any N equal strings | Function to check if the freq of any character is divisible by N ; Stores the frequency of characters ; If frequency of a character is not divisible by n ; If no character has frequency at least N ; Driver Code ; Function Call"
How can the above be solved in JS?

function isSame ( str , n ) { var mp = { } ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( mp . hasOwnProperty ( str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) { mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] + 1 ; } else { mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = 1 ; } } for ( const [ key , value ] of Object . entries ( mp ) ) { if ( value >= n ) { return true ; } } return false ; } var str = " " ; var n = 4 ; if ( isSame ( str , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if end of given Binary string can be reached by choosing jump value in between given range | Function to check if it is possible to reach the end of the binary string using the given jumps ; Stores the DP states ; Stores count of indices from which it is possible to reach index i ; Traverse the given string ; Update the values of pre accordingly ; If the jump size is out of the range [ L , R ] ; Return answer ; Driver Code"
How can the above be solved in JS?

const canReach = ( s , L , R ) => { let dp = new Array ( s . length ) . fill ( 1 ) ; let pre = 0 ; for ( let i = 1 ; i < s . length ; i ++ ) { if ( i >= L ) { pre += dp [ i - L ] ; } if ( i > R ) { pre -= dp [ i - R - 1 ] ; } dp [ i ] = ( pre > 0 ) && ( s [ i ] == ' ' ) ; } return dp [ s . length - 1 ] ; } let S = " " ; let L = 2 , R = 3 ; if ( canReach ( S , L , R ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if frequency of character in one string is a factor or multiple of frequency of same character in other string | Function that checks if the frequency of character are a factor or multiple of each other ; map store frequency of each character ; if any frequency is 0 , then continue as condition is satisfied ; if factor or multiple , then condition satisfied ; if condition not satisfied ; Driver code"
How can the above be solved in JS?

function multipleOrFactor ( s1 , s2 ) { let m1 = new Map ( ) ; let m2 = new Map ( ) ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( m1 [ s1 [ i ] ] ) m1 [ s1 [ i ] ] ++ ; else m1 [ s1 [ i ] ] = 1 } for ( let i = 0 ; i < s2 . length ; i ++ ) { if ( m2 [ s2 [ i ] ] ) m2 [ s2 [ i ] ] ++ ; else m2 [ s2 [ i ] ] = 1 } for ( var it in m1 ) { if ( ! ( m2 [ it ] ) ) continue ; if ( m2 [ it ] % m1 [ it ] == 0 m1 [ it ] % m2 [ it ] == 0 ) continue ; else return false ; } return true ; } let s1 = " " ; let s2 = " " ; multipleOrFactor ( s1 , s2 ) ? document . write ( " " ) : document . write ( " " ) ;


"Check if given Sudoku solution is valid or not | JavaScript program to implement the above approach ; Function to check if all elements of the board [ ] [ ] array store value in the range [ 1 , 9 ] ; Traverse board [ ] [ ] array ; Check if board [ i ] [ j ] lies in the range ; Function to check if the solution of sudoku puzzle is valid or not ; Check if all elements of board [ ] [ ] stores value in the range [ 1 , 9 ] ; Stores unique value from 1 to N ; Traverse each row of the given array ; Initialize unique [ ] array to false ; Traverse each column of current row ; Stores the value of board [ i ] [ j ] ; Check if current row stores duplicate value ; Traverse each column of the given array ; Traverse each row of current column ; Stores the value of board [ j ] [ i ] ; Check if current column stores duplicate value ; Traverse each block of size 3 * 3 in board [ ] [ ] array ; j stores first column of each 3 * 3 block ; Initialize unique [ ] array to false ; Traverse current block ; Stores row number of current block ; Stores column number of current block ; Stores the value of board [ X ] [ Y ] ; Check if current block stores duplicate value ; If all conditions satisfied ; Driver Code"
How can the above be solved in JS?

var N = 9 ; function isinRange ( board ) { for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( board [ i ] [ j ] <= 0 board [ i ] [ j ] > 9 ) { return false ; } } } return true ; } function isValidSudoku ( board ) { if ( isinRange ( board ) == false ) { return false ; } var unique = Array ( N + 1 ) . fill ( false ) ; for ( var i = 0 ; i < N ; i ++ ) { unique = Array ( N + 1 ) . fill ( false ) ; unique = Array ( N + 1 ) . fill ( false ) ; for ( var j = 0 ; j < N ; j ++ ) { var Z = board [ i ] [ j ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { var Z = board [ j ] [ i ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( var i = 0 ; i < N - 2 ; i += 3 ) { for ( var j = 0 ; j < N - 2 ; j += 3 ) { unique = Array ( N + 1 ) . fill ( false ) ; for ( var k = 0 ; k < 3 ; k ++ ) { for ( var l = 0 ; l < 3 ; l ++ ) { var X = i + k ; var Y = j + l ; var Z = board [ X ] [ Y ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } } } return true ; } var board = [ [ 7 , 9 , 2 , 1 , 5 , 4 , 3 , 8 , 6 ] , [ 6 , 4 , 3 , 8 , 2 , 7 , 1 , 5 , 9 ] , [ 8 , 5 , 1 , 3 , 9 , 6 , 7 , 2 , 4 ] , [ 2 , 6 , 5 , 9 , 7 , 3 , 8 , 4 , 1 ] , [ 4 , 8 , 9 , 5 , 6 , 1 , 2 , 7 , 3 ] , [ 3 , 1 , 7 , 4 , 8 , 2 , 9 , 6 , 5 ] , [ 1 , 3 , 6 , 7 , 4 , 8 , 5 , 9 , 2 ] , [ 9 , 7 , 4 , 2 , 1 , 5 , 6 , 3 , 8 ] , [ 5 , 2 , 8 , 6 , 3 , 9 , 4 , 1 , 7 ] ] ; if ( isValidSudoku ( board ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if given four integers ( or sides ) make rectangle | Function to check if the given integers value make a rectangle ; Square is also a rectangle ; Driver code"
How can the above be solved in JS?

function isRectangle ( a , b , c , d ) { if ( a == b && a == c && a == d && c == d && b == c && b == d ) return true ; else if ( a == b && c == d ) return true ; else if ( a == d && c == b ) return true ; else if ( a == c && d == b ) return true ; else return false ; } let a = 1 , b = 2 , c = 3 , d = 4 ; if ( isRectangle ( a , b , c , d ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if given number is Emirp Number or not | Returns true if n is prime . Else false . ; Corner case ; Check from 2 to n - 1 ; Function will check whether number is Emirp or not ; Check if n is prime ; Find reverse of n ; If both Original and Reverse are Prime , then it is an Emirp number ; Input number"
How can the above be solved in JS?

function isPrime ( n ) { if ( n <= 1 ) return false ; for ( i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function isEmirp ( n ) { if ( isPrime ( n ) == false ) return false ; var rev = 0 ; while ( n != 0 ) { var d = n % 10 ; rev = rev * 10 + d ; n = parseInt ( n / 10 ) ; } return isPrime ( rev ) ; } var n = 13 ; if ( isEmirp ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if it is possible to make x and y zero at same time with given operation | Function to check if it is possible to make x and y can become 0 at same time ; Check the given conditions ; Driver Code ; Function Call"
How can the above be solved in JS?

function canBeReduced ( x , y ) { var maxi = Math . max ( x , y ) ; var mini = Math . min ( x , y ) ; if ( ( ( x + y ) % 3 ) == 0 && maxi <= 2 * mini ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } var x = 6 , y = 9 ; canBeReduced ( x , y ) ;


"Check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd"
How can the above be solved in JS?

function is_possible ( s ) { let l = s . length ; let one = 0 , zero = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( s [ i ] == ' ' ) zero ++ ; else one ++ ; } if ( l % 2 == 0 ) return ( one == zero ) ; else return ( Math . abs ( one - zero ) == 1 ) ; } let s = " " ; if ( is_possible ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if it is possible to split given Array into K odd | Function to check if array can be split in required K subsets ; Store count of odd numbers ; Check if element is odd ; Check if split is possible ; Driver code"
How can the above be solved in JS?

function checkArray ( n , k , arr ) { var cnt = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) cnt += 1 ; } if ( cnt >= k && cnt % 2 == k % 2 ) return true ; else return false ; } var arr = [ 1 , 3 , 4 , 7 , 5 , 3 , 1 ] ; var n = arr . length ; var k = 4 ; if ( checkArray ( n , k , arr ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if minimum element in array is less than or equals half of every other element | Function to Check if the minimum element in the array is greater than or equal to half of every other element ; Initialise the variables to store smallest and second smallest ; Check if current element is smaller than smallest , the current smallest will become secondSmallest and current element will be the new smallest ; Check if current element is smaller than secondSmallest simply update the latter ; Driver code"
How can the above be solved in JS?

function checkMin ( arr , len ) { var smallest = Number . INFINITY , secondSmallest = Number . INFINITY ; for ( var i = 0 ; i < len ; i ++ ) { if ( arr [ i ] < smallest ) { secondSmallest = smallest ; smallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest ) { secondSmallest = arr [ i ] ; } } if ( 2 * smallest <= secondSmallest ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 2 , 3 , 4 , 5 ] ; var len = 4 ; checkMin ( arr , len ) ;


"Check if number formed by joining two Numbers is Perfect Cube | Function to check if a number is a perfect Cube or not ; Function to check if concatenation of two numbers is a perfect cube or not ; Convert numbers to string using to_string ( ) ; Concatenate the numbers and convert it into integer ; Check if concatenated value is perfect cube or not ; Driver Code"
How can the above be solved in JS?

function isPerfectCube ( x ) { var cr = Math . round ( Math . cbrt ( x ) ) ; return ( cr * cr * cr == x ) ; } function checkCube ( a , b ) { s1 = a . toString ( ) ; s2 = b . toString ( ) ; var c = parseInt ( s1 + s2 ) ; if ( isPerfectCube ( c ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var a = 6 ; var b = 4 ; checkCube ( a , b ) ;


"Check if right triangle possible from given area and hypotenuse | Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1. ; Descriminant of the equation ; applying the linear equation formula to find both the roots ; Driver code"
How can the above be solved in JS?

function findRightAngle ( A , H ) { let D = Math . pow ( H , 4 ) - 16 * A * A ; if ( D >= 0 ) { let root1 = ( H * H + Math . sqrt ( D ) ) / 2 ; let root2 = ( H * H - Math . sqrt ( D ) ) / 2 ; let a = Math . sqrt ( root1 ) ; let b = Math . sqrt ( root2 ) ; if ( b >= a ) document . write ( a + " " + b + " " + H + " " ) ; else document . write ( b + " " + a + " " + H + " " ) ; } else document . write ( " " ) ; } findRightAngle ( 6 , 5 ) ;


"Check if roots of a Quadratic Equation are reciprocal of each other or not | Function to check if the roots of a quadratic equation are reciprocal of each other or not ; Driver code"
How can the above be solved in JS?

function checkSolution ( a , b , c ) { if ( a == c ) document . write ( " " ) ; else document . write ( " " ) ; } let a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ;


"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"
How can the above be solved in JS?

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 ) ) ;


"Check if sum Y can be obtained from the Array by the given operations | Function to check if it is possible to obtain sum Y from a sequence of sum X from the array arr [ ] ; Store the difference ; Iterate over the array ; If diff reduced to 0 ; Driver Code"
How can the above be solved in JS?

function solve ( arr , n , X , Y ) { var diff = Y - X ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 1 ) { diff = diff % ( arr [ i ] - 1 ) ; } } if ( diff == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 7 , 9 , 10 ] ; var n = arr . length ; var X = 11 , Y = 13 ; solve ( arr , n , X , Y ) ;


"Check if sum of digits of a number exceeds the product of digits of that number | Function to check if the sum of the digits of N is strictly greater than the product of the digits of N or not ; Stores the sum and the product of the digits of N ; Stores the last digit if N ; Increment the value of sumOfDigits ; Update the prodOfDigit ; Divide N by 10 ; Prlet the result ; Driver Code"
How can the above be solved in JS?

function check ( n ) { let sumOfDigit = 0 ; let prodOfDigit = 1 ; while ( n > 0 ) { let rem ; rem = n % 10 ; sumOfDigit += rem ; prodOfDigit *= rem ; n = Math . floor ( n / 10 ) ; } if ( sumOfDigit > prodOfDigit ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 1234 ; check ( N ) ;


"Check if the Xor of the frequency of all digits of a number N is zero or not | Javascript implementation of the above approach ; creating a frequency array ; Finding the last digit of the number ; Dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; Driver function"
How can the above be solved in JS?

function check ( s ) { let freq = new Array ( 10 ) . fill ( 0 ) , r ; while ( s != 0 ) { r = s % 10 ; s = parseInt ( s / 10 ) ; freq [ r ] += 1 ; } let xor__ = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { xor__ = xor__ ^ freq [ i ] ; if ( xor__ == 0 ) return true ; else return false ; } } let s = 122233 ; if ( check ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver code"
How can the above be solved in JS?

function isPrime ( n ) { var i , c = 0 ; for ( i = 1 ; i < n / 2 ; i ++ ) { if ( n % i == 0 ) c ++ ; } if ( c == 1 ) return 1 ; else return 0 ; } function findMinNum ( arr , n ) { var first = 0 , last = 0 , num , rev , i ; var hash = new Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } document . write ( " " ) ; for ( var i = 0 ; i <= 9 ; i ++ ) { for ( var j = 0 ; j < hash [ i ] ; j ++ ) document . write ( i ) ; } document . write ( " " ) ; for ( i = 0 ; i <= 9 ; i ++ ) { if ( hash [ i ] != 0 ) { first = i ; break ; } } for ( i = 9 ; i >= 0 ; i -- ) { if ( hash [ i ] != 0 ) { last = i ; break ; } } num = first * 10 + last ; rev = last * 10 + first ; document . write ( " " ) ; if ( isPrime ( num ) && isPrime ( rev ) ) document . write ( num + " " + rev ) ; else if ( isPrime ( num ) ) document . write ( num ) ; else if ( isPrime ( rev ) ) document . write ( rev ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 4 , 7 , 8 ] ; findMinNum ( arr , 5 ) ;


"Check if the given number is Ore number or not | Javascript program to check if the given number is Ore number ; Function that returns harmonic mean ; Note that this loop runs till square root ; If divisors are equal , store ' i ' ; else Otherwise store ' i ' and ' n / i ' both ; Utility function to calculate harmonic mean of the divisors ; Declare sum variables and initialize with zero . ; calculate denominator ; Calculate harmonic mean and return ; Function to check if a number is ore number ; Calculate Harmonic mean of divisors of n ; Check if harmonic mean is an integer or not ; Driver Code"
How can the above be solved in JS?

var arr = [ ] ; function generateDivisors ( n ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) arr . push ( i ) ; { arr . push ( i ) ; arr . push ( n / i ) ; } } } } function harmonicMean ( n ) { generateDivisors ( n ) ; var sum = 0.0 ; var len = arr . length ; for ( var i = 0 ; i < len ; i ++ ) sum = sum + ( n / arr [ i ] ) ; sum = ( sum / n ) ; return ( arr . length / sum ) ; } function isOreNumber ( n ) { var mean = harmonicMean ( n ) ; if ( mean - parseInt ( mean ) == 0 ) return true ; else return false ; } var n = 28 ; if ( isOreNumber ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if the given string is shuffled substring of another string | This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver code"
How can the above be solved in JS?

function compare ( arr1 , arr2 ) { for ( let i = 0 ; i < 256 ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } function search ( pat , txt ) { let M = pat . length ; let N = txt . length ; let countP = new Array ( 256 ) ; let countTW = new Array ( 256 ) ; for ( let i = 0 ; i < 256 ; i ++ ) { countP [ i ] = 0 ; countTW [ i ] = 0 ; } for ( let i = 0 ; i < 256 ; i ++ ) { countP [ i ] = 0 ; countTW [ i ] = 0 ; } for ( let i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] . charCodeAt ( 0 ) ] ) ++ ; ( countTW [ txt [ i ] . charCodeAt ( 0 ) ] ) ++ ; } for ( let i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) return true ; ( countTW [ txt [ i ] . charCodeAt ( 0 ) ] ) ++ ; countTW [ txt [ i - M ] . charCodeAt ( 0 ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } let txt = " " ; let pat = " " ; if ( search ( pat , txt ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if the remainder of N | Function to check if a number holds the condition ( N - 1 ) ! % N = N - 1 ; Corner cases ; Number divisible by 2 or 3 are not prime ; Iterate from 5 and keep checking for prime ; Function to check the expression for the value N ; Driver code"
How can the above be solved in JS?

function isPrime ( n ) { if ( n == 1 ) return true ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function checkExpression ( n ) { if ( isPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 3 ; checkExpression ( N ) ;


"Check if two arrays are permutations of each other using Mathematical Operation | Function to check if arrays are permutations of each other . ; Calculating sum and multiply of first array ; Calculating sum and multiply of second array ; If sum and mul of both arrays are equal , return true , else return false . ; Driver code"
How can the above be solved in JS?

function arePermutations ( a , b , n , m ) { let sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; for ( let i = 0 ; i < n ; i ++ ) { sum1 += a [ i ] ; mul1 *= a [ i ] ; } for ( let i = 0 ; i < m ; i ++ ) { sum2 += b [ i ] ; mul2 *= b [ i ] ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } let a = [ 1 , 3 , 2 ] ; let b = [ 3 , 1 , 2 ] ; let n = a . length ; let m = b . length ; if ( arePermutations ( a , b , n , m ) == true ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if two circles intersect such that the third circle passes through their points of intersections and centers | Structure of the circle ; Utility function to check if given circles satisfy required criteria ; Stores the distance between the centres of C1 and C2 ; Stores the status if the given given criteria is satisfied or not ; If C1C2 is less than the sum of the radii of the first 2 circles ; If C3 is the midpoint of the centres at C1 and C2 ; Mark flag true ; Return flag ; Function to check if the given circles satisfy required criteria ; Check for the current combination of circles ; Check for the next combination ; Driver Code"
How can the above be solved in JS?

class circle { constructor ( x , y , r ) { this . x = x ; this . y = y ; this . r = r ; } } function check ( C ) { let C1C2 = Math . sqrt ( ( C [ 1 ] . x - C [ 0 ] . x ) * ( C [ 1 ] . x - C [ 0 ] . x ) + ( C [ 1 ] . y - C [ 0 ] . y ) * ( C [ 1 ] . y - C [ 0 ] . y ) ) ; let flag = false ; if ( C1C2 < ( C [ 0 ] . r + C [ 1 ] . r ) ) { if ( ( C [ 0 ] . x + C [ 1 ] . x ) == 2 * C [ 2 ] . x && ( C [ 0 ] . y + C [ 1 ] . y ) == 2 * C [ 2 ] . y ) { flag = true ; } } return flag ; } function IsFairTriplet ( c ) { let f = false ; f |= check ( c ) ; for ( let i = 0 ; i < 2 ; i ++ ) { swap ( c [ 0 ] , c [ 2 ] ) ; f |= check ( c ) ; } return f ; } function swap ( circle1 , circle2 ) { let temp = circle1 ; circle1 = circle2 ; circle2 = temp ; } let C = new Array ( 3 ) ; C [ 0 ] = new circle ( 0 , 0 , 8 ) ; C [ 1 ] = new circle ( 0 , 10 , 6 ) ; C [ 2 ] = new circle ( 0 , 5 , 5 ) ; if ( IsFairTriplet ( C ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if two items can be selected from two different categories without exceeding price | Function to check if two items can be selected from two different categories without exceeding the total price ; Loop to choose two different pairs using two nested loops ; Condition to check if the price of these two elements is less than S ; Driver code ; Function call"
How can the above be solved in JS?

function check ( S , prices , type , n ) { for ( let j = 0 ; j < n ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) { if ( ( type [ j ] == 0 && type [ k ] == 1 ) || ( type [ j ] == 1 && type [ k ] == 0 ) ) { if ( prices [ j ] + prices [ k ] <= S ) { return " " ; } } } } return " " ; } let prices = [ 3 , 8 , 6 , 5 ] ; let type = [ 0 , 1 , 1 , 0 ] ; let S = 10 ; let n = 4 ; document . write ( check ( S , prices , type , n ) ) ;


"Check if two strings are same ignoring their cases | Function to compare two Strings ignoring their cases ; Convert to lowercase using toUpperCase function ; Comparing both using inbuilt function ; if Strings are equal , return true otherwise false ; Function to print the same or not same if Strings are equal or not equal ; Driver Code"
How can the above be solved in JS?

function equalIgnoreCase ( str1 , str2 ) { let i = 0 ; str1 = str1 . toUpperCase ( ) ; str2 = str2 . toUpperCase ( ) ; let x = str1 == ( str2 ) ; if ( ! x ) { return false ; } else { return true ; } } function equalIgnoreCaseUtil ( str1 , str2 ) { let res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str1 , str2 ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ;


"Check if two strings are same ignoring their cases | Function to compare two strings ignoring their cases ; Convert to lowercase using toLowerCase function ; Comparing both using inbuilt function ; if strings are equal , return true otherwise false ; Function to print the same or not same if strings are equal or not equal ; Driver Code"
How can the above be solved in JS?

function equalIgnoreCase ( str1 , str2 ) { let i = 0 ; str1 = str1 . toLowerCase ( ) ; str2 = str2 . toLowerCase ( ) ; let x = ( str1 == ( str2 ) ) ; return x == true ; } function equalIgnoreCaseUtil ( str1 , str2 ) { let res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) document . write ( " " ) ; else document . write ( " " ) ; } let str1 , str2 ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ;