codeparrot/xlcost-text-to-code huggingface dataset:
Program to check if the points are parallel to X axis or Y axis | To check for parallel line ; checking for parallel to X and Y axis condition ; To display the output ; Driver 's Code
def parallel ( n , a ) : NEW_LINE INDENT x = True ; NEW_LINE y = True ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] [ 0 ] != a [ i + 1 ] [ 0 ] ) : NEW_LINE INDENT x = False ; NEW_LINE DEDENT if ( a [ i ] [ 1 ] != a [ i + 1 ] [ 1 ] ) : NEW_LINE INDENT y = False ; NEW_LINE DEDENT DEDENT if ( x ) : NEW_LINE INDENT print ( " Parallel ▁ to ▁ Y ▁ Axis " ) ; NEW_LINE DEDENT elif ( y ) : NEW_LINE INDENT print ( " Parallel ▁ to ▁ X ▁ Axis " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Parallel ▁ to ▁ X ▁ and ▁ Y ▁ Axis " ) ; NEW_LINE DEDENT DEDENT a = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 1 , 6 ] , [ 1 , 0 ] ] ; NEW_LINE n = len ( a ) ; NEW_LINE parallel ( n , a ) ; NEW_LINE
Triangular Matchstick Number | Python program to find X - th triangular matchstick number ; Driver code
def numberOfSticks ( x ) : NEW_LINE INDENT return ( 3 * x * ( x + 1 ) ) / 2 NEW_LINE DEDENT print ( int ( numberOfSticks ( 7 ) ) ) NEW_LINE
Total area of two overlapping rectangles | Python program to find total area of two overlapping Rectangles Returns Total Area of two overlap rectangles ; Area of 1 st Rectangle ; Area of 2 nd Rectangle ; Length of intersecting part i . e start from max ( l1 [ x ] , l2 [ x ] ) of x - coordinate and end at min ( r1 [ x ] , r2 [ x ] ) x - coordinate by subtracting start from end we get required lengths ; Driver 's Code ; Function call
def overlappingArea ( l1 , r1 , l2 , r2 ) : NEW_LINE INDENT x = 0 NEW_LINE y = 1 NEW_LINE area1 = abs ( l1 [ x ] - r1 [ x ] ) * abs ( l1 [ y ] - r1 [ y ] ) NEW_LINE area2 = abs ( l2 [ x ] - r2 [ x ] ) * abs ( l2 [ y ] - r2 [ y ] ) NEW_LINE x_dist = ( min ( r1 [ x ] , r2 [ x ] ) - max ( l1 [ x ] , l2 [ x ] ) ) NEW_LINE y_dist = ( min ( r1 [ y ] , r2 [ y ] ) - max ( l1 [ y ] , l2 [ y ] ) ) NEW_LINE areaI = 0 NEW_LINE if x_dist > 0 and y_dist > 0 : NEW_LINE INDENT areaI = x_dist * y_dist NEW_LINE DEDENT return ( area1 + area2 - areaI ) NEW_LINE DEDENT l1 = [ 2 , 2 ] NEW_LINE r1 = [ 5 , 7 ] NEW_LINE l2 = [ 3 , 4 ] NEW_LINE r2 = [ 6 , 9 ] NEW_LINE print ( overlappingArea ( l1 , r1 , l2 , r2 ) ) NEW_LINE
Area of square Circumscribed by Circle | Function to find area of square ; Radius of a circle ; Call Function to find an area of square
def find_Area ( r ) : NEW_LINE INDENT return ( 2 * r * r ) NEW_LINE DEDENT r = 3 NEW_LINE print ( " ▁ Area ▁ of ▁ square ▁ = ▁ " , find_Area ( r ) ) NEW_LINE
Check whether triangle is valid or not if sides are given | function to check if three sides form a triangle or not ; check condition ; driver code ; function calling and print output
def checkValidity ( a , b , c ) : NEW_LINE INDENT if ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT a = 7 NEW_LINE b = 10 NEW_LINE c = 5 NEW_LINE if checkValidity ( a , b , c ) : NEW_LINE INDENT print ( " Valid " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT
Find the Surface area of a 3D figure | Declaring the size of the matrix ; Absolute Difference between the height of two consecutive blocks ; Function To calculate the Total surfaceArea . ; Traversing the matrix . ; If we are traveling the topmost row in the matrix , we declare the wall above it as 0 as there is no wall above it . ; If we are traveling the leftmost column in the matrix , we declare the wall left to it as 0 as there is no wall left it . ; If its not the topmost row ; If its not the leftmost column ; Summing up the contribution of by the current block ; If its the rightmost block of the matrix it will contribute area equal to its height as a wall on the right of the figure ; If its the lowest block of the matrix it will contribute area equal to its height as a wall on the bottom of the figure ; Adding the contribution by the base and top of the figure ; Driver Code
M = 3 ; NEW_LINE N = 3 ; NEW_LINE def contribution_height ( current , previous ) : NEW_LINE INDENT return abs ( current - previous ) ; NEW_LINE DEDENT def surfaceArea ( A ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT up = 0 ; NEW_LINE left = 0 ; NEW_LINE if ( i > 0 ) : NEW_LINE INDENT up = A [ i - 1 ] [ j ] ; NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT left = A [ i ] [ j - 1 ] ; NEW_LINE DEDENT ans += contribution_height ( A [ i ] [ j ] , up ) + contribution_height ( A [ i ] [ j ] , left ) ; NEW_LINE if ( i == N - 1 ) : NEW_LINE INDENT ans += A [ i ] [ j ] ; NEW_LINE DEDENT if ( j == M - 1 ) : NEW_LINE INDENT ans += A [ i ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT ans += N * M * 2 ; NEW_LINE return ans ; NEW_LINE DEDENT A = [ [ 1 , 3 , 4 ] , [ 2 , 2 , 3 ] , [ 1 , 2 , 4 ] ] ; NEW_LINE print ( surfaceArea ( A ) ) ; NEW_LINE
Program to calculate area and volume of a Tetrahedron | Python3 Program to Calculate area of tetrahedron ;  ; Driver Code
import math NEW_LINE / * Utility Function * / NEW_LINE def area_of_tetrahedron ( side ) : NEW_LINE INDENT return ( math . sqrt ( 3 ) * ( side * side ) ) ; NEW_LINE DEDENT side = 3 ; NEW_LINE print ( " Area ▁ of ▁ Tetrahedron ▁ = ▁ " , round ( area_of_tetrahedron ( side ) , 4 ) ) ; NEW_LINE
Program to calculate area and volume of a Tetrahedron | Python code to find the volume of a tetrahedron ; Function to calculate volume ; Driver Code
import math NEW_LINE def vol_tetra ( side ) : NEW_LINE INDENT volume = ( side ** 3 / ( 6 * math . sqrt ( 2 ) ) ) NEW_LINE return round ( volume , 2 ) NEW_LINE DEDENT side = 3 NEW_LINE vol = vol_tetra ( side ) NEW_LINE print ( vol ) NEW_LINE
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
def numberOfWays ( x ) : NEW_LINE INDENT dp = [ ] NEW_LINE dp . append ( 1 ) NEW_LINE dp . append ( 1 ) NEW_LINE for i in range ( 2 , x + 1 ) : NEW_LINE INDENT dp . append ( dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ) NEW_LINE DEDENT return ( dp [ x ] ) NEW_LINE DEDENT x = 3 NEW_LINE print ( numberOfWays ( x ) ) NEW_LINE
Program to find slope of a line | Python3 program to find slope ; Function to find the slope of a straight line ; Driver code
import sys NEW_LINE def slope ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT if x1 == x2 : NEW_LINE INDENT return ( sys . maxsize ) NEW_LINE DEDENT return ( ( y2 - y1 ) / ( x2 - x1 ) ) NEW_LINE DEDENT x1 = 4 NEW_LINE y1 = 2 NEW_LINE x2 = 2 NEW_LINE y2 = 5 NEW_LINE print ( " Slope ▁ is ▁ : " , slope ( 4 , 2 , 2 , 5 ) ) NEW_LINE
Program to calculate area and perimeter of equilateral triangle | Python3 program to calculate Area and Perimeter of equilateral Triangle ; Function to calculate Area of equilateral triangle ; Function to calculate Perimeter of equilateral triangle ; Driver code
from math import * NEW_LINE def area_equilateral ( side ) : NEW_LINE INDENT area = ( sqrt ( 3 ) / 4 ) * side * side NEW_LINE print ( " Area ▁ of ▁ Equilateral ▁ Triangle : ▁ % ▁ f " % area ) NEW_LINE DEDENT def perimeter ( side ) : NEW_LINE INDENT perimeter = 3 * side NEW_LINE print ( " Perimeter ▁ of ▁ Equilateral ▁ Triangle : ▁ % ▁ f " % perimeter ) NEW_LINE DEDENT side = 4 NEW_LINE area_equilateral ( side ) NEW_LINE perimeter ( side ) NEW_LINE
Maximum integral co | Making set of coordinates such that any two points are non - integral distance apart ; Used to avoid duplicates in result ; Driver code
def printSet ( x , y ) : NEW_LINE INDENT arr = [ ] NEW_LINE for i in range ( min ( x , y ) + 1 ) : NEW_LINE INDENT pq = [ i , min ( x , y ) - i ] NEW_LINE arr . append ( pq ) NEW_LINE DEDENT for it in arr : NEW_LINE INDENT print ( it [ 0 ] , it [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 4 NEW_LINE y = 4 NEW_LINE printSet ( x , y ) NEW_LINE DEDENT
Program for Volume and Surface Area of Cuboid | utility function ; driver function
def volumeCuboid ( l , h , w ) : NEW_LINE INDENT return ( l * h * w ) NEW_LINE DEDENT def surfaceAreaCuboid ( l , h , w ) : NEW_LINE INDENT return ( 2 * l * w + 2 * w * h + 2 * l * h ) NEW_LINE DEDENT l = 1 NEW_LINE h = 5 NEW_LINE w = 7 NEW_LINE print ( " Volume ▁ = " , volumeCuboid ( l , h , w ) ) NEW_LINE print ( " Total ▁ Surface ▁ Area ▁ = " , surfaceAreaCuboid ( l , h , w ) ) NEW_LINE
Program to find Circumference of a Circle | Python3 code to find circumference of circle ; utility function ; driver function
PI = 3.1415 NEW_LINE def circumference ( r ) : NEW_LINE INDENT return ( 2 * PI * r ) NEW_LINE DEDENT print ( ' % .3f ' % circumference ( 5 ) ) NEW_LINE
Program to check if three points are collinear | function to check if point collinear or not ; Driver Code
def collinear ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT x1 , x2 , x3 , y1 , y2 , y3 = 1 , 1 , 0 , 1 , 6 , 9 NEW_LINE collinear ( x1 , y1 , x2 , y2 , x3 , y3 ) ; NEW_LINE
Number of rectangles in N * M grid | Python3 program to count number of rectangles in a n x m grid ; Driver code
def rectCount ( n , m ) : NEW_LINE INDENT return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4 NEW_LINE DEDENT n , m = 5 , 4 NEW_LINE print ( rectCount ( n , m ) ) NEW_LINE
Number of unique rectangles formed using N unit squares | Python3 program to count rotationally equivalent rectangles with n unit squares ; height >= length is maintained ; Driver code
import math NEW_LINE def countRect ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for length in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT height = length NEW_LINE while ( height * length <= n ) : NEW_LINE INDENT ans += 1 NEW_LINE height += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE print ( countRect ( n ) ) NEW_LINE
Find the Missing Point of Parallelogram | Main method ; coordinates of A ; coordinates of B ; coordinates of C
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ax , ay = 5 , 0 NEW_LINE bx , by = 1 , 1 NEW_LINE cx , cy = 2 , 5 NEW_LINE print ( ax + cx - bx , " , " , ay + cy - by ) NEW_LINE DEDENT
Represent a given set of points by the best possible straight line | function to calculate m and c that best fit points represented by x [ ] and y [ ] ; Driver main function
def bestApproximate ( x , y , n ) : NEW_LINE INDENT sum_x = 0 NEW_LINE sum_y = 0 NEW_LINE sum_xy = 0 NEW_LINE sum_x2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum_x += x [ i ] NEW_LINE sum_y += y [ i ] NEW_LINE sum_xy += x [ i ] * y [ i ] NEW_LINE sum_x2 += pow ( x [ i ] , 2 ) NEW_LINE DEDENT m = ( float ) ( ( n * sum_xy - sum_x * sum_y ) / ( n * sum_x2 - pow ( sum_x , 2 ) ) ) ; NEW_LINE c = ( float ) ( sum_y - m * sum_x ) / n ; NEW_LINE print ( " m ▁ = ▁ " , m ) ; NEW_LINE print ( " c ▁ = ▁ " , c ) ; NEW_LINE DEDENT x = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE y = [ 14 , 27 , 40 , 55 , 68 ] NEW_LINE n = len ( x ) NEW_LINE bestApproximate ( x , y , n ) NEW_LINE
Check for star graph | define the size of incidence matrix ; def to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; Driver code
size = 4 NEW_LINE def checkStar ( mat ) : NEW_LINE INDENT global size NEW_LINE vertexD1 = 0 NEW_LINE vertexDn_1 = 0 NEW_LINE if ( size == 1 ) : NEW_LINE INDENT return ( mat [ 0 ] [ 0 ] == 0 ) NEW_LINE DEDENT if ( size == 2 ) : NEW_LINE INDENT return ( mat [ 0 ] [ 0 ] == 0 and mat [ 0 ] [ 1 ] == 1 and mat [ 1 ] [ 0 ] == 1 and mat [ 1 ] [ 1 ] == 0 ) NEW_LINE DEDENT for i in range ( 0 , size ) : NEW_LINE INDENT degreeI = 0 NEW_LINE for j in range ( 0 , size ) : NEW_LINE INDENT if ( mat [ i ] [ j ] ) : NEW_LINE INDENT degreeI = degreeI + 1 NEW_LINE DEDENT DEDENT if ( degreeI == 1 ) : NEW_LINE INDENT vertexD1 = vertexD1 + 1 NEW_LINE DEDENT elif ( degreeI == size - 1 ) : NEW_LINE INDENT vertexDn_1 = vertexDn_1 + 1 NEW_LINE DEDENT DEDENT return ( vertexD1 == ( size - 1 ) and vertexDn_1 == 1 ) NEW_LINE DEDENT mat = [ [ 0 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 ] ] NEW_LINE if ( checkStar ( mat ) ) : NEW_LINE INDENT print ( " Star ▁ Graph " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ Star ▁ Graph " ) NEW_LINE DEDENT
Find all angles of a given triangle | Python3 code to find all three angles of a triangle given coordinate of all three vertices ; returns square of distance b / w two points ; Square of lengths be a2 , b2 , c2 ; length of sides be a , b , c ; From Cosine law ; Converting to degree ; printing all the angles ; Driver code
import math NEW_LINE def lengthSquare ( X , Y ) : NEW_LINE INDENT xDiff = X [ 0 ] - Y [ 0 ] NEW_LINE yDiff = X [ 1 ] - Y [ 1 ] NEW_LINE return xDiff * xDiff + yDiff * yDiff NEW_LINE DEDENT def printAngle ( A , B , C ) : NEW_LINE INDENT a2 = lengthSquare ( B , C ) NEW_LINE b2 = lengthSquare ( A , C ) NEW_LINE c2 = lengthSquare ( A , B ) NEW_LINE a = math . sqrt ( a2 ) ; NEW_LINE b = math . sqrt ( b2 ) ; NEW_LINE c = math . sqrt ( c2 ) ; NEW_LINE alpha = math . acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; NEW_LINE betta = math . acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; NEW_LINE gamma = math . acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; NEW_LINE alpha = alpha * 180 / math . pi ; NEW_LINE betta = betta * 180 / math . pi ; NEW_LINE gamma = gamma * 180 / math . pi ; NEW_LINE print ( " alpha ▁ : ▁ % f " % ( alpha ) ) NEW_LINE print ( " betta ▁ : ▁ % f " % ( betta ) ) NEW_LINE print ( " gamma ▁ : ▁ % f " % ( gamma ) ) NEW_LINE DEDENT A = ( 0 , 0 ) NEW_LINE B = ( 0 , 1 ) NEW_LINE C = ( 1 , 0 ) NEW_LINE printAngle ( A , B , C ) ; NEW_LINE
Triangle with no point inside | Python3 program to find triangle with no point inside ; method to get square of distance between ( x1 , y1 ) and ( x2 , y2 ) ; Method prints points which make triangle with no point inside ; any point can be chosen as first point of triangle ; choose nearest point as second point of triangle ; Get distance from first point and choose nearest one ; Pick third point by finding the second closest point with different slope . ; if already chosen point then skip them ; get distance from first point ; here cross multiplication is compared instead of division comparison ; Driver code
import sys NEW_LINE def getDistance ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT return ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) NEW_LINE DEDENT def triangleWithNoPointInside ( points , N ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE third = 0 NEW_LINE minD = sys . maxsize NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if i == first : NEW_LINE INDENT continue NEW_LINE DEDENT d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) NEW_LINE if minD > d : NEW_LINE INDENT minD = d NEW_LINE second = i NEW_LINE DEDENT DEDENT minD = sys . maxsize NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if i == first or i == second : NEW_LINE INDENT continue NEW_LINE DEDENT d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) NEW_LINE if ( ( points [ i ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ second ] [ 1 ] - points [ first ] [ 1 ] ) != ( points [ second ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ i ] [ 1 ] - points [ first ] [ 1 ] ) and minD > d ) : NEW_LINE INDENT minD = d NEW_LINE third = i NEW_LINE DEDENT DEDENT print ( points [ first ] [ 0 ] , ' , ▁ ' , points [ first ] [ 1 ] ) NEW_LINE print ( points [ second ] [ 0 ] , ' , ▁ ' , points [ second ] [ 1 ] ) NEW_LINE print ( points [ third ] [ 0 ] , ' , ▁ ' , points [ third ] [ 1 ] ) NEW_LINE DEDENT points = [ [ 0 , 0 ] , [ 0 , 2 ] , [ 2 , 0 ] , [ 2 , 2 ] , [ 1 , 1 ] ] NEW_LINE N = len ( points ) NEW_LINE triangleWithNoPointInside ( points , N ) NEW_LINE
Minimize swaps required to make all prime | Python program for the above approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Driver Code ; Pre - calculate prime [ ]
import math NEW_LINE mxn = 10000 + 1 NEW_LINE prime = [ True for _ in range ( mxn + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT global prime NEW_LINE for p in range ( 2 , int ( math . sqrt ( mxn ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , mxn + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def countMin ( arr , n ) : NEW_LINE INDENT cMinSwaps = 0 NEW_LINE cPrimeIndices = 0 NEW_LINE cPrimeNos = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( prime [ i + 1 ] ) : NEW_LINE INDENT cPrimeIndices += 1 NEW_LINE if ( not prime [ arr [ i ] ] ) : NEW_LINE INDENT cMinSwaps += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cPrimeNos += 1 NEW_LINE DEDENT DEDENT elif ( prime [ arr [ i ] ] ) : NEW_LINE INDENT cPrimeNos += 1 NEW_LINE DEDENT DEDENT if ( cPrimeNos >= cPrimeIndices ) : NEW_LINE INDENT return cMinSwaps NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE n = 5 NEW_LINE arr = [ 2 , 7 , 8 , 5 , 13 ] NEW_LINE print ( countMin ( arr , n ) ) NEW_LINE DEDENT
Find the size of Largest Subset with positive Bitwise AND | Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver Code
def largestSubset ( a , N ) : NEW_LINE INDENT bit = [ 0 for i in range ( 32 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = 31 NEW_LINE while ( a [ i ] > 0 ) : NEW_LINE INDENT if ( a [ i ] & 1 == 1 ) : NEW_LINE INDENT bit [ x ] += 1 NEW_LINE DEDENT a [ i ] = a [ i ] >> 1 NEW_LINE x -= 1 NEW_LINE DEDENT DEDENT print ( max ( bit ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 13 , 8 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE largestSubset ( arr , N ) NEW_LINE DEDENT
Maximum element in connected component of given node for Q queries | Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; If the rank are the same ; Update the maximum value ; Function to find the maximum element of the set which belongs to the element queries [ i ] ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the maxValue of the sets ; Update parent [ i ] and maxValue [ i ] to i ; Add arr [ i ] . first and arr [ i ] . second elements to the same set ; Find the parent element of the element queries [ i ] ; Print the maximum value of the set which belongs to the element P ; Driver Code
def Find ( parent , a ) : NEW_LINE INDENT if ( parent [ parent [ a ] ] != parent [ a ] ) : NEW_LINE INDENT parent [ a ] = findParent ( parent , parent [ a ] ) NEW_LINE DEDENT return parent [ a ] NEW_LINE DEDENT def Union ( parent , rank , maxValue , a , b ) : NEW_LINE INDENT a = Find ( parent , a ) NEW_LINE b = Find ( parent , b ) NEW_LINE if ( a == b ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( rank [ a ] == rank [ b ] ) : NEW_LINE INDENT rank [ a ] += 1 NEW_LINE DEDENT if ( rank [ a ] < rank [ b ] ) : NEW_LINE INDENT temp = a NEW_LINE a = b NEW_LINE b = temp NEW_LINE DEDENT parent [ b ] = a NEW_LINE maxValue [ a ] = max ( maxValue [ a ] , maxValue [ b ] ) NEW_LINE DEDENT def findMaxValueOfSet ( arr , queries , R , N , M ) : NEW_LINE INDENT parent = [ 1 for i in range ( R + 1 ) ] NEW_LINE rank = [ 0 for i in range ( R + 1 ) ] NEW_LINE maxValue = [ 0 for i in range ( R + 1 ) ] NEW_LINE for i in range ( 1 , R + 1 , 1 ) : NEW_LINE INDENT parent [ i ] = maxValue [ i ] = i NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT Union ( parent , rank , maxValue , arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT P = Find ( parent , queries [ i ] ) NEW_LINE print ( maxValue [ P ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R = 5 NEW_LINE arr = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ; NEW_LINE queries = [ 2 , 4 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( queries ) NEW_LINE findMaxValueOfSet ( arr , queries , R , N , M ) NEW_LINE DEDENT
Maximum sum of segments among all segments formed in array after Q queries | Python 3 program for the above approach ; Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code
import sys NEW_LINE maxAns = - sys . maxsize - 1 NEW_LINE def Find ( parent , a ) : NEW_LINE INDENT if ( parent [ a ] == a ) : NEW_LINE INDENT return a NEW_LINE DEDENT return Find ( parent , parent [ a ] ) NEW_LINE DEDENT def Union ( parent , rank , setSum , a , b ) : NEW_LINE INDENT a = Find ( parent , a ) NEW_LINE b = Find ( parent , b ) NEW_LINE if ( a == b ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( rank [ a ] > rank [ b ] ) : NEW_LINE INDENT rank [ a ] += 1 NEW_LINE DEDENT if ( rank [ b ] > rank [ a ] ) : NEW_LINE INDENT swap ( a , b ) NEW_LINE DEDENT parent [ b ] = a NEW_LINE setSum [ a ] += setSum [ b ] NEW_LINE DEDENT def maxValues ( arr , queries , N ) : NEW_LINE INDENT global maxAns NEW_LINE rank = [ 0 ] * ( N + 1 ) NEW_LINE setSum = [ 0 ] * ( N + 1 ) NEW_LINE currMax = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT parent [ i ] = - 1 NEW_LINE setSum [ i ] = arr [ i - 1 ] NEW_LINE DEDENT currMax . append ( 0 ) NEW_LINE for i in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( parent [ queries [ i ] ] == - 1 ) : NEW_LINE INDENT parent [ queries [ i ] ] = queries [ i ] NEW_LINE DEDENT if ( queries [ i ] - 1 >= 0 and parent [ queries [ i ] - 1 ] != - 1 ) : NEW_LINE INDENT Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) NEW_LINE DEDENT if ( queries [ i ] + 1 <= N and parent [ queries [ i ] + 1 ] != - 1 ) : NEW_LINE INDENT Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) NEW_LINE DEDENT maxAns = max ( setSum [ queries [ i ] ] , maxAns ) NEW_LINE currMax . append ( maxAns ) NEW_LINE DEDENT for i in range ( len ( currMax ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( currMax [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 5 ] NEW_LINE queries = [ 3 , 4 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE maxValues ( arr , queries , N ) NEW_LINE DEDENT