codeparrot/xlcost-text-to-code huggingface dataset:
Find the array element having equal count of Prime Numbers on its left and right | Function to find the index of the array such that the count of prime numbers to its either ends are same ; Store the maximum value in the array ; Traverse the array arr [ ] ; Stores all the numbers ; Iterate over the range [ 1 , Max ] ; Increment the value of st [ i ] ; Removes 1 from the map St ; Perform Sieve of Prime Numbers ; While i * j is less than the maxValue ; If i * j is in map St ; Erase the value ( i * j ) ; Increment the value of j ; Stores the count of prime from index 0 to i ; Stores the count of prime numbers ; Traverse the array arr [ ] ; If arr [ i ] is present in the map st ; Stores the count of prime from index i to N - 1 ; Stores the count of prime numbers ; Iterate over the range [ 0 , N - 1 ] in reverse order ; If arr [ i ] is in map st ; Iterate over the range [ 0 , N - 1 ] ; If prefix [ i ] is equal to the Suffix [ i ] ; Return - 1 if no such index is present ; Driver Code
function findIndex ( arr , N ) { let maxValue = Number . MIN_VALUE ; ; for ( let i = 0 ; i < N ; i ++ ) { maxValue = Math . max ( maxValue , arr [ i ] ) ; } var St = new Map ( ) ; for ( let i = 1 ; i <= maxValue ; i ++ ) { St . set ( i , 1 ) ; } if ( St . has ( 1 ) ) { St . delete ( 1 ) ; } for ( let i = 2 ; i <= Math . sqrt ( maxValue ) ; i ++ ) { let j = 2 ; while ( ( i * j ) <= maxValue ) { if ( St . has ( i * j ) ) { St . delete ( i * j ) ; } j ++ ; } } let LeftCount = 0 ; let Prefix = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { Prefix [ i ] = LeftCount ; if ( St . has ( arr [ i ] ) ) { LeftCount ++ ; } } let RightCount = 0 ; let Suffix = new Array ( N ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { Suffix [ i ] = RightCount ; if ( St . has ( arr [ i ] ) ) { RightCount ++ ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( Prefix [ i ] == Suffix [ i ] ) { return i ; } } return - 1 ; } let arr = [ 2 , 3 , 4 , 7 , 5 , 10 , 1 , 8 ] ; let N = arr . length ; document . write ( findIndex ( arr , N ) ) ;
Numbers of pairs from an array whose average is also present in the array | Function to count the number of pairs from the array having sum S ; Stores the total count of pairs whose sum is 2 * S ; Generate all possible pairs and check their sums ; If the sum is S , then increment the count ; Return the total count of pairs ; Function to count of pairs having whose average exists in the array ; Initialize the count ; Use set to remove duplicates ; Add elements in the set ; For every sum , count all possible pairs ; Return the total count ; Driver code
function getCountPairs ( arr , N , S ) { let count = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { if ( ( arr [ i ] + arr [ j ] ) == S ) count ++ ; } } return count ; } function countPairs ( arr , N ) { let count = 0 ; let S = [ ] ; for ( let i = 0 ; i < N ; i ++ ) S . push ( arr [ i ] ) ; for ( let ele in S ) { let sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } let arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2 | Function to check if N is power of 2 or not ; Base Case ; Return true if N is power of 2 ; Function to print array of strings in ascending order ; Sort strings in ascending order ; Print the array ; Function to sort the strings after modifying each string according to the given conditions ; Store the frequency of each characters of the string ; Stores the required array of strings ; Traverse the array of strings ; Temporary string ; Stores frequency of each alphabet of the string ; Update frequency of S [ i ] [ j ] ; Traverse the map freq ; Check if the frequency of i . first is a power of 2 ; Update string st ; Clear the map ; Null string ; Sort the string in descending order ; Update res ; Print the array of strings ; Driver Code
function isPowerOfTwo ( n ) { if ( n == 0 ) return false ; return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } function printArray ( res ) { res . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } } function sortedStrings ( s , N ) { let freq = new Map ( ) ; let res = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let st = " " ; for ( let j = 0 ; j < s [ i ] . length ; j ++ ) { if ( freq . has ( s [ i ] [ j ] ) ) { freq . set ( s [ i ] [ j ] , freq . get ( s [ i ] [ j ] ) + 1 ) } else { freq . set ( s [ i ] [ j ] , 1 ) } } for ( let ch of freq . keys ( ) ) { if ( isPowerOfTwo ( freq . get ( ch ) ) ) { for ( let j = 0 ; j < freq . get ( ch ) ; j ++ ) { st += ch ; } } } freq . clear ( ) ; if ( st . length == 0 ) continue ; let myCharArr = st . split ( " " ) ; myCharArr . sort ( ) ; let ns = " " ; for ( let j = myCharArr . length - 1 ; j >= 0 ; -- j ) ns += myCharArr [ j ] ; res . push ( ns ) ; } printArray ( res ) ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; sortedStrings ( arr , N ) ;
Modify string by replacing all occurrences of given characters by specified replacing characters | Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver code
function replaceCharacters ( s , p ) { var n = s . length , k = p . length ; var arr = new Array ( 26 ) . fill ( 0 ) ; var brr = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s [ i ] ; brr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s [ i ] ; } for ( var j = 0 ; j < k ; j ++ ) { var a = p [ j ] [ 0 ] , b = p [ j ] [ 1 ] ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] === a ) { brr [ i ] = b ; } } } for ( var i = 0 ; i < n ; i ++ ) { document . write ( brr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ) ; } } var S = " " ; var P = [ [ " " , " " ] , [ " " , " " ] , [ " " , " " ] , ] ; replaceCharacters ( S , P ) ;
Smallest Semi | JavaScript program to implement the above approach ; Function to find all the prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the smallest semi - prime number having a difference between any of its two divisors at least N ; Stores the prime numbers ; Fill the prime array ; Initialize the first divisor ; Find the value of the first prime number ; Initialize the second divisor ; Find the second prime number ; Print the semi - prime number ; Driver code
let MAX = 100001 ; function SieveOfEratosthenes ( prime ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } function smallestSemiPrime ( n ) { let prime = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime ) ; let num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } let num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } document . write ( num1 * num2 ) ; } let N = 2 ; smallestSemiPrime ( N ) ;
Check if a number can be represented as product of two positive perfect cubes | Function to check if N can be represented as the product of two perfect cubes or not ; Stores the perfect cubes ; Traverse the Map ; Stores the first number ; Stores the second number ; Search the pair for the first number to obtain product N from the Map ; If N cannot be represented as the product of the two positive perfect cubes ; Driver code
function productOfTwoPerfectCubes ( N ) { let cubes = new Map ( ) ; for ( let i = 1 ; i * i * i <= N ; i ++ ) cubes . set ( i * i * i , i ) ; for ( let [ key , value ] of cubes . entries ( ) ) { let firstNumber = key ; if ( N % key == 0 ) { let secondNumber = N / key ; if ( cubes . has ( secondNumber ) ) { document . write ( " " ) ; return ; } } } document . write ( " " ) ; } let N = 216 ; productOfTwoPerfectCubes ( N ) ;
Check if a number can be represented as product of two positive perfect cubes | Function to check if the number N can be represented as the product of two perfect cubes or not ; If cube of cube_root is N ; Otherwise , print No ; Driver code
function productOfTwoPerfectCubes ( N ) { var cube_root ; cube_root = Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { document . write ( " " ) ; return ; } else { document . write ( " " ) ; return ; } } var N = 216 ; productOfTwoPerfectCubes ( N ) ;
Size of smallest square that contains N non | Function to check if side of square X can pack all the N rectangles or not ; Find the number of rectangle it can pack ; If val is atleast N , then return true ; Otherwise , return false ; Function to find the size of the smallest square that can contain N rectangles of dimensions W * H ; Stores the lower bound ; Stores the upper bound ; Iterate until i is less than j ; Calculate the mid value ; If the current size of square cam contain N rectangles ; Otherwise , update i ; Return the minimum size of the square required ; Driver code ; Function Call
function bound ( w , h , N , x ) { let val = parseInt ( x / w ) * parseInt ( x / h ) ; if ( val >= N ) return true ; else return false ; } function FindSquare ( N , W , H ) { let i = 1 ; let j = W * H * N ; while ( i < j ) { let mid = i + parseInt ( ( j - i ) / 2 ) ; if ( bound ( W , H , N , mid ) ) j = mid ; else i = mid + 1 ; } return j ; } let W = 2 ; let H = 3 ; let N = 10 ; document . write ( FindSquare ( N , W , H ) ) ;
Count integers up to N that are equal to at least 2 nd power of any integer exceeding 1 | Function to count the integers up to N that can be represented as a ^ b , where a & b > 1 ; Initialize a HashSet ; Iterating over the range [ 2 , sqrt ( N ) ] ; Generate all possible power of x ; Multiply x by i ; If the generated number lies in the range [ 1 , N ] then insert it in HashSet ; Print the total count ; Driver Code
function printNumberOfPairs ( N ) { var st = new Set ( ) ; for ( let i = 2 ; i * i <= N ; i ++ ) { let x = i ; while ( x <= N ) { x *= i ; if ( x <= N ) { st . add ( x ) ; } } } document . write ( st . size ) ; } let N = 10000 ; printNumberOfPairs ( N ) ;
Maximize product of lengths of strings having no common characters | Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver Code
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function maximumProduct ( words ) { let bits = new Array ( words . length ) . fill ( 0 ) ; for ( let i = 0 ; i < words . length ; i ++ ) { for ( let j = 0 ; j < words [ i ] . length ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } } let result = 0 ; for ( let i = 0 ; i < bits . length ; i ++ ) { for ( let j = i + 1 ; j < bits . length ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { let L = countSetBits ( bits [ i ] ) ; let R = countSetBits ( bits [ j ] ) ; result = Math . max ( L * R , result ) ; } } } document . write ( result ) ; } let arr = [ " " , " " , " " , " " , " " , " " ] ; maximumProduct ( arr ) ;
Modify a sentence by reversing order of occurrences of all Palindrome Words | Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code
function palindrome ( str ) { var st = 0 ; var ed = str . length - 1 ; while ( st < ed ) { if ( str [ st ] == str [ ed ] ) { st ++ ; ed -- ; } else return false ; } return true ; } function printReverse ( sentence ) { var newlist = [ ] ; var lis = [ ] ; var temp = " " ; for ( var i = 0 ; i < sentence . length ; i ++ ) { if ( sentence [ i ] == ' ' ) { lis . push ( temp ) ; temp = " " ; } else temp += sentence [ i ] ; } lis . push ( temp ) ; for ( var i = 0 ; i < lis . length ; i ++ ) { if ( palindrome ( lis [ i ] ) ) newlist . push ( lis [ i ] ) ; } newlist . reverse ( ) ; var j = 0 ; for ( var i = 0 ; i < lis . length ; i ++ ) { if ( palindrome ( lis [ i ] ) ) { lis [ i ] = newlist [ j ] ; j = j + 1 ; } } for ( var i = 0 ; i < lis . length ; i ++ ) { document . write ( lis [ i ] + " " ) ; } } var sentence = " " ; printReverse ( sentence ) ;
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code
function minCount ( A , B , N ) { var i = 0 for ( let j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) i += 1 } return N - i } var A = [ 1 , 2 , 3 , 4 , 5 ] var B = [ 1 , 5 , 2 , 3 , 4 ] N = A . length document . write ( minCount ( A , B , N ) )
Minimize maximum array element possible by at most K splits on the given array | Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
function possible ( A , N , mid , K ) { var count = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { count += Math . floor ( ( A [ i ] - 1 ) / mid ) ; } if ( count <= K ) return true ; else return false } function minimumMaximum ( A , N , K ) { var lo = 1 ; var hi = Math . max . apply ( Math , A ) ; var mid ; while ( lo < hi ) { mid = Math . floor ( ( lo + hi ) / 2 ) ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } var arr = [ 2 , 4 , 8 , 2 ] ; var K = 4 ; var N = arr . length ; document . write ( minimumMaximum ( arr , N , K ) ) ;
Maximum value of X such that difference between any array element and X does not exceed K | Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
function maximumNumber ( arr , N , K ) { let minimum = Math . min ( ... arr ) let ans = minimum + K ; for ( let i = 0 ; i < N ; i ++ ) { if ( Math . abs ( arr [ i ] - ans ) > K ) { ans = - 1 ; break ; } } document . write ( ans ) } let arr = [ 1 , 2 , 5 ] let K = 2 let N = arr . length maximumNumber ( arr , N , K ) ;
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Function to check if a number is prime or not ; Base Cases ; Check if n is divisible by 2 or 3 ; Iterate over every 6 number from the range [ 5 , sqrt ( N ) ] ; If n is found to be non - prime ; Otherwise , return true ; Function to check if a number can be represented as the sum of a prime number and a perfect square or not ; Stores all perfect squares less than N ; Store the perfect square in the array ; Iterate over all perfect squares ; Store the difference of perfect square from n ; If difference is prime ; Update flag ; Break out of the loop ; If N is the sum of a prime number and a perfect square ; Driver Code
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 sumOfPrimeSquare ( n ) { let i = 0 ; let squares = [ ] ; while ( i * i < n ) { squares . push ( i * i ) ; i ++ ; } let flag = false ; for ( i = 0 ; i < squares . length ; i ++ ) { let difference = n - squares [ i ] ; if ( isPrime ( difference ) ) { flag = true ; break ; } } if ( flag ) { document . write ( " " ) ; } else document . write ( " " ) ; } let N = 27 ; sumOfPrimeSquare ( N ) ;
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Cod
function SieveOfEratosthenes ( prime , n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } function sumOfPrimeSquare ( n ) { let flag = false ; let prime = new Array ( n + 1 ) . fill ( true ) ; SieveOfEratosthenes ( prime , n ) ; for ( let i = 0 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) continue ; let dif = n - i ; if ( Math . ceil ( Math . sqrt ( dif ) ) == Math . floor ( Math . sqrt ( dif ) ) ) { flag = true ; break ; } } if ( flag ) { document . write ( " " ) ; } else document . write ( " " ) ; } let N = 27 ; sumOfPrimeSquare ( N ) ;
Check if any subarray of length M repeats at least K times consecutively or not | Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver Code
function check ( arr , M , K , ind ) { for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 1 ; j < K ; j ++ ) { if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) { return false ; } } } return true ; } function SubarrayRepeatsKorMore ( arr , N , M , K ) { for ( let ind = 0 ; ind <= N - M * K ; ind ++ ) { if ( check ( arr , M , K , ind ) ) { return true ; } } return false ; } let arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ] ; let M = 2 , K = 2 ; let N = arr . length ; if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if any subarray of length M repeats at least K times consecutively or not | Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code
function checkExists ( arr , N , M , K ) { let count = 0 ; for ( let i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } let arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ] ; let N = arr . length ; let M = 2 , K = 2 ; if ( checkExists ( arr , N , M , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Number of common digits present in two given numbers | Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Input
function CommonDigits ( N , M ) { var count = 0 ; var freq1 = Array ( 10 ) . fill ( 0 ) ; var freq2 = Array ( 10 ) . fill ( 0 ) ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = Math . floor ( N / 10 ) ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = Math . floor ( M / 10 ) ; } var i ; for ( i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } var N = 748294 ; var M = 34298156 ; document . write ( CommonDigits ( N , M ) ) ;
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
function minimumNumber ( K , X ) { if ( K > X ) { document . write ( " " ) ; return ; } let ans = 0 ; let sum = 0 ; for ( let i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } document . write ( ans ) ; } let K = 5 , X = 13 ; minimumNumber ( K , X ) ;
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
function isGreaterEqual ( N , K , X ) { return ( ( N * parseInt ( ( N + 1 ) / 2 ) ) - ( ( K - 1 ) * parseInt ( K / 2 ) ) ) >= X ; } function minimumNumber ( K , X ) { if ( K > X ) { document . write ( " " ) ; return ; } let low = K , high = X , res = - 1 ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( isGreaterEqual ( mid , K , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } document . write ( res ) ; } let K = 5 , X = 13 ; minimumNumber ( K , X ) ;
Count pairs with product of indices equal to the product of elements present at those indices | Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
function CountPairs ( arr , n ) { var count = 0 ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } var arr = [ 1 , 0 , 3 , 2 , 6 ] ; var N = arr . length ; document . write ( CountPairs ( arr , N ) ) ;