codeparrot/xlcost-text-to-code huggingface dataset:
Count of substrings having the most frequent character in the string as first character | Python3 program for the above approach ; Function to find all substrings whose first character occurs maximum number of times ; Stores frequency of characters ; Stores character that appears maximum number of times max_char = ; Stores max frequency of character ; Updates frequency of characters ; Update maxfreq ; Character that occures maximum number of times ; Update the maximum frequency character ; Stores all count of substrings ; Traverse over string ; Get the current character ; Update count of substrings ; Return the count of all valid substrings ; Driver Code ; Function Call
import sys NEW_LINE def substringCount ( s ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE DEDENT INDENT maxfreq = - sys . maxsize - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( maxfreq < freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT maxfreq = freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( maxfreq == freq [ i ] ) : NEW_LINE INDENT max_char = chr ( i + ord ( ' a ' ) ) NEW_LINE break NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ch = s [ i ] NEW_LINE if ( max_char == ch ) : NEW_LINE INDENT ans += ( len ( s ) - i ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcab " NEW_LINE print ( substringCount ( S ) ) NEW_LINE DEDENT
Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Driver Code ; Given array arr [ ] ; Function Call
def minSum ( arr , n ) : NEW_LINE INDENT ans = [ 1 ] * ( n ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT ans [ i ] = max ( ans [ i ] , ans [ i - 1 ] + 1 ) NEW_LINE DEDENT DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT ans [ i ] = max ( ans [ i ] , ans [ i + 1 ] + 1 ) NEW_LINE DEDENT DEDENT s = 0 NEW_LINE for x in ans : NEW_LINE INDENT s = s + x NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minSum ( arr , N ) NEW_LINE DEDENT
Maximize sum of assigned weights by flipping at most K bits in given Binary String | Function to find maximum sum of weights of binary string after at most K flips ; Stores lengths of substrings of the form 1. . 00. . 1 s ; Stores the index of last 1 encountered in the string ; Stores the index of first 1 encountered ; Stores lengths of all substrings having of 0 s enclosed by 1 s at both ends ; Traverse the string ; If character is 0 ; If character is 1 First Priority ; Second Priority ; Add according to the first priority ; Stores length of the shortest substring of 0 s ; Convert shortest substrings of 0 s to 1 s ; Add according to the first priority ; Add according to the third priority ; If more 0 s can be made into 1 , then check for 0 s at ends ; Update the ans ; If K is non - zero , then flip 0 s at the beginning ; Return the final weights ; Driver Code ; Given string str ; Given K flips ; Function Call
def findMax ( s , n , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE l = 0 ; NEW_LINE ind = - 1 ; NEW_LINE indf = - 1 ; NEW_LINE ls = set ( [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT elif ( s [ i ] == '1' and l > 0 and ans != 0 ) : NEW_LINE INDENT ls . add ( l ) ; NEW_LINE l = 0 ; NEW_LINE DEDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ind = i ; NEW_LINE l = 0 ; NEW_LINE if ( indf == - 1 ) : NEW_LINE INDENT indf = i ; NEW_LINE DEDENT if ( i > 0 and s [ i - 1 ] == '1' ) : NEW_LINE INDENT ans += 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT curr = 0 NEW_LINE while ( k > 0 and len ( ls ) != 0 ) : NEW_LINE INDENT for i in ls : NEW_LINE curr = i NEW_LINE break NEW_LINE if ( k >= curr ) : NEW_LINE INDENT ans += ( 2 * curr + 1 ) ; NEW_LINE k -= curr ; NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( 2 * k ) ; NEW_LINE k = 0 ; NEW_LINE DEDENT ls . remove ( curr ) ; NEW_LINE DEDENT if ( k > 0 ) : NEW_LINE INDENT ans += ( 2 * min ( k , n - ( ind + 1 ) ) - 1 ) ; NEW_LINE k -= min ( k , n - ( ind + 1 ) ) ; NEW_LINE if ( ind > - 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( k > 0 ) : NEW_LINE INDENT ans += ( min ( indf , k ) * 2 - 1 ) ; NEW_LINE if ( indf > - 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "1110000101" ; NEW_LINE N = len ( s ) NEW_LINE K = 3 ; NEW_LINE print ( findMax ( s , N , K ) ) ; NEW_LINE DEDENT
Minimum removal of consecutive similar characters required to empty a Binary String | Python3 program for the above approach ; Function to find minimum steps to make the empty ; Stores the modified string ; Size of string ; Removing substring of same character from modified string ; Print the minimum steps required ; Driver Code ; Given S ; Function Call
from math import ceil NEW_LINE def minSteps ( S ) : NEW_LINE INDENT new_str = " " NEW_LINE N = len ( S ) NEW_LINE i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT new_str += S [ i ] NEW_LINE j = i NEW_LINE while ( i < N and S [ i ] == S [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT print ( ceil ( ( len ( new_str ) + 1 ) / 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "0010100" NEW_LINE minSteps ( S ) NEW_LINE DEDENT
Count pairs with equal Bitwise AND and Bitwise OR value | Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
def countPairs ( arr , N ) : NEW_LINE INDENT cntPairs = 0 NEW_LINE mp = { } NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for freq in mp : NEW_LINE INDENT cntPairs += int ( ( mp [ freq ] * ( mp [ freq ] - 1 ) ) / 2 ) NEW_LINE DEDENT return cntPairs NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Minimum increments required to make given matrix palindromic | Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
def palindromeMatrix ( N , M , arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( ( N + 1 ) // 2 ) : NEW_LINE INDENT for j in range ( ( M + 1 ) // 2 ) : NEW_LINE INDENT s = { } NEW_LINE s [ ( i , j ) ] = 1 NEW_LINE s [ ( i , M - j - 1 ) ] = 1 NEW_LINE s [ ( N - i - 1 , j ) ] = 1 NEW_LINE s [ ( N - i - 1 , M - j - 1 ) ] = 1 NEW_LINE values = [ ] NEW_LINE for p , q in s : NEW_LINE INDENT values . append ( arr [ p ] [ q ] ) NEW_LINE DEDENT maxm = max ( values ) NEW_LINE for k in range ( len ( values ) ) : NEW_LINE INDENT ans += maxm - values [ k ] NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , M = 3 , 3 NEW_LINE arr = [ [ 1 , 2 , 1 ] , [ 3 , 4 , 1 ] , [ 1 , 2 , 1 ] ] NEW_LINE palindromeMatrix ( N , M , arr ) NEW_LINE DEDENT
Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Driver Code ; Given Number N ; Function Call
def minimumMoves ( n ) : NEW_LINE INDENT cnt2 = 0 NEW_LINE cnt5 = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n //= 2 NEW_LINE cnt2 += 1 NEW_LINE DEDENT while ( n % 5 == 0 ) : NEW_LINE INDENT n //= 5 NEW_LINE cnt5 += 1 NEW_LINE DEDENT if ( n == 1 and cnt2 <= cnt5 ) : NEW_LINE INDENT return 2 * cnt5 - cnt2 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE print ( minimumMoves ( N ) ) NEW_LINE DEDENT
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code
def check ( n ) : NEW_LINE INDENT if ( ( n & n - 1 ) != 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE check ( n ) NEW_LINE DEDENT
Minimize adding odd and subtracting even numbers to make all array elements equal to K | Function to find the minimum operations required to make array elements equal to K ; Stores minimum count of operations ; Traverse the given array ; If K is greater than arr [ i ] ; If ( K - arr [ i ] ) is even ; Update cntOpe ; Update cntOpe ; If K is less than arr [ i ] ; If ( arr [ i ] - K ) is even ; Update cntOpe ; Update cntOpe ; Driver Code
def MinOperation ( arr , N , K ) : NEW_LINE INDENT cntOpe = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( K > arr [ i ] ) : NEW_LINE INDENT if ( ( K - arr [ i ] ) % 2 == 0 ) : NEW_LINE INDENT cntOpe += 2 NEW_LINE DEDENT else : NEW_LINE INDENT cntOpe += 1 NEW_LINE DEDENT DEDENT elif ( K < arr [ i ] ) : NEW_LINE INDENT if ( ( K - arr [ i ] ) % 2 == 0 ) : NEW_LINE INDENT cntOpe += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntOpe += 2 NEW_LINE DEDENT DEDENT DEDENT return cntOpe NEW_LINE DEDENT arr = [ 8 , 7 , 2 , 1 , 3 ] NEW_LINE K = 5 NEW_LINE N = len ( arr ) NEW_LINE print ( MinOperation ( arr , N , K ) ) NEW_LINE
Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Driver Code ; Given vector A ; Given vector B ; Size of the vector ; Function Call
def rearrangeArray ( A , B , N ) : NEW_LINE INDENT m = { } NEW_LINE xor_value = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT xor_value ^= A [ i ] NEW_LINE xor_value ^= B [ i ] NEW_LINE if B [ i ] in m : NEW_LINE m [ B [ i ] ] = m [ B [ i ] ] + 1 NEW_LINE else : NEW_LINE m [ B [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT B [ i ] = A [ i ] ^ xor_value NEW_LINE if B [ i ] in m : NEW_LINE m [ B [ i ] ] = m [ B [ i ] ] - 1 NEW_LINE else : NEW_LINE X = [ ] NEW_LINE return X NEW_LINE DEDENT return B NEW_LINE DEDENT def rearrangeArrayUtil ( A , B , N ) : NEW_LINE INDENT ans = rearrangeArray ( A , B , N ) NEW_LINE if ( len ( ans ) > 0 ) : NEW_LINE INDENT for x in ans : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 13 , 21 , 33 , 49 , 53 ] NEW_LINE B = [ 54 , 50 , 34 , 22 , 14 ] NEW_LINE N = len ( A ) NEW_LINE rearrangeArrayUtil ( A , B , N ) NEW_LINE DEDENT
Count sequences of given length having non | Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call
def binCoff ( n , r ) : NEW_LINE INDENT val = 1 NEW_LINE if ( r > ( n - r ) ) : NEW_LINE INDENT r = ( n - r ) NEW_LINE DEDENT for i in range ( 0 , r ) : NEW_LINE INDENT val *= ( n - i ) NEW_LINE val //= ( i + 1 ) NEW_LINE DEDENT return val NEW_LINE DEDENT def findWays ( M ) : NEW_LINE INDENT n = M // 2 NEW_LINE a = binCoff ( 2 * n , n ) NEW_LINE b = a // ( n + 1 ) NEW_LINE print ( b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 4 NEW_LINE X = 5 NEW_LINE findWays ( M ) NEW_LINE DEDENT
XOR of array elements whose modular inverse with a given number exists | Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Drive Code ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def countInverse ( arr , N , M ) : NEW_LINE INDENT XOR = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT gcdOfMandelement = gcd ( M , arr [ i ] ) NEW_LINE if ( gcdOfMandelement == 1 ) : NEW_LINE INDENT XOR = XOR ^ arr [ i ] NEW_LINE DEDENT DEDENT print ( XOR ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE M = 4 NEW_LINE N = len ( arr ) NEW_LINE countInverse ( arr , N , M ) NEW_LINE DEDENT
Count positions in a chessboard that can be visited by the Queen which are not visited by the King | Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Driver Code ; Dimension of Board ; Position of Cell ; Function Call
def Moves_Calculator ( x , y , row , col ) : NEW_LINE INDENT total_moves = 0 NEW_LINE if ( row - x ) > 0 and ( col - y ) > 0 : NEW_LINE INDENT total_moves += min ( ( row - x ) , ( col - y ) ) NEW_LINE DEDENT if ( y - 1 ) > 0 and ( x - 1 ) > 0 : NEW_LINE INDENT total_moves += min ( ( y - 1 ) , ( x - 1 ) ) NEW_LINE DEDENT if ( x - 1 ) > 0 and ( col - y ) > 0 : NEW_LINE INDENT total_moves += min ( ( x - 1 ) , ( col - y ) ) NEW_LINE DEDENT if ( row - x ) > 0 and ( y - 1 ) > 0 : NEW_LINE INDENT total_moves += min ( ( row - x ) , ( y - 1 ) ) NEW_LINE DEDENT total_moves += ( row - 1 ) + ( col - 1 ) NEW_LINE king_moves = 0 NEW_LINE if x + 1 <= m : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if x - 1 > 0 : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if y + 1 <= n : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if y - 1 > 0 : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if x + 1 <= m and y + 1 <= n : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if x + 1 <= m and y - 1 > 0 : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if x - 1 > 0 and y - 1 > 0 : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT if x - 1 > 0 and y + 1 <= n : NEW_LINE INDENT king_moves += 1 NEW_LINE DEDENT return total_moves - king_moves NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , m = 8 , 8 NEW_LINE x , y = 1 , 1 NEW_LINE print ( Moves_Calculator ( x , y , m , n ) ) NEW_LINE DEDENT
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number | Function to find the nearest smaller number satisfying the condition ; Driver Code
def clstNum ( N ) : NEW_LINE INDENT return ( N - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE print ( clstNum ( N ) ) NEW_LINE DEDENT
Count array elements having modular inverse under given prime number P equal to itself | Function to get the count of elements that satisfy the given condition . ; Stores count of elements that satisfy the condition ; Traverse the given array . ; If square of current element is equal to 1 ; Driver Code
def equvInverse ( arr , N , P ) : NEW_LINE INDENT cntElem = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) : NEW_LINE INDENT cntElem = cntElem + 1 NEW_LINE DEDENT DEDENT return cntElem NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 6 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE P = 7 NEW_LINE print ( equvInverse ( arr , N , P ) ) NEW_LINE DEDENT
Count ways to split array into K non | Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver Code
def power ( K , M ) : NEW_LINE INDENT res = 1 NEW_LINE while ( M > 0 ) : NEW_LINE INDENT if ( ( M & 1 ) == 1 ) : NEW_LINE INDENT res = ( res * K ) NEW_LINE DEDENT M = M >> 1 NEW_LINE K = ( K * K ) NEW_LINE DEDENT return res NEW_LINE DEDENT def cntWays ( arr , N , K ) : NEW_LINE INDENT cntways = 0 NEW_LINE M = 0 NEW_LINE st = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT st . add ( arr [ i ] ) NEW_LINE DEDENT M = len ( st ) NEW_LINE cntways = power ( K , M ) NEW_LINE return cntways NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( cntWays ( arr , N , K ) ) NEW_LINE DEDENT
Minimize product of first N | Python3 program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Driver Code ; Given number N ; Function call
mod = 1e9 + 7 NEW_LINE def minProduct ( n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , ( n - 4 ) // 2 + 1 ) : NEW_LINE INDENT ans = ( ans * ( n - 2 ) ) % mod NEW_LINE DEDENT ans = ( ans * ( n - 2 ) * ( n - 1 ) ) % mod NEW_LINE print ( int ( ans ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE minProduct ( N ) NEW_LINE DEDENT
Bitwise XOR of all unordered pairs from a given array | Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver code
def TotalXorPair ( arr , N ) : NEW_LINE INDENT totalXOR = 0 ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT totalXOR ^= arr [ i ] ^ arr [ j ] ; NEW_LINE DEDENT DEDENT return totalXOR ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( TotalXorPair ( arr , N ) ) ; NEW_LINE DEDENT
Count permutations of all integers upto N that can form an acyclic graph based on given conditions | Find the count of possible graphs ; Driver code
def possibleAcyclicGraph ( N ) : NEW_LINE INDENT print ( pow ( 2 , N - 1 ) ) NEW_LINE return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE possibleAcyclicGraph ( N ) NEW_LINE DEDENT
Minimize positive product of two given numbers by at most N decrements | Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code
def minProd ( X , Y , N ) : NEW_LINE INDENT if ( X <= Y ) : NEW_LINE INDENT if ( N < X ) : NEW_LINE INDENT return ( X - N ) * Y NEW_LINE DEDENT else : NEW_LINE INDENT return max ( Y - ( N - X + 1 ) , 1 ) NEW_LINE DEDENT DEDENT if ( Y >= N ) : NEW_LINE INDENT return ( Y - N ) * X NEW_LINE DEDENT return max ( X - ( N - Y + 1 ) , 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 47 NEW_LINE Y = 42 NEW_LINE N = 167 NEW_LINE print ( minProd ( X , Y , N ) ) NEW_LINE DEDENT
Count pairs from an array having product of their sum and difference equal to 1 | Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
def countPairs ( arr , n ) : NEW_LINE INDENT oneCount = 0 NEW_LINE desiredPair = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT oneCount += 1 NEW_LINE DEDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT desiredPair += oneCount NEW_LINE DEDENT DEDENT return desiredPair NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Smallest missing non | Function to prthe smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code
def smlstNonNeg ( arr , N ) : NEW_LINE INDENT smNonNeg = 0 NEW_LINE hash = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and arr [ i ] < N ) : NEW_LINE INDENT hash [ arr [ i ] ] = True NEW_LINE DEDENT while ( hash [ smNonNeg ] ) : NEW_LINE INDENT smNonNeg += 1 NEW_LINE DEDENT print ( smNonNeg , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 2 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE smlstNonNeg ( arr , N ) NEW_LINE DEDENT