codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to remove objects such that exactly M equidistant objects remain | Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If length > n ; Update the number of ways ; Print the result ; Driver Code
def waysToRemove ( n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT print ( n ) NEW_LINE return NEW_LINE DEDENT d = 0 NEW_LINE while d >= 0 : NEW_LINE INDENT length = m + ( m - 1 ) * d NEW_LINE if ( length > n ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += ( n - length ) + 1 NEW_LINE d += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE M = 3 NEW_LINE waysToRemove ( N , M ) NEW_LINE DEDENT
Count unique stairs that can be reached by moving given number of steps forward or backward | Python3 program for the above approach ; Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; Push the starting position ; Mark the starting position S as visited ; Iterate until queue is not empty ; Store the current stair number ; Check for all possible moves from the current stair ; Store the new stair number ; If it is valid and unvisited ; Push it into queue ; Mark the stair as visited ; Store the result ; Count the all visited stairs ; Print the result ; Driver Code
from collections import deque NEW_LINE def countStairs ( n , x , a , b ) : NEW_LINE INDENT vis = [ 0 ] * ( n + 1 ) NEW_LINE moves = [ + a , - a , + b , - b ] NEW_LINE q = deque ( ) NEW_LINE q . append ( x ) NEW_LINE vis [ x ] = 1 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT currentStair = q . popleft ( ) NEW_LINE for j in range ( 4 ) : NEW_LINE INDENT newStair = currentStair + moves [ j ] NEW_LINE if ( newStair > 0 and newStair <= n and ( not vis [ newStair ] ) ) : NEW_LINE INDENT q . append ( newStair ) NEW_LINE vis [ newStair ] = 1 NEW_LINE DEDENT DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( vis [ i ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT print ( cnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , S , A , B = 10 , 2 , 5 , 7 NEW_LINE countStairs ( N , S , A , B ) NEW_LINE DEDENT
Count smaller elements present in the array for each array element | Function to count for each array element , the number of elements that are smaller than that element ; Traverse the array ; Stores the count ; Traverse the array ; Increment count ; Print the count of smaller elements for the current element ; Driver Code
def smallerNumbers ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 1 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE smallerNumbers ( arr , N ) NEW_LINE DEDENT
Minimize divisions by 2 , 3 , or 5 required to make two given integers equal | Function to calculate GCD of two numbers ; Base Case ; Calculate GCD recursively ; Function to count the minimum number of divisions required to make X and Y equal ; Calculate GCD of X and Y ; Divide X and Y by their GCD ; Stores the number of divisions ; Iterate until X != Y ; Maintain the order X <= Y ; If X is divisible by 2 , then divide X by 2 ; If X is divisible by 3 , then divide X by 3 ; If X is divisible by 5 , then divide X by 5 ; If X is not divisible by 2 , 3 , or 5 , then pr - 1 ; Increment count by 1 ; Print the value of count as the minimum number of operations ; 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 minimumOperations ( X , Y ) : NEW_LINE INDENT GCD = gcd ( X , Y ) NEW_LINE X = X // GCD NEW_LINE Y = Y // GCD NEW_LINE count = 0 NEW_LINE while ( X != Y ) : NEW_LINE INDENT if ( Y > X ) : NEW_LINE INDENT X , Y = Y , X NEW_LINE DEDENT if ( X % 2 == 0 ) : NEW_LINE INDENT X = X // 2 NEW_LINE DEDENT elif ( X % 3 == 0 ) : NEW_LINE INDENT X = X // 3 NEW_LINE DEDENT elif ( X % 5 == 0 ) : NEW_LINE INDENT X = X // 5 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT count += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X , Y = 15 , 20 NEW_LINE minimumOperations ( X , Y ) NEW_LINE DEDENT
Minimum increments or decrements required to convert a sorted array into a power sequence | Function to find the minimum number of increments or decrements required to convert array into a power sequence ; Initialize the count to f ( X ) for X = 1 ; Calculate the value of f ( X ) X ^ ( n - 1 ) <= f ( 1 ) + a [ n - 1 ] ; Calculate F ( x ) ; Check if X ^ ( n - 1 ) > f ( 1 ) + a [ n - 1 ] ; Update ans to store the minimum of ans and F ( x ) ; Return the minimum number of operations required ; Driver Code
def minOperations ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += a [ i ] NEW_LINE DEDENT ans -= n NEW_LINE x = 1 NEW_LINE while ( 1 ) : NEW_LINE INDENT curPow = 1 NEW_LINE curCost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT curCost += abs ( a [ i ] - curPow ) NEW_LINE curPow *= x NEW_LINE DEDENT if ( curPow / x > ans + a [ n - 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = min ( ans , curCost ) NEW_LINE x += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minOperations ( arr , N ) ) NEW_LINE DEDENT
Find the absolute difference between the nearest powers of two given integers for every array element | Python3 program for the above approach ; Function to print the array ; Traverse the array ; Function to modify array elements by absolute difference of the nearest perfect power of a and b ; Traverse the array arr [ ] ; Find the log a of arr [ i ] ; Find the power of a less than and greater than a ; Find the log b of arr [ i ] ; Find the power of b less than and greater than b ; Update arr [ i ] with absolute difference of log_a & log _b ; Print the modified array ; Driver Code
import math NEW_LINE def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def nearestPowerDiff ( arr , N , a , b ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT log_a = int ( math . log ( arr [ i ] ) / math . log ( a ) ) NEW_LINE A = int ( pow ( a , log_a ) ) NEW_LINE B = int ( pow ( a , log_a + 1 ) ) NEW_LINE if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) : NEW_LINE INDENT log_a = A NEW_LINE DEDENT else : NEW_LINE INDENT log_a = B NEW_LINE DEDENT log_b = int ( math . log ( arr [ i ] ) / math . log ( b ) ) NEW_LINE A = int ( pow ( b , log_b ) ) NEW_LINE B = int ( pow ( b , log_b + 1 ) ) NEW_LINE if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) : NEW_LINE INDENT log_b = A NEW_LINE DEDENT else : NEW_LINE INDENT log_b = B NEW_LINE DEDENT arr [ i ] = abs ( log_a - log_b ) NEW_LINE DEDENT printArray ( arr , N ) NEW_LINE DEDENT arr = [ 5 , 12 , 25 ] NEW_LINE A = 2 NEW_LINE B = 3 NEW_LINE N = len ( arr ) NEW_LINE nearestPowerDiff ( arr , N , A , B ) NEW_LINE
Count subarrays having even Bitwise OR | Function to count the number of subarrays having even Bitwise OR ; Store number of subarrays having even bitwise OR ; Store the length of the current subarray having even numbers ; Traverse the array ; If the element is even ; Increment size of the current continuous sequence of even array elements ; If arr [ i ] is odd ; If length is non zero ; Adding contribution of subarrays consisting only of even numbers ; Make length of subarray as 0 ; Add contribution of previous subarray ; Return total count of subarrays ; Driver Code ; Function Call
def bitOr ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE length = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( length != 0 ) : NEW_LINE INDENT count += ( ( length ) * ( length + 1 ) ) // 2 NEW_LINE DEDENT length = 0 NEW_LINE DEDENT DEDENT count += ( ( length ) * ( length + 1 ) ) // 2 NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 4 , 2 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( bitOr ( arr , N ) ) NEW_LINE DEDENT
Generate an array having sum of Bitwise OR of same | Function to print the resultant array ; Stores the sum of the array ; Calculate sum of the array ; If sum > K ; Not possible to construct the array ; Update K ; Stores the resultant array ; Traverse the array ; Stores the current element ; If jth bit is not set and value of 2 ^ j is less than K ; Update curr ; Update K ; Push curr into B ; If K is greater than 0 ; Print the vector B ; Otherwise ; Driver Code ; Input ; Size of the array ; Given input ; Function call to print the required array
def constructArr ( A , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT if ( sum > K ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT K -= sum NEW_LINE B = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr = A [ i ] NEW_LINE j = 32 NEW_LINE while ( j >= 0 and K > 0 ) : NEW_LINE INDENT if ( ( curr & ( 1 << j ) ) == 0 and ( 1 << j ) <= K ) : NEW_LINE INDENT curr |= ( 1 << j ) NEW_LINE K -= ( 1 << j ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT B . append ( curr ) NEW_LINE DEDENT if ( K == 0 ) : NEW_LINE INDENT for it in B : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 32 NEW_LINE constructArr ( arr , N , K ) NEW_LINE DEDENT
Find all pairs raised to power K differs by exactly N | Function to prpairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code
def ValidPairs ( X , K ) : NEW_LINE INDENT count = 0 NEW_LINE for A in range ( - 1000 , 1001 , 1 ) : NEW_LINE INDENT for B in range ( - 1000 , 1001 , 1 ) : NEW_LINE INDENT if ( pow ( A , K ) - pow ( B , K ) == X ) : NEW_LINE INDENT count += 1 NEW_LINE print ( A , B ) NEW_LINE DEDENT DEDENT DEDENT if ( count == 0 ) : NEW_LINE INDENT cout << " - 1" NEW_LINE DEDENT DEDENT X = 33 NEW_LINE K = 5 NEW_LINE ValidPairs ( X , K ) NEW_LINE
Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum | Function to find the maximum Bitwise AND of connected components possible by connecting a graph using M edges ; Stores total number of ways to connect the graph ; Stores the maximum Bitwise AND ; Iterate over the range [ 0 , 2 ^ n ] ; Store the Bitwise AND of the connected vertices ; Store the count of the connected vertices ; Check for all the bits ; If i - th bit is set ; If the first vertex is added ; Set andans equal to arr [ i ] ; Calculate Bitwise AND of arr [ i ] with andans ; Increase the count of connected vertices ; If number of connected vertices is ( m + 1 ) , no cycle is formed ; Find the maximum Bitwise AND value possible ; Return the maximum Bitwise AND possible ; Driver Code
def maximumAND ( arr , n , m ) : NEW_LINE INDENT tot = 1 << n NEW_LINE mx = 0 NEW_LINE for bm in range ( tot ) : NEW_LINE INDENT andans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( bm >> i ) & 1 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT andans = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT andans = andans & arr [ i ] NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == ( m + 1 ) ) : NEW_LINE INDENT mx = max ( mx , andans ) NEW_LINE DEDENT DEDENT return mx NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE print ( maximumAND ( arr , N , M ) ) NEW_LINE DEDENT
Calculate sum of the array generated by given operations | Function to find the sum of the array formed by performing given set of operations while traversing the array ops [ ] ; If the size of array is 0 ; Stores the required sum ; Traverse the array ops [ ] ; If the character is C , remove the top element from the stack ; If the character is D , then push 2 * top element into stack ; If the character is + , add sum of top two elements from the stack ; Otherwise , push x and add it to ans ; Print the resultant sum ; Driver Code
def findTotalSum ( ops ) : NEW_LINE INDENT if ( len ( ops ) == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT pts = [ ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ( ops ) ) : NEW_LINE INDENT if ( ops [ i ] == " C " ) : NEW_LINE INDENT ans -= pts [ - 1 ] NEW_LINE pts . pop ( ) NEW_LINE DEDENT elif ( ops [ i ] == " D " ) : NEW_LINE INDENT pts . append ( pts [ - 1 ] * 2 ) NEW_LINE ans += pts [ - 1 ] NEW_LINE DEDENT elif ( ops [ i ] == " + " ) : NEW_LINE INDENT a = pts [ - 1 ] NEW_LINE pts . pop ( ) NEW_LINE b = pts [ - 1 ] NEW_LINE pts . append ( a ) NEW_LINE ans += ( a + b ) NEW_LINE pts . append ( a + b ) NEW_LINE DEDENT else : NEW_LINE INDENT n = int ( ops [ i ] ) NEW_LINE ans += n NEW_LINE pts . append ( n ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ "5" , " - 2" , " C " , " D " , " + " ] NEW_LINE findTotalSum ( arr ) NEW_LINE DEDENT
XOR of major diagonal elements of a 3D Matrix | Function to calculate Bitwise XOR of major diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; If element is part of major diagonal ; Print the resultant Bitwise XOR ; Driver Code
def findXOR ( mat , N ) : NEW_LINE INDENT XOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT for k in range ( N ) : NEW_LINE INDENT if ( ( i == j and j == k ) ) : NEW_LINE INDENT XOR ^= mat [ i ] [ j ] [ k ] NEW_LINE XOR ^= mat [ i ] [ j ] [ N - k - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( XOR ) NEW_LINE DEDENT mat = [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] NEW_LINE N = len ( mat ) NEW_LINE findXOR ( mat , N ) NEW_LINE
XOR of major diagonal elements of a 3D Matrix | Function to find the Bitwise XOR of both diagonal elements of 3D matrix ; Stores the Bitwise XOR of the major diagonal elements ; Print the resultant Bitwise XOR ; Driver Code
def findXOR ( mat , N ) : NEW_LINE INDENT XOR = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT XOR ^= mat [ i ] [ i ] [ i ] NEW_LINE XOR ^= mat [ i ] [ i ] [ N - i - 1 ] NEW_LINE DEDENT print ( XOR ) NEW_LINE DEDENT mat = [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] NEW_LINE N = len ( mat ) NEW_LINE findXOR ( mat , N ) NEW_LINE
Count of subtrees possible from an N | Python3 program of the above approach ; Adjacency list to represent the graph ; Stores the count of subtrees possible from given N - ary Tree ; Utility function to count the number of subtrees possible from given N - ary Tree ; Stores the count of subtrees when cur node is the root ; Traverse the adjacency list ; Iterate over every ancestor ; Calculate product of the number of subtrees for each child node ; Update the value of ans ; Return the resultant count ; Function to count the number of subtrees in the given tree ; Initialize an adjacency matrix ; Add the edges ; Function Call to count the number of subtrees possible ; Prcount of subtrees ; Driver Code
MAX = 300004 NEW_LINE graph = [ [ ] for i in range ( MAX ) ] NEW_LINE mod = 10 ** 9 + 7 NEW_LINE ans = 0 NEW_LINE def countSubtreesUtil ( cur , par ) : NEW_LINE INDENT global mod , ans NEW_LINE res = 1 NEW_LINE for i in range ( len ( graph [ cur ] ) ) : NEW_LINE INDENT v = graph [ cur ] [ i ] NEW_LINE if ( v == par ) : NEW_LINE INDENT continue NEW_LINE DEDENT res = ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod NEW_LINE DEDENT ans = ( ans + res ) % mod NEW_LINE return res NEW_LINE DEDENT def countSubtrees ( N , adj ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT a = adj [ i ] [ 0 ] NEW_LINE b = adj [ i ] [ 1 ] NEW_LINE graph [ a ] . append ( b ) NEW_LINE graph [ b ] . append ( a ) NEW_LINE DEDENT countSubtreesUtil ( 1 , 1 ) NEW_LINE print ( ans + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE adj = [ [ 0 , 1 ] , [ 1 , 2 ] ] NEW_LINE countSubtrees ( N , adj ) NEW_LINE DEDENT
Find the player who wins the game of placing alternate + and | Function to check which player wins the game ; Stores the difference between + ve and - ve array elements ; Traverse the array ; Update diff ; Checks if diff is even ; Driver Code ; Given Input ; Function call to check which player wins the game
def checkWinner ( arr , N ) : NEW_LINE INDENT diff = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT diff -= arr [ i ] NEW_LINE DEDENT if ( diff % 2 == 0 ) : NEW_LINE INDENT print ( " A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE checkWinner ( arr , N ) NEW_LINE DEDENT
Generate an array having sum of Euler Totient Function of all elements equal to N | Python3 program for the above approach ; Function to construct the array such the sum of values of Euler Totient functions of all array elements is N ; Stores the resultant array ; Find divisors in sqrt ( N ) ; If N is divisible by i ; Push the current divisor ; If N is not a perfect square ; Push the second divisor ; Print the resultant array ; Driver Code ; Function Call
from math import sqrt NEW_LINE def constructArray ( N ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( 1 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT ans . append ( i ) NEW_LINE if ( N != ( i * i ) ) : NEW_LINE INDENT ans . append ( N / i ) NEW_LINE DEDENT DEDENT DEDENT for it in ans : NEW_LINE INDENT print ( int ( it ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE constructArray ( N ) NEW_LINE DEDENT
Place N boys and M girls in different rows such that count of persons placed in each row is maximized | Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row
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 maximumRowValue ( n , m ) : NEW_LINE INDENT return gcd ( n , m ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 2 NEW_LINE print ( maximumRowValue ( N , M ) ) NEW_LINE DEDENT
Program to generate an array having convolution of two given arrays | Python3 program for the above approach ; Function to generate a convolution array of two given arrays ; Stores the size of arrays ; Stores the final array ; Traverse the two given arrays ; Update the convolution array ; Print the convolution array c [ ] ; Driver Code
MOD = 998244353 NEW_LINE def findConvolution ( a , b ) : NEW_LINE INDENT global MOD NEW_LINE n , m = len ( a ) , len ( b ) NEW_LINE c = [ 0 ] * ( n + m - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD NEW_LINE DEDENT DEDENT for k in range ( len ( c ) ) : NEW_LINE INDENT c [ k ] %= MOD NEW_LINE print ( c [ k ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 ] NEW_LINE B = [ 5 , 6 , 7 , 8 , 9 ] NEW_LINE findConvolution ( A , B ) NEW_LINE DEDENT
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | Python3 program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Driver Code ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A
P = 51 NEW_LINE def count1s ( start_i , start_j , end_i , end_j , mat ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( start_i , end_i ) : NEW_LINE INDENT for y in range ( start_j , end_j ) : NEW_LINE INDENT if ( mat [ x ] [ y ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def findMinimumCount ( N , M , A , B , mat ) : NEW_LINE INDENT minimum = 1e9 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( i + A <= N and j + B <= M ) : NEW_LINE INDENT count = count1s ( i , j , i + A , j + B , mat ) NEW_LINE minimum = min ( count , minimum ) NEW_LINE DEDENT if ( i + B <= N and j + A <= M ) : NEW_LINE INDENT count = count1s ( i , j , i + B , j + A , mat ) NEW_LINE minimum = min ( count , minimum ) NEW_LINE DEDENT DEDENT DEDENT return minimum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 2 NEW_LINE B = 2 NEW_LINE N = 3 NEW_LINE M = 4 NEW_LINE mat = [ [ 1 , 0 , 1 , 0 ] , [ 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 0 ] ] NEW_LINE print ( findMinimumCount ( N , M , A , B , mat ) ) NEW_LINE DEDENT
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 ; Iterate till x is N ; If x is even then return true ; Increment x ; If N is not a power of 2 ; Driver Code
def checkEvenPower ( n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( x < n ) : NEW_LINE INDENT value = pow ( 2 , x ) NEW_LINE if ( value == n ) : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE if checkEvenPower ( N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to check if a number can be expressed as an even power of 2 or not | Function to check if N can be expressed as an even power of 2 or not ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver code
def checkEvenPower ( n ) : NEW_LINE INDENT low , high = 0 , n NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 NEW_LINE value = pow ( 2 , mid ) NEW_LINE if ( value == n ) : NEW_LINE INDENT if ( mid % 2 == 1 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT elif ( value < n ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT N = 4 NEW_LINE print ( checkEvenPower ( N ) ) NEW_LINE