codeparrot/xlcost-text-to-code huggingface dataset:
A backtracking approach to generate n bit Gray Codes | we have 2 choices for each of the n bits either we can include i . e invert the bit or we can exclude the bit i . e we can leave the number as it is . ; base case when we run out bits to process we simply include it in gray code sequence . ; ignore the bit . ; invert the bit . ; returns the vector containing the gray code sequence of n bits . ; num is passed by reference to keep track of current code . ; Driver Code
def grayCodeUtil ( res , n , num ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT res . append ( num [ 0 ] ) NEW_LINE return NEW_LINE DEDENT grayCodeUtil ( res , n - 1 , num ) NEW_LINE num [ 0 ] = num [ 0 ] ^ ( 1 << ( n - 1 ) ) NEW_LINE grayCodeUtil ( res , n - 1 , num ) NEW_LINE DEDENT def grayCodes ( n ) : NEW_LINE INDENT res = [ ] NEW_LINE num = [ 0 ] NEW_LINE grayCodeUtil ( res , n , num ) NEW_LINE return res NEW_LINE DEDENT n = 3 NEW_LINE code = grayCodes ( n ) NEW_LINE for i in range ( len ( code ) ) : NEW_LINE INDENT print ( code [ i ] ) NEW_LINE DEDENT
Remove Invalid Parentheses | Method checks if character is parenthesis ( openor closed ) ; method returns true if contains valid parenthesis ; method to remove invalid parenthesis ; visit set to ignore already visited ; queue to maintain BFS ; pushing given as starting node into queu ; If answer is found , make level true so that valid of only that level are processed . ; Removing parenthesis from str and pushing into queue , if not visited already ; Driver Code
def isParenthesis ( c ) : NEW_LINE INDENT return ( ( c == ' ( ' ) or ( c == ' ) ' ) ) NEW_LINE DEDENT def isValidString ( str ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == ' ( ' ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT elif ( str [ i ] == ' ) ' ) : NEW_LINE INDENT cnt -= 1 NEW_LINE DEDENT if ( cnt < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return ( cnt == 0 ) NEW_LINE DEDENT def removeInvalidParenthesis ( str ) : NEW_LINE INDENT if ( len ( str ) == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT visit = set ( ) NEW_LINE q = [ ] NEW_LINE temp = 0 NEW_LINE level = 0 NEW_LINE q . append ( str ) NEW_LINE visit . add ( str ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT str = q [ 0 ] NEW_LINE q . pop ( ) NEW_LINE if ( isValidString ( str ) ) : NEW_LINE INDENT print ( str ) NEW_LINE level = True NEW_LINE DEDENT if ( level ) : NEW_LINE INDENT continue NEW_LINE DEDENT for i in range ( len ( str ) ) : NEW_LINE INDENT if ( not isParenthesis ( str [ i ] ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT temp = str [ 0 : i ] + str [ i + 1 : ] NEW_LINE if temp not in visit : NEW_LINE INDENT q . append ( temp ) NEW_LINE visit . add ( temp ) NEW_LINE DEDENT DEDENT DEDENT DEDENT expression = " ( ) ( ) ) ( ) " NEW_LINE removeInvalidParenthesis ( expression ) NEW_LINE expression = " ( ) v ) " NEW_LINE removeInvalidParenthesis ( expression ) NEW_LINE
N Queen Problem | Backtracking | Python3 program to solve N Queen Problem using backtracking ; ld is an array where its indices indicate row - col + N - 1 ( N - 1 ) is for shifting the difference to store negative indices ; rd is an array where its indices indicate row + col and used to check whether a queen can be placed on right diagonal or not ; column array where its indices indicates column and used to check whether a queen can be placed in that row or not ; A utility function to print solution ; A recursive utility function to solve N Queen problem ; base case : If all queens are placed then return True ; Consider this column and try placing this queen in all rows one by one ; A check if a queen can be placed on board [ row ] [ col ] . We just need to check ld [ row - col + n - 1 ] and rd [ row + coln ] where ld and rd are for left and right diagonal respectively ; Place this queen in board [ i ] [ col ] ; recur to place rest of the queens ; board [ i ] [ col ] = 0 BACKTRACK ; If the queen cannot be placed in any row in this colum col then return False ; This function solves the N Queen problem using Backtracking . It mainly uses solveNQUtil ( ) to solve the problem . It returns False if queens cannot be placed , otherwise , return True and prints placement of queens in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; Driver Code
N = 4 NEW_LINE ld = [ 0 ] * 30 NEW_LINE rd = [ 0 ] * 30 NEW_LINE cl = [ 0 ] * 30 NEW_LINE def printSolution ( board ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( board [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def solveNQUtil ( board , col ) : NEW_LINE INDENT if ( col >= N ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( ( ld [ i - col + N - 1 ] != 1 and rd [ i + col ] != 1 ) and cl [ i ] != 1 ) : NEW_LINE INDENT board [ i ] [ col ] = 1 NEW_LINE ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 1 NEW_LINE if ( solveNQUtil ( board , col + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 0 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solveNQ ( ) : NEW_LINE INDENT board = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE if ( solveNQUtil ( board , 0 ) == False ) : NEW_LINE INDENT printf ( " Solution ▁ does ▁ not ▁ exist " ) NEW_LINE return False NEW_LINE DEDENT printSolution ( board ) NEW_LINE return True NEW_LINE DEDENT solveNQ ( ) NEW_LINE
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Function to print pairs ; Initializing the rem values with 0 's. ; Perform the remainder operation only if the element is x , as numbers greater than x can 't  be used to get a sum x.Updating  the count of remainders. ; Traversing the remainder list from start to middle to find pairs ; The elements with remainders i and x - i will result to a sum of x . Once we get two elements which add up to x , we print x and break . ; Once we reach middle of remainder array , we have to do operations based on x . ; If x is even and we have more than 1 elements with remainder x / 2 , then we will have two distinct elements which add up to x . if we dont have than 1 element , print " No " . ; When x is odd we continue the same process which we did in previous loop . ; Driver Code ; Function calling
def printPairs ( a , n , x ) : NEW_LINE INDENT rem = [ ] NEW_LINE for i in range ( x ) : NEW_LINE INDENT rem . append ( 0 ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] < x ) : NEW_LINE INDENT rem [ a [ i ] % x ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , x // 2 ) : NEW_LINE INDENT if ( rem [ i ] > 0 and rem [ x - i ] > 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( i >= x // 2 ) : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT if ( rem [ x // 2 ] > 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( rem [ x // 2 ] > 0 and rem [ x - x // 2 ] > 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT A = [ 1 , 4 , 45 , 6 , 10 , 8 ] NEW_LINE n = 16 NEW_LINE arr_size = len ( A ) NEW_LINE printPairs ( A , arr_size , n ) NEW_LINE
Find remainder when a number A raised to N factorial is divided by P | Function to calculate factorial of a Number ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
def fact ( n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT ans *= i NEW_LINE DEDENT return ans NEW_LINE DEDENT def remainder ( n , a , p ) : NEW_LINE INDENT len1 = fact ( n ) NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , len1 + 1 , 1 ) : NEW_LINE INDENT ans = ( ans * a ) % p NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 NEW_LINE N = 1 NEW_LINE P = 2 NEW_LINE print ( remainder ( N , A , P ) ) NEW_LINE DEDENT
Check if a number N can be expressed in base B | Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a boolean variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Driver code ; Given number N and base B ; Function call
def check ( n , w ) : NEW_LINE INDENT a = [ 0 for i in range ( 105 ) ] ; NEW_LINE p = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a [ p ] = n % w NEW_LINE p += 1 NEW_LINE n //= w NEW_LINE DEDENT flag = True NEW_LINE for i in range ( 101 ) : NEW_LINE INDENT if ( a [ i ] == 0 or a [ i ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( a [ i ] == w or a [ i ] == w - 1 ) : NEW_LINE INDENT a [ i + 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE DEDENT DEDENT return flag NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT B = 3 NEW_LINE N = 7 NEW_LINE if ( check ( N , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of groups among N people having only one leader in each group | Python3 program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Driver code ; Given N number of people ; Function call
mod = 1000000007 NEW_LINE def exponentMod ( A , B ) : NEW_LINE INDENT if ( A == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT y = 0 ; NEW_LINE if ( B % 2 == 0 ) : NEW_LINE INDENT y = exponentMod ( A , B // 2 ) ; NEW_LINE y = ( y * y ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT y = A % mod ; NEW_LINE y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; NEW_LINE DEDENT return ( ( y + mod ) % mod ) ; NEW_LINE DEDENT def countWays ( N ) : NEW_LINE INDENT select = exponentMod ( 2 , N - 1 ) ; NEW_LINE ways = ( ( N % mod ) * ( select % mod ) ) ; NEW_LINE ways %= mod ; NEW_LINE print ( ways ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE countWays ( N ) ; NEW_LINE DEDENT
Expected number of moves to reach the end of a board | Matrix Exponentiation | Python3 implementation of the approach ; Function to multiply two 7 * 7 matrix ; Function to perform matrix exponentiation ; 7 * 7 identity matrix ; Loop to find the power ; Function to return the required count ; Base cases ; Multiplier matrix ; Finding the required multiplier i . e mul ^ ( X - 6 ) ; Final answer ; Driver code
import numpy as np NEW_LINE maxSize = 50 NEW_LINE def matrix_product ( a , b ) : NEW_LINE INDENT c = np . zeros ( ( 7 , 7 ) ) ; NEW_LINE for i in range ( 7 ) : NEW_LINE INDENT for j in range ( 7 ) : NEW_LINE INDENT for k in range ( 7 ) : NEW_LINE INDENT c [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return c NEW_LINE DEDENT def mul_expo ( mul , p ) : NEW_LINE INDENT s = [ [ 1 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ] ; NEW_LINE while ( p != 1 ) : NEW_LINE INDENT if ( p % 2 == 1 ) : NEW_LINE INDENT s = matrix_product ( s , mul ) ; NEW_LINE DEDENT mul = matrix_product ( mul , mul ) ; NEW_LINE p //= 2 ; NEW_LINE DEDENT return matrix_product ( mul , s ) ; NEW_LINE DEDENT def expectedSteps ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( x <= 6 ) : NEW_LINE INDENT return 6 ; NEW_LINE DEDENT mul = [ [ 7 / 6 , 1 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ - 1 / 6 , 0 , 0 , 0 , 0 , 0 , 0 ] ] ; NEW_LINE mul = mul_expo ( mul , x - 6 ) ; NEW_LINE return ( mul [ 0 ] [ 0 ] + mul [ 1 ] [ 0 ] + mul [ 2 ] [ 0 ] + mul [ 3 ] [ 0 ] + mul [ 4 ] [ 0 ] + mul [ 5 ] [ 0 ] ) * 6 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE print ( expectedSteps ( n - 1 ) ) ; NEW_LINE DEDENT
Place the prisoners into cells to maximize the minimum difference between any two | Function that returns true if the prisoners can be placed such that the minimum distance between any two prisoners is at least sep ; Considering the first prisoner is placed at 1 st cell ; If the first prisoner is placed at the first cell then the last_prisoner_placed will be the first prisoner placed and that will be in cell [ 0 ] ; Checking if the prisoner can be placed at ith cell or not ; If all the prisoners got placed then return true ; Function to return the maximized distance ; Sort the array so that binary search can be applied on it ; Minimum possible distance for the search space ; Maximum possible distance for the search space ; To store the result ; Binary search ; If the prisoners can be placed such that the minimum distance between any two prisoners is at least mid ; Update the answer ; Driver code
def canPlace ( a , n , p , sep ) : NEW_LINE INDENT prisoners_placed = 1 NEW_LINE last_prisoner_placed = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT current_cell = a [ i ] NEW_LINE if ( current_cell - last_prisoner_placed >= sep ) : NEW_LINE INDENT prisoners_placed += 1 NEW_LINE last_prisoner_placed = current_cell NEW_LINE if ( prisoners_placed == p ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def maxDistance ( cell , n , p ) : NEW_LINE INDENT cell = sorted ( cell ) NEW_LINE start = 0 NEW_LINE end = cell [ n - 1 ] - cell [ 0 ] NEW_LINE ans = 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( ( end - start ) // 2 ) NEW_LINE if ( canPlace ( cell , n , p , mid ) ) : NEW_LINE INDENT ans = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT cell = [ 1 , 2 , 8 , 4 , 9 ] NEW_LINE n = len ( cell ) NEW_LINE p = 3 NEW_LINE print ( maxDistance ( cell , n , p ) ) NEW_LINE
Find N in the given matrix that follows a pattern | Function to return the row and the column of the given integer ; Binary search for the row number ; Condition to get the maximum x that satisfies the criteria ; Binary search for the column number ; Condition to get the maximum y that satisfies the criteria ; Get the row and the column number ; Return the pair ; Driver code
def solve ( n ) : NEW_LINE INDENT low = 1 NEW_LINE high = 10 ** 4 NEW_LINE x , p = n , 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( x - sum >= 1 ) : NEW_LINE INDENT p = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT start , end , y , q = 1 , 10 ** 4 , 1 , 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( y + sum <= n ) : NEW_LINE INDENT q = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT x = x - ( p * ( p + 1 ) ) // 2 NEW_LINE y = y + ( q * ( q + 1 ) ) // 2 NEW_LINE r = x NEW_LINE c = q + 1 - n + y NEW_LINE return r , c NEW_LINE DEDENT n = 5 NEW_LINE r , c = solve ( n ) NEW_LINE print ( r , c ) NEW_LINE
Find the count of distinct numbers in a range | Function to perform queries in a range ; No overlap ; Totally Overlap ; Partial Overlap ; Finding the Answer for the left Child ; Finding the Answer for the right Child ; Combining the BitMasks ; Function to perform update operation in the Segment seg ; Forming the BitMask ; Updating the left Child ; Updating the right Child ; Updating the BitMask ; Building the Segment Tree ; Building the Initial BitMask ; Building the left seg tree ; Building the right seg tree ; Forming the BitMask ; Utility Function to answer the queries ; Counting the set bits which denote the distinct elements ; Updating the value ; Driver Code
def query ( start , end , left , right , node , seg ) : NEW_LINE INDENT if ( end < left or start > right ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( start >= left and end <= right ) : NEW_LINE INDENT return seg [ node ] NEW_LINE DEDENT else : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE leftChild = query ( start , mid , left , right , 2 * node , seg ) NEW_LINE rightChild = query ( mid + 1 , end , left , right , 2 * node + 1 , seg ) NEW_LINE return ( leftChild rightChild ) NEW_LINE DEDENT DEDENT def update ( left , right , index , Value , node , ar , seg ) : NEW_LINE INDENT if ( left == right ) : NEW_LINE INDENT ar [ index ] = Value NEW_LINE seg [ node ] = ( 1 << Value ) NEW_LINE return NEW_LINE DEDENT mid = ( left + right ) // 2 NEW_LINE if ( index > mid ) : NEW_LINE INDENT update ( mid + 1 , right , index , Value , 2 * node + 1 , ar , seg ) NEW_LINE DEDENT else : NEW_LINE INDENT update ( left , mid , index , Value , 2 * node , ar , seg ) NEW_LINE DEDENT seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) NEW_LINE DEDENT def build ( left , right , node , ar , eg ) : NEW_LINE INDENT if ( left == right ) : NEW_LINE INDENT seg [ node ] = ( 1 << ar [ left ] ) NEW_LINE return NEW_LINE DEDENT mid = ( left + right ) // 2 NEW_LINE build ( left , mid , 2 * node , ar , seg ) NEW_LINE build ( mid + 1 , right , 2 * node + 1 , ar , seg ) NEW_LINE seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) NEW_LINE DEDENT def getDistinctCount ( queries , ar , seg , n ) : NEW_LINE INDENT for i in range ( len ( queries ) ) : NEW_LINE INDENT op = queries [ i ] [ 0 ] NEW_LINE if ( op == 2 ) : NEW_LINE INDENT l = queries [ i ] [ 1 ] NEW_LINE r = queries [ i ] [ 2 ] NEW_LINE tempMask = query ( 0 , n - 1 , l - 1 , r - 1 , 1 , seg ) NEW_LINE countOfBits = 0 NEW_LINE for i in range ( 63 , - 1 , - 1 ) : NEW_LINE INDENT if ( tempMask & ( 1 << i ) ) : NEW_LINE INDENT countOfBits += 1 NEW_LINE DEDENT DEDENT print ( countOfBits ) NEW_LINE DEDENT else : NEW_LINE INDENT index = queries [ i ] [ 1 ] NEW_LINE val = queries [ i ] [ 2 ] NEW_LINE update ( 0 , n - 1 , index - 1 , val , 1 , ar , seg ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE ar = [ 1 , 2 , 1 , 3 , 1 , 2 , 1 ] NEW_LINE seg = [ 0 ] * 4 * n NEW_LINE build ( 0 , n - 1 , 1 , ar , seg ) NEW_LINE q = 5 NEW_LINE queries = [ [ 2 , 1 , 4 ] , [ 1 , 4 , 2 ] , [ 1 , 5 , 2 ] , [ 2 , 4 , 6 ] , [ 2 , 1 , 7 ] ] NEW_LINE getDistinctCount ( queries , ar , seg , n ) NEW_LINE DEDENT
Smallest subarray with GCD as 1 | Segment Tree | Python3 implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Buildinng the segment tree ; Two pointer variables ; To store the final answer ; Loopinng ; Incrementing j till we don 't get a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code
from math import gcd as __gcd NEW_LINE maxLen = 30 NEW_LINE seg = [ 0 for i in range ( 3 * maxLen ) ] NEW_LINE def build ( l , r , inn , arr ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT seg [ inn ] = arr [ l ] NEW_LINE return seg [ inn ] NEW_LINE DEDENT mid = ( l + r ) // 2 NEW_LINE seg [ inn ] = __gcd ( build ( l , mid , 2 * inn + 1 , arr ) , build ( mid + 1 , r , 2 * inn + 2 , arr ) ) NEW_LINE return seg [ inn ] NEW_LINE DEDENT def query ( l , r , l1 , r1 , inn ) : NEW_LINE INDENT if ( l1 <= l and r <= r1 ) : NEW_LINE INDENT return seg [ inn ] NEW_LINE DEDENT if ( l > r1 or r < l1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mid = ( l + r ) // 2 NEW_LINE x = __gcd ( query ( l , mid , l1 , r1 , 2 * inn + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * inn + 2 ) ) NEW_LINE return x NEW_LINE DEDENT def findLen ( arr , n ) : NEW_LINE INDENT build ( 0 , n - 1 , 0 , arr ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE ans = 10 ** 9 NEW_LINE while ( i < n ) : NEW_LINE INDENT while ( j < n and query ( 0 , n - 1 , i , j , 0 ) != 1 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ans = minn ( ( j - i + 1 ) , ans ) NEW_LINE i += 1 NEW_LINE j = max ( j , i ) NEW_LINE DEDENT if ( ans == 10 ** 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ans NEW_LINE DEDENT DEDENT arr = [ 2 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findLen ( arr , n ) ) NEW_LINE
Find an N x N grid whose xor of every row and column is equal | Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code
def findGrid ( n ) : NEW_LINE INDENT arr = [ [ 0 for k in range ( n ) ] for l in range ( n ) ] NEW_LINE x = 0 NEW_LINE for i in range ( n // 4 ) : NEW_LINE INDENT for j in range ( n // 4 ) : NEW_LINE INDENT for k in range ( 4 ) : NEW_LINE INDENT for l in range ( 4 ) : NEW_LINE INDENT arr [ i * 4 + k ] [ j * 4 + l ] = x NEW_LINE x += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE findGrid ( n ) NEW_LINE
Kth smallest element in the array using constant space when array can 't be modified | Function to return the kth smallest element from the array ; Minimum and maximum element from the array ; Modified binary search ; To store the count of elements from the array which are less than mid and the elements which are equal to mid ; If mid is the kth smallest ; If the required element is less than mid ; If the required element is greater than mid ; Driver code
def kthSmallest ( arr , k , n ) : NEW_LINE INDENT low = min ( arr ) ; NEW_LINE high = max ( arr ) ; NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 ; NEW_LINE countless = 0 ; countequal = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < mid ) : NEW_LINE INDENT countless += 1 ; NEW_LINE DEDENT elif ( arr [ i ] == mid ) : NEW_LINE INDENT countequal += 1 ; NEW_LINE DEDENT DEDENT if ( countless < k and ( countless + countequal ) >= k ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT elif ( countless >= k ) : NEW_LINE INDENT high = mid - 1 ; NEW_LINE DEDENT elif ( countless < k and countless + countequal < k ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 10 , 4 , 3 , 20 , 15 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 3 ; NEW_LINE print ( kthSmallest ( arr , k , n ) ) ; NEW_LINE DEDENT
Interactive Problems in Competitive Programming |  ; Iterating from lower_bound to upper_bound ; Input the response from the judge
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT lower_bound = 2 ; NEW_LINE upper_bound = 10 ; NEW_LINE for i in range ( lower_bound , upper_bound + 1 ) : NEW_LINE INDENT print ( i ) NEW_LINE response = int ( input ( ) ) NEW_LINE if ( response == 0 ) : NEW_LINE INDENT print ( " Number ▁ guessed ▁ is ▁ : " , i , end = ' ' ) NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT
Find 2 ^ ( 2 ^ A ) % B | Function to return 2 ^ ( 2 ^ A ) % B ; Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ A ) % B
def F ( A , B ) : NEW_LINE INDENT if ( A == 1 ) : NEW_LINE INDENT return ( 4 % B ) ; NEW_LINE DEDENT else : NEW_LINE INDENT temp = F ( A - 1 , B ) ; NEW_LINE return ( temp * temp ) % B ; NEW_LINE DEDENT DEDENT A = 25 ; NEW_LINE B = 50 ; NEW_LINE print ( F ( A , B ) ) ; NEW_LINE