codeparrot/xlcost-text-to-code huggingface dataset:
Append digits to the end of duplicate strings to make all strings in an array unique | Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call
def replaceDuplicates ( names ) : NEW_LINE INDENT hash = { } NEW_LINE for i in range ( 0 , len ( names ) ) : NEW_LINE INDENT if names [ i ] not in hash : NEW_LINE INDENT hash [ names [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = hash [ names [ i ] ] NEW_LINE hash [ names [ i ] ] += 1 NEW_LINE names [ i ] += str ( count ) NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( names ) ) : NEW_LINE INDENT print ( names [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = [ " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " ] NEW_LINE replaceDuplicates ( str1 ) NEW_LINE DEDENT
Minimum cost of flipping characters required to convert Binary String to 0 s only | Function to find the minimum cost to convert given string to 0 s only ; Length of string ; Stores the index of leftmost '1' in str ; Update the index of leftmost '1' in str ; Stores the index of rightmost '1' in str ; Update the index of rightmost '1' in str ; If str does not contain any '1' s ; No changes required ; Stores minimum cost ; Iterating through str form left_1 to right_1 ; Stores length of consecutive 0 s ; Calculate length of consecutive 0 s ; If a substring of 0 s exists ; Update minimum cost ; Printing the minimum cost ; Driver Code
def convert_to_allzeroes ( st , a , b ) : NEW_LINE INDENT length = len ( st ) NEW_LINE left_1 = 0 NEW_LINE i = 0 NEW_LINE while ( i < length and st [ i ] == '0' ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT left_1 = i NEW_LINE right_1 = 0 NEW_LINE i = length - 1 NEW_LINE while ( i >= 0 and st [ i ] == '0' ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT right_1 = i NEW_LINE if ( left_1 == length and right_1 == - 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT cost = a NEW_LINE for i in range ( left_1 , right_1 + 1 ) : NEW_LINE INDENT zeroes = 0 NEW_LINE while ( i < length and st [ i ] == '0' ) : NEW_LINE INDENT zeroes += 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( zeroes ) : NEW_LINE INDENT cost += min ( zeroes * b , a ) NEW_LINE DEDENT DEDENT print ( cost ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "01101110" NEW_LINE A = 5 NEW_LINE B = 1 NEW_LINE convert_to_allzeroes ( st , A , B ) NEW_LINE DEDENT
Minimum distance to visit given K points on X | Python3 program to implement the above approach ; Function to find the minimum distance travelled to visit K point ; Stores minimum distance travelled to visit K point ; Stores distance travelled to visit points ; Traverse the array arr [ ] ; If arr [ i ] and arr [ i + K - 1 ] are positive ; Update dist ; Update dist ; Update res ; Driver Code ; Initial the array
import sys NEW_LINE def MinDistK ( arr , N , K ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE dist = 0 NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and arr [ i + K - 1 ] >= 0 ) : NEW_LINE INDENT dist = max ( arr [ i ] , arr [ i + K - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dist = ( abs ( arr [ i ] ) + abs ( arr [ i + K - 1 ] ) + min ( abs ( arr [ i ] ) , abs ( arr [ i + K - 1 ] ) ) ) NEW_LINE DEDENT res = min ( res , dist ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 3 NEW_LINE arr = [ - 30 , - 10 , 10 , 20 , 50 ] NEW_LINE N = len ( arr ) NEW_LINE print ( MinDistK ( arr , N , K ) ) NEW_LINE DEDENT
Maximize the minimum array element by M subarray increments of size S | Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array for M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver Code ; Function Call
def min ( a , n ) : NEW_LINE INDENT minIndex = 0 NEW_LINE minValue = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] < minValue ) : NEW_LINE INDENT minValue = a [ i ] NEW_LINE minIndex = i NEW_LINE DEDENT DEDENT return minIndex NEW_LINE DEDENT def maximizeMin ( A , N , S , M ) : NEW_LINE INDENT minIndex , left , right = 0 , 0 , 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT minIndex = min ( A , N ) NEW_LINE A [ minIndex ] += 1 NEW_LINE left = minIndex - 1 NEW_LINE right = minIndex + 1 NEW_LINE for j in range ( S - 1 ) : NEW_LINE INDENT if ( left == - 1 ) : NEW_LINE INDENT A [ right ] += 1 NEW_LINE right += 1 NEW_LINE DEDENT elif ( right == N ) : NEW_LINE INDENT A [ left ] += 1 NEW_LINE left -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ left ] < A [ right ] ) : NEW_LINE INDENT A [ left ] += 1 NEW_LINE left -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT A [ right ] += 1 NEW_LINE right += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT minIndex = min ( A , N ) NEW_LINE return A [ minIndex ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE S = 2 NEW_LINE M = 3 NEW_LINE print ( maximizeMin ( arr , N , S , M ) ) NEW_LINE DEDENT
Sum of nodes in the path from root to N | Python3 program for the above approach ; Function to find sum of all nodes from root to N ; If N is equal to 1 ; If N is equal to 2 or 3 ; Stores the number of nodes at ( i + 1 ) - th level ; Stores the number of nodes ; Stores if the current level is even or odd ; If level is odd ; If level is even ; If level with node N is reached ; Push into vector ; Compute prefix sums of count of nodes in each level ; Stores the level in which node N s present ; Stores the required sum ; Add temp to the sum ; Driver Code ; Function Call
from bisect import bisect_left , bisect NEW_LINE def sumOfPathNodes ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( N == 2 or N == 3 ) : NEW_LINE INDENT return N + 1 NEW_LINE DEDENT arr = [ ] NEW_LINE arr . append ( 1 ) NEW_LINE k = 1 NEW_LINE flag = True NEW_LINE while ( k < N ) : NEW_LINE INDENT if ( flag == True ) : NEW_LINE INDENT k *= 2 NEW_LINE flag = False NEW_LINE DEDENT else : NEW_LINE INDENT k *= 4 NEW_LINE flag = True NEW_LINE DEDENT if ( k > N ) : NEW_LINE INDENT break NEW_LINE DEDENT arr . append ( k ) NEW_LINE DEDENT lenn = len ( arr ) NEW_LINE prefix = [ 0 ] * ( lenn ) NEW_LINE prefix [ 0 ] = 1 NEW_LINE for i in range ( 1 , lenn ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] + prefix [ i - 1 ] NEW_LINE DEDENT it = bisect_left ( prefix , N ) NEW_LINE ind = it NEW_LINE final_ans = 0 NEW_LINE temp = N NEW_LINE while ( ind > 1 ) : NEW_LINE INDENT val = temp - prefix [ ind - 1 ] NEW_LINE if ( ind % 2 != 0 ) : NEW_LINE INDENT temp = prefix [ ind - 2 ] + ( val + 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT temp = prefix [ ind - 2 ] + ( val + 3 ) // 4 NEW_LINE DEDENT ind -= 1 NEW_LINE final_ans += temp NEW_LINE DEDENT final_ans += ( N + 1 ) NEW_LINE return final_ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 NEW_LINE print ( sumOfPathNodes ( N ) ) NEW_LINE DEDENT
Check if given point lies in range of any of the given towers | Python3 program to implement the above approach ; Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise
from math import sqrt NEW_LINE def checkPointRange ( arr , X , Y , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT dist = sqrt ( ( arr [ i ] [ 0 ] - X ) * ( arr [ i ] [ 0 ] - X ) + ( arr [ i ] [ 1 ] - Y ) * ( arr [ i ] [ 1 ] - Y ) ) NEW_LINE if ( dist <= arr [ i ] [ 2 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 1 , 3 ] , [ 10 , 10 , 3 ] , [ 15 , 15 , 15 ] ] NEW_LINE X = 5 NEW_LINE Y = 5 NEW_LINE N = len ( arr ) NEW_LINE if ( checkPointRange ( arr , X , Y , N ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT
Find the winner of the Game of removing odd or replacing even array elements | Function to evaluate the winner of the game ; Stores count of odd array elements ; Stores count of even array elements ; Traverse the array ; If array element is odd ; Otherwise ; If count of even is zero ; If count of odd is even ; If count of odd is odd ; If count of odd is odd and count of even is one ; Otherwise ; Driver code
def findWinner ( arr , N ) : NEW_LINE INDENT 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 ( even == 0 ) : NEW_LINE INDENT if ( odd % 2 == 0 ) : NEW_LINE INDENT print ( " Player ▁ 2" ) NEW_LINE DEDENT elif ( odd % 2 == 1 ) : NEW_LINE INDENT print ( " Player ▁ 1" ) NEW_LINE DEDENT DEDENT elif ( even == 1 and odd % 2 == 1 ) : NEW_LINE INDENT print ( " Player ▁ 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 9 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findWinner ( arr , N ) NEW_LINE DEDENT
Rearrange array to maximize count of local minima | Function to rearrange array elements to maximize count of local minima in the array ; Sort the array in ascending order ; Stores index of left pointer ; Stores index of right pointer ; Traverse the array elements ; if right is less than N ; Print array element ; Update right ; Print array element ; Update left ; Driver Code
def rearrangeArrMaxcntMinima ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE left = 0 NEW_LINE right = N // 2 NEW_LINE while ( left < N // 2 or right < N ) : NEW_LINE INDENT if ( right < N ) : NEW_LINE INDENT print ( arr [ right ] , end = " ▁ " ) NEW_LINE right += 1 NEW_LINE DEDENT if ( left < N // 2 ) : NEW_LINE INDENT print ( arr [ left ] , end = " ▁ " ) NEW_LINE left += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArrMaxcntMinima ( arr , N ) NEW_LINE DEDENT
Minimum jumps required to make a group of persons sit together | Python3 program for the above approach ; Function to find the minimum jumps required to make the whole group sit adjacently ; Store the indexes ; Stores the count of occupants ; Length of the string ; Traverse the seats ; If current place is occupied ; Push the current position in the vector ; Base Case : ; The index of the median element ; The value of the median element ; Traverse the position [ ] ; Update the ans ; Return the final count ; Driver Code ; Given arrange of seats ; Function Call
MOD = 10 ** 9 + 7 NEW_LINE def minJumps ( seats ) : NEW_LINE INDENT position = [ ] NEW_LINE count = 0 NEW_LINE lenn = len ( seats ) NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( seats [ i ] == ' x ' ) : NEW_LINE INDENT position . append ( i - count ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT if ( count == lenn or count == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT med_index = ( count - 1 ) // 2 NEW_LINE med_val = position [ med_index ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ( position ) ) : NEW_LINE INDENT ans = ( ans % MOD + abs ( position [ i ] - med_val ) % MOD ) % MOD NEW_LINE DEDENT return ans % MOD NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " . . . . x . . xx . . . x . . " NEW_LINE print ( minJumps ( S ) ) NEW_LINE DEDENT
Count pairs whose product contains single distinct prime factor | Function to find a single distinct prime factor of N ; Stores distinct prime factors of N ; Calculate prime factor of N ; Calculate distinct prime factor ; Insert i into disPrimeFact ; Update N ; If N is not equal to 1 ; Insert N into disPrimeFact ; If N contains a single distinct prime factor ; Return single distinct prime factor of N ; If N contains more than one distinct prime factor ; Function to count pairs in the array whose product contains only single distinct prime factor ; Stores count of 1 s in the array ; mp [ i ] : Stores count of array elements whose distinct prime factor is only i ; Traverse the array arr [ ] ; If current element is 1 ; Store distinct prime factor of arr [ i ] ; If arr [ i ] contains more than one prime factor ; If arr [ i ] contains a single prime factor ; Stores the count of pairs whose product of elements contains only a single distinct prime factor ; Traverse the map mp [ ] ; Stores count of array elements whose prime factor is ( it . first ) ; Update res ; Driver Code
def singlePrimeFactor ( N ) : NEW_LINE INDENT disPrimeFact = { } NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT while ( N % i == 0 ) : NEW_LINE INDENT disPrimeFact [ i ] = 1 NEW_LINE N //= i NEW_LINE DEDENT DEDENT if ( N != 1 ) : NEW_LINE INDENT disPrimeFact [ N ] = 1 NEW_LINE DEDENT if ( len ( disPrimeFact ) == 1 ) : NEW_LINE INDENT return list ( disPrimeFact . keys ( ) ) [ 0 ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def cntsingleFactorPair ( arr , N ) : NEW_LINE INDENT countOf1 = 0 NEW_LINE mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT countOf1 += 1 NEW_LINE continue NEW_LINE DEDENT factorValue = singlePrimeFactor ( arr [ i ] ) NEW_LINE if ( factorValue == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT mp [ factorValue ] = mp . get ( factorValue , 0 ) + 1 NEW_LINE DEDENT DEDENT res = 0 NEW_LINE for it in mp : NEW_LINE INDENT X = mp [ it ] NEW_LINE res += countOf1 * X + ( X * ( X - 1 ) ) // 2 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntsingleFactorPair ( arr , N ) ) NEW_LINE DEDENT
Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; if str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1
def MincntBothPalin ( str1 , str2 , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = N - 1 NEW_LINE cntOp = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( str1 [ i ] == str1 [ j ] and str2 [ i ] != str2 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT elif ( str1 [ i ] != str1 [ j ] and str2 [ i ] == str2 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT elif ( str1 [ i ] != str1 [ j ] and str2 [ i ] != str2 [ j ] ) : NEW_LINE INDENT if ( str1 [ i ] == str2 [ j ] and str2 [ i ] == str1 [ j ] ) : NEW_LINE INDENT cntOp += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntOp += 2 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return cntOp NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " dbba " NEW_LINE str2 = " abcd " NEW_LINE N = len ( str1 ) NEW_LINE print ( MincntBothPalin ( str1 , str2 , N ) ) NEW_LINE DEDENT
Find an integral solution of the non | Python3 program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X ; Update xMax ; Stores maximum possible of Y ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code
from math import log2 NEW_LINE def power ( x , N ) : NEW_LINE INDENT res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT res = ( res * x ) NEW_LINE DEDENT x = ( x * x ) NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def findValX_Y ( N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE print ( - 1 ) NEW_LINE return NEW_LINE xMax = 0 NEW_LINE xMax = int ( log2 ( N ) ) NEW_LINE yMax = 0 NEW_LINE yMax = int ( log2 ( N ) / log2 ( 5.0 ) ) NEW_LINE for i in range ( 1 , xMax + 1 ) : NEW_LINE INDENT for j in range ( 1 , yMax + 1 ) : NEW_LINE INDENT a = power ( 2 , i ) NEW_LINE b = power ( 5 , j ) NEW_LINE if ( a + b == N ) : NEW_LINE INDENT print ( i , j ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 129 NEW_LINE findValX_Y ( N ) NEW_LINE DEDENT
Count pairs from two arrays with difference exceeding K | set 2 | Python3 program for the above approach ; Function to count pairs that satisfy the given conditions ; Stores the count of pairs ; If v1 [ ] is smaller than v2 [ ] ; Sort the array v1 [ ] ; Traverse the array v2 [ ] ; Returns the address of the first number which is >= v2 [ j ] - k ; Increase the count by all numbers less than v2 [ j ] - k ; Otherwise ; Sort the array v2 [ ] ; Traverse the array v1 [ ] ; Returns the address of the first number which is > v1 [ i ] + k ; Increase the count by all numbers greater than v1 [ i ] + k ; Return the total count of pairs ; Driver Code
from bisect import bisect_left , bisect_right NEW_LINE def countPairs ( v1 , v2 , n , m , k ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n <= m ) : NEW_LINE INDENT v1 = sorted ( v1 ) NEW_LINE for j in range ( m ) : NEW_LINE INDENT index = bisect_left ( v1 , v2 [ j ] - k ) NEW_LINE count += index NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT v2 = sorted ( v2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT index = bisect_right ( v2 , v1 [ i ] + k ) NEW_LINE count += m - index NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 9 , 1 , 8 ] NEW_LINE brr = [ 10 , 12 , 7 , 4 , 2 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE print ( countPairs ( arr , brr , N , M , K ) ) NEW_LINE DEDENT
Maximum sum subarray of size K with sum less than X | Function to calculate maximum sum among all subarrays of size K with the sum less than X ; Initialize sum_K to 0 ; Calculate sum of first K elements ; If sum_K is less than X ; Initialize MaxSum with sum_K ; Iterate over the array from ( K + 1 ) - th index ; Subtract the first element from the previous K elements and add the next element ; If sum_K is less than X ; Update the Max_Sum ; Driver Code ; Size of Array ; Function Call
def maxSumSubarr ( A , N , K , X ) : NEW_LINE INDENT sum_K = 0 NEW_LINE for i in range ( 0 , K ) : NEW_LINE INDENT sum_K += A [ i ] NEW_LINE DEDENT Max_Sum = 0 NEW_LINE if ( sum_K < X ) : NEW_LINE INDENT Max_Sum = sum_K NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT sum_K -= ( A [ i - K ] - A [ i ] ) NEW_LINE if ( sum_K < X ) : NEW_LINE INDENT Max_Sum = max ( Max_Sum , sum_K ) NEW_LINE DEDENT DEDENT print ( Max_Sum ) NEW_LINE DEDENT arr = [ - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 ] NEW_LINE K = 5 NEW_LINE X = 30 NEW_LINE N = len ( arr ) NEW_LINE maxSumSubarr ( arr , N , K , X ) NEW_LINE
Rearrange array to make sum of all subarrays starting from first index non | Function to rearrange the array such that sum of all elements of subarrays from the 1 st index is non - zero ; Initialize sum of subarrays ; Sum of all elements of array ; If sum is 0 , the required array could never be formed ; If sum is non zero , array might be formed ; Sort array in ascending order ; When current subarray sum becomes 0 replace it with the largest element ; Swap Operation ; If largest element is same as element to be replaced , then rearrangement impossible ; If b = 1 , then rearrangement is not possible . Hence check with reverse configuration ; Sort array in descending order ; When current subarray sum becomes 0 replace it with the smallest element ; Swap Operation ; If smallest element is same as element to be replaced , then rearrangement impossible ; If neither of the configurations worked then pr " - 1" ; Otherwise , print the formed rearrangement ; Driver Code ; Given array ; Size of array ; Function Call
def rearrangeArray ( a , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT sum = 0 NEW_LINE b = 0 NEW_LINE a = sorted ( a ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT if ( a [ i ] != a [ N - 1 ] ) : NEW_LINE INDENT sum -= a [ i ] NEW_LINE a [ i ] , a [ N - 1 ] = a [ N - 1 ] , a [ i ] NEW_LINE sum += a [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT b = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( b == 1 ) : NEW_LINE INDENT b = 0 NEW_LINE sum = 0 NEW_LINE a = sorted ( a ) NEW_LINE a = a [ : : - 1 ] NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT if ( a [ i ] != a [ 0 ] ) : NEW_LINE INDENT sum -= a [ i ] NEW_LINE a [ i ] , a [ 0 ] = a [ 0 ] , a [ i ] NEW_LINE sum += a [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT b = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( b == 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , 2 , 4 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArray ( arr , N ) NEW_LINE DEDENT