codeparrot/xlcost-text-to-code huggingface dataset:
Largest palindromic string possible from given strings by rearranging the characters | Python3 program for the above approach ; Function to find largest palindrome possible from S and P after rearranging characters to make palindromic string T ; Using unordered_map to store frequency of elements mapT will have freq of elements of T ; Size of both the strings ; Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map ; Check if a unique character is present in both string S and P ; Making string T in two halves half1 - first half half2 - second half ; Reverse the half2 to attach with half1 to make palindrome ; If same unique element is present in both S and P , then taking that only which is already taken through mapT ; If same unique element is not present in S and P , then take characters that make string T lexicographically smallest ; If no unique element is present in both string S and P ; Given string S and P ; Function call
from collections import defaultdict NEW_LINE def mergePalindromes ( S , P ) : NEW_LINE INDENT mapS = defaultdict ( lambda : 0 ) NEW_LINE mapP = defaultdict ( lambda : 0 ) NEW_LINE mapT = defaultdict ( lambda : 0 ) NEW_LINE n = len ( S ) NEW_LINE m = len ( P ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapS [ ord ( S [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT mapP [ ord ( P [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] % 2 == 0 ) : NEW_LINE INDENT mapT [ i ] += mapS [ i ] NEW_LINE mapS [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT mapT [ i ] += mapS [ i ] - 1 NEW_LINE mapS [ i ] = 1 NEW_LINE DEDENT if ( mapP [ i ] % 2 == 0 ) : NEW_LINE INDENT mapT [ i ] += mapP [ i ] NEW_LINE mapP [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT mapT [ i ] += mapP [ i ] - 1 NEW_LINE mapP [ i ] = 1 NEW_LINE DEDENT DEDENT check = 0 NEW_LINE for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] > 0 and mapP [ i ] > 0 ) : NEW_LINE INDENT mapT [ i ] += 2 NEW_LINE check = 1 NEW_LINE break NEW_LINE DEDENT DEDENT half1 , half2 = " " , " " NEW_LINE for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( ( 2 * j ) < mapT [ i ] ) : NEW_LINE INDENT half1 += chr ( i ) NEW_LINE half2 += chr ( i ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT half2 = half2 [ : : - 1 ] NEW_LINE if ( check ) : NEW_LINE INDENT return half1 + half2 NEW_LINE DEDENT for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( mapS [ i ] > 0 or mapP [ i ] > 0 ) : NEW_LINE INDENT half1 += chr ( i ) NEW_LINE return half1 + half2 NEW_LINE DEDENT DEDENT return half1 + half2 NEW_LINE DEDENT S = " aeabb " NEW_LINE P = " dfedf " NEW_LINE print ( mergePalindromes ( S , P ) ) NEW_LINE
Count of subarrays having sum equal to its length | Python3 program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Decrementing all the elements of the array by 1 ; Making prefix sum array ; Declare map to store count of elements upto current element ; To count all the subarrays whose prefix sum is 0 ; Iterate the array ; Increment answer by count of current element of prefix array ; Return the answer ; Given array arr [ ] ; Function call
from collections import defaultdict NEW_LINE def countOfSubarray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] -= 1 NEW_LINE DEDENT pref = [ 0 ] * N NEW_LINE pref [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + arr [ i ] NEW_LINE DEDENT mp = defaultdict ( lambda : 0 ) NEW_LINE answer = 0 NEW_LINE mp [ 0 ] += 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT answer += mp [ pref [ i ] ] NEW_LINE mp [ pref [ i ] ] += 1 NEW_LINE DEDENT return answer NEW_LINE DEDENT arr = [ 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countOfSubarray ( arr , N ) ) NEW_LINE
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Python3 program to print subset at the nth position ordered by the sum of the elements ; Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; Create a vector for storing the elements of subsets ; Doing until all the set bits of n are used ; This part is executed only when the last bit is set ; Right shift the bit by one position ; Increasing the count each time by one ; Printing the values os elements ; Driver Code
import math NEW_LINE def printsubset ( n , k ) : NEW_LINE INDENT count = 0 NEW_LINE x = 0 NEW_LINE vec = [ ] NEW_LINE while ( n > 0 ) : NEW_LINE INDENT x = n & 1 NEW_LINE if ( x ) : NEW_LINE INDENT vec . append ( pow ( k , count ) ) NEW_LINE DEDENT n = n >> 1 NEW_LINE count += 1 NEW_LINE DEDENT for item in vec : NEW_LINE INDENT print ( item , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 7 NEW_LINE k = 4 NEW_LINE printsubset ( n , k ) NEW_LINE
Minimum digits to be removed to make either all digits or alternating digits same | Function to find longest possible subsequence of s beginning with x and y ; Iterate over the string ; Increment count ; Swap the positions ; Return the result ; Function that finds all the possible pairs ; Update count ; Return the answer ; Given string s ; Find the size of the string ; Function call ; This value is the count of minimum element to be removed
def solve ( s , x , y ) : NEW_LINE INDENT res = 0 NEW_LINE for c in s : NEW_LINE INDENT if ( ord ( c ) - ord ( '0' ) == x ) : NEW_LINE INDENT res += 1 NEW_LINE x , y = y , x NEW_LINE DEDENT DEDENT if ( x != y and res % 2 == 1 ) : NEW_LINE INDENT res -= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def find_min ( s ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT count = max ( count , solve ( s , i , j ) ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT s = "100120013" NEW_LINE n = len ( s ) NEW_LINE answer = find_min ( s ) NEW_LINE print ( n - answer ) NEW_LINE
Count total set bits in all numbers from range L to R | Function to count set bits in x ; Base Case ; Recursive Call ; Function that returns count of set bits present in all numbers from 1 to N ; Initialize the result ; Return the setbit count ; Driver Code ; Given L and R ; Function Call
def countSetBitsUtil ( x ) : NEW_LINE INDENT if ( x < 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( x % 2 == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + ( countSetBitsUtil ( x / 2 ) ) ; NEW_LINE DEDENT DEDENT def countSetBits ( L , R ) : NEW_LINE INDENT bitCount = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT bitCount += countSetBitsUtil ( i ) ; NEW_LINE DEDENT return bitCount ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 3 ; NEW_LINE R = 5 ; NEW_LINE print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " , countSetBits ( L , R ) ) ; NEW_LINE DEDENT
Possible values of Q such that , for any value of R , their product is equal to X times their sum | Function to find all possible values of Q ; Vector initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver Code
def values_of_Q ( X ) : NEW_LINE INDENT val_Q = [ ] NEW_LINE for i in range ( 1 , X + 1 ) : NEW_LINE INDENT if ( ( ( ( X + i ) * X ) ) % i == 0 ) : NEW_LINE INDENT val_Q . append ( X + i ) NEW_LINE DEDENT DEDENT print ( len ( val_Q ) ) NEW_LINE for i in range ( len ( val_Q ) ) : NEW_LINE INDENT print ( val_Q [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT X = 3 NEW_LINE values_of_Q ( X ) NEW_LINE
Program to find Greatest Common Divisor ( GCD ) of N strings | Function that finds gcd of 2 strings ; If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) ; If str1 is not the concatenation of str2 ; GCD string is found ; Cut off the common prefix part of str1 & then recur ; Function to find GCD of array of strings ; Return the GCD of strings ; Given array of strings ; Function Call
def gcd ( str1 , str2 ) : NEW_LINE INDENT if ( len ( str1 ) < len ( str2 ) ) : NEW_LINE INDENT return gcd ( str2 , str1 ) NEW_LINE DEDENT elif ( not str1 . startswith ( str2 ) ) : NEW_LINE INDENT return " " NEW_LINE DEDENT elif ( len ( str2 ) == 0 ) : NEW_LINE INDENT return str1 NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( str1 [ len ( str2 ) : ] , str2 ) NEW_LINE DEDENT DEDENT def findGCD ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = gcd ( result , arr [ i ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " ] NEW_LINE n = len ( arr ) NEW_LINE print ( findGCD ( arr , n ) ) NEW_LINE
Check whether the given Matrix is balanced or not | Define the size of the matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Given matrix mat [ ] [ ] ; Function call
N = 4 NEW_LINE M = 4 NEW_LINE def balancedMatrix ( mat ) : NEW_LINE INDENT is_balanced = True NEW_LINE i = 0 NEW_LINE while i < N and is_balanced : NEW_LINE INDENT j = 0 NEW_LINE while j < N and is_balanced : NEW_LINE INDENT if ( ( i == 0 or i == N - 1 ) and ( j == 0 or j == M - 1 ) ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 2 : NEW_LINE INDENT isbalanced = False NEW_LINE DEDENT DEDENT elif ( i == 0 or i == N - 1 or j == 0 or j == M - 1 ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 3 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if mat [ i ] [ j ] >= 4 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if is_balanced : NEW_LINE INDENT return " Balanced " NEW_LINE DEDENT else : NEW_LINE INDENT return " Unbalanced " NEW_LINE DEDENT DEDENT mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] NEW_LINE print ( balancedMatrix ( mat ) ) NEW_LINE
Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Driver code ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS
def unixTimeToHumanReadable ( seconds ) : NEW_LINE INDENT ans = " " NEW_LINE daysOfMonth = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] NEW_LINE ( currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag ) = ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) NEW_LINE daysTillNow = seconds // ( 24 * 60 * 60 ) NEW_LINE extraTime = seconds % ( 24 * 60 * 60 ) NEW_LINE currYear = 1970 NEW_LINE while ( daysTillNow >= 365 ) : NEW_LINE INDENT if ( currYear % 400 == 0 or ( currYear % 4 == 0 and currYear % 100 != 0 ) ) : NEW_LINE INDENT daysTillNow -= 366 NEW_LINE DEDENT else : NEW_LINE INDENT daysTillNow -= 365 NEW_LINE DEDENT currYear += 1 NEW_LINE DEDENT extraDays = daysTillNow + 1 NEW_LINE if ( currYear % 400 == 0 or ( currYear % 4 == 0 and currYear % 100 != 0 ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT month = 0 NEW_LINE index = 0 NEW_LINE if ( flag == 1 ) : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT if ( index == 1 ) : NEW_LINE INDENT if ( extraDays - 29 < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= 29 NEW_LINE DEDENT else : NEW_LINE INDENT if ( extraDays - daysOfMonth [ index ] < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= daysOfMonth [ index ] NEW_LINE DEDENT index += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT if ( extraDays - daysOfMonth [ index ] < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= daysOfMonth [ index ] NEW_LINE index += 1 NEW_LINE DEDENT DEDENT if ( extraDays > 0 ) : NEW_LINE INDENT month += 1 NEW_LINE date = extraDays NEW_LINE DEDENT else : NEW_LINE INDENT if ( month == 2 and flag == 1 ) : NEW_LINE INDENT date = 29 NEW_LINE DEDENT else : NEW_LINE INDENT date = daysOfMonth [ month - 1 ] NEW_LINE DEDENT DEDENT hours = extraTime // 3600 NEW_LINE minutes = ( extraTime % 3600 ) // 60 NEW_LINE secondss = ( extraTime % 3600 ) % 60 NEW_LINE ans += str ( date ) NEW_LINE ans += " / " NEW_LINE ans += str ( month ) NEW_LINE ans += " / " NEW_LINE ans += str ( currYear ) NEW_LINE ans += " ▁ " NEW_LINE ans += str ( hours ) NEW_LINE ans += " : " NEW_LINE ans += str ( minutes ) NEW_LINE ans += " : " NEW_LINE ans += str ( secondss ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT T = 1595497956 NEW_LINE ans = unixTimeToHumanReadable ( T ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | Function to find area of rectangle inscribed another rectangle of length L and width W ; Area of rectangle ; Return the area ; Driver Code ; Given Dimensions ; Function Call
def AreaofRectangle ( L , W ) : NEW_LINE INDENT area = ( W + L ) * ( W + L ) / 2 NEW_LINE return area NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 18 NEW_LINE W = 12 NEW_LINE print ( AreaofRectangle ( L , W ) ) NEW_LINE DEDENT
Minimum number of operations required to reduce N to 0 | Python3 program to implement the above approach ; Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code
import math NEW_LINE import sys NEW_LINE def downToZero ( n ) : NEW_LINE INDENT if ( n <= 3 ) : NEW_LINE INDENT return n NEW_LINE DEDENT dp = [ - 1 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 1 NEW_LINE dp [ 2 ] = 2 NEW_LINE dp [ 3 ] = 3 NEW_LINE for i in range ( 4 , n + 1 ) : NEW_LINE INDENT sqr = ( int ) ( math . sqrt ( i ) ) NEW_LINE best = sys . maxsize NEW_LINE while ( sqr > 1 ) : NEW_LINE INDENT if ( i % sqr == 0 ) : NEW_LINE INDENT best = min ( best , 1 + dp [ sqr ] ) NEW_LINE DEDENT sqr -= 1 NEW_LINE DEDENT best = min ( best , 1 + dp [ i - 1 ] ) NEW_LINE dp [ i ] = best NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( downToZero ( n ) ) NEW_LINE DEDENT
Minimum number of operations required to reduce N to 0 | Function to find the minimum steps required to reduce n ; Base case ; Return answer based on parity of n ; Driver Code
def downToZero ( n ) : NEW_LINE INDENT if ( n <= 3 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 3 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 4 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( downToZero ( n ) ) ; NEW_LINE DEDENT
Minimum count of elements required to obtain the given Array by repeated mirror operations | Function to find minimum number of elements required to form A [ ] by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the final answer ; Driver code
def minimumrequired ( A , N ) : NEW_LINE INDENT K = N NEW_LINE while ( K > 0 ) : NEW_LINE INDENT if ( K % 2 ) == 1 : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT ispalindrome = 1 NEW_LINE for i in range ( 0 , K // 2 ) : NEW_LINE INDENT if ( A [ i ] != A [ K - 1 - i ] ) : NEW_LINE INDENT ispalindrome = 0 NEW_LINE DEDENT DEDENT if ( ispalindrome == 1 ) : NEW_LINE INDENT ans = K // 2 NEW_LINE K = K // 2 NEW_LINE DEDENT else : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumrequired ( A , N ) ) NEW_LINE
Sum of product of all integers upto N with their count of divisors | Function to find the sum of the product of all the integers and their positive divisors up to N ; Iterate for every number between 1 and N ; Find the first multiple of i between 1 and N ; Find the last multiple of i between 1 and N ; Find the total count of multiple of in [ 1 , N ] ; Compute the contribution of i using the formula ; Add the contribution of i to the answer ; Return the result ; Given N ; Function call
def sumOfFactors ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT first = i NEW_LINE last = ( N // i ) * i NEW_LINE factors = ( last - first ) // i + 1 NEW_LINE totalContribution = ( ( ( factors * ( factors + 1 ) ) // 2 ) * i ) NEW_LINE ans += totalContribution NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 3 NEW_LINE print ( sumOfFactors ( N ) ) NEW_LINE
Find a number M < N such that difference between their XOR and AND is maximum | Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Driver Code ; Given number N ; Function call
def getMaxDifference ( N ) : NEW_LINE INDENT M = - 1 ; NEW_LINE maxDiff = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT diff = ( N ^ i ) - ( N & i ) ; NEW_LINE if ( diff >= maxDiff ) : NEW_LINE INDENT maxDiff = diff ; NEW_LINE M = i ; NEW_LINE DEDENT DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE print ( getMaxDifference ( N ) ) ; NEW_LINE DEDENT
Find a number M < N such that difference between their XOR and AND is maximum | Python3 program for the above approach ; Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Driver Code ; Given number ; Function call
import math NEW_LINE def findM ( N ) : NEW_LINE INDENT M = 0 ; NEW_LINE MSB = int ( math . log ( N ) ) ; NEW_LINE for i in range ( MSB ) : NEW_LINE INDENT if ( ( N & ( 1 << i ) ) == 0 ) : NEW_LINE INDENT M += ( 1 << i ) ; NEW_LINE DEDENT DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE print ( findM ( N ) ) ; NEW_LINE DEDENT
Number of containers that can be filled in the given time | Matrix of containers ; Function to find the number of containers that will be filled in X seconds ; Container on top level ; If container gets filled ; Dividing the liquid equally in two halves ; Driver code
cont = [ [ 0 for i in range ( 1000 ) ] for j in range ( 1000 ) ] NEW_LINE def num_of_containers ( n , x ) : NEW_LINE INDENT count = 0 NEW_LINE cont [ 1 ] [ 1 ] = x NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( cont [ i ] [ j ] >= 1 ) : NEW_LINE INDENT count += 1 NEW_LINE cont [ i + 1 ] [ j ] += ( cont [ i ] [ j ] - 1 ) / 2 NEW_LINE cont [ i + 1 ] [ j + 1 ] += ( cont [ i ] [ j ] - 1 ) / 2 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT n = 3 NEW_LINE x = 5 NEW_LINE num_of_containers ( n , x ) NEW_LINE
Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | Python3 program for the above approach ; Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h3 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Driver Code ; Given array ; Function Call
import sys NEW_LINE def findTriplet ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE st = [ ] NEW_LINE h3 = - sys . maxsize - 1 NEW_LINE h1 = sys . maxsize NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT h1 = arr [ i ] NEW_LINE while ( len ( st ) > 0 and st [ - 1 ] < arr [ i ] ) : NEW_LINE INDENT h3 = st [ - 1 ] NEW_LINE del st [ - 1 ] NEW_LINE DEDENT st . append ( arr [ i ] ) NEW_LINE if ( h1 < h3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 5 , 6 ] NEW_LINE if ( findTriplet ( arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum number of distinct elements after removing M items | Set 2 | Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver Code ; Initialize array ; Size of array ; Function call
def distinctNumbers ( arr , m , n ) : NEW_LINE INDENT count = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] = count . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT fre_arr = [ 0 ] * ( n + 1 ) NEW_LINE for it in count : NEW_LINE INDENT fre_arr [ count [ it ] ] += 1 NEW_LINE DEDENT ans = len ( count ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = fre_arr [ i ] NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT t = min ( temp , m // i ) NEW_LINE ans -= t NEW_LINE m -= i * t NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE m = 2 NEW_LINE print ( distinctNumbers ( arr , m , n ) ) NEW_LINE DEDENT
Find minimum moves to bring all elements in one cell of a matrix | Python3 implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Driver Code ; Coordinates of given cell ; Given Matrix ; Element to be moved ; Function call
M = 4 NEW_LINE N = 5 NEW_LINE def no_of_moves ( Matrix , x , y ) : NEW_LINE INDENT moves = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( Matrix [ i ] [ j ] != 0 ) : NEW_LINE INDENT moves += abs ( x - i ) NEW_LINE moves += abs ( y - j ) NEW_LINE DEDENT DEDENT DEDENT print ( moves ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 3 NEW_LINE y = 2 NEW_LINE Matrix = [ [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] ] NEW_LINE num = 1 NEW_LINE no_of_moves ( Matrix , x , y ) NEW_LINE DEDENT
Check if all array elements can be removed by the given operations | Function to check if it is possible to remove all array elements ; Condition if we can remove all elements from the array ; Driver code
def removeAll ( arr , n ) : NEW_LINE INDENT if arr [ 0 ] < arr [ n - 1 ] : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT arr = [ 10 , 4 , 7 , 1 , 3 , 6 ] NEW_LINE removeAll ( arr , len ( arr ) ) NEW_LINE