codeparrot/xlcost-text-to-code huggingface dataset:
Maximum non | Function to prthe maximum rooks and their positions ; Marking the location of already placed rooks ; Print number of non - attacking rooks that can be placed ; To store the placed rook location ; Print lexographically smallest order ; Driver Code ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call
def countRooks ( n , k , pos ) : NEW_LINE INDENT row = [ 0 for i in range ( n ) ] NEW_LINE col = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT row [ pos [ i ] [ 0 ] - 1 ] = 1 NEW_LINE col [ pos [ i ] [ 1 ] - 1 ] = 1 NEW_LINE DEDENT res = n - k NEW_LINE print ( res ) NEW_LINE ri = 0 NEW_LINE ci = 0 NEW_LINE while ( res > 0 ) : NEW_LINE INDENT while ( row [ ri ] == 1 ) : NEW_LINE INDENT ri += 1 NEW_LINE DEDENT while ( col [ ci ] == 1 ) : NEW_LINE INDENT ci += 1 NEW_LINE DEDENT print ( ( ri + 1 ) , ( ci + 1 ) ) NEW_LINE ri += 1 NEW_LINE ci += 1 NEW_LINE res -= 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE K = 2 NEW_LINE pos = [ [ 1 , 4 ] , [ 2 , 2 ] ] NEW_LINE countRooks ( N , K , pos ) NEW_LINE DEDENT
Minimum insertions to form a palindrome with permutations allowed | Python3 program to find minimum number of insertions to make a string palindrome ; Function will return number of characters to be added ; To store str1ing length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if str1ing is already palindrome return 0 ; Driver Code
import math as mt NEW_LINE def minInsertion ( tr1 ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE res = 0 NEW_LINE count = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( count [ i ] % 2 == 1 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT if ( res == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return res - 1 NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE print ( minInsertion ( str1 ) ) NEW_LINE
Check for Palindrome after every character replacement Query | Function to check if string is Palindrome or Not ; Takes two inputs for Q queries . For every query , it prints Yes if string becomes palindrome and No if not . ; Process all queries one by one ; query 1 : i1 = 3 , i2 = 0 , ch = ' e ' query 2 : i1 = 0 , i2 = 2 , ch = ' s ' replace character at index i1 & i2 with new ' ch ' ; check string is palindrome or not ; Driver Code
def isPalindrome ( string : list ) -> bool : NEW_LINE INDENT n = len ( string ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT if string [ i ] != string [ n - 1 - i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Query ( string : list , Q : int ) -> None : NEW_LINE INDENT for i in range ( Q ) : NEW_LINE INDENT inp = list ( input ( ) . split ( ) ) NEW_LINE i1 = int ( inp [ 0 ] ) NEW_LINE i2 = int ( inp [ 1 ] ) NEW_LINE ch = inp [ 2 ] NEW_LINE string [ i1 ] = string [ i2 ] = ch NEW_LINE if isPalindrome ( string ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = list ( " geeks " ) NEW_LINE Q = 2 NEW_LINE Query ( string , Q ) NEW_LINE DEDENT
Generate a string which differs by only a single character from all given strings | Function to check if a given string differs by a single character from all the strings in an array ; Traverse over the strings ; Stores the count of characters differing from the strings ; If differs by more than one character ; Function to find the string which only differ at one position from the all given strings of the array ; Size of the array ; Length of a string ; Replace i - th character by all possible characters ; Check if it differs by a single character from all other strings ; If desired string is obtained ; Print the answer ; Driver Code ; Function call
def check ( ans , s , n , m ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( m ) : NEW_LINE INDENT if ( ans [ j ] != s [ i ] [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findString ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE m = len ( s [ 0 ] ) NEW_LINE ans = s [ 0 ] NEW_LINE flag = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT x = list ( ans ) NEW_LINE x [ i ] = chr ( j + ord ( ' a ' ) ) NEW_LINE if ( check ( x , s , n , m ) ) : NEW_LINE INDENT ans = x NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT else : NEW_LINE INDENT return ' ' . join ( ans ) NEW_LINE DEDENT DEDENT s = [ " geeks " , " teeds " ] NEW_LINE print ( findString ( s ) ) NEW_LINE
Minimum increments of Non | Function to return to the minimum number of operations required to make the array non - decreasing ; Stores the count of operations ; If arr [ i ] > arr [ i + 1 ] , add arr [ i ] - arr [ i + 1 ] to the answer Otherwise , add 0 ; Driver Code
def getMinOps ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT ans += max ( arr [ i ] - arr [ i + 1 ] , 0 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 3 , 1 , 2 , 4 ] NEW_LINE print ( getMinOps ( arr ) ) NEW_LINE
Maximum difference between frequency of two elements such that element having greater frequency is also greater | Python program to find maximum difference between frequency of any two element such that element with greater frequency is also greater in value . ; Return the maximum difference between frequencies of any two elements such that element with greater frequency is also greater in value . ; Finding the frequency of each element . ; finding difference such that element having greater frequency is also greater in value . ; Driver Code
from collections import defaultdict NEW_LINE def maxdiff ( arr , n ) : NEW_LINE INDENT freq = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if freq [ arr [ i ] ] > freq [ arr [ j ] ] and arr [ i ] > arr [ j ] : NEW_LINE INDENT ans = max ( ans , freq [ arr [ i ] ] - freq [ arr [ j ] ] ) NEW_LINE DEDENT elif freq [ arr [ i ] ] < freq [ arr [ j ] ] and arr [ i ] < arr [ j ] : NEW_LINE INDENT ans = max ( ans , freq [ arr [ j ] ] - freq [ arr [ i ] ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 1 , 3 , 2 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxdiff ( arr , n ) ) NEW_LINE
Maximum difference between frequency of two elements such that element having greater frequency is also greater | Return the maximum difference between frequencies of any two elements such that element with greater frequency is also greater in value . ; Finding the frequency of each element . ; Sorting the distinct element ; Iterate through all sorted distinct elements . For each distinct element , maintaining the element with minimum frequency than that element and also finding the maximum frequency difference ; Driven Program
def maxdiff ( arr , n ) : NEW_LINE INDENT freq = { } NEW_LINE dist = [ 0 ] * n NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] not in freq ) : NEW_LINE INDENT dist [ j ] = arr [ i ] NEW_LINE j += 1 NEW_LINE freq [ arr [ i ] ] = 0 NEW_LINE DEDENT if ( arr [ i ] in freq ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT DEDENT dist = dist [ : j ] NEW_LINE dist . sort ( ) NEW_LINE min_freq = n + 1 NEW_LINE ans = 0 NEW_LINE for i in range ( j ) : NEW_LINE INDENT cur_freq = freq [ dist [ i ] ] NEW_LINE ans = max ( ans , cur_freq - min_freq ) NEW_LINE min_freq = min ( min_freq , cur_freq ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 1 , 3 , 2 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxdiff ( arr , n ) ) NEW_LINE
Maximum Sum possible by selecting X elements from a Matrix based on given conditions | Python3 program to implement the above approach ; Function to calculate the maximum possible sum by selecting X elements from the Matrix ; Generate prefix sum of the matrix ; Initialize [ , ] dp ; Maximum possible sum by selecting 0 elements from the first i rows ; If a single row is present ; If elements from the current row is not selected ; Iterate over all possible selections from current row ; Return maximum possible sum ; Driver Code
import sys NEW_LINE def maxSum ( grid ) : NEW_LINE INDENT prefsum = [ [ 0 for x in range ( m ) ] for y in range ( m ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for x in range ( m ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT prefsum [ i ] [ x ] = grid [ i ] [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT prefsum [ i ] [ x ] = ( prefsum [ i ] [ x - 1 ] + grid [ i ] [ x ] ) NEW_LINE DEDENT DEDENT DEDENT dp = [ [ - sys . maxsize - 1 for x in range ( X + 1 ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT for i in range ( 1 , min ( m , X ) ) : NEW_LINE INDENT dp [ 0 ] [ i ] = ( dp [ 0 ] [ i - 1 ] + grid [ 0 ] [ i - 1 ] ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 1 , X + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE for x in range ( 1 , min ( j , m ) + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - x ] + prefsum [ i ] [ x - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n - 1 ] [ X ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE m = 4 NEW_LINE X = 6 NEW_LINE grid = [ [ 3 , 2 , 6 , 1 ] , [ 1 , 9 , 2 , 4 ] , [ 4 , 1 , 3 , 9 ] , [ 3 , 8 , 2 , 1 ] ] NEW_LINE ans = maxSum ( grid ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Difference between highest and least frequencies in an array | Python3 code to find the difference between highest nd least frequencies ; sort the array ; checking consecutive elements ; Driver Code
def findDiff ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 ; max_count = 0 ; min_count = n NEW_LINE for i in range ( 0 , ( n - 1 ) ) : NEW_LINE INDENT if arr [ i ] == arr [ i + 1 ] : NEW_LINE INDENT count += 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT max_count = max ( max_count , count ) NEW_LINE min_count = min ( min_count , count ) NEW_LINE count = 0 NEW_LINE DEDENT DEDENT return max_count - min_count NEW_LINE DEDENT arr = [ 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findDiff ( arr , n ) ) NEW_LINE
Minimum Decrements on Subarrays required to reduce all Array elements to zero | Function to count the minimum number of subarrays that are required to be decremented by 1 ; Base case ; Initializing ans to first element ; For A [ i ] > A [ i - 1 ] , operation ( A [ i ] - A [ i - 1 ] ) is required ; Return the count ; Driver Code
def min_operations ( A ) : NEW_LINE INDENT if len ( A ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = A [ 0 ] NEW_LINE for i in range ( 1 , len ( A ) ) : NEW_LINE INDENT if A [ i ] > A [ i - 1 ] : NEW_LINE INDENT ans += A [ i ] - A [ i - 1 ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 1 , 2 , 3 , 2 , 1 ] NEW_LINE print ( min_operations ( A ) ) NEW_LINE
Difference between highest and least frequencies in an array | Python code to find the difference between highest and least frequencies ; Put all elements in a hash map ; Find counts of maximum and minimum frequent elements ; Driver code
from collections import defaultdict NEW_LINE def findDiff ( arr , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT max_count = 0 ; min_count = n NEW_LINE for key , values in mp . items ( ) : NEW_LINE INDENT max_count = max ( max_count , values ) NEW_LINE min_count = min ( min_count , values ) NEW_LINE DEDENT return max_count - min_count NEW_LINE DEDENT arr = [ 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findDiff ( arr , n ) ) NEW_LINE
Iterative function to check if two trees are identical | Iterative Python3 program to check if two trees are identical ; Utility function to create a new tree node ; Iterative method to find height of Binary Tree ; Return true if both trees are empty ; Return false if one is empty and other is not ; Create an empty queues for simultaneous traversals ; Enqueue Roots of trees in respective queues ; Get front nodes and compare them ; Remove front nodes from queues ; Enqueue left children of both nodes ; If one left child is empty and other is not ; Right child code ( Similar to left child code ) ; Driver Code
from queue import Queue NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def areIdentical ( root1 , root2 ) : NEW_LINE INDENT if ( root1 and root2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root1 or root2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT q1 = Queue ( ) NEW_LINE q2 = Queue ( ) NEW_LINE q1 . put ( root1 ) NEW_LINE q2 . put ( root2 ) NEW_LINE while ( not q1 . empty ( ) and not q2 . empty ( ) ) : NEW_LINE INDENT n1 = q1 . queue [ 0 ] NEW_LINE n2 = q2 . queue [ 0 ] NEW_LINE if ( n1 . data != n2 . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT q1 . get ( ) NEW_LINE q2 . get ( ) NEW_LINE if ( n1 . left and n2 . left ) : NEW_LINE INDENT q1 . put ( n1 . left ) NEW_LINE q2 . put ( n2 . left ) NEW_LINE DEDENT elif ( n1 . left or n2 . left ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n1 . right and n2 . right ) : NEW_LINE INDENT q1 . put ( n1 . right ) NEW_LINE q2 . put ( n2 . right ) NEW_LINE DEDENT elif ( n1 . right or n2 . right ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 1 ) NEW_LINE root1 . left = newNode ( 2 ) NEW_LINE root1 . right = newNode ( 3 ) NEW_LINE root1 . left . left = newNode ( 4 ) NEW_LINE root1 . left . right = newNode ( 5 ) NEW_LINE root2 = newNode ( 1 ) NEW_LINE root2 . left = newNode ( 2 ) NEW_LINE root2 . right = newNode ( 3 ) NEW_LINE root2 . left . left = newNode ( 4 ) NEW_LINE root2 . left . right = newNode ( 5 ) NEW_LINE if areIdentical ( root1 , root2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph | Maximum number of nodes or vertices that can be present in the graph ; Store the parent of each vertex ; Stores the size of each set ; Function to initialize the parent of each vertices ; Function to find the representative of the set which contain element v ; Path compression technique to optimize the time complexity ; Function to merge two different set into a single set by finding the representative of each set and merge the smallest set with the larger one ; Finding the set representative of each element ; Check if they have different set repersentative ; Compare the set sizes ; Assign parent of smaller set to the larger one ; Add the size of smaller set to the larger one ; Function to check the vertices are on the same set or not ; Check if they have same set representative or not ; Driver code ; Connected vertices and taking them into single set ; Number of queries ; Function call
MAX_NODES = 100005 NEW_LINE parent = [ 0 for i in range ( MAX_NODES ) ] ; NEW_LINE size_set = [ 0 for i in range ( MAX_NODES ) ] ; NEW_LINE def make_set ( v ) : NEW_LINE INDENT parent [ v ] = v ; NEW_LINE size_set [ v ] = 1 ; NEW_LINE DEDENT def find_set ( v ) : NEW_LINE INDENT if ( v == parent [ v ] ) : NEW_LINE INDENT return v ; NEW_LINE DEDENT parent [ v ] = find_set ( parent [ v ] ) ; NEW_LINE return parent [ v ] NEW_LINE DEDENT def union_set ( a , b ) : NEW_LINE INDENT a = find_set ( a ) ; NEW_LINE b = find_set ( b ) ; NEW_LINE if ( a != b ) : NEW_LINE INDENT if ( size_set [ a ] < size_set [ b ] ) : NEW_LINE INDENT swap ( a , b ) ; NEW_LINE DEDENT parent [ b ] = a ; NEW_LINE size_set [ a ] += size_set [ b ] ; NEW_LINE DEDENT DEDENT def check ( a , b ) : NEW_LINE INDENT a = find_set ( a ) ; NEW_LINE b = find_set ( b ) ; NEW_LINE if a == b : NEW_LINE INDENT return ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 3 ; NEW_LINE make_set ( 1 ) ; NEW_LINE make_set ( 2 ) ; NEW_LINE make_set ( 3 ) ; NEW_LINE make_set ( 4 ) ; NEW_LINE make_set ( 5 ) ; NEW_LINE union_set ( 1 , 3 ) ; NEW_LINE union_set ( 3 , 4 ) ; NEW_LINE union_set ( 3 , 5 ) ; NEW_LINE q = 3 ; NEW_LINE print ( check ( 1 , 5 ) ) NEW_LINE print ( check ( 3 , 2 ) ) NEW_LINE print ( check ( 5 , 2 ) ) NEW_LINE DEDENT
Count of N | Function to calculate the total count of N - digit numbers such that the sum of digits at even positions and odd positions are divisible by A and B respectively ; For single digit numbers ; Largest possible number ; Count of possible odd digits ; Count of possible even digits ; Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero ; Calculate total count of sequences of length odd_count with sum divisible by B where cannot be zero ; Return their product as answer ; Driver Code
def count ( N , A , B ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return 9 // B + 1 NEW_LINE DEDENT max_sum = 9 * N NEW_LINE odd_count = N // 2 + N % 2 NEW_LINE even_count = N - odd_count NEW_LINE dp = [ [ 0 for x in range ( max_sum + 1 ) ] for y in range ( even_count ) ] NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT dp [ 0 ] [ i % A ] += 1 NEW_LINE DEDENT for i in range ( 1 , even_count ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT for k in range ( max_sum + 1 ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ k ] > 0 ) : NEW_LINE INDENT dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] NEW_LINE DEDENT DEDENT DEDENT DEDENT dp1 = [ [ 0 for x in range ( max_sum ) ] for y in range ( odd_count ) ] NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT dp1 [ 0 ] [ i % B ] += 1 NEW_LINE DEDENT for i in range ( 1 , odd_count ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT for k in range ( max_sum + 1 ) : NEW_LINE INDENT if ( dp1 [ i - 1 ] [ k ] > 0 ) : NEW_LINE INDENT dp1 [ i ] [ ( j + k ) % B ] += dp1 [ i - 1 ] [ k ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return dp [ even_count - 1 ] [ 0 ] * dp1 [ odd_count - 1 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE A = 2 NEW_LINE B = 5 NEW_LINE print ( count ( N , A , B ) ) NEW_LINE DEDENT
Maximum possible difference of two subsets of an array | Python3 find maximum difference of subset sum ; function for maximum subset diff ; if frequency of any element is two make both equal to zero ; Driver Code
import math NEW_LINE def maxDiff ( arr , n ) : NEW_LINE INDENT SubsetSum_1 = 0 NEW_LINE SubsetSum_2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT isSingleOccurance = True NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT isSingleOccurance = False NEW_LINE arr [ i ] = arr [ j ] = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( isSingleOccurance == True ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT SubsetSum_1 += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT SubsetSum_2 += arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return abs ( SubsetSum_1 - SubsetSum_2 ) NEW_LINE DEDENT arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ Difference ▁ = ▁ { } " . format ( maxDiff ( arr , n ) ) ) NEW_LINE
Count of all possible Paths in a Tree such that Node X does not appear before Node Y | Maximum number of nodes ; Vector to store the tree ; Function to perform DFS Traversal ; Mark the node as visited ; Initialize the subtree size of each node as 1 ; If the node is same as A ; Mark check_subtree [ node ] as true ; Otherwise ; Iterate over the adjacent nodes ; If the adjacent node is not visited ; Update the size of the subtree of current node ; Check if the subtree of current node contains node A ; Return size of subtree of node ; Function to add edges to the tree ; Function to calculate the number of possible paths ; Stores the size of subtree of each node ; Stores which nodes are visited ; Stores if the subtree of a node contains node A ; DFS Call ; Stores the difference between total number of nodes and subtree size of an immediate child of Y lies between the path from A to B ; Iterate over the adjacent nodes B ; If the node is in the path from A to B ; Calculate the difference ; Return the final answer ; Driver Code ; Insert Edges
NN = int ( 3e5 ) NEW_LINE G = [ ] NEW_LINE for i in range ( NN + 1 ) : NEW_LINE INDENT G . append ( [ ] ) NEW_LINE DEDENT def dfs ( node , A , subtree_size , visited , check_subtree ) : NEW_LINE INDENT visited [ node ] = True NEW_LINE subtree_size [ node ] = 1 NEW_LINE if ( node == A ) : NEW_LINE INDENT check_subtree [ node ] = True NEW_LINE DEDENT else : NEW_LINE INDENT check_subtree [ node ] = False NEW_LINE DEDENT for v in G [ node ] : NEW_LINE INDENT if ( not visited [ v ] ) : NEW_LINE INDENT subtree_size [ node ] += dfs ( v , A , subtree_size , visited , check_subtree ) NEW_LINE check_subtree [ node ] = ( check_subtree [ node ] check_subtree [ v ] ) NEW_LINE DEDENT DEDENT return subtree_size [ node ] NEW_LINE DEDENT def addedge ( node1 , node2 ) : NEW_LINE INDENT G [ node1 ] += [ node2 ] NEW_LINE G [ node2 ] += [ node1 ] NEW_LINE DEDENT def numberOfPairs ( N , B , A ) : NEW_LINE INDENT subtree_size = [ 0 ] * ( N + 1 ) NEW_LINE visited = [ 0 ] * ( N + 1 ) NEW_LINE check_subtree = [ 0 ] * ( N + 1 ) NEW_LINE dfs ( B , A , subtree_size , visited , check_subtree ) NEW_LINE difference = 0 NEW_LINE for v in G [ B ] : NEW_LINE INDENT if ( check_subtree [ v ] ) : NEW_LINE INDENT difference = N - subtree_size [ v ] NEW_LINE break NEW_LINE DEDENT DEDENT return ( ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ) NEW_LINE DEDENT N = 9 NEW_LINE X = 5 NEW_LINE Y = 3 NEW_LINE addedge ( 0 , 2 ) NEW_LINE addedge ( 1 , 2 ) NEW_LINE addedge ( 2 , 3 ) NEW_LINE addedge ( 3 , 4 ) NEW_LINE addedge ( 4 , 6 ) NEW_LINE addedge ( 4 , 5 ) NEW_LINE addedge ( 5 , 7 ) NEW_LINE addedge ( 5 , 8 ) NEW_LINE print ( numberOfPairs ( N , Y , X ) ) NEW_LINE
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | Python3 program to implement the above approach ; Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
from math import log2 NEW_LINE def equalXORandOR ( n ) : NEW_LINE INDENT m = 0 NEW_LINE MSB = int ( log2 ( n ) ) NEW_LINE for i in range ( MSB + 1 ) : NEW_LINE INDENT if ( not ( n & ( 1 << i ) ) ) : NEW_LINE INDENT m += ( 1 << i ) NEW_LINE DEDENT DEDENT return m NEW_LINE DEDENT n = 14 NEW_LINE print ( equalXORandOR ( n ) ) NEW_LINE
Maximum possible difference of two subsets of an array | function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; Driver Code
def maxDiff ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) != abs ( arr [ i + 1 ] ) ) : NEW_LINE INDENT result += abs ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT if ( arr [ n - 2 ] != arr [ n - 1 ] ) : NEW_LINE INDENT result += abs ( arr [ n - 1 ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( arr , n ) ) NEW_LINE DEDENT