codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the count of adjacent element pairs with even sum by rearranging the Array | Function to find maximum count pair of adjacent elements with even sum ; Stores count of odd numbers ; Stores count of even numbers ; Traverse the array arr [ ] ; If arr [ i ] % 2 is 1 ; Else ; If odd and even both are greater than 0 ; Otherwise ; Driver Code
def maximumCount ( 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 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( odd and even ) : NEW_LINE INDENT return N - 2 NEW_LINE DEDENT else : NEW_LINE INDENT return N - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumCount ( arr , N ) ) NEW_LINE DEDENT
Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code
def minPossibleValue ( N , K , X ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( X <= K ) : NEW_LINE INDENT X = X * 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT X = X + K ; NEW_LINE DEDENT DEDENT return X ; NEW_LINE DEDENT N = 7 ; NEW_LINE K = 4 ; NEW_LINE X = 1 ; NEW_LINE print ( minPossibleValue ( N , K , X ) ) ; NEW_LINE
Smallest number that can replace all | Python 3 program for the above approach ; Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code
import sys NEW_LINE def findMissingValue ( arr , N ) : NEW_LINE INDENT minE = sys . maxsize NEW_LINE maxE = - sys . maxsize - 1 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] == - 1 and arr [ i + 1 ] != - 1 ) : NEW_LINE INDENT minE = min ( minE , arr [ i + 1 ] ) NEW_LINE maxE = max ( maxE , arr [ i + 1 ] ) NEW_LINE DEDENT if ( arr [ i ] != - 1 and arr [ i + 1 ] == - 1 ) : NEW_LINE INDENT minE = min ( minE , arr [ i ] ) NEW_LINE maxE = max ( maxE , arr [ i ] ) NEW_LINE DEDENT DEDENT if ( minE == sys . maxsize and maxE == - sys . maxsize - 1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( minE + maxE ) // 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , - 1 , - 1 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE findMissingValue ( arr , N ) NEW_LINE DEDENT
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times | Function to find the last element after performing given operations ; Length of the array ; Increment j until condition is satisfied ; In each pair every value is repeating r number of times ; Print the result according to the value of k ; Driver Code ; Given K ; Given arr [ ] ; Function call
def findLastElement ( N , A ) : NEW_LINE INDENT l = len ( A ) NEW_LINE j = 0 NEW_LINE while ( N > l * ( 2 ** j ) ) : NEW_LINE INDENT N = N - l * 2 ** j NEW_LINE j += 1 NEW_LINE DEDENT k = 1 NEW_LINE r = 2 ** j NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT if N > r * i : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( A ) ) : NEW_LINE INDENT if ( i + 1 == k ) : NEW_LINE INDENT print ( A [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 7 NEW_LINE A = [ 1 , 2 , 3 ] NEW_LINE findLastElement ( K , A ) NEW_LINE DEDENT
Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def findMaxD ( arr , N , K ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT arr [ i ] = abs ( arr [ i ] - K ) NEW_LINE DEDENT D = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT D = gcd ( D , arr [ i ] ) NEW_LINE DEDENT return D NEW_LINE DEDENT arr = [ 1 , 7 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( findMaxD ( arr , N , K ) ) NEW_LINE
Maximize the number of times a character can be removed from substring 01 from given Binary String | Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Driver code ; Input ; Function call
def maxOperations ( S , N ) : NEW_LINE INDENT X = 0 NEW_LINE Y = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT break NEW_LINE DEDENT Y += 1 NEW_LINE DEDENT i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT break NEW_LINE DEDENT X += 1 NEW_LINE DEDENT if ( N == X + Y ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return N - ( X + Y ) - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "001111" NEW_LINE N = len ( S ) NEW_LINE print ( maxOperations ( S , N ) ) NEW_LINE DEDENT
Maximize frequency sum of K chosen characters from given string | Function to find the maximum sum of frequencies of the exactly K chosen characters from the string S ; Stores the resultant maximum sum ; Stores the frequency of array elements ; Find the frequency of character ; Sort the frequency array in the descending order ; Iterate to choose K elements greedily ; If the freq [ i ] cards are chosen ; K cards have been picked ; Return the resultant sum ; Driver Code
def maximumSum ( S , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE freq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ ord ( S [ i ] ) ] += 1 NEW_LINE DEDENT freq = sorted ( freq ) [ : : - 1 ] NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT if ( K > freq [ i ] ) : NEW_LINE INDENT sum += freq [ i ] * freq [ i ] NEW_LINE K -= freq [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sum += freq [ i ] * K NEW_LINE break NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " GEEKSFORGEEKS " NEW_LINE K = 10 NEW_LINE N = len ( S ) NEW_LINE print ( maximumSum ( S , N , K ) ) NEW_LINE DEDENT
Count of N | Python3 program for the above approach ; Function to count N - digit numbers having absolute difference between adjacent digits in non - increasing order ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If the current digit is 1 , then any digit from [ 1 - 9 ] can be placed ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit i can be placed which satisfies abs ( prev1 - i ) <= abs ( prev1 - prev2 ) ; If absolute difference is less than or equal to diff ; Function to count N - digit numbers with absolute difference between adjacent digits in non increasing order ; Initialize dp table with - 1 ; Function Call ; Driver code
dp = [ [ [ 0 for i in range ( 10 ) ] for col in range ( 10 ) ] for row in range ( 100 ) ] NEW_LINE def countOfNumbers ( digit , prev1 , prev2 , n ) : NEW_LINE INDENT if ( digit == n + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ digit ] [ prev1 ] [ prev2 ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT i = 1 NEW_LINE if n == 1 : NEW_LINE INDENT i = 0 NEW_LINE DEDENT for j in range ( i , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , j , prev1 , n ) NEW_LINE DEDENT DEDENT elif ( digit == 2 ) : NEW_LINE INDENT for i in range ( 0 , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT diff = abs ( prev2 - prev1 ) NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT if ( abs ( prev1 - i ) <= diff ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT DEDENT return val NEW_LINE DEDENT def countNumbersUtil ( N ) : NEW_LINE INDENT for i in range ( 0 , 100 ) : NEW_LINE INDENT for j in range ( 0 , 10 ) : NEW_LINE INDENT for k in range ( 0 , 10 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = - 1 NEW_LINE DEDENT DEDENT DEDENT print ( countOfNumbers ( 1 , 0 , 0 , N ) ) NEW_LINE DEDENT N = 3 NEW_LINE countNumbersUtil ( N ) NEW_LINE
Print all numbers that can be obtained by adding A or B to N exactly M times | Function to find all possible numbers that can be obtained by adding A or B to N exactly N times ; If number of steps is 0 and only possible number is N ; Add A to N and make a recursive call for M - 1 steps ; Add B to N and make a recursive call for M - 1 steps . ; Driver Code ; Given Inputs ; Stores all possible numbers ; Function call ; Print all possible numbers
def possibleNumbers ( numbers , N , M , A , B ) : NEW_LINE INDENT if ( M == 0 ) : NEW_LINE INDENT numbers . add ( N ) NEW_LINE return NEW_LINE DEDENT possibleNumbers ( numbers , N + A , M - 1 , A , B ) NEW_LINE possibleNumbers ( numbers , N + B , M - 1 , A , B ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 3 NEW_LINE A = 4 NEW_LINE B = 6 NEW_LINE numbers = set ( ) NEW_LINE possibleNumbers ( numbers , N , M , A , B ) NEW_LINE for x in numbers : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Maximum sum of array after removing a positive or negative subarray | python 3 program for the above approach ; Function to find the maximum sum of array after removing either the contiguous positive or negative elements ; Store the total sum of array ; Store the maximum contiguous negative sum ; Store the sum of current contiguous negative elements ; Store the minimum element of array ; Traverse the array , arr [ ] ; Update the overall sum ; Store minimum element of array ; If arr [ i ] is positive ; Update temp_sum to 0 ; Add arr [ i ] to temp_sum ; Update max_neg ; If no negative element in array then remove smallest positive element ; Print the required sum ; Driver Code ; Given Input ; Function Call
import sys NEW_LINE def maxSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE max_neg = sys . maxsize NEW_LINE tempsum = 0 NEW_LINE small = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE small = min ( small , arr [ i ] ) NEW_LINE if ( arr [ i ] > 0 ) : NEW_LINE INDENT tempsum = 0 NEW_LINE DEDENT else : NEW_LINE INDENT tempsum += arr [ i ] NEW_LINE DEDENT max_neg = min ( max_neg , tempsum ) NEW_LINE DEDENT if ( max_neg == 0 ) : NEW_LINE INDENT max_neg = small NEW_LINE DEDENT print ( sum - max_neg ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE n = len ( arr ) NEW_LINE maxSum ( arr , n ) NEW_LINE DEDENT
Count characters of a string which when removed individually makes the string equal to another string | Function to count characters from A whose removal makes the strings A and B equal ; Stores the index of the longest prefix ; Stores the index of the longest suffix ; Traverse the B ; Traverse the B ; If N - M is equal to 1 and Y is less than or equal to X ; Print count of characters ; Print positions of the characters ; Otherwise ; Driver Code
def RemoveOneChar ( A , B , N , M ) : NEW_LINE INDENT X = 0 NEW_LINE Y = N - 1 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( A [ X ] != B [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT X += 1 NEW_LINE DEDENT for i in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ Y ] != B [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT Y -= 1 NEW_LINE DEDENT if ( N - M == 1 and Y < X ) : NEW_LINE INDENT print ( X - Y + 1 ) NEW_LINE for i in range ( Y , X + 1 ) : NEW_LINE INDENT print ( i + 1 , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " abaac " NEW_LINE B = " abac " NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE RemoveOneChar ( A , B , N , M ) NEW_LINE DEDENT
Minimum flips or swapping of adjacent characters required to make a string equal to another | Function to find minimum operations required to convert A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call
def minimumOperation ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE i = 0 NEW_LINE minoperation = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] == b [ i ] ) : NEW_LINE INDENT i = i + 1 NEW_LINE continue NEW_LINE DEDENT elif ( a [ i ] == b [ i + 1 ] and a [ i + 1 ] == b [ i ] and i < n - 1 ) : NEW_LINE INDENT minoperation += 1 NEW_LINE i = i + 2 NEW_LINE DEDENT elif ( a [ i ] != b [ i ] ) : NEW_LINE INDENT minoperation += 1 NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT print ( minoperation ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = "10010010" NEW_LINE b = "00001000" NEW_LINE minimumOperation ( a , b ) NEW_LINE DEDENT
Minimum replacements required to make sum of all K | Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Driver Code ; Given Input ; Function Call
def findMinOperations ( arr , N , K ) : NEW_LINE INDENT operations = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT freq = { } NEW_LINE for j in range ( i , N , K ) : NEW_LINE INDENT if arr [ j ] in freq : NEW_LINE INDENT freq [ arr [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ arr [ j ] ] = 1 NEW_LINE DEDENT DEDENT max1 = 0 NEW_LINE num = 0 NEW_LINE for key , value in freq . items ( ) : NEW_LINE INDENT if ( value > max1 ) : NEW_LINE INDENT max1 = value NEW_LINE num = key NEW_LINE DEDENT DEDENT for key , value in freq . items ( ) : NEW_LINE INDENT if ( key != num ) : NEW_LINE INDENT operations += value NEW_LINE DEDENT DEDENT DEDENT print ( operations ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 3 , 5 , 6 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE findMinOperations ( arr , N , K ) NEW_LINE DEDENT
Count of pairs of integers up to X and Y that generates equal Quotient and Remainder | python 3 Program for the above approach ; Function to calculate the number of pairs satisfying ( m / n = m % n ) ; Iterate from 1 to sqrt ( x ) ; Combining the conditions - 1 ) n > k 2 ) n <= y 3 ) n <= ( x / k - 1 ) ; Driver code
from math import sqrt NEW_LINE def countOfPairs ( x , y ) : NEW_LINE INDENT count = 0 NEW_LINE for k in range ( 1 , int ( sqrt ( x ) ) + 1 , 1 ) : NEW_LINE INDENT count += max ( 0 , min ( y , x / k - 1 ) - k ) NEW_LINE DEDENT print ( int ( count ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 4 NEW_LINE y = 5 NEW_LINE countOfPairs ( x , y ) NEW_LINE DEDENT
Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square | python program for the above approach ; Function to count total number of permutation of the first N natural number having the sum of adjacent elements as perfect square ; Create an adjacency matrix ; bCount elements whose indegree bis 1 ; bGenerate adjacency matrix ; Find the sum of i and j ; If sum is perfect square . then move from i to j ; Add it in adjacency list of i ; If any list is of size 1 , then the indegree is 1 ; If there is no element whose indegree is 1 , then N such permutations are possible ; If there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; If there are more than 2 elements whose indegree is 1 , then return 0 ; Driver Code
from math import sqrt , floor , ceil NEW_LINE def countPermutations ( N ) : NEW_LINE INDENT adj = [ [ ] for i in range ( 105 ) ] NEW_LINE indeg = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT sum = i + j NEW_LINE if ( ceil ( sqrt ( sum ) ) == floor ( sqrt ( sum ) ) ) : NEW_LINE INDENT adj [ i ] . append ( j ) NEW_LINE DEDENT DEDENT if ( len ( adj [ i ] ) == 1 ) : NEW_LINE INDENT indeg += 1 NEW_LINE DEDENT DEDENT if ( indeg == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT elif ( indeg <= 2 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 17 NEW_LINE print ( countPermutations ( N ) ) NEW_LINE DEDENT
Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | Function to construct the lst with exactly K unique adjacent element differences ; Stores the resultant array ; Stores the left and the right most element of the range ; Traverse the array ; If k is even , the add left to array and increment the left ; If k is odd , the add right to array and decrement the right ; Repeat the steps for k - 1 times ; Print the resultant lst ; Driver Code
def makelst ( N , K ) : NEW_LINE INDENT lst = [ 0 for i in range ( N ) ] NEW_LINE left = 1 NEW_LINE right = N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( K % 2 == 0 ) : NEW_LINE INDENT lst [ i ] = left NEW_LINE left = left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT lst [ i ] = right NEW_LINE right = right - 1 NEW_LINE DEDENT if ( K > 1 ) : NEW_LINE INDENT K -= 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( lst [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE K = 3 NEW_LINE makelst ( N , K ) NEW_LINE DEDENT
Find the date after next half year from a given date | Function to find the date after the next half - year ; Stores the number of days in the months of a leap year ; List of months ; Days in half of a year ; Index of current month ; Starting day ; Decrement the value of cnt by 1 ; Increment cur_date ; If cnt is equal to 0 , then break out of the loop ; Update cur_month ; Update cur_date ; Print the resultant date ; Driver Code ; Function Call
def getDate ( d , m ) : NEW_LINE INDENT days = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] NEW_LINE month = [ ' January ' , ' February ' , ' March ' , ' April ' , ' May ' , ' June ' , ' July ' , ' August ' , ' September ' , ' October ' , ' November ' , ' December ' ] NEW_LINE cnt = 183 NEW_LINE cur_month = month . index ( m ) NEW_LINE cur_date = d NEW_LINE while ( 1 ) : NEW_LINE INDENT while ( cnt > 0 and cur_date <= days [ cur_month ] ) : NEW_LINE INDENT cnt -= 1 NEW_LINE cur_date += 1 NEW_LINE DEDENT if ( cnt == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT cur_month = ( cur_month + 1 ) % 12 NEW_LINE cur_date = 1 NEW_LINE DEDENT print ( cur_date , month [ cur_month ] ) NEW_LINE DEDENT D = 15 NEW_LINE M = " January " NEW_LINE getDate ( D , M ) NEW_LINE
Maximum number made up of distinct digits whose sum is equal to N | Function to find the largest positive number made up of distinct digits having the sum of its digits as N ; If given number is greater than 45 , print - 1 ; Store the required number and the digit to be considered ; Loop until N > 0 and digit > 0 ; If the current digit is at most N then , add it to number num ; Update the value of num ; Decrement N by digit ; Consider the next lower digit ; Add 0 at the end and return the number num ; Driver Code
def largestNumber ( N ) : NEW_LINE INDENT if ( N > 45 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT num = 0 NEW_LINE digit = 9 NEW_LINE while ( N > 0 and digit > 0 ) : NEW_LINE INDENT if ( digit <= N ) : NEW_LINE INDENT num *= 10 NEW_LINE num += digit NEW_LINE N -= digit NEW_LINE DEDENT digit -= 1 NEW_LINE DEDENT return num * 10 NEW_LINE DEDENT N = 25 NEW_LINE print ( largestNumber ( N ) ) NEW_LINE
Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S ; Stores the count of multiplication operator in the string ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; append last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
def minimumSum ( A , N , S ) : NEW_LINE INDENT mul = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == " * " ) : NEW_LINE INDENT mul += 1 NEW_LINE DEDENT DEDENT ans = 1000000 NEW_LINE for i in range ( 1 << ( N - 1 ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE v = [ ] NEW_LINE for j in range ( N - 1 ) : NEW_LINE INDENT if ( ( 1 << j ) & i ) : NEW_LINE INDENT cnt += 1 NEW_LINE v . append ( " * " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( " + " ) NEW_LINE DEDENT DEDENT if ( cnt == mul ) : NEW_LINE INDENT d = [ ] NEW_LINE d . append ( A [ 0 ] ) NEW_LINE for j in range ( N - 1 ) : NEW_LINE INDENT if ( v [ j ] == " * " ) : NEW_LINE INDENT x = d [ len ( d ) - 1 ] NEW_LINE d . pop ( ) NEW_LINE x = x * A [ j + 1 ] NEW_LINE d . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT d . append ( A [ j + 1 ] ) NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE while ( len ( d ) > 0 ) : NEW_LINE INDENT x = d [ 0 ] NEW_LINE sum += x NEW_LINE d . pop ( 0 ) NEW_LINE DEDENT ans = min ( ans , sum ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 2 , 2 , 2 , 2 ] NEW_LINE S = " * * + " NEW_LINE N = len ( A ) NEW_LINE print ( minimumSum ( A , N , S ) ) NEW_LINE
Minimum sum of medians of all possible K length subsequences of a sorted array | Function to find the minimum sum of all the medians of the K sized sorted arrays formed from the given array ; Stores the distance between the medians ; Stores the number of subsequences required ; Stores the resultant sum ; Iterate from start and add all the medians ; Add the value of arr [ i ] to the variable minsum ; Increment i by select the median to get the next median index ; Decrement the value of totalArrays by 1 ; Print the resultant minimum sum ; Driver Code
def sumOfMedians ( arr , N , K ) : NEW_LINE INDENT selectMedian = ( K + 1 ) // 2 NEW_LINE totalArrays = N // K NEW_LINE minSum = 0 NEW_LINE i = selectMedian - 1 NEW_LINE while ( i < N and totalArrays != 0 ) : NEW_LINE INDENT minSum = minSum + arr [ i ] NEW_LINE i = i + selectMedian NEW_LINE totalArrays -= 1 NEW_LINE DEDENT print ( minSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE sumOfMedians ( arr , N , K ) NEW_LINE DEDENT