codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the minimum difference between any element pair by selecting K elements from given Array | To check if selection of K elements is possible such that difference between them is greater than dif ; Selecting first element in the sorted array ; prev is the previously selected element initially at index 0 as first element is already selected ; Check if selection of K - 1 elements from array with a minimum difference of dif is possible ; If the current element is atleast dif difference apart from the previously selected element then select the current element and increase the count ; If selection of K elements with a min difference of dif is possible then return true ; Prev will become current element for the next iteration ; If selection of K elements with minimum difference of dif is not possible then return false ; Minimum largest difference possible is 1 ; Check if selection of K elements is possible with a minimum difference of dif ; If dif is greater than previous ans we update ans ; Continue to search for better answer . Try finding greater dif ; K elements cannot be selected ; Driver code ; arr should be in a sorted order
function isPossibleToSelect ( arr , N , dif , K ) { let count = 1 ; let prev = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } function binarySearch ( arr , left , right , K , N ) { let ans = 1 ; while ( left <= right ) { let dif = left + Math . floor ( ( right - left ) / 2 ) ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = Math . max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } let N , K ; N = 7 , K = 4 ; let arr = [ 1 , 4 , 9 , 0 , 2 , 13 , 3 ] ; arr . sort ( function ( a , b ) { return a - b } ) document . write ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) + ' ' ) ;
Find maximum height to cut all chocolates horizontally such that at least K amount remains | Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code
function cal ( arr , mid ) { let chocolate = 0 for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] >= mid ) chocolate += arr [ i ] - mid } return chocolate } function maximumCut ( arr , K ) { let low = 0 let high = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } while ( low <= high ) { mid = Math . floor ( ( low + high ) / 2 ) ; chocolate = cal ( arr , mid ) if ( chocolate == K ) { return mid } else if ( chocolate < K ) { high = mid - 1 } else { low = mid + 1 if ( mid > high ) high = mid } } return high } let N = 4 ; let K = 7 ; let arr = [ 15 , 20 , 8 , 17 ] ; document . write ( maximumCut ( arr , K ) )
Find MEX of every subtree in given Tree | Stores the edges of the tree ; Function to add edges ; Function to merge two sorted vectors ; To store the result ; Iterating both vectors ; Pushing remaining elements of vector a ; Pushing remaining elements of vector b ; Function to perform the DFS Traversal that returns the subtree of node in sorted manner ; Iterate the childrens ; All values of subtree i in sorted manner ; Binary search to find MEX ; Find the mid ; Update the ranges ; Update the MEX for the current tree node ; Function to find MEX of each subtree of tree ; Function Call ; Printe the ans for each nodes ; Driver Code
let edges = [ ] ; function add_edge ( x , y ) { edges . push ( [ x , y ] ) ; } function merge ( a , b ) { let res = [ ] ; let i = 0 , j = 0 ; let n = a . length , m = b . length ; while ( i < n && j < m ) { if ( a [ i ] < b [ j ] ) res . push ( a [ i ++ ] ) ; else if ( b [ j ] < a [ i ] ) res . push ( b [ j ++ ] ) ; } while ( i < n ) res . push ( a [ i ++ ] ) ; while ( j < m ) res . push ( b [ j ++ ] ) ; return res ; } function help ( tree , x , p , c , sol ) { let res = [ ] ; res . push ( c [ x ] ) ; for ( let i of tree [ x ] ) { if ( i != p ) { let tmp = help ( tree , i , x , c , sol ) ; res = merge ( res , tmp ) ; } } let l = 0 , r = res . length - 1 ; let ans = res . length ; while ( l <= r ) { let mid = Math . floor ( ( l + r ) / 2 ) ; if ( res [ mid ] > mid ) r = mid - 1 ; else { ans = mid + 1 ; l = mid + 1 ; } } if ( res [ 0 ] != 0 ) ans = 0 ; sol [ x ] = ans ; return res ; } function solve ( A , C ) { let n = A ; let tree = new Array ( n + 1 ) . fill ( 0 ) . map ( ( ) => [ ] ) ; for ( let i of edges ) { tree [ i [ 0 ] ] . push ( i [ 1 ] ) ; tree [ i [ 1 ] ] . push ( i [ 0 ] ) ; } let sol = new Array ( n ) . fill ( 0 ) ; help ( tree , 0 , - 1 , C , sol ) ; for ( let i of sol ) document . write ( i + " " ) ; } let N = 6 ; add_edge ( 0 , 1 ) ; add_edge ( 1 , 2 ) ; add_edge ( 0 , 3 ) ; add_edge ( 3 , 4 ) ; add_edge ( 3 , 5 ) ; let val = [ 4 , 3 , 5 , 1 , 0 , 2 ] ; solve ( N , val ) ;
Koko Eating Bananas | Javascript implementation for the above approach ; to get the ceil value ; in case of odd number ; in case of even number ; check if time is less than or equals to given hour ; as minimum speed of eating must be 1 ; Maximum speed of eating is the maximum bananas in given piles ; Check if the mid ( hours ) is valid ; If valid continue to search lower speed ; If cant finish bananas in given hours , then increase the speed ; Driver code
function check ( bananas , mid_val , H ) { let time = 0 ; for ( let i = 0 ; i < bananas . length ; i ++ ) { if ( bananas [ i ] % mid_val != 0 ) { time += Math . floor ( bananas [ i ] / mid_val ) + 1 ; } else { time += Math . floor ( bananas [ i ] / mid_val ) ; } } if ( time <= H ) { return true ; } else { return false ; } } function minEatingSpeed ( piles , H ) { let start = 1 ; let end = [ ... piles ] . sort ( ( a , b ) => b - a ) [ 0 ] ; while ( start < end ) { let mid = start + Math . floor ( ( end - start ) / 2 ) ; if ( check ( piles , mid , H ) == true ) { end = mid ; } else { start = mid + 1 ; } } return end ; } let piles = [ 30 , 11 , 23 , 4 , 20 ] ; let H = 6 ; document . write ( minEatingSpeed ( piles , H ) ) ;
Minimize the sum of pair which upon removing divides the Array into 3 subarrays | Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Given array
function minSumPair ( arr , N ) { if ( N < 5 ) { return - 1 ; } let prefixMin = new Array ( N ) . fill ( 0 ) ; prefixMin [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } let ans = Number . MAX_VALUE ; for ( let i = 3 ; i < N - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } let arr = [ 5 , 2 , 4 , 6 , 3 , 7 ] ; let N = arr . length ; document . write ( minSumPair ( arr , N ) ) ;
Minimum time remaining for safety alarm to start | Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code
function check ( H , A , mid , N , M , L ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let speed = mid * A [ i ] + H [ i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } function buzzTime ( N , M , L , H , A ) { let low = 0 , high = 1e10 ; let ans = 0 ; while ( high >= low ) { let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } let M = 400 , L = 120 ; let H = [ 20 , 50 , 20 ] ; let A = [ 20 , 70 , 90 ] ; let N = A . length ; document . write ( buzzTime ( N , M , L , H , A ) ) ;
Count of different numbers divisible by 3 that can be obtained by changing at most one digit | Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Given number
function findCount ( number ) { let sum = 0 ; for ( let i = 0 ; i < number . length ; ++ i ) { sum += number [ i ] - 48 ; } let count = 0 ; for ( let i = 0 ; i < number . length ; ++ i ) { let remaining_sum = sum - ( number [ i ] - 48 ) ; for ( let j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number [ i ] - 48 ) { ++ count ; } } } document . write ( count ) ; } let number = " " ; findCount ( number ) ;
Maximum number of teams of size K possible with each player from different country | Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code
function is_possible ( teams , T , k ) { let sum = 0 ; for ( let i = 0 ; i < teams . length ; i ++ ) { sum += Math . min ( T , teams [ i ] ) ; } return sum >= T * k ; } function countOfTeams ( teams_list , N , K ) { let lb = 0 , ub = 1e9 ; while ( lb <= ub ) { let mid = Math . floor ( lb + ( ub - lb ) / 2 ) ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } let arr = [ 2 , 3 , 4 ] ; let K = 2 ; let N = arr . length ; document . write ( countOfTeams ( arr , N , K ) ) ;
Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 | Function to find minimum operation require to make all elements 0 ; Stores the maximum element and its position in the array ; Traverse the array ; Update the maximum element and its index ; Print the minimum number of operations required ; Driver Code
function minimumOperations ( arr , N ) { let mx = 0 , pos = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } document . write ( ( mx - 1 ) * N + pos + 1 ) ; } let arr = [ 2 , 0 , 2 ] ; let N = arr . length ; minimumOperations ( arr , N ) ;
Maximize the smallest array element by incrementing all elements in a K | Javascript program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Given Input ; Function Call
let n = 0 , m = 0 , k = 0 , l = 0 , r = 0 , i = 0 ; function check ( v , a ) { let tec = 0 , ans = 0 ; let b = new Array ( n + k + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { let mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + k ] = mov ; } } return ans <= m ; } function FindLargest ( a ) { l = 1 ; r = Math . pow ( 10 , 10 ) ; while ( r - l > 0 ) { let tm = Math . floor ( ( l + r + 1 ) / 2 ) ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } let a = [ 2 , 2 , 2 , 2 , 1 , 1 ] ; m = 2 ; k = 3 ; n = a . length ; document . write ( FindLargest ( a ) ) ;
Altitude of largest Triangle that can be inscribed in a Rectangle | Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Given Input ; Function call
function largestAltitude ( L , B ) { if ( L > B ) { let temp = B ; B = L ; L = temp ; } let low = 0 , high = L ; let res = 0 ; while ( low <= high ) { let mid = Math . floor ( low + ( high - low ) / 2 ) ; if ( mid <= Math . floor ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } let L = 3 ; let B = 4 ; document . write ( largestAltitude ( L , B ) ) ;
Find the frequency of each element in a sorted array | Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Given Input ; Function Call
function printFreq ( arr , N ) { let freq = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { document . write ( " " + parseInt ( arr [ i - 1 ] ) + " " + parseInt ( freq ) + " " ) ; freq = 1 ; } } document . write ( " " + parseInt ( arr [ N - 1 ] ) + " " + parseInt ( freq ) + " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let N = arr . length ; printFreq ( arr , N ) ;
Maximize median of a KxK sub | Function to determine if a given value can be median ; Stores the prefix sum array ; Traverse the matrix arr [ ] [ ] ; Update Pre [ i + 1 ] [ j + 1 ] ; If arr [ i ] [ j ] is less than or equal to mid ; Stores the count of elements should be less than mid ; Stores if the median mid can be possible or not ; Iterate over the range [ K , N ] ; Iterate over the range [ K , N ] ; Stores count of elements less than or equal to the value mid in submatrix with bottom right vertices at ( i , j ) ; If X is less than or equal to required ; Return flag ; Function to find the maximum median of a subsquare of the given size ; Stores the range of the search space ; Iterate until low is less than high ; Stores the mid value of the range [ low , high ] ; If the current median can be possible ; Update the value of low ; Update the value of high ; Return value stored in low as answer ; Driver Code
function isMaximumMedian ( arr , N , K , mid ) { let Pre = Array ( N + 5 ) . fill ( ) . map ( ( ) => Array ( N + 5 ) . fill ( 0 ) ) ; for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N ; ++ j ) { Pre [ i + 1 ] [ j + 1 ] = Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ; if ( arr [ i ] [ j ] <= mid ) Pre [ i + 1 ] [ j + 1 ] ++ ; } } let required = Math . floor ( ( K * K + 1 ) / 2 ) ; let flag = 0 ; for ( let i = K ; i <= N ; ++ i ) { for ( let j = K ; j <= N ; ++ j ) { let X = Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ; if ( X < required ) flag = 1 ; } } return flag ; } function maximumMedian ( arr , N , K ) { let low = 0 , high = 1e9 ; while ( low < high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } let arr = [ [ 1 , 5 , 12 ] , [ 6 , 7 , 11 ] , [ 8 , 9 , 10 ] ] ; let N = arr . length ; let K = 2 ; document . write ( maximumMedian ( arr , N , K ) ) ;
Minimum days to make Array elements with value at least K sum at least X | Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
function findMinDays ( arr , R , N , X , K ) { let low = 0 , high = X ; let minDays ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } document . write ( minDays ) ; } let X = 100 , K = 45 ; let arr = [ 2 , 5 , 2 , 6 ] ; let R = [ 10 , 13 , 15 , 12 ] ; let N = arr . length findMinDays ( arr , R , N , X , K ) ;
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Generate all possible pairs from the array arr [ ] ; If the given condition satisfy then increment the value of count ; Return the resultant count ; Driver Code
function getPairsCount ( a , n ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == Math . abs ( i - j ) ) count ++ ; } } return count ; } let arr = [ 1 , 1 , 2 , 4 ] ; let N = arr . length ; document . write ( getPairsCount ( arr , N ) ) ;
Lexicographically smallest string formed repeatedly deleting character from substring 10 | Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the string S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the string str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Input ; Function Call
function lexicographicallySmallestString ( S , N ) { var LastZe = - 1 ; var ans = " " ; for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == ' ' ) { LastZe = i ; break ; } } for ( var i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S . charAt ( i ) == ' ' ) ans += S . charAt ( i ) ; else if ( i > LastZe ) ans += S . charAt ( i ) ; } return ans ; } var S = " " ; var N = S . length ; document . write ( lexicographicallySmallestString ( S , N ) ) ;
Maximize boxes required to keep at least one black and one white shirt | Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print the result ; Driver Code
function numberofBoxes ( W , B , O ) { let low = 0 , high = Math . min ( W , B ) ; let ans = 0 ; while ( low <= high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( ( ( W >= mid ) && ( B >= mid ) ) && ( ( W - mid ) + ( B - mid ) + O ) >= mid ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } document . write ( ans ) ; } let W = 3 , B = 3 , O = 1 ; numberofBoxes ( W , B , O ) ;
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B | Function to count the minimum number of swaps required to convert matrix A to matrix B ; Stores number of cells such that matrix A contains 0 and matrix B contains 1 ; Stores number of cells such that matrix A contains 1 and matrix B contains 0 ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; If A [ i ] [ j ] = 1 and B [ i ] [ j ] = 0 ; If A [ i ] [ j ] = 0 and B [ i ] [ j ] = 1 ; If count01 is equal to count10 ; Otherwise , ; Driver Code
function minSwaps ( N , M , A , B ) { let count01 = 0 ; let count10 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { if ( A [ i ] [ j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return - 1 ; } let A = [ [ 1 , 1 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] ] ; let B = [ [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; let N = A . length ; let M = B [ 0 ] . length ; document . write ( minSwaps ( N , M , A , B ) ) ;
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Input ; Function call
function Count ( N , K ) { if ( K == 0 ) return 0 ; let sum = 0 ; let res = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } let N = 6 , K = 14 ; document . write ( Count ( N , K ) ) ;
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Input ; Function call
function maximumIndices ( arr , N ) { let temp = new Array ( N ) . fill ( 0 ) ; let maxIndices = parseInt ( ( N - 1 ) / 2 ) ; arr . sort ( function ( a , b ) { return a - b ; } ) for ( let i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } let j = 0 ; for ( let i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( temp [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; maximumIndices ( arr , N ) ;
Find the repeating element in an Array of size N consisting of first M natural numbers | Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Input ; Function call
function repeatingElement ( arr , N ) { let M = 0 , sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . max ( M , arr [ i ] ) ; } let sum1 = parseInt ( M * ( M + 1 ) / 2 ) ; let ans = parseInt ( ( sum - sum1 ) / ( N - M ) ) ; return ans ; } let arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ] ; let N = arr . length ; document . write ( repeatingElement ( arr , N ) ) ;