codeparrot/xlcost-text-to-code huggingface dataset:
Longest increasing sequence by the boundary elements of an Array | Python3 program to print the longest increasing subsequence from the boundary elements of an array ; Function to return the length of Longest Increasing subsequence ; Set pointers at both ends ; Stores the recent value added to the subsequence ; Stores the length of the subsequence ; Check if both elements can be added to the subsequence ; Check if the element on the left can be added to the subsequence only ; Check if the element on the right can be added to the subsequence only ; If none of the values can be added to the subsequence ; Driver code ; Length of array
import sys NEW_LINE def longestSequence ( n , arr ) : NEW_LINE INDENT l = 0 NEW_LINE r = n - 1 NEW_LINE prev = - sys . maxsize - 1 NEW_LINE ans = 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT if ( arr [ l ] > prev and arr [ r ] > prev ) : NEW_LINE INDENT if ( arr [ l ] < arr [ r ] ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ r ] NEW_LINE r -= 1 NEW_LINE DEDENT DEDENT elif ( arr [ l ] > prev ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT elif ( arr [ r ] > prev ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = arr [ r ] NEW_LINE r -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 5 , 1 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestSequence ( n , arr ) ) NEW_LINE
Total number of cells covered in a matrix after D days | Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Driver Code ; Dimensions of cell ; Starting Coordinates ; Number of Days ; Function Call
def solve ( n , m , x , y , d ) : NEW_LINE INDENT top = min ( d , x - 1 ) NEW_LINE down = min ( d , n - x ) NEW_LINE left = min ( d , y - 1 ) NEW_LINE right = min ( d , m - y ) NEW_LINE quad1 = top * left NEW_LINE quad2 = left * down NEW_LINE quad3 = down * right NEW_LINE quad4 = right * top NEW_LINE totalsq = ( quad1 + quad2 + quad3 + quad4 ) NEW_LINE singleBlocks = ( top + down + left + right + 1 ) NEW_LINE return totalsq + singleBlocks NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE m = 10 NEW_LINE x = 7 NEW_LINE y = 8 NEW_LINE d = 4 NEW_LINE d -= 1 NEW_LINE print ( solve ( n , m , x , y , d ) ) NEW_LINE DEDENT
Smallest subsequence with sum of absolute difference of consecutive elements maximized | Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of final subsequence ; Print the subsequence ; Driver code
def getSubsequence ( arr , n ) : NEW_LINE INDENT req = [ ] NEW_LINE req . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] and arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT req . append ( arr [ i ] ) NEW_LINE DEDENT elif ( arr [ i ] < arr [ i + 1 ] and arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT req . append ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT req . append ( arr [ n - 1 ] ) ; NEW_LINE print ( len ( req ) ) NEW_LINE for x in req : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 6 , 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE getSubsequence ( arr , n ) NEW_LINE DEDENT
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Python3 implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Driver Code ; Function Call
M = 3 NEW_LINE N = 3 NEW_LINE def minchanges ( mat ) : NEW_LINE INDENT count = 0 NEW_LINE left = 0 NEW_LINE right = N + M - 2 NEW_LINE while ( left < right ) : NEW_LINE INDENT mp = { } NEW_LINE totalsize = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( i + j == left ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = NEW_LINE mp . get ( mat [ i ] [ j ] , 0 ) + 1 NEW_LINE totalsize += 1 NEW_LINE DEDENT elif ( i + j == right ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = NEW_LINE mp . get ( mat [ i ] [ j ] , 0 ) + 1 NEW_LINE totalsize += 1 NEW_LINE DEDENT DEDENT DEDENT changes = 0 NEW_LINE for itr in mp : NEW_LINE INDENT changes = max ( changes , mp [ itr ] ) NEW_LINE DEDENT count += totalsize - changes NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 4 , 1 ] , [ 2 , 5 , 3 ] , [ 1 , 3 , 1 ] ] NEW_LINE print ( minchanges ( mat ) ) NEW_LINE DEDENT
Count of longest possible subarrays with sum not divisible by K | Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code
def CountLongestSubarrays ( arr , n , k ) : NEW_LINE INDENT s = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += arr [ i ] NEW_LINE DEDENT if ( s % k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT ini = 0 NEW_LINE while ( ini < n and arr [ ini ] % k == 0 ) : NEW_LINE INDENT ini += 1 NEW_LINE DEDENT final = n - 1 NEW_LINE while ( final >= 0 and arr [ final ] % k == 0 ) : NEW_LINE INDENT final -= 1 NEW_LINE DEDENT sum , count = 0 , 0 NEW_LINE if ( ini == n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT length = max ( n - 1 - ini , final ) NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % k != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for i in range ( length , n ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE sum = sum + arr [ i - length ] NEW_LINE if ( sum % k != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( CountLongestSubarrays ( arr , n , k ) ) NEW_LINE DEDENT
Count of triplets in a given Array having GCD K | Python3 program to count the number of triplets in the array with GCD equal to K ; Frequency array ; mul [ i ] stores the count of multiples of i ; cnt [ i ] stores the count of triplets with gcd = i ; Return nC3 ; Function to count and return the number of triplets in the array with GCD equal to K ; Store frequency of array elements ; Store the multiples of i present in the array ; Count triplets with gcd equal to any multiple of i ; Remove all triplets which have gcd equal to a multiple of i ; Driver Code
MAXN = int ( 1e6 + 1 ) NEW_LINE freq = [ 0 ] * MAXN NEW_LINE mul = [ 0 ] * MAXN NEW_LINE cnt = [ 0 ] * MAXN NEW_LINE def nC3 ( n ) : NEW_LINE INDENT if ( n < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 NEW_LINE DEDENT def count_triplet ( arr , N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , 1000000 + 1 ) : NEW_LINE INDENT for j in range ( i , 1000000 + 1 , i ) : NEW_LINE INDENT mul [ i ] += freq [ j ] NEW_LINE DEDENT cnt [ i ] = nC3 ( mul [ i ] ) NEW_LINE DEDENT for i in range ( 1000000 , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 2 * i , 1000000 + 1 , i ) : NEW_LINE INDENT cnt [ i ] -= cnt [ j ] NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ triplets ▁ with ▁ GCD " " ▁ { 0 } ▁ are ▁ { 1 } " . format ( K , int ( cnt [ K ] ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 7 , 12 , 6 , 15 , 9 ] NEW_LINE N = 6 NEW_LINE K = 3 NEW_LINE count_triplet ( arr , N , K ) NEW_LINE DEDENT
Check if a subsequence of length K with odd sum exists | Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd element exists or no even element exists when K even ; Subsequence is not possible ; Otherwise possible ; Driver code
def isSubseqPossible ( arr , N , K ) : NEW_LINE INDENT i = 0 NEW_LINE odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( odd == 0 or ( even == 0 and K % 2 == 0 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 7 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( " Yes " if isSubseqPossible ( arr , N , K ) else " No " ) NEW_LINE DEDENT
Path traversed using exactly M coins in K jumps | Function that pr the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver code ; Function call
def print_path ( N , jump , coin ) : NEW_LINE INDENT if ( jump > coin or jump * ( N - 1 ) < coin ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT pos = 1 ; NEW_LINE while ( jump > 0 ) : NEW_LINE INDENT tmp = min ( N - 1 , coin - ( jump - 1 ) ) ; NEW_LINE if ( pos + tmp <= N ) : NEW_LINE INDENT pos += tmp ; NEW_LINE DEDENT else : NEW_LINE INDENT pos -= tmp ; NEW_LINE DEDENT print ( pos , end = " ▁ " ) NEW_LINE coin -= tmp ; NEW_LINE jump -= 1 ; NEW_LINE DEDENT DEDENT DEDENT N = 5 NEW_LINE K = 4 NEW_LINE M = 12 NEW_LINE print_path ( N , K , M ) ; NEW_LINE
Minimum salary hike for each employee such that no employee feels unfair | Python3 program for the above approach ; Function that print minimum hike ; Insert INF at begin and end of array ; To store hike of each employee ; For Type 1 employee ; For Type 2 employee ; For Type 3 employee ; For Type 4 employee ; Print the min hike for each employee ; Driver Code ; Given array of rating of employees ; Function Call
INF = 1e9 NEW_LINE def findMinHike ( arr , n ) : NEW_LINE INDENT arr . insert ( 0 , INF ) NEW_LINE arr . append ( INF ) NEW_LINE hike = [ 0 ] * ( n + 2 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] >= arr [ i ] and arr [ i ] <= arr [ i + 1 ] ) : NEW_LINE INDENT hike [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] < arr [ i ] and arr [ i ] <= arr [ i + 1 ] ) : NEW_LINE INDENT hike [ i ] = hike [ i - 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] >= arr [ i ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT hike [ i ] = hike [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] < arr [ i ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT hike [ i ] = max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( hike [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 4 , 2 , 1 , 6 ] NEW_LINE findMinHike ( arr , len ( arr ) ) NEW_LINE DEDENT
Minimize the cost to make all the adjacent elements distinct in an Array | Function that prints minimum cost required ; Dp - table ; Base case Not increasing the first element ; Increasing the first element by 1 ; Increasing the first element by 2 ; Condition if current element is not equal to previous non - increased element ; Condition if current element is not equal to previous element after being increased by 1 ; Condition if current element is not equal to previous element after being increased by 2 ; Take the minimum from all cases ; Finding the minimum cost ; Printing the minimum cost required to make all adjacent elements distinct ; Driver Code
def minimumCost ( arr , cost , N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 3 ) ] for i in range ( N ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE dp [ 0 ] [ 1 ] = cost [ 0 ] NEW_LINE dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT minimum = 1e6 NEW_LINE if ( j + arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT minimum = min ( minimum , dp [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) : NEW_LINE INDENT minimum = min ( minimum , dp [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) : NEW_LINE INDENT minimum = min ( minimum , dp [ i - 1 ] [ 2 ] ) NEW_LINE DEDENT dp [ i ] [ j ] = j * cost [ i ] + minimum NEW_LINE DEDENT DEDENT ans = 1e6 NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT ans = min ( ans , dp [ N - 1 ] [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 3 , 4 ] NEW_LINE cost = [ 3 , 2 , 5 , 4 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minimumCost ( arr , cost , N ) NEW_LINE DEDENT
Subarray with largest sum after excluding its maximum element | Function to find the maximum sum subarray by excluding the maximum element from the array ; Loop to store all the positive elements in the map ; Loop to iterating over the map and considering as the maximum element of the current including subarray ; Make the current element maximum ; Iterate through array and apply kadane 's algorithm ; Condition if current element is greater than mx then make the element - infinity ; Store the indices in some variable ; Driver Code ; Function Call
def maximumSumSubarray ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and arr [ i ] not in mp ) : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT first = 0 NEW_LINE last = 0 NEW_LINE ans = 0 NEW_LINE INF = 1e6 NEW_LINE for i in mp : NEW_LINE INDENT mx = i NEW_LINE curr = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( curr == 0 ) : NEW_LINE INDENT curr_start = j NEW_LINE DEDENT if arr [ j ] > mx : NEW_LINE INDENT val = - INF NEW_LINE DEDENT else : NEW_LINE INDENT val = arr [ j ] ; NEW_LINE DEDENT curr += val NEW_LINE if ( curr < 0 ) : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT if ( curr > ans ) : NEW_LINE INDENT ans = curr NEW_LINE first = curr_start NEW_LINE last = j NEW_LINE DEDENT DEDENT DEDENT print ( first + 1 , last + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , - 2 , 10 , - 1 , 4 ] NEW_LINE size = len ( arr ) NEW_LINE maximumSumSubarray ( arr , size ) NEW_LINE DEDENT
Minimum insertions to make XOR of an Array equal to half of its sum | Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver code
def make_xor_half ( arr ) : NEW_LINE INDENT sum = 0 ; xr = 0 ; NEW_LINE for a in arr : NEW_LINE INDENT sum += a ; NEW_LINE xr ^= a ; NEW_LINE DEDENT if ( 2 * xr == sum ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( xr == 0 ) : NEW_LINE INDENT arr . append ( sum ) ; NEW_LINE return 1 ; NEW_LINE DEDENT arr . append ( xr ) ; NEW_LINE arr . append ( sum + xr ) ; NEW_LINE return 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 ; NEW_LINE nums = [ 3 , 4 , 7 , 1 , 2 , 5 , 6 ] ; NEW_LINE count = make_xor_half ( nums ) ; NEW_LINE if ( count == - 1 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT elif ( count == 1 ) : NEW_LINE INDENT print ( nums [ N ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( nums [ N ] , nums [ N + 1 ] ) ; NEW_LINE DEDENT DEDENT
Count number of triangles possible for the given sides range | Function to count the number of possible triangles for the given sides ranges ; Iterate for every possible of x ; Range of y is [ b , c ] From this range First we will find the number of x + y greater than d ; For x + y greater than d we can choose all z from [ c , d ] Total permutation will be ; Now we will find the number of x + y in between the [ c , d ] ; [ l , r ] will be the range from total [ c , d ] x + y belongs For any r such that r = x + y We can choose z , in the range [ c , d ] only less than r , Thus total permutation be ; Driver Code
def count_triangles ( a , b , c , d ) : NEW_LINE INDENT ans = 0 NEW_LINE for x in range ( a , b + 1 ) : NEW_LINE INDENT num_greater_than_d = ( max ( d , c + x ) - max ( d , b + x - 1 ) ) NEW_LINE ans = ( ans + num_greater_than_d * ( d - c + 1 ) ) NEW_LINE r = min ( max ( c , c + x ) , d ) - c ; NEW_LINE l = min ( max ( c , b + x - 1 ) , d ) - c ; NEW_LINE x1 = int ( ( r * ( r + 1 ) ) / 2 ) NEW_LINE x2 = int ( ( l * ( l + 1 ) ) / 2 ) NEW_LINE ans = ans + ( x1 - x2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = 2 NEW_LINE b = 3 NEW_LINE c = 4 NEW_LINE d = 5 NEW_LINE print ( count_triangles ( a , b , c , d ) , end =   ' ' ) NEW_LINE
Minimum number of swaps required to make the string K periodic | Python3 code to find the minimum number of swaps required to make the string K periodic ; Mark all allowed characters as true ; Initialize the freq array to 0 ; Increase the frequency of each character ; Total number of periods of size K in the string ; Check if the current character is present in allowed and whether the current frequency is greater than all previous frequencies for this position ; Update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the string apart from the n / k characters then add 1. ; Driver code
def minFlip ( s , n , k , a , p ) : NEW_LINE INDENT allowed = [ 0 ] * 26 NEW_LINE for i in range ( p ) : NEW_LINE INDENT allowed [ ord ( a [ i ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT freq = [ [ 0 for x in range ( 26 ) ] for y in range ( k ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT freq [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT freq [ i % k ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE totalpositions = n // k NEW_LINE for i in range ( k ) : NEW_LINE INDENT maxfrequency = 0 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] [ j ] > maxfrequency and allowed [ j ] == True ) : NEW_LINE INDENT maxfrequency = freq [ i ] [ j ] NEW_LINE DEDENT DEDENT ans += ( totalpositions - maxfrequency ) NEW_LINE if ( i % k < n % k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " nihsiakyt " NEW_LINE n = len ( S ) NEW_LINE K = 3 NEW_LINE A = [ ' n ' , ' i ' , ' p ' , ' s ' , ' q ' ] NEW_LINE p = len ( A ) NEW_LINE minFlip ( S , n , K , A , p ) NEW_LINE DEDENT
Minimum number of colors required to color a Circular Array | Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call
def colorRequired ( arr , n ) : NEW_LINE INDENT all_same = True NEW_LINE one_adjacent_same = False NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT all_same = False NEW_LINE DEDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT one_adjacent_same = True NEW_LINE DEDENT DEDENT if ( all_same == True ) : NEW_LINE INDENT print ( 1 ) NEW_LINE return NEW_LINE DEDENT if ( n % 2 == 0 or one_adjacent_same == True ) : NEW_LINE INDENT print ( 2 ) NEW_LINE return NEW_LINE DEDENT print ( 3 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE colorRequired ( arr , n ) NEW_LINE DEDENT
Maximize the sum of sum of the Array by removing end elements | Function to find the maximum sum of sum ; Compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; Remove the left end element ; If the right end element is smaller than left end ; Remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code
def maxRemainingSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT i = 0 NEW_LINE j = n - 1 NEW_LINE result = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] ) : NEW_LINE INDENT sum -= arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum -= arr [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT result += sum ; NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 1 , 7 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxRemainingSum ( arr , N ) ) NEW_LINE
Split a number as sum of K numbers which are not divisible by K | Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code
def printKParts ( N , K ) : NEW_LINE INDENT if ( N % K == 0 ) : NEW_LINE INDENT for i in range ( 1 , K ) : NEW_LINE INDENT print ( "1 , ▁ " ) ; NEW_LINE DEDENT print ( N - ( K - 1 ) , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( K == 2 ) : NEW_LINE INDENT print ( " Not ▁ Possible " , end = " " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT for i in range ( 1 , K - 1 ) : NEW_LINE INDENT print ( 1 , end = " , ▁ " ) ; NEW_LINE DEDENT print ( 2 , " , ▁ " , ( N - K ) , end = " " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 18 ; NEW_LINE K = 5 ; NEW_LINE printKParts ( N , K ) ; NEW_LINE DEDENT
Make the intervals non | Function to assign the intervals to two different processors ; Loop to pair the interval with their indices ; sorting the interval by their startb times ; Loop to iterate over the intervals with their start time ; Condition to check if there is a possible solution ; form = ''.join(form) ; Driver Code ; Function Call
def assignIntervals ( interval , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT interval [ i ] . append ( i ) NEW_LINE DEDENT interval . sort ( key = lambda x : x [ 0 ] ) NEW_LINE firstEndTime = - 1 NEW_LINE secondEndTime = - 1 NEW_LINE fin = ' ' NEW_LINE flag = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if interval [ i ] [ 0 ] >= firstEndTime : NEW_LINE INDENT firstEndTime = interval [ i ] [ 1 ] NEW_LINE interval [ i ] . append ( ' S ' ) NEW_LINE DEDENT elif interval [ i ] [ 0 ] >= secondEndTime : NEW_LINE INDENT secondEndTime = interval [ i ] [ 1 ] NEW_LINE interval [ i ] . append ( ' F ' ) NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if flag : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT form = [ ' ' ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT indi = interval [ i ] [ 2 ] NEW_LINE form [ indi ] = interval [ i ] [ 3 ] NEW_LINE DEDENT print ( form , " , ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT intervals = [ [ 360 , 480 ] , [ 420 , 540 ] , [ 600 , 660 ] ] NEW_LINE assignIntervals ( intervals , len ( intervals ) ) NEW_LINE DEDENT