codeparrot/xlcost-text-to-code huggingface dataset:
Count of integers up to N which are non divisors and non coprime with N | Function to return the count of integers less than N satisfying given conditions ; Stores Euler counts ; Store Divisor counts ; Based on Sieve of Eratosthenes ; Update phi values of all multiples of i ; Update count of divisors ; Return the final count ; Driver code
def count ( n ) : NEW_LINE INDENT phi = [ 0 ] * ( n + 1 ) NEW_LINE divs = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT phi [ i ] += i NEW_LINE for j in range ( i * 2 , n + 1 , i ) : NEW_LINE INDENT phi [ j ] -= phi [ i ] ; NEW_LINE DEDENT for j in range ( i , n + 1 , i ) : NEW_LINE INDENT divs [ j ] += 1 NEW_LINE DEDENT DEDENT return ( n - phi [ n ] - divs [ n ] + 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 42 NEW_LINE print ( count ( N ) ) NEW_LINE DEDENT
Sum of all differences between Maximum and Minimum of increasing Subarrays | Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver Code
def sum_of_differences ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE INDENT if arr [ i ] < arr [ i + 1 ] : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( i + 1 , N - 1 ) : NEW_LINE INDENT if arr [ j ] >= arr [ j + 1 ] : NEW_LINE INDENT sum += ( arr [ j ] - arr [ i ] ) NEW_LINE i = j NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if flag == 0 and arr [ i ] < arr [ N - 1 ] : NEW_LINE INDENT sum += ( arr [ N - 1 ] - arr [ i ] ) NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 6 , 1 , 2 , 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( sum_of_differences ( arr , N ) ) NEW_LINE
Construct a Maximum Binary Tree from two given Binary Trees | A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and None left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to find the maximum binary tree from two binary trees ; Driver Code ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8
class Node : NEW_LINE INDENT def __init__ ( self , data , left , right ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT return Node ( data , None , None ) ; NEW_LINE DEDENT def inorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT inorder ( node . left ) ; NEW_LINE print ( node . data , end = ' ▁ ' ) ; NEW_LINE inorder ( node . right ) ; NEW_LINE DEDENT def MaximumBinaryTree ( t1 , t2 ) : NEW_LINE INDENT if ( t1 == None ) : NEW_LINE INDENT return t2 ; NEW_LINE DEDENT if ( t2 == None ) : NEW_LINE INDENT return t1 ; NEW_LINE DEDENT t1 . data = max ( t1 . data , t2 . data ) ; NEW_LINE t1 . left = MaximumBinaryTree ( t1 . left , t2 . left ) ; NEW_LINE t1 . right = MaximumBinaryTree ( t1 . right , t2 . right ) ; NEW_LINE return t1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 3 ) ; NEW_LINE root1 . left = newNode ( 2 ) ; NEW_LINE root1 . right = newNode ( 6 ) ; NEW_LINE root1 . left . left = newNode ( 20 ) ; NEW_LINE root2 = newNode ( 5 ) ; NEW_LINE root2 . left = newNode ( 1 ) ; NEW_LINE root2 . right = newNode ( 8 ) ; NEW_LINE root2 . left . right = newNode ( 2 ) ; NEW_LINE root2 . right . right = newNode ( 8 ) ; NEW_LINE root3 = MaximumBinaryTree ( root1 , root2 ) ; NEW_LINE inorder ( root3 ) ; NEW_LINE DEDENT
Minimum size binary string required such that probability of deleting two 1 's at random is 1/X | Python3 implementation of the above approach ; Function returns the minimum size of the string ; From formula ; Left limit of r ; Right limit of r ; Smallest integer in the valid range ; Driver Code
from math import sqrt NEW_LINE def MinimumString ( x ) : NEW_LINE INDENT b = 1 NEW_LINE left_lim = sqrt ( x ) + 1.0 NEW_LINE right_lim = sqrt ( x ) + 2.0 NEW_LINE for i in range ( int ( left_lim ) , int ( right_lim ) + 1 ) : NEW_LINE INDENT if ( i > left_lim and i < right_lim ) : NEW_LINE INDENT r = i NEW_LINE break NEW_LINE DEDENT DEDENT return b + r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 2 NEW_LINE print ( MinimumString ( X ) ) NEW_LINE DEDENT
Minimum number of squares whose sum equals to a given number N | Set | Python3 program for the above approach ; Function that returns True if N is a perfect square ; Function that returns True check if N is sum of three squares ; Factor out the powers of 4 ; N is NOT of the form 4 ^ a * ( 8 b + 7 ) ; Function that finds the minimum number of square whose sum is N ; If N is perfect square ; If N is sum of 2 perfect squares ; If N is sum of 3 perfect squares ; Otherwise , N is the sum of 4 perfect squares ; Driver code ; Given number ; Function call
from math import sqrt , floor , ceil NEW_LINE def isPerfectSquare ( N ) : NEW_LINE INDENT floorSqrt = floor ( sqrt ( N ) ) NEW_LINE return ( N == floorSqrt * floorSqrt ) NEW_LINE DEDENT def legendreFunction ( N ) : NEW_LINE INDENT while ( N % 4 == 0 ) : NEW_LINE INDENT N //= 4 NEW_LINE DEDENT if ( N % 8 != 7 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def minSquares ( N ) : NEW_LINE INDENT if ( isPerfectSquare ( N ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if i * i < N : NEW_LINE INDENT break NEW_LINE DEDENT if ( isPerfectSquare ( N - i * i ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT if ( legendreFunction ( N ) ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT return 4 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 123 NEW_LINE print ( minSquares ( N ) ) NEW_LINE DEDENT
Check if N leaves only distinct remainders on division by all values up to K | Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert into the set ; Driver Code
def is_distinct ( n , k ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT tmp = n % i NEW_LINE if ( tmp in s ) : NEW_LINE INDENT return False NEW_LINE DEDENT s . add ( tmp ) NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE if ( is_distinct ( N , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of multiplicative partitions of N | Function to return number of ways of factoring N with all factors greater than 1 ; Variable to store number of ways of factoring n with all factors greater than 1 ; Driver code ; 2 is the minimum factor of number other than 1. So calling recursive function to find number of ways of factoring N with all factors greater than 1
def getDivisors ( min , n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( min , n ) : NEW_LINE INDENT if ( n % i == 0 and n // i >= i ) : NEW_LINE INDENT total += 1 NEW_LINE if ( n // i > i ) : NEW_LINE INDENT total += getDivisors ( i , n // i ) NEW_LINE DEDENT DEDENT DEDENT return total NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 30 NEW_LINE print ( 1 + getDivisors ( 2 , n ) ) NEW_LINE DEDENT
Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; de represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getLastTwoPerson ( n ) : NEW_LINE INDENT total = n NEW_LINE head = newNode ( 1 ) NEW_LINE temp = head NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT temp . next = newNode ( i ) NEW_LINE temp = temp . next NEW_LINE DEDENT temp . next = head NEW_LINE temp = head NEW_LINE de = None NEW_LINE while ( total != 2 ) : NEW_LINE INDENT de = temp . next . next NEW_LINE temp . next . next = temp . next . next . next NEW_LINE temp = temp . next NEW_LINE del de NEW_LINE total -= 1 NEW_LINE DEDENT print ( temp . val , temp . next . val ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE getLastTwoPerson ( n ) NEW_LINE DEDENT
Philaland Coin | TCS Mockvita 2020 | Python3 implementation to find the minimum number of denominations required for any number ; Function to find the minimum number of denomminations required ; Driver Code ; Function call
from math import log2 , floor NEW_LINE def findMinDenomin ( n ) : NEW_LINE INDENT return log2 ( n ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( floor ( findMinDenomin ( n ) ) ) NEW_LINE DEDENT
Content of a Polynomial | Python3 implementation to find the content of the polynomial ; Function to find the content of the polynomial ; Loop to iterate over the elements of the array ; __gcd ( a , b ) is a inbuilt function for Greatest Common Divisor ; Driver Code ; Function call
from math import gcd NEW_LINE def findContent ( arr , n ) : NEW_LINE INDENT content = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT content = gcd ( content , arr [ i ] ) NEW_LINE DEDENT return content NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE arr = [ 9 , 6 , 12 ] NEW_LINE print ( findContent ( arr , n ) ) NEW_LINE DEDENT
Check if the given array is same as its inverse permutation | Function to check if the inverse permutation of the given array is same as the original array ; Stores the inverse permutation ; Generate the inverse permutation ; Check if the inverse permutation is same as the given array ; Driver code
def inverseEqual ( arr , n ) : NEW_LINE INDENT brr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT present_index = arr [ i ] - 1 NEW_LINE brr [ present_index ] = i + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] != brr [ i ] : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT n = 4 NEW_LINE arr = [ 1 , 4 , 3 , 2 ] NEW_LINE inverseEqual ( arr , n ) NEW_LINE
Linear Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Driver Code ; Seed value ; Modulus parameter ; Multiplier term ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
def linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) : NEW_LINE INDENT randomNums [ 0 ] = Xo NEW_LINE for i in range ( 1 , noOfRandomNums ) : NEW_LINE INDENT randomNums [ i ] = ( ( randomNums [ i - 1 ] * a ) + c ) % m NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Xo = 5 NEW_LINE m = 7 NEW_LINE a = 3 NEW_LINE c = 3 NEW_LINE noOfRandomNums = 10 NEW_LINE randomNums = [ 0 ] * ( noOfRandomNums ) NEW_LINE linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) NEW_LINE for i in randomNums : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Multiplicative Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Driver Code ; Seed value ; Modulus parameter ; Multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
def multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) : NEW_LINE INDENT randomNums [ 0 ] = Xo NEW_LINE for i in range ( 1 , noOfRandomNums ) : NEW_LINE INDENT randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Xo = 3 NEW_LINE m = 15 NEW_LINE a = 7 NEW_LINE noOfRandomNums = 10 NEW_LINE randomNums = [ 0 ] * ( noOfRandomNums ) NEW_LINE multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) NEW_LINE for i in randomNums : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Product of divisors of a number from a given list of its prime factors | Python3 program to implement the above approach ; Function to calculate ( a ^ b ) % m ; Function to calculate and return the product of divisors ; Stores the frequencies of prime divisors ; Iterate over the prime divisors ; Update the product ; Update the count of divisors ; Driver Code
from collections import defaultdict NEW_LINE MOD = 1000000007 NEW_LINE def power ( a , b , m ) : NEW_LINE INDENT a %= m NEW_LINE res = 1 NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( b & 1 ) : NEW_LINE INDENT res = ( ( res % m ) * ( a % m ) ) % m NEW_LINE DEDENT a = ( ( a % m ) * ( a % m ) ) % m NEW_LINE b >>= 1 NEW_LINE DEDENT return res % m NEW_LINE DEDENT def productOfDivisors ( p , n ) : NEW_LINE INDENT prime = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT prime [ p [ i ] ] += 1 NEW_LINE DEDENT product , d = 1 , 1 NEW_LINE for itr in prime . keys ( ) : NEW_LINE INDENT val = ( power ( itr , ( prime [ itr ] ) * ( prime [ itr ] + 1 ) // 2 , MOD ) ) NEW_LINE product = ( power ( product , prime [ itr ] + 1 , MOD ) * power ( val , d , MOD ) % MOD ) NEW_LINE d = ( d * ( prime [ itr ] + 1 ) ) % ( MOD - 1 ) NEW_LINE DEDENT return product NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 11 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( productOfDivisors ( arr , n ) ) NEW_LINE DEDENT
Print all proper fractions with denominators less than equal to N | Function to print all proper functions ; If the numerator and denominator are coprime ; Driver code
def printfractions ( n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n + 1 ) : NEW_LINE if __gcd ( i , j ) == 1 : NEW_LINE INDENT a = str ( i ) NEW_LINE b = str ( j ) NEW_LINE print ( a + ' / ' + b , end = " , ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE printfractions ( n ) NEW_LINE DEDENT
Maximum number of objects that can be created as per given conditions | Function for finding the maximum number of objects from N type - 1 and M type - 2 items ; Storing minimum of N and M ; Storing maximum number of objects from given items ; Driver Code
def numberOfObjects ( N , M ) : NEW_LINE INDENT initial = min ( N , M ) NEW_LINE final = ( N + M ) // 3 NEW_LINE return min ( initial , final ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE M = 7 NEW_LINE print ( numberOfObjects ( N , M ) ) NEW_LINE DEDENT
Square root of a number by Repeated Subtraction method | Function to return the square root of the given number ; Subtract n - th odd number ; Return the result ; Driver Code
def SquareRoot ( num ) : NEW_LINE INDENT count = 0 NEW_LINE for n in range ( 1 , num + 1 , 2 ) : NEW_LINE INDENT num = num - n NEW_LINE count = count + 1 NEW_LINE if ( num == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 81 NEW_LINE print ( SquareRoot ( N ) ) NEW_LINE
Maximize count of distinct elements possible in an Array from the given operation | Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver Code
def gcd ( x , y ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return y NEW_LINE DEDENT return gcd ( y % x , x ) NEW_LINE DEDENT def findDistinct ( arr , n ) : NEW_LINE INDENT maximum = max ( arr ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return ( maximum // gcd ( arr [ 0 ] , arr [ 1 ] ) ) NEW_LINE DEDENT k = gcd ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT k = gcd ( k , arr [ i ] ) NEW_LINE DEDENT return ( maximum // k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findDistinct ( arr , n ) ) NEW_LINE DEDENT
Find indices of all local maxima and local minima in an Array | Function to find all the local maxima and minima in the given array arr [ ] ; Empty lists to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or neither ; Iterating over all points to check local maxima and local minima ; Condition for local minima ; Condition for local maxima ; Checking whether the last point is local maxima or minima or neither ; Print all the local maxima and local minima indexes stored ; Driver Code ; Given array arr [ ] ; Function Call
def findLocalMaximaMinima ( n , arr ) : NEW_LINE INDENT mx = [ ] NEW_LINE mn = [ ] NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT mx . append ( 0 ) NEW_LINE DEDENT elif ( arr [ 0 ] < arr [ 1 ] ) : NEW_LINE INDENT mn . append ( 0 ) NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] > arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT mn . append ( i ) NEW_LINE DEDENT elif ( arr [ i - 1 ] < arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT mx . append ( i ) NEW_LINE DEDENT DEDENT if ( arr [ - 1 ] > arr [ - 2 ] ) : NEW_LINE INDENT mx . append ( n - 1 ) NEW_LINE DEDENT elif ( arr [ - 1 ] < arr [ - 2 ] ) : NEW_LINE INDENT mn . append ( n - 1 ) NEW_LINE DEDENT if ( len ( mx ) > 0 ) : NEW_LINE INDENT print ( " Points ▁ of ▁ Local ▁ maxima " \ " ▁ are ▁ : ▁ " , end = ' ' ) NEW_LINE print ( * mx ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ points ▁ of " \ " ▁ Local ▁ maxima . " ) NEW_LINE DEDENT if ( len ( mn ) > 0 ) : NEW_LINE INDENT print ( " Points ▁ of ▁ Local ▁ minima " \ " ▁ are ▁ : ▁ " , end = ' ' ) NEW_LINE print ( * mn ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ points " \ " ▁ of ▁ Local ▁ minima . " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE arr = [ 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 ] NEW_LINE findLocalMaximaMinima ( N , arr ) NEW_LINE DEDENT
Smallest N digit number with none of its digits as its divisor | Function to calculate power ; Function to check if the N - digit number satisfies the given condition or not ; Getting the last digit ; Every number is divisible by 1 and dividing a number by 0 isn 't possible. Thus  numbers with 0 as a digit  must be discarded. ; If any digit divides the number , return false ; If no digit divides the number , return true ; ; Function to find the smallest number not divisible by any of its digits . ; Get the lower range of n digit number ; Get the high range of n digit number ; check all the N - digit numbers ; If the first number to satify the constraints is found print it , set the flag value and break out of the loop . ; If no such digit found , return - 1 as per problem statement ; Driver code
def power ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( b == 1 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT tmp = power ( a , b // 2 ) ; NEW_LINE result = tmp * tmp ; NEW_LINE if ( b % 2 == 1 ) : NEW_LINE INDENT result *= a ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT temp = n ; NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT last_digit = temp % 10 ; NEW_LINE if ( last_digit == 0 or last_digit == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n % last_digit == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT temp = temp // 10 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def solve ( n ) : NEW_LINE INDENT L = power ( 10 , n - 1 ) ; NEW_LINE R = power ( 10 , n ) - 1 ; NEW_LINE flag = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT answer = check ( i ) ; NEW_LINE if ( answer ) : NEW_LINE INDENT print ( i ) NEW_LINE flag += 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE solve ( N ) ; NEW_LINE DEDENT
Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; Initializing cost = 0 ; To store the removed element ; This will store the sum of the subsequence ; Checking all the element of the vector ; Storing the value of arr [ i ] in temp variable ; If the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; Insert the element in the set removedElements ; Storing the value of arr [ i ] in temp ; Taking the element not in removedElements ; Adding the value of elements of subsequence ; If we have to remove the element then we need to add the cost associated with the element ; Printing the sum of the subsequence with minimum length possible ; Printing the cost incurred in creating subsequence ; Driver code ; Calling the function
def costOfSubsequence ( N , arr , costArray ) : NEW_LINE INDENT i , temp , cost = 0 , 0 , 0 NEW_LINE removedElements = { ' ' } NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if ( ( ( arr [ i - 1 ] < temp ) and ( temp < arr [ i + 1 ] ) ) or ( ( arr [ i - 1 ] > temp ) and ( temp > arr [ i + 1 ] ) ) ) : NEW_LINE INDENT removedElements . add ( temp ) NEW_LINE DEDENT DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE if ( temp not in removedElements ) : NEW_LINE INDENT ans = ans + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT cost += costArray [ i ] NEW_LINE DEDENT DEDENT print ( ans , end = " , ▁ " ) NEW_LINE print ( cost ) NEW_LINE DEDENT N = 4 NEW_LINE arr = [ 1 , 3 , 4 , 2 ] NEW_LINE costArray = [ 0 , 1 , 0 , 0 ] NEW_LINE costOfSubsequence ( N , arr , costArray ) NEW_LINE
XOR of a subarray ( range of elements ) | Set 2 | Function to find XOR in a range from L to R ; Compute xor from arr [ 0 ] to arr [ i ] ; Process every query in constant time ; If L == 0 ; Driver code ; query [ ]
def find_Xor ( arr , query , N , Q ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] = arr [ i ] ^ arr [ i - 1 ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( Q ) : NEW_LINE INDENT if query [ i ] [ 0 ] == 0 : NEW_LINE INDENT ans = arr [ query [ i ] [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( arr [ query [ i ] [ 0 ] - 1 ] ^ arr [ query [ i ] [ 1 ] ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 ] NEW_LINE N = 8 NEW_LINE Q = 2 NEW_LINE query = [ [ 1 , 4 ] , [ 3 , 7 ] ] NEW_LINE find_Xor ( arr , query , N , Q ) NEW_LINE DEDENT main ( ) NEW_LINE