codeparrot/xlcost-text-to-code huggingface dataset:
Check if all disks can be placed at a single rod based on given conditions | Function to check if it is possible to move all disks to a single rod ; Stores if it is possible to move all disks to a single rod ; Traverse the array ; If i - th element is smaller than both its adjacent elements ; If flag is true ; Otherwise ; Driver Code
function check ( a , n ) { flag = false ; for ( i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] && a [ i ] < a [ i - 1 ] ) flag = true ; } if ( flag ) return false ; else return true ; } var arr = [ 1 , 3 , 5 , 2 ] ; var N = arr . length ; if ( check ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Smallest positive integer that does not divide any elements of the given array | Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call
function smallestNumber ( arr , len ) { let maxi = 0 ; for ( let i = 0 ; i < len ; i ++ ) { maxi = Math . max ( maxi , arr [ i ] ) ; } let ans = - 1 ; for ( let i = 2 ; i < maxi + 2 ; i ++ ) { let flag = true ; for ( let j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } document . write ( ans ) ; } let arr = [ 3 , 2 , 6 , 9 , 2 ] ; let N = arr . length ; smallestNumber ( arr , N ) ;
Find the duplicate characters in a string in O ( 1 ) space | Function to find duplicate characters in string without using any additional data structure ; Check if ( i + ' a ' ) is present in str at least once or not . ; Check if ( i + ' a ' ) is present in str at least twice or not . ; Iterate over the characters of the string str ; If str [ i ] has already occurred in str ; Set ( str [ i ] - ' a ' ) - th bit of second ; Set ( str [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver code
function findDuplicate ( str , N ) { let first = 0 ; let second = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( first & ( 1 << ( str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ) ) != 0 ) { second = second | ( 1 << ( str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ) ; } else { first = first | ( 1 << ( str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ) ; } } for ( let i = 0 ; i < 26 ; i ++ ) { if ( ( ( first & ( 1 << i ) ) & ( second & ( 1 << i ) ) ) != 0 ) { document . write ( String . fromCharCode ( i + ' ' . charCodeAt ( ) ) + " " ) ; } } } let str = " " ; let N = str . length ; findDuplicate ( str , N ) ;
Minimize insertions required to make all characters of a given string equal | Function to calculate the minimum number of operations required to make all characters of the string same ; Stores count of operations ; Traverse the string ; Check if adjacent characters are same or not ; Increment count ; Print the count obtained ; Driver Code
function minOperations ( S ) { var count = 0 ; for ( var i = 1 ; i < S . length ; i ++ ) { if ( S [ i ] !== S [ i - 1 ] ) { count += 1 ; } } document . write ( count ) ; } var S = " " ; minOperations ( S ) ;
Count pairs made up of an element divisible by the other from an array consisting of powers of 2 | Function to count the number of pairs as per the given conditions ; Initialize array set_bits as 0 ; Store the total number of required pairs ; Traverse the array arr [ ] ; Store arr [ i ] in x ; Store the position of the leftmost set bit in arr [ i ] ; Increase bit position ; Divide by 2 to shift bits in right at each step ; Count of pairs for index i till its set bit position ; Increasing count of set bit position of current elelement ; Print the answer ; Driver Code ; Function Call
function numberOfPairs ( arr , N ) { let set_bits = [ ] ; for ( let i = 0 ; i < 31 ; i ++ ) { set_bits [ i ] = 0 ; } let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let x = arr [ i ] ; let bitpos = - 1 ; while ( x > 0 ) { bitpos ++ ; x = Math . floor ( x / 2 ) ; } for ( let j = 0 ; j <= bitpos ; j ++ ) { count += set_bits [ j ] ; } set_bits [ bitpos ] ++ ; } document . write ( count ) ; } let arr = [ 4 , 16 , 8 , 64 ] ; let N = arr . length ; numberOfPairs ( arr , N ) ;
Queries to find minimum sum of array elements from either end of an array | Function to calculate the minimum sum from either end of the arrays for the given queries ; Traverse the query [ ] array ; Stores sum from start and end of the array ; Calculate distance from start ; Calculate distance from end ; Driver code
function calculateQuery ( arr , N , query , M ) { for ( let i = 0 ; i < M ; i ++ ) { let X = query [ i ] ; let sum_start = 0 , sum_end = 0 ; for ( let j = 0 ; j < N ; j ++ ) { sum_start += arr [ j ] ; if ( arr [ j ] == X ) break ; } for ( let j = N - 1 ; j >= 0 ; j -- ) { sum_end += arr [ j ] ; if ( arr [ j ] == X ) break ; } document . write ( Math . min ( sum_end , sum_start ) + " " ) ; } } let arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 30 ] ; let queries = [ 6 , 5 ] ; let N = arr . length ; let M = queries . length ; calculateQuery ( arr , N , queries , M ) ;
Queries to find minimum sum of array elements from either end of an array | Function to find the minimum sum for the given queries ; Stores prefix and suffix sums ; Stores pairs of prefix and suffix sums ; Traverse the array ; Add element to prefix ; Store prefix for each element ; Traverse the array in reverse ; Add element to suffix ; Storing suffix for each element ; Traverse the array queries [ ] ; Minimum of suffix and prefix sums ; Driver Code
function calculateQuery ( arr , N , query , M ) { var prefix = 0 , suffix = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { prefix += arr [ i ] ; if ( ! mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , [ 0 , 0 ] ) ; } var tmp = mp . get ( arr [ i ] ) ; tmp [ 0 ] = prefix ; mp . set ( arr [ i ] , tmp ) ; } for ( var i = N - 1 ; i >= 0 ; i -- ) { suffix += arr [ i ] ; if ( ! mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , [ 0 , 0 ] ) ; } var tmp = mp . get ( arr [ i ] ) ; tmp [ 1 ] = suffix ; mp . set ( arr [ i ] , tmp ) ; } for ( var i = 0 ; i < M ; i ++ ) { var X = query [ i ] ; var tmp = mp . get ( X ) ; document . write ( Math . min ( tmp [ 0 ] , tmp [ 1 ] ) + " " ) ; } } var arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 30 ] ; var queries = [ 6 , 5 ] ; var N = arr . length ; var M = queries . length ; calculateQuery ( arr , N , queries , M ) ;
Queries to find the maximum array element after removing elements from a given range | Function to find the maximum element after removing elements in range [ l , r ] ; Store prefix maximum element ; Store suffix maximum element ; Prefix max till first index is first index itself ; Traverse the array to fill prefix max array ; Store maximum till index i ; Suffix max till last index is last index itself ; Traverse the array to fill suffix max array ; Store maximum till index i ; Traverse all queries ; Store the starting and the ending index of the query ; Edge Cases ; Otherwise ; Driver Code
function findMaximum ( arr , N , Q , queries ) { let prefix_max = [ ] ; let suffix_max = [ ] ; prefix_max [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { prefix_max [ i ] = Math . max ( prefix_max [ i - 1 ] , arr [ i ] ) ; } suffix_max [ N - 1 ] = arr [ N - 1 ] ; for ( let i = N - 2 ; i >= 0 ; i -- ) { suffix_max [ i ] = Math . max ( suffix_max [ i + 1 ] , arr [ i ] ) ; } for ( let i = 0 ; i < Q ; i ++ ) { let l = queries [ i ] [ 0 ] ; let r = queries [ i ] [ 1 ] ; if ( l == 0 && r == ( N - 1 ) ) document . write ( " " ) ; else if ( l == 0 ) document . write ( suffix_max [ r + 1 ] + " " ) ; else if ( r == ( N - 1 ) ) document . write ( prefix_max [ l - 1 ] + " " ) ; else document . write ( Math . max ( prefix_max [ l - 1 ] , suffix_max [ r + 1 ] ) + " " ) ; } } let arr = [ 5 , 6 , 8 , 10 , 15 ] ; let N = arr . length ; let queries = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 4 ] ] ; let Q = queries . length ; findMaximum ( arr , N , Q , queries ) ;
Find index of the element differing in parity with all other array elements | Function to print the element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Given array ; Size of the array
function oddOneOut ( arr , N ) { let odd = 0 , even = 0 ; let lastOdd = 0 , lastEven = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { document . write ( lastOdd ) ; } else { document . write ( lastEven ) ; } } let arr = [ 2 , 4 , 7 , 8 , 10 ] ; let N = arr . length ; oddOneOut ( arr , N ) ;
Reduce a string to a valid email address of minimum length by replacing specified substrings | Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' # such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; To display the result
function minEmail ( email ) { let ans = " " ; let len = email . length ; ans += email [ 0 ] ; let i = 1 ; let notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email [ i ] == ' ' && email [ i + 1 ] == ' ' ) { ans += ' ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email [ i ] == ' ' && email [ i + 1 ] == ' ' && email [ i + 2 ] == ' ' ) { ans += ' ' ; i += 2 ; } else { ans += email [ i ] ; } i += 1 ; } return ans ; } let email = " " ; document . write ( minEmail ( email ) ) ;
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Input ; Finds minimum value of N such that sum of first N natural number >= X
function isGreaterEqual ( N , X ) { return parseInt ( ( N * ( N + 1 ) ) / 2 ) >= X ; } function minimumPossible ( X ) { for ( let i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } } let X = 14 ; document . write ( minimumPossible ( X ) ) ;
Queries to count numbers from given range which are divisible by all its digits | Javascript program to implement the above approach ; Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
const Max = 1000005 ; function CheckDivByAllDigits ( number ) { let n = number ; while ( n > 0 ) { if ( n % 10 ) if ( number % ( n % 10 ) ) { return false ; } n = parseInt ( n / 10 ) ; } return true ; } function cntNumInRang ( arr , N ) { let prefCntDiv = new Array ( Max ) . fill ( 0 ) ; for ( let i = 1 ; i <= Max ; i ++ ) { prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) ; } for ( let i = 0 ; i < N ; i ++ ) document . write ( ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] ) + " " ) ; } let arr = [ [ 1 , 5 ] , [ 12 , 14 ] ] ; let N = arr . length ; cntNumInRang ( arr , N ) ;
Longest subset of nested elements from a given array | Function to find length of longest subset such that subset { arr [ i ] , arr [ arr [ i ] ] , . . } ; Stores length of the longest subset that satisfy the condition ; Traverse in the array , arr [ ] ; If arr [ i ] equals to i ; Update res ; Count of elements in a subset ; Stores index of elements in the current subset ; Calculate length of a subset that satisfy the condition ; Make visited the current index ; Update curr_index ; Update count ; Update res ; Driver Code
function arrayNesting ( arr ) { let res = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == i ) { res = Math . max ( res , 1 ) ; } else { let count = 0 ; let curr_index = i ; while ( arr [ curr_index ] != curr_index ) { let next_index = arr [ curr_index ] ; arr [ curr_index ] = curr_index ; curr_index = next_index ; count ++ ; } res = Math . max ( res , count ) ; } } return res ; } let arr = [ 5 , 4 , 0 , 3 , 1 , 6 , 2 ] ; let res = arrayNesting ( arr ) ; document . write ( res ) ;
Count numbers from a given range having exactly 5 distinct factors | javascript program of the above approach ; Stores all prime numbers up to 2 * 10 ^ 5 ; Function to generate all prime numbers up to 2 * 10 ^ 5 using Sieve of Eratosthenes ; Mark 0 and 1 as non - prime ; If i is prime ; Mark all its factors as non - prime ; If current number is prime ; Store the prime ; Function to count numbers in the range [ L , R ] having exactly 5 factors ; Stores the required count ; Driver Code
let N = 200000 ; let prime = new Array ( 20000 ) . fill ( 0 ) ; let index = 0 ; function Sieve ( ) { index = 0 ; let p = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i <= N ; i ++ ) { p [ i ] = 1 ; } p [ 0 ] = p [ 1 ] = 0 ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == 1 ) { for ( let j = i * i ; j <= N ; j += i ) { p [ j ] = 0 ; } } } for ( let i = 1 ; i < N ; i ++ ) { if ( p [ i ] == 1 ) { prime [ index ++ ] = ( Math . pow ( i , 4 ) ) ; } } } function countNumbers ( L , R ) { let Count = 0 ; for ( let i = 0 ; i < index ; i ++ ) { let p = prime [ i ] ; if ( p >= L && p <= R ) { Count ++ ; } } document . write ( Count ) ; } let L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ;
Minimize remaining array sizes by removing equal pairs of first array elements | Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 [ ] ; Store the count of 0 s in L2 [ ] ; Traverse the array L1 [ ] ; If condition is true ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 [ ] ; If condition is true ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call
function countRemainingElements ( L1 , L2 , n ) { let one = 0 ; let zero = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( L1 [ i ] == 1 ) one ++ ; else zero ++ ; } let ans = n ; for ( let i = 0 ; i < n ; i ++ ) { if ( L2 [ i ] == 1 ) { one -- ; if ( one < 0 ) { ans = i ; break ; } } else { zero -- ; if ( zero < 0 ) { ans = i ; break ; } } } document . write ( n - ans ) ; } let L1 = [ 1 , 1 , 0 , 0 ] ; let L2 = [ 0 , 0 , 0 , 1 ] ; let N = L1 . length ; countRemainingElements ( L1 , L2 , N ) ;
Count pairs of similar rectangles possible from a given array | Function to calculate the count of similar rectangles ; Input
function getCount ( rows , columns , A ) { var res = 0 ; for ( var i = 0 ; i < rows ; i ++ ) { for ( var j = i + 1 ; j < rows ; j ++ ) { if ( A [ i ] [ 0 ] * A [ j ] [ 1 ] == A [ i ] [ 1 ] * A [ j ] [ 0 ] ) { res ++ ; } } } return res ; } var A = [ [ 4 , 8 ] , [ 10 , 20 ] , [ 15 , 30 ] , [ 3 , 6 ] ] ; var columns = 2 ; var rows = 4 ; document . write ( getCount ( rows , columns , A ) ) ;
Find the index with minimum score from a given array | Function to find the index with minimum score ; Stores the score of current index ; Traverse the array in reverse ; Update minimum score ; Print the index with minimum score ; Driver Code
function Min_Score_Index ( N , A ) { var Score = Array ( N ) . fill ( 0 ) ; for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] + i < N ) Score [ i ] = A [ i ] * Score [ A [ i ] + i ] ; else Score [ i ] = A [ i ] ; } var min_value = 1000000000 ; Score . forEach ( i => { min_value = Math . min ( i , min_value ) ; } ) ; var ind = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( Score [ i ] == min_value ) ind = i ; } document . write ( ind ) ; } var N = 5 ; var A = [ 1 , 2 , 3 , 4 , 5 ] ; Min_Score_Index ( N , A ) ;
Minimize length of a string by removing occurrences of another string from it as a substring | Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Print the final answer ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
function minLength ( S , T , N , M ) { let temp = " " ; let subtract = 0 ; for ( let i = 0 ; i < N ; ++ i ) { temp += S [ i ] ; if ( temp . length >= M ) { if ( T == temp . substr ( temp . length - M , temp . length ) ) { subtract += M ; let cnt = 0 ; while ( cnt != M ) { temp = temp . substr ( 0 , temp . length - 1 ) ; ++ cnt ; } } } } document . write ( ( N - subtract ) ) ; } let S = " " , T = " " ; let N = S . length ; let M = T . length ; minLength ( S , T , N , M ) ;
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call
function maximumCount ( arr1 , arr2 , s1 , s2 ) { let sum1 = 0 , sum2 = 0 ; for ( let i = 0 ; i < s1 ; i ++ ) { sum1 += arr1 [ i ] ; } for ( let j = 0 ; j < s2 ; j ++ ) { sum2 += arr2 [ j ] ; } let len = 0 ; if ( s1 >= s2 ) { len = s2 ; } else { len = s1 ; } arr1 . sort ( ) ; arr2 . sort ( ) ; let j = 0 , k = s2 - 1 , count = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( sum1 <= sum2 ) { if ( arr2 [ k ] >= arr1 [ i ] ) { let dif1 = arr1 [ j ] , dif2 = arr2 [ k ] ; sum1 -= dif1 ; sum1 += dif2 ; sum2 -= dif2 ; sum2 += dif1 ; j ++ ; k -- ; count ++ ; } else { break ; } } else { break ; } } return count ; } let arr1 = [ 1 , 3 , 2 , 4 ] ; let arr2 = [ 6 , 7 , 8 ] ; let N = arr1 . length ; let M = arr2 . length ; document . write ( maximumCount ( arr1 , arr2 , N , M ) ) ;
Count array elements whose all distinct digits appear in K | Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
function isValidDigit ( digit , K ) { while ( K != 0 ) { if ( K % 10 == digit ) { return true ; } K = parseInt ( K / 10 ) ; } return false ; } function noOfValidNumbers ( K , arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let no = arr [ i ] ; let flag = true ; while ( no != 0 ) { let digit = no % 10 ; if ( ! isValidDigit ( digit , K ) ) { flag = false ; break ; } no = parseInt ( no / 10 ) ; } if ( flag == true ) { count ++ ; } } return count ; } let K = 12 ; let arr = [ 1 , 12 , 1222 , 13 , 2 ] ; let n = arr . length ; document . write ( noOfValidNumbers ( K , arr , n ) ) ;
Queries to find the minimum index in given array having at least value X | JavaScript program for above approach ; Function to find the smallest index of an array element whose value is less than or equal to Q [ i ] ; Stores size of array ; Stores coun of queries ; Store array elements along with the index ; Store smallest index of an array element whose value is greater than or equal to i ; Traverse the array ; Insert { arr [ i ] , i } into storeArrIdx [ ] ; Sort the array ; Sort the storeArrIdx ; Stores index of arr [ N - 1 ] in sorted order ; Traverse the array storeArrIdx [ ] ; Update minIdx [ i ] ; Traverse the array Q [ ] ; Store the index of lower_bound of Q [ i ] ; If no index found whose value greater than or equal to arr [ i ] ; Print smallest index whose value greater than or equal to Q [ i ] ; Driver function
class pair { constructor ( element , index ) { this . element = element ; this . index = index ; } } function minimumIndex ( arr , Q ) { let N = arr . length ; let M = Q . length ; let storeArrIdx = [ ] ; let minIdx = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { minIdx [ i ] = 0 ; } for ( let i = 0 ; i < N ; ++ i ) { storeArrIdx . push ( [ arr [ i ] , i ] ) ; } ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; storeArrIdx . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; minIdx [ N - 1 ] = storeArrIdx [ N - 1 ] [ 1 ] ; for ( let i = N - 2 ; i >= 0 ; i -- ) { minIdx [ i ] = Math . min ( minIdx [ i + 1 ] , storeArrIdx [ i ] [ 1 ] ) ; } for ( let i = 0 ; i < M ; i ++ ) { let pos = lower_bound ( arr , Q [ i ] ) ; if ( pos == N ) { document . write ( " " + " " ) ; continue ; } document . write ( minIdx [ pos ] + " " ) ; } } function lower_bound ( arr , element ) { for ( let i = 0 ; i < arr . length ; i ++ ) if ( element <= arr [ i ] ) return i ; return arr . length ; } let arr = [ 1 , 9 ] ; let Q = [ 7 , 10 , 0 ] ; minimumIndex ( arr , Q ) ;
Find a K | Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Given array ; Size of the array ; Given K ; Function Call
function isSubarrayExistUtil ( arr , K , N ) { var totalXOR = 0 ; var SubarrayXOR = 0 ; for ( i = 0 ; i < N ; i ++ ) totalXOR ^= arr [ i ] ; for ( i = 0 ; i < K ; i ++ ) SubarrayXOR ^= arr [ i ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; for ( i = K ; i < N ; i ++ ) { SubarrayXOR ^= arr [ i ] ; SubarrayXOR ^= arr [ i - 1 ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; } return false ; } function isSubarrayExist ( arr , K , N ) { if ( isSubarrayExistUtil ( arr , K , N ) ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 ] ; var N = arr . length ; var K = 5 ; isSubarrayExist ( arr , K , N ) ;