codeparrot/xlcost-text-to-code huggingface dataset:
Sort a string lexicographically by reversing a substring | Function to find the substring in S required to be reversed ; Stores the size of the string ; Stores the starting point of the substring ; Iterate over the string S while i < N ; Increment the value of i ; Stores the ending index of the substring ; If start <= 0 or i >= N ; If start >= 1 and i <= N ; Return the boolean value ; If start >= 1 ; Return the boolean value ; If i < N ; Return true if S [ start ] is less than or equal to S [ i ] ; Otherwise ; Function to check if it is possible to sort the string or not ; global start , end , i Stores the starting and the ending index of substring ; Stores whether it is possible to sort the substring ; Traverse the range [ 1 , N ] ; If S [ i ] is less than S [ i - 1 ] ; If flag stores true ; If adjust ( S , i , start , end ) return false ; Pr - 1 ; Unset the flag ; Otherwise ; Pr - 1 ; If start is equal to - 1 ; Update start and end ; Print the value of start and end ; Driver Code
def adjust ( S , i , start , end ) : NEW_LINE INDENT N = len ( S ) NEW_LINE start = i - 1 NEW_LINE while ( i < N and S [ i ] < S [ i - 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT end = i - 1 NEW_LINE if ( start <= 0 and i >= N ) : NEW_LINE INDENT return True , start , i , end NEW_LINE DEDENT if ( start >= 1 and i <= N ) : NEW_LINE INDENT return ( S [ end ] >= S [ start - 1 ] and S [ start ] <= S [ i ] ) , start , i , end NEW_LINE DEDENT if ( start >= 1 ) : NEW_LINE INDENT return ( S [ end ] >= S [ start - 1 ] ) , start , i , end NEW_LINE DEDENT if ( i < N ) : NEW_LINE INDENT return ( S [ start ] <= S [ i ] ) , start , i , end NEW_LINE DEDENT return False , start , i , end NEW_LINE DEDENT def isPossible ( S , N ) : NEW_LINE INDENT start , end = - 1 , - 1 NEW_LINE flag = True NEW_LINE i = 1 NEW_LINE while i < N : NEW_LINE INDENT if ( S [ i ] < S [ i - 1 ] ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT f , start , i , end = adjust ( S , i , start , end ) NEW_LINE if ( f == False ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT flag = False NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( start == - 1 ) : NEW_LINE INDENT start , end = 1 , 1 NEW_LINE DEDENT print ( start , end ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcyxuz " NEW_LINE N = len ( S ) NEW_LINE isPossible ( S , N ) NEW_LINE DEDENT
Split array into K non | Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
def maximumSum ( arr , S , N , K ) : NEW_LINE INDENT ans = 0 NEW_LINE arr = sorted ( arr ) [ : : - 1 ] NEW_LINE for i in range ( K ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT S = sorted ( S ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT if ( S [ i ] == 1 ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT S [ i ] -= 1 NEW_LINE DEDENT counter = K - 1 NEW_LINE for i in range ( K ) : NEW_LINE INDENT counter = counter + S [ i ] NEW_LINE if ( S [ i ] != 0 ) : NEW_LINE INDENT ans += arr [ counter ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 13 , 7 , 17 ] NEW_LINE S = [ 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = len ( S ) NEW_LINE print ( maximumSum ( arr , S , N , K ) ) NEW_LINE DEDENT
Modify an array by sorting after reversal of bits of each array element | Function to convert binary number to equivalent decimal ; Set base value to 1 , i . e 2 ^ 0 ; Function to convert a decimal to equivalent binary representation ; Stores the binary representation ; Since ASCII value of '0' , '1' are 48 and 49 ; As the string is already reversed , no further reversal is required ; Function to convert the reversed binary representation to equivalent integer ; Stores reversed binary representation of given decimal ; Stores equivalent decimal value of the binary representation ; Return the resultant integer ; Utility function to print the sorted array ; Sort the array ; Traverse the array ; Print the array elements ; Utility function to reverse the binary representations of all array elements and sort the modified array ; Traverse the array ; Passing array elements to reversedBinaryDecimal function ; Pass the array to the sorted array ; Driver Code
def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE length = len ( num ) NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def decimalToBinary ( n ) : NEW_LINE INDENT binstr = " " NEW_LINE while ( n > 0 ) : NEW_LINE INDENT binstr += chr ( n % 2 + 48 ) NEW_LINE n = n // 2 NEW_LINE DEDENT return binstr NEW_LINE DEDENT def reversedBinaryDecimal ( N ) : NEW_LINE INDENT decimal_to_binar = decimalToBinary ( N ) NEW_LINE binary_to_decimal = binaryToDecimal ( decimal_to_binar ) NEW_LINE return binary_to_decimal NEW_LINE DEDENT def printSortedArray ( arr , size ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def modifyArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) NEW_LINE DEDENT printSortedArray ( arr , size ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 98 , 43 , 66 , 83 ] NEW_LINE n = len ( arr ) NEW_LINE modifyArray ( arr , n ) NEW_LINE DEDENT
Check if sequence of removed middle elements from an array is sorted or not | Function to check if sequence of removed middle elements from an array is sorted or not ; Points toa the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
def isSortedArray ( arr , n ) : NEW_LINE INDENT l = 0 NEW_LINE r = ( n - 1 ) NEW_LINE while ( ( l + 1 ) < r ) : NEW_LINE INDENT if ( arr [ l ] >= max ( arr [ l + 1 ] , arr [ r - 1 ] ) and arr [ r ] >= max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) : NEW_LINE INDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 1 , 2 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE if ( isSortedArray ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Driver Code ; Given array ; Store the size of the array
def maxLenSubset ( a , n ) : NEW_LINE INDENT a . sort ( reverse = False ) NEW_LINE index = 0 NEW_LINE maxlen = - 1 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT j = i NEW_LINE len1 = 1 NEW_LINE while ( j < n - 1 ) : NEW_LINE INDENT if ( 2 * a [ j ] >= a [ j + 1 ] ) : NEW_LINE INDENT len1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( maxlen < len1 ) : NEW_LINE INDENT maxlen = len1 NEW_LINE index = i NEW_LINE DEDENT j += 1 NEW_LINE i = j NEW_LINE DEDENT i = index NEW_LINE while ( maxlen > 0 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE maxlen -= 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 3 , 1 , 5 , 11 ] NEW_LINE n = len ( a ) NEW_LINE maxLenSubset ( a , n ) NEW_LINE DEDENT
Maximize count of intersecting line segments | Python3 program for the above approach ; Function to find the maximum number of intersecting line segments possible ; Stores pairs of line segments { X [ i ] , Y [ i ] ) ; Push { X [ i ] , Y [ i ] } into p ; Sort p in ascending order of points on X number line ; Stores the points on Y number line in descending order ; Insert the first Y pofrom p ; Binary search to find the lower bound of p [ i ] [ 1 ] ; If lower_bound doesn 't exist ; Insert p [ i ] [ 1 ] into the set ; Erase the next lower _bound from the set ; Insert p [ i ] [ 1 ] into the set ; Return the size of the set as the final result ; Driver Code ; Given Input ; Function call to find the maximum number of intersecting line segments
from bisect import bisect_left NEW_LINE def solve ( N , X , Y ) : NEW_LINE INDENT p = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT p . append ( [ X [ i ] , Y [ i ] ] ) NEW_LINE DEDENT p = sorted ( p ) NEW_LINE s = { } NEW_LINE s [ p [ 0 ] [ 1 ] ] = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr = list ( s . keys ( ) ) NEW_LINE it = bisect_left ( arr , p [ i ] [ 1 ] ) NEW_LINE if ( it == len ( s ) ) : NEW_LINE INDENT s [ p [ i ] [ 1 ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT del s [ arr [ it ] ] NEW_LINE s [ p [ i ] [ 1 ] ] = 1 NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE X = [ 1 , 2 , 0 ] NEW_LINE Y = [ 2 , 0 , 1 ] NEW_LINE maxintersection = solve ( N , X , Y ) NEW_LINE print ( maxintersection ) NEW_LINE DEDENT
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driver Code ; Function Call to check if an array can be reduced to a single element
def canReduceArray ( arr , N , K ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i + 1 ] > K ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE canReduceArray ( arr , N , K ) NEW_LINE DEDENT
Lexicographically smallest string possible by merging two sorted strings | Function to find lexicographically smallest possible by merging two sorted strings ; Stores length of s1 ; Stores length of s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
def mergeStrings ( s1 , s2 ) : NEW_LINE INDENT len1 = len ( s1 ) NEW_LINE len2 = len ( s2 ) NEW_LINE pntr1 = 0 NEW_LINE pntr2 = 0 NEW_LINE ans = " " NEW_LINE while ( pntr1 < len1 and pntr2 < len2 ) : NEW_LINE INDENT if ( s1 [ pntr1 ] < s2 [ pntr2 ] ) : NEW_LINE INDENT ans += s1 [ pntr1 ] NEW_LINE pntr1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s2 [ pntr2 ] NEW_LINE pntr2 += 1 NEW_LINE DEDENT DEDENT if ( pntr1 < len1 ) : NEW_LINE INDENT ans += s1 [ pntr1 : pntr1 + len1 ] NEW_LINE DEDENT if ( pntr2 < len2 ) : NEW_LINE INDENT ans += s2 [ pntr2 : pntr2 + len2 ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " abdcdtx " NEW_LINE S2 = " achilp " NEW_LINE mergeStrings ( S1 , S2 ) NEW_LINE DEDENT
Sort a string without altering the position of vowels | Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
def sortStr ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE temp = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] != ' a ' and S [ i ] != ' e ' and S [ i ] != ' i ' and S [ i ] != ' o ' and S [ i ] != ' u ' ) : NEW_LINE INDENT temp += S [ i ] NEW_LINE DEDENT DEDENT if ( len ( temp ) ) : NEW_LINE INDENT p = list ( temp ) NEW_LINE p . sort ( ) NEW_LINE temp = ' ' . join ( p ) NEW_LINE DEDENT ptr = 0 NEW_LINE for i in range ( N ) : NEW_LINE S = list ( S ) NEW_LINE if ( S [ i ] != ' a ' and S [ i ] != ' e ' and S [ i ] != ' i ' and S [ i ] != ' o ' and S [ i ] != ' u ' ) : NEW_LINE INDENT S [ i ] = temp [ ptr ] NEW_LINE ptr += 1 NEW_LINE DEDENT print ( ' ' . join ( S ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE sortStr ( S ) NEW_LINE DEDENT
Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | Python3 program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Prthe result ; Given array , arr [ ] ; Store the size of the array
from bisect import bisect_right NEW_LINE def countPairs ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT high = bisect_right ( arr , k * arr [ i ] ) NEW_LINE ans += high - i - 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 2 , 3 , 9 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE countPairs ( arr , n , k ) NEW_LINE
Kth highest XOR of diagonal elements from a Matrix | Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
def findXOR ( mat , K ) : NEW_LINE INDENT N = len ( mat ) NEW_LINE M = len ( mat [ 0 ] ) NEW_LINE digXOR = [ ] NEW_LINE for l in range ( 1 , N + M , 1 ) : NEW_LINE INDENT s_col = max ( 0 , l - N ) NEW_LINE count = min ( [ l , ( M - s_col ) , N ] ) NEW_LINE currXOR = 0 NEW_LINE for j in range ( count ) : NEW_LINE INDENT currXOR = ( currXOR ^ mat [ min ( N , l ) - j - 1 ] [ s_col + j ] ) NEW_LINE DEDENT digXOR . append ( currXOR ) NEW_LINE DEDENT digXOR . sort ( reverse = False ) NEW_LINE print ( digXOR [ N + M - 1 - K ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE K = 3 NEW_LINE findXOR ( mat , K ) NEW_LINE DEDENT
Maximum sum of absolute differences between distinct pairs of a triplet from an array | Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code
def maximumSum ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE arr . sort ( ) NEW_LINE sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; NEW_LINE print ( sum ) NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE maximumSum ( arr , N ) NEW_LINE
Maximum and minimum sum of Bitwise XOR of pairs from an array | Python3 program for the above approach ; c [ int , [ a , b ] ] Function to find the required sum ; Keeps the track of the visited array elements ; Stores the result ; Keeps count of visited elements ; Traverse the vector , V ; If n elements are visited , break out of the loop ; Store the pair ( i , j ) and their Bitwise XOR ; If i and j both are unvisited ; Add xorResult to res and mark i and j as visited ; Increment count by 2 ; Return the result ; Function to find the maximum and minimum possible sum of Bitwise XOR of all the pairs from the array ; Stores the XOR of all pairs ( i , j ) ; Store the XOR of all pairs ( i , j ) ; Update Bitwise XOR ; Sort the vector ; Initialize variables to store maximum and minimum possible sums ; Find the minimum sum possible ; Reverse the vector , v ; Find the maximum sum possible ; Print the result ; Driver Code
v = [ ] NEW_LINE def findSum ( n ) : NEW_LINE INDENT global v NEW_LINE um = { } NEW_LINE res = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( cnt == n ) : NEW_LINE INDENT break NEW_LINE DEDENT x = v [ i ] [ 1 ] [ 0 ] NEW_LINE y = v [ i ] [ 1 ] [ 1 ] NEW_LINE xorResult = v [ i ] [ 0 ] NEW_LINE if ( x in um and um [ x ] == False and y in um and um [ y ] == False ) : NEW_LINE INDENT res += xorResult NEW_LINE um [ x ] = True NEW_LINE um [ y ] = True NEW_LINE cnt += 2 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def findMaxMinSum ( a , n ) : NEW_LINE INDENT global v NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT xorResult = a [ i ] ^ a [ j ] NEW_LINE v . append ( [ xorResult , [ a [ i ] , a [ j ] ] ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = False ) NEW_LINE maxi = 0 NEW_LINE mini = 0 NEW_LINE mini = findSum ( n ) NEW_LINE mini = 6 NEW_LINE v = v [ : : - 1 ] NEW_LINE maxi = findSum ( n ) NEW_LINE maxi = 10 NEW_LINE print ( mini , maxi ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxMinSum ( arr , N ) NEW_LINE DEDENT
Lexicographically smallest permutation having maximum sum of differences between adjacent elements | Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Print the required permutation ; Driver Code
def maximumSumPermutation ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE arr . sort ( ) ; NEW_LINE temp = arr [ 0 ] ; NEW_LINE arr [ 0 ] = arr [ N - 1 ] ; NEW_LINE arr [ N - 1 ] = temp ; NEW_LINE for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE maximumSumPermutation ( arr ) ; NEW_LINE DEDENT
Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | Python3 program to implement the above approach ; Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Prthe answer ; Driver Code
import math NEW_LINE def findMaxNumbers ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT opr = math . ceil ( math . log2 ( arr [ i ] ) ) NEW_LINE k -= opr NEW_LINE if ( k < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT print ( cnt ) NEW_LINE DEDENT arr = [ 5 , 8 , 4 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE findMaxNumbers ( arr , N , K ) NEW_LINE
Mean of given array after removal of K percent of smallest and largest array elements | Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
def meanOfRemainingElements ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE kthPercent = ( N * K ) / 100 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i >= kthPercent and i < ( N - kthPercent ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT mean = sum / ( N - 2 * kthPercent ) NEW_LINE print ( ' % .5f ' % mean ) NEW_LINE DEDENT arr = [ 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 ] NEW_LINE arr_size = len ( arr ) NEW_LINE K = 5 NEW_LINE meanOfRemainingElements ( arr , arr_size , K ) NEW_LINE
Make the array elements equal by performing given operations minimum number of times | Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array
def minOperation ( a , N ) : NEW_LINE INDENT totOps = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT totOps += abs ( a [ i ] - a [ i + 1 ] ) NEW_LINE DEDENT maxOps = max ( abs ( a [ 0 ] - a [ 1 ] ) , abs ( a [ N - 1 ] - a [ N - 2 ] ) ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT maxOps = max ( maxOps , abs ( a [ i ] - a [ i - 1 ] ) + abs ( a [ i ] - a [ i + 1 ] ) - abs ( a [ i - 1 ] - a [ i + 1 ] ) ) NEW_LINE DEDENT print ( totOps - maxOps ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minOperation ( arr , N ) NEW_LINE DEDENT
Count swaps required to sort an array using Insertion Sort | Stores the sorted array elements ; Function to count the number of swaps required to merge two sorted subarray in a sorted form ; Stores the count of swaps ; Function to count the total number of swaps required to sort the array ; Stores the total count of swaps required ; Find the middle index splitting the two halves ; Count the number of swaps required to sort the left subarray ; Count the number of swaps required to sort the right subarray ; Count the number of swaps required to sort the two sorted subarrays ; Driver Code
temp = [ 0 ] * 100000 NEW_LINE def merge ( A , left , mid , right ) : NEW_LINE INDENT swaps = 0 NEW_LINE i , j , k = left , mid , left NEW_LINE while ( i < mid and j <= right ) : NEW_LINE INDENT if ( A [ i ] <= A [ j ] ) : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k , i = k + 1 , i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k , j = k + 1 , j + 1 NEW_LINE swaps += mid - i NEW_LINE DEDENT DEDENT while ( i < mid ) : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k , i = k + 1 , i + 1 NEW_LINE DEDENT while ( j <= right ) : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k , j = k + 1 , j + 1 NEW_LINE DEDENT while ( left <= right ) : NEW_LINE INDENT A [ left ] = temp [ left ] NEW_LINE left += 1 NEW_LINE DEDENT return swaps NEW_LINE DEDENT def mergeInsertionSwap ( A , left , right ) : NEW_LINE INDENT swaps = 0 NEW_LINE if ( left < right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE swaps += mergeInsertionSwap ( A , left , mid ) NEW_LINE swaps += mergeInsertionSwap ( A , mid + 1 , right ) NEW_LINE swaps += merge ( A , left , mid + 1 , right ) NEW_LINE DEDENT return swaps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 1 , 3 , 1 , 2 ] NEW_LINE N = len ( A ) NEW_LINE print ( mergeInsertionSwap ( A , 0 , N - 1 ) ) NEW_LINE DEDENT