codeparrot/xlcost-text-to-code huggingface dataset:
Difference between maximum and minimum average of all K | Function to find the difference between averages of the maximum and the minimum subarrays of length k ; Stores min and max sum ; Iterate through starting points ; Sum up next K elements ; Update max and min moving sum ; Return the difference between max and min average ; Given Input ; Function Call
def Avgdifference ( arr , N , K ) : NEW_LINE INDENT min = 1000000 ; NEW_LINE max = - 1 ; NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for j in range ( K ) : NEW_LINE INDENT sum += arr [ i + j ] ; NEW_LINE DEDENT if ( min > sum ) : NEW_LINE INDENT min = sum ; NEW_LINE DEDENT if ( max < sum ) : NEW_LINE INDENT max = sum ; NEW_LINE DEDENT DEDENT return ( max - min ) / K ; NEW_LINE DEDENT arr = [ 3 , 8 , 9 , 15 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 2 ; NEW_LINE print ( Avgdifference ( arr , N , K ) ) ; NEW_LINE
Find a number in minimum steps | Python3 program to Find a number in minimum steps ; Steps sequence ; Current sum ; Sign of the number ; Basic steps required to get sum >= required value . ; If we have reached ahead to destination . ; If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum . ; If the current time instance is even and sum is odd than it takes 2 more steps and few negations in previous elements to reach there . ; Driver code
def find ( n ) : NEW_LINE INDENT ans = [ ] NEW_LINE Sum = 0 NEW_LINE i = 0 NEW_LINE sign = 0 NEW_LINE if ( n >= 0 ) : NEW_LINE INDENT sign = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sign = - 1 NEW_LINE DEDENT n = abs ( n ) NEW_LINE i = 1 NEW_LINE while ( Sum < n ) : NEW_LINE INDENT ans . append ( sign * i ) NEW_LINE Sum += i NEW_LINE i += 1 NEW_LINE DEDENT if ( Sum > sign * n ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT Sum -= n NEW_LINE if ( Sum % 2 != 0 ) : NEW_LINE INDENT ans . append ( sign * i ) NEW_LINE Sum += i NEW_LINE i += 1 NEW_LINE DEDENT ans [ int ( Sum / 2 ) - 1 ] *= - 1 NEW_LINE DEDENT else : NEW_LINE INDENT Sum -= n NEW_LINE if ( Sum % 2 != 0 ) : NEW_LINE INDENT Sum -= 1 NEW_LINE ans . append ( sign * i ) NEW_LINE ans . append ( sign * - 1 * ( i + 1 ) ) NEW_LINE DEDENT ans [ int ( ( sum / 2 ) ) - 1 ] *= - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT n = 20 NEW_LINE if ( n == 0 ) : NEW_LINE INDENT print ( " Minimum number of Steps : 0 Step sequence : 0 " ) NEW_LINE DEDENT else : NEW_LINE INDENT a = find ( n ) NEW_LINE print ( " Minimum ▁ number ▁ of ▁ Steps : " , len ( a ) ) NEW_LINE print ( " Step ▁ sequence : " ) NEW_LINE print ( * a , sep = " ▁ " ) NEW_LINE DEDENT
Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | Python 3 program for the above approach ; Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; Driver Code ; Given Input ; Function Call
import sys NEW_LINE def MaxRange ( A , n ) : NEW_LINE INDENT left = [ 0 ] * n NEW_LINE right = [ 0 ] * n NEW_LINE s = [ ] NEW_LINE s . append ( ( sys . maxsize , - 1 ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( s [ - 1 ] [ 0 ] < A [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT left [ i ] = s [ - 1 ] [ 1 ] NEW_LINE s . append ( ( A [ i ] , i ) ) NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT s . append ( ( sys . maxsize , n ) ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( s [ - 1 ] [ 0 ] < A [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT right [ i ] = s [ - 1 ] [ 1 ] NEW_LINE s . append ( ( A [ i ] , i ) ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( left [ i ] + 1 , ' ▁ ' , right [ i ] - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE MaxRange ( arr , n ) NEW_LINE DEDENT
Find Nth smallest number having exactly 4 divisors | Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Given Input ; Function Call
def nthNumber ( n ) : NEW_LINE INDENT divs = [ 0 for i in range ( 1000000 ) ] ; NEW_LINE vis = [ 0 for i in range ( 1000000 ) ] ; NEW_LINE cnt = 0 ; NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( divs [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , 1000000 ) : NEW_LINE INDENT if ( vis [ j ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT vis [ j ] = 1 ; NEW_LINE currNum = j ; NEW_LINE count = 0 ; NEW_LINE while ( currNum % i == 0 ) : NEW_LINE INDENT divs [ j ] += 1 ; NEW_LINE currNum = currNum // i ; NEW_LINE count += 1 ; NEW_LINE DEDENT if ( currNum == 1 and count == 3 and divs [ j ] == 3 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT elif ( currNum != 1 and divs [ currNum ] == 0 and count == 1 and divs [ j ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( cnt == n ) : NEW_LINE INDENT return j ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return - 1 ; NEW_LINE DEDENT N = 24 ; NEW_LINE print ( nthNumber ( N ) ) ; NEW_LINE
Maximum number formed from the digits of given three numbers | Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Driver Code ; Given Input ; Function call
def findkey ( A , B , C ) : NEW_LINE INDENT ans = 0 NEW_LINE cur = 1 NEW_LINE while ( A > 0 ) : NEW_LINE INDENT a = A % 10 NEW_LINE b = B % 10 NEW_LINE c = C % 10 NEW_LINE A = A // 10 NEW_LINE B = B // 10 NEW_LINE C = C // 10 NEW_LINE m = max ( a , max ( c , b ) ) NEW_LINE ans += cur * m NEW_LINE cur = cur * 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 3521 NEW_LINE B = 2452 NEW_LINE C = 1352 NEW_LINE print ( findkey ( A , B , C ) ) NEW_LINE DEDENT
Count of distinct GCDs among all the non | Python 3 program for the above approach ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; calculate largest number in A and mapping A to Mp ; iterate over all possible values of GCD ; variable to check current GCD ; iterate over all multiples of i ; If j is present in A ; calculate gcd of all encountered multiples of i ; current GCD is possible ; return answer ; Driver code ; Input ; Function calling
from math import gcd NEW_LINE def distinctGCDs ( arr , N ) : NEW_LINE INDENT M = - 1 NEW_LINE ans = 0 NEW_LINE Mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT M = max ( M , arr [ i ] ) NEW_LINE Mp [ arr [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , M + 1 , 1 ) : NEW_LINE INDENT currGcd = 0 NEW_LINE for j in range ( i , M + 1 , i ) : NEW_LINE INDENT if ( j in Mp ) : NEW_LINE INDENT currGcd = gcd ( currGcd , j ) NEW_LINE if ( currGcd == i ) : NEW_LINE INDENT ans += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 11 , 14 , 6 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE print ( distinctGCDs ( arr , N ) ) NEW_LINE DEDENT
Compress a Binary Tree into an integer diagonally | Python program for the above approach ; Function to compress the elements in an array into an integer ; Check for each bit position ; Update the count of set and non - set bits ; If number of set bits exceeds the number of non - set bits , then add set bits value to ans ; Function to compress a given Binary Tree into an integer ; Declare a map ; Perform Inorder Traversal on the Binary Tree ; Store all nodes of the same line together as a vector ; Increase the vertical distance of left child ; Vertical distance remains same for right child ; Store all the compressed values of diagonal elements in an array ; Compress the array into an integer ; Driver Code Given Input ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = ' ' , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def findCompressValue ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE getBit = 1 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT S = 0 NEW_LINE NS = 0 NEW_LINE for j in arr : NEW_LINE INDENT if getBit & j : NEW_LINE INDENT S += 1 NEW_LINE DEDENT else : NEW_LINE INDENT NS += 1 NEW_LINE DEDENT DEDENT if S > NS : NEW_LINE INDENT ans += 2 ** i NEW_LINE DEDENT getBit <<= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def findInteger ( root ) : NEW_LINE INDENT mp = { } NEW_LINE def diagonalOrder ( root , d , mp ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT try : NEW_LINE INDENT mp [ d ] . append ( root . val ) NEW_LINE DEDENT except KeyError : NEW_LINE INDENT mp [ d ] = [ root . val ] NEW_LINE DEDENT diagonalOrder ( root . left , d + 1 , mp ) NEW_LINE diagonalOrder ( root . right , d , mp ) NEW_LINE DEDENT diagonalOrder ( root , 0 , mp ) NEW_LINE arr = [ ] NEW_LINE for i in mp : NEW_LINE INDENT arr . append ( findCompressValue ( mp [ i ] ) ) NEW_LINE DEDENT return findCompressValue ( arr ) NEW_LINE DEDENT root = TreeNode ( 6 ) NEW_LINE root . left = TreeNode ( 5 ) NEW_LINE root . right = TreeNode ( 3 ) NEW_LINE root . left . left = TreeNode ( 3 ) NEW_LINE root . left . right = TreeNode ( 5 ) NEW_LINE root . right . left = TreeNode ( 3 ) NEW_LINE root . right . right = TreeNode ( 4 ) NEW_LINE print ( findInteger ( root ) ) NEW_LINE
Egg Dropping Puzzle | DP |  ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver Code
import sys NEW_LINE def eggDrop ( n , k ) : NEW_LINE INDENT if ( k == 1 or k == 0 ) : NEW_LINE INDENT return k NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return k NEW_LINE DEDENT min = sys . maxsize NEW_LINE for x in range ( 1 , k + 1 ) : NEW_LINE INDENT res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) NEW_LINE if ( res < min ) : NEW_LINE INDENT min = res NEW_LINE DEDENT DEDENT return min + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE k = 10 NEW_LINE print ( " Minimum ▁ number ▁ of ▁ trials ▁ in ▁ worst ▁ case ▁ with " , n , " eggs ▁ and " , k , " floors ▁ is " , eggDrop ( n , k ) ) NEW_LINE DEDENT
K | Function to find the kth digit from last in an eger n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Pr the right most digit ; Driver code ; Given Input ; Function call
def kthDigitFromLast ( n , k ) : NEW_LINE INDENT if ( k <= 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT while ( ( k - 1 ) > 0 and n > 0 ) : NEW_LINE INDENT n = n / 10 NEW_LINE k -= 1 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( int ( n % 10 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2354 NEW_LINE k = 2 NEW_LINE kthDigitFromLast ( n , k ) NEW_LINE DEDENT
Edit Distance | DP | A Space efficient Dynamic Programming based Python3 program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second String is empty then we remove all characters ; Start filling the DP This loop run for every character in second String ; This loop compares the char from second String with first String characters ; If first String is empty then we have to perform add character operation to get second String ; If character from both String is same then we do not perform any operation . here i % 2 is for bound the row number . ; If character from both String is not same then we take the minimum from three specified operation ; After complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver code
def EditDistDP ( str1 , str2 ) : NEW_LINE INDENT len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE DP = [ [ 0 for i in range ( len1 + 1 ) ] for j in range ( 2 ) ] ; NEW_LINE for i in range ( 0 , len1 + 1 ) : NEW_LINE INDENT DP [ 0 ] [ i ] = i NEW_LINE DEDENT for i in range ( 1 , len2 + 1 ) : NEW_LINE INDENT for j in range ( 0 , len1 + 1 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT DP [ i % 2 ] [ j ] = i NEW_LINE DEDENT elif ( str1 [ j - 1 ] == str2 [ i - 1 ] ) : NEW_LINE INDENT DP [ i % 2 ] [ j ] = DP [ ( i - 1 ) % 2 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT DP [ i % 2 ] [ j ] = ( 1 + min ( DP [ ( i - 1 ) % 2 ] [ j ] , min ( DP [ i % 2 ] [ j - 1 ] , DP [ ( i - 1 ) % 2 ] [ j - 1 ] ) ) ) NEW_LINE DEDENT DEDENT DEDENT print ( DP [ len2 % 2 ] [ len1 ] , " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " food " NEW_LINE str2 = " money " NEW_LINE EditDistDP ( str1 , str2 ) NEW_LINE DEDENT
Sum of Bitwise XOR of elements of an array with all elements of another array | Function to calculate sum of Bitwise XOR of elements of arr [ ] with k ; Initialize sum to be zero ; Iterate over each set bit ; Stores contribution of i - th bet to the sum ; If the i - th bit is set ; Stores count of elements whose i - th bit is not set ; Update value ; Update value ; Add value to sum ; Move to the next power of two ; Stores the count of elements whose i - th bit is set ; Traverse the array ; Iterate over each bit ; If the i - th bit is set ; Increase count ; Driver Code
def xorSumOfArray ( arr , n , k , count ) : NEW_LINE INDENT sum = 0 NEW_LINE p = 1 NEW_LINE for i in range ( 31 ) : NEW_LINE INDENT val = 0 NEW_LINE if ( ( k & ( 1 << i ) ) != 0 ) : NEW_LINE INDENT not_set = n - count [ i ] NEW_LINE val = ( ( not_set ) * p ) NEW_LINE DEDENT else : NEW_LINE INDENT val = ( count [ i ] * p ) NEW_LINE DEDENT sum += val NEW_LINE p = ( p * 2 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumOfXors ( arr , n , queries , q ) : NEW_LINE INDENT count = [ 0 for i in range ( 32 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 31 ) : NEW_LINE INDENT if ( arr [ i ] & ( 1 << j ) ) : NEW_LINE INDENT count [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT k = queries [ i ] NEW_LINE print ( xorSumOfArray ( arr , n , k , count ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 3 ] NEW_LINE queries = [ 3 , 8 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE q = len ( queries ) NEW_LINE sumOfXors ( arr , n , queries , q ) NEW_LINE DEDENT
Minimum time required to schedule K processes | Function to execute k processes that can be gained in minimum amount of time ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
def executeProcesses ( A , N , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( A [ i ] ) NEW_LINE DEDENT ans = 0 NEW_LINE pq . sort ( ) NEW_LINE while ( len ( pq ) > 0 and K > 0 ) : NEW_LINE INDENT top = pq . pop ( ) NEW_LINE ans += 1 NEW_LINE K -= top NEW_LINE top //= 2 NEW_LINE pq . append ( top ) NEW_LINE pq . sort ( ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT A = [ 3 , 1 , 7 , 4 , 2 ] NEW_LINE K = 15 NEW_LINE N = len ( A ) NEW_LINE executeProcesses ( A , N , K ) NEW_LINE
Median of all nodes from a given range in a Binary Search Tree ( BST ) | Tree Node structure ; Function to create a new BST node ; Function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Return the node pointer ; Function to find all the nodes that lies over the range [ node1 , node2 ] ; If the tree is empty , return ; Traverse for the left subtree ; If a second node is found , then update the flag as false ; Traverse the right subtree ; Function to find the median of all the values in the given BST that lies over the range [ node1 , node2 ] ; Stores all the nodes in the range [ node1 , node2 ] ; Store the size of the array ; Print the median of array based on the size of array ; Given BST
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT interNodes = [ ] NEW_LINE def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE return temp NEW_LINE DEDENT def insertNode ( node , key ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if ( key < node . key ) : NEW_LINE INDENT node . left = insertNode ( node . left , key ) NEW_LINE DEDENT elif ( key > node . key ) : NEW_LINE INDENT node . right = insertNode ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT def getIntermediateNodes ( root , node1 , node2 ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT getIntermediateNodes ( root . left , node1 , node2 ) NEW_LINE if ( root . key <= node2 and root . key >= node1 ) : NEW_LINE INDENT interNodes . append ( root . key ) NEW_LINE DEDENT getIntermediateNodes ( root . right , node1 , node2 ) NEW_LINE DEDENT def findMedian ( root , node1 , node2 ) : NEW_LINE INDENT getIntermediateNodes ( root , node1 , node2 ) NEW_LINE nSize = len ( interNodes ) NEW_LINE if nSize % 2 == 1 : NEW_LINE INDENT return interNodes [ int ( nSize / 2 ) ] NEW_LINE DEDENT else : NEW_LINE INDENT return ( interNodes [ int ( ( nSize - 1 ) / 2 ) ] + interNodes [ nSize / 2 ] ) / 2 NEW_LINE DEDENT DEDENT root = None NEW_LINE root = insertNode ( root , 8 ) NEW_LINE insertNode ( root , 3 ) NEW_LINE insertNode ( root , 1 ) NEW_LINE insertNode ( root , 6 ) NEW_LINE insertNode ( root , 4 ) NEW_LINE insertNode ( root , 11 ) NEW_LINE insertNode ( root , 15 ) NEW_LINE print ( findMedian ( root , 3 , 11 ) ) NEW_LINE
Count number of pairs ( i , j ) up to N that can be made equal on multiplying with a pair from the range [ 1 , N / 2 ] | Function to compute totient of all numbers smaller than or equal to N ; Iterate over the range [ 2 , N ] ; If phi [ p ] is not computed already then p is prime ; Phi of a prime number p is ( p - 1 ) ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Function to count the pairs ( i , j ) from the range [ 1 , N ] , satisfying the given condition ; Stores the counts of first and second type of pairs respectively ; Count of first type of pairs ; Stores the phi or totient values ; Calculate the Phi values ; Iterate over the range [ N / 2 + 1 , N ] ; Update the value of cnt_type2 ; Print the total count ; Driver Code
def computeTotient ( N , phi ) : NEW_LINE INDENT for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if phi [ p ] == p : NEW_LINE INDENT phi [ p ] = p - 1 NEW_LINE for i in range ( 2 * p , N + 1 , p ) : NEW_LINE INDENT phi [ i ] = ( phi [ i ] // p ) * ( p - 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def countPairs ( N ) : NEW_LINE INDENT cnt_type1 = 0 NEW_LINE cnt_type2 = 0 NEW_LINE half_N = N // 2 NEW_LINE cnt_type1 = ( half_N * ( half_N - 1 ) ) // 2 NEW_LINE phi = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT phi [ i ] = i NEW_LINE DEDENT computeTotient ( N , phi ) NEW_LINE for i in range ( ( N // 2 ) + 1 , N + 1 ) : NEW_LINE INDENT cnt_type2 += ( i - phi [ i ] - 1 ) NEW_LINE DEDENT print ( cnt_type1 + cnt_type2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE countPairs ( N ) NEW_LINE DEDENT
Sum of subsets nearest to K possible from two given arrays | Stores the sum closest to K ; Stores the minimum absolute difference ; Function to choose the elements from the array B [ ] ; If absolute difference is less then minimum value ; Update the minimum value ; Update the value of ans ; If absolute difference between curr and K is equal to minimum ; Update the value of ans ; If i is greater than M - 1 ; Includes the element B [ i ] once ; Includes the element B [ i ] twice ; Excludes the element B [ i ] ; Function to find a subset sum whose sum is closest to K ; Traverse the array A [ ] ; Function Call ; Return the ans ; Driver Code ; Input ; Function Call
ans = 10 ** 8 NEW_LINE mini = 10 ** 8 NEW_LINE def findClosestTarget ( i , curr , B , M , K ) : NEW_LINE INDENT global ans , mini NEW_LINE if ( abs ( curr - K ) < mini ) : NEW_LINE INDENT mini = abs ( curr - K ) NEW_LINE ans = curr NEW_LINE DEDENT if ( abs ( curr - K ) == mini ) : NEW_LINE INDENT ans = min ( ans , curr ) NEW_LINE DEDENT if ( i >= M ) : NEW_LINE INDENT return NEW_LINE DEDENT findClosestTarget ( i + 1 , curr + B [ i ] , B , M , K ) NEW_LINE findClosestTarget ( i + 1 , curr + 2 * B [ i ] , B , M , K ) NEW_LINE findClosestTarget ( i + 1 , curr , B , M , K ) NEW_LINE DEDENT def findClosest ( A , B , N , M , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT findClosestTarget ( 0 , A [ i ] , B , M , K ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 3 ] NEW_LINE B = [ 4 , 5 , 30 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE K = 18 NEW_LINE print ( findClosest ( A , B , N , M , K ) ) NEW_LINE DEDENT
Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations |  ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver Code ; Given number of nodes ; Given Directed Tree
/ * Function to check if there is a NEW_LINE INDENT node in tree from where all other NEW_LINE nodes are accessible or not * / NEW_LINE DEDENT def findNode ( mp , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = mp [ i + 1 ] NEW_LINE DEDENT count0 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT count0 -= 1 NEW_LINE if ( count0 <= ( n ) / ( 2 ) ) : 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 = 3 NEW_LINE mp = { } NEW_LINE mp [ 1 ] = 0 NEW_LINE mp [ 2 ] = 2 NEW_LINE mp [ 3 ] = 0 NEW_LINE findNode ( mp , N ) NEW_LINE DEDENT