codeparrot/xlcost-text-to-code huggingface dataset:
Shortest Palindromic Substring | Function return the shortest palindromic substring ; One by one consider every character as center point of even and length palindromes ; Find the longest odd length palindrome with center point as i ; Find the even length palindrome with center points as i - 1 and i . ; Smallest substring which is not empty ; Driver code
def ShortestPalindrome ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT l = i NEW_LINE r = i NEW_LINE ans1 = " " NEW_LINE ans2 = " " NEW_LINE while ( ( l >= 0 ) and ( r < n ) and ( s [ l ] == s [ r ] ) ) : NEW_LINE INDENT ans1 += s [ l ] NEW_LINE l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT l = i - 1 NEW_LINE r = i NEW_LINE while ( ( l >= 0 ) and ( r < n ) and ( s [ l ] == s [ r ] ) ) : NEW_LINE INDENT ans2 += s [ l ] NEW_LINE l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT v . append ( ans1 ) NEW_LINE v . append ( ans2 ) NEW_LINE DEDENT ans = v [ 0 ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ i ] != " " ) : NEW_LINE INDENT ans = min ( ans , v [ i ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE print ( ShortestPalindrome ( s ) ) NEW_LINE
Find numbers which are multiples of first array and factors of second array | Python3 implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
from math import gcd NEW_LINE def lcm ( x , y ) : NEW_LINE INDENT temp = ( x * y ) // gcd ( x , y ) ; NEW_LINE return temp ; NEW_LINE DEDENT def findNumbers ( a , n , b , m ) : NEW_LINE INDENT lcmA = 1 ; __gcdB = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT lcmA = lcm ( lcmA , a [ i ] ) ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT __gcdB = gcd ( __gcdB , b [ i ] ) ; NEW_LINE DEDENT if ( __gcdB % lcmA != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT num = lcmA ; NEW_LINE while ( num <= __gcdB ) : NEW_LINE INDENT if ( __gcdB % num == 0 ) : NEW_LINE INDENT print ( num , end = " ▁ " ) ; NEW_LINE DEDENT num += lcmA ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 4 ] ; NEW_LINE b = [ 16 , 32 , 64 ] ; NEW_LINE n = len ( a ) ; NEW_LINE m = len ( b ) ; NEW_LINE findNumbers ( a , n , b , m ) ; NEW_LINE DEDENT
Probability such that two subset contains same number of elements | Python3 implementation of the above approach ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
import math NEW_LINE def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 NEW_LINE if ( k > n - k ) : NEW_LINE INDENT k = n - k NEW_LINE DEDENT for i in range ( 0 , k ) : NEW_LINE INDENT res = res * ( n - i ) NEW_LINE res = res // ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = res * x NEW_LINE DEDENT y = y // 2 NEW_LINE x = x * x NEW_LINE DEDENT return res NEW_LINE DEDENT def FindProbability ( n ) : NEW_LINE INDENT up = binomialCoeff ( 2 * n , n ) NEW_LINE down = power ( 2 , 2 * n ) NEW_LINE g = math . gcd ( up , down ) NEW_LINE up = up // g NEW_LINE down = down // g NEW_LINE print ( up , " / " , down ) NEW_LINE DEDENT N = 8 NEW_LINE FindProbability ( N ) NEW_LINE
How to learn Pattern printing easily ? | Python3 implementation of the approach
N = 4 NEW_LINE print ( " Value ▁ of ▁ N : ▁ " , N ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT min = i if i < j else j NEW_LINE print ( N - min + 1 , end = " ▁ " ) NEW_LINE DEDENT for j in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT min = i if i < j else j NEW_LINE print ( N - min + 1 , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT for i in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT min = i if i < j else j NEW_LINE print ( N - min + 1 , end = " ▁ " ) NEW_LINE DEDENT for j in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT min = i if i < j else j NEW_LINE print ( N - min + 1 , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT
Largest perfect square number in an Array | Python3 program to find the largest perfect square number among n numbers ; Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver code
from math import sqrt NEW_LINE def checkPerfectSquare ( n ) : NEW_LINE INDENT d = sqrt ( n ) NEW_LINE if d * d == n : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def largestPerfectSquareNumber ( a , n ) : NEW_LINE INDENT maxi = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( checkPerfectSquare ( a [ i ] ) ) : NEW_LINE INDENT maxi = max ( a [ i ] , maxi ) NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 16 , 20 , 25 , 2 , 3 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( largestPerfectSquareNumber ( a , n ) ) NEW_LINE DEDENT
Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code
def convert ( n , a ) : NEW_LINE INDENT z = max ( a ) + 1 NEW_LINE ff = [ 0 ] * z NEW_LINE for i in range ( n ) : NEW_LINE INDENT ff [ a [ i ] ] += 1 NEW_LINE DEDENT cc = 0 NEW_LINE for i in range ( z ) : NEW_LINE INDENT cc += ff [ i ] // 4 NEW_LINE ff [ i ] = ff [ i ] % 4 NEW_LINE DEDENT vv = 0 NEW_LINE for i in range ( z ) : NEW_LINE INDENT if ( ff [ i ] >= 2 ) : NEW_LINE INDENT vv += 1 NEW_LINE DEDENT DEDENT cc += vv // 2 NEW_LINE print ( cc ) NEW_LINE DEDENT n = 4 NEW_LINE a = [ 1 , 2 , 1 , 2 ] NEW_LINE convert ( n , a ) NEW_LINE
Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | Python3 implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
from math import gcd NEW_LINE def find_LCM ( x , y ) : NEW_LINE INDENT return ( x * y ) // gcd ( x , y ) NEW_LINE DEDENT def CountPairs ( n , m , A , B ) : NEW_LINE INDENT cnt = 0 NEW_LINE lcm = find_LCM ( A , B ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT cnt += ( m + ( i % lcm ) ) // lcm NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m , A , B = 60 , 90 , 5 , 10 NEW_LINE print ( CountPairs ( n , m , A , B ) ) NEW_LINE DEDENT
Sorting without comparison of elements | Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code
def sortArr ( arr , n , min_no , max_no ) : NEW_LINE INDENT m = max_no - min_no + 1 NEW_LINE c = [ 0 ] * m NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ arr [ i ] - min_no ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT for j in range ( ( c [ i ] ) ) : NEW_LINE INDENT print ( ( i + min_no ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 10 , 10 , 1 , 4 , 4 , 100 , 0 ] NEW_LINE min_no , max_no = 0 , 100 NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n , min_no , max_no ) NEW_LINE
Find Binary permutations of given size not present in the Array | Python 3 program for the above approach ; Function to find a Binary String of same length other than the Strings present in the array ; Map all the strings present in the array ; Find all the substring that can be made ; If num already exists then increase counter ; If not found print ; If all the substrings are present then print - 1 ; Driver Code
from math import pow NEW_LINE def findMissingBinaryString ( nums , N ) : NEW_LINE INDENT s = set ( ) NEW_LINE counter = 0 NEW_LINE for x in nums : NEW_LINE INDENT s . add ( x ) NEW_LINE DEDENT total = int ( pow ( 2 , N ) ) NEW_LINE ans = " " NEW_LINE for i in range ( total ) : NEW_LINE INDENT num = " " NEW_LINE j = N - 1 NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT num += '1' NEW_LINE DEDENT else : NEW_LINE INDENT num += '0' NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT if ( num in s ) : NEW_LINE INDENT continue NEW_LINE counter += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( num , end = " , ▁ " ) NEW_LINE DEDENT DEDENT if ( counter == total ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE arr = [ "101" , "111" , "001" , "011" , "100" , "110" ] NEW_LINE findMissingBinaryString ( arr , N ) NEW_LINE DEDENT
Queries to find number of connected grid components of given sizes in a Matrix | Python 3 implementation for the above approach ; stores information about which cell are already visited in a particular BFS ; Stores the final result grid ; Stores the count of cells in the largest connected component ; Function checks if a cell is valid , i . e . it is inside the grid and equal to 1 ; Map to count the frequency of each connected component ; function to calculate the largest connected component ; Iterate over every cell ; Stores the indices of the matrix cells ; Mark the starting cell as visited and push it into the queue ; Iterate while the queue is not empty ; Go to the adjacent cells ; Drivers Code ; Given input array of 1 s and 0 s ; queries array ; sizeof queries array ; Count the frequency of each connected component ; Iterate over the given queries array and And answer the queries
n = 6 NEW_LINE m = 6 NEW_LINE dx = [ 0 , 1 , - 1 , 0 ] NEW_LINE dy = [ 1 , 0 , 0 , - 1 ] NEW_LINE visited = [ [ False for i in range ( m ) ] for j in range ( n ) ] NEW_LINE result = [ [ 0 for i in range ( m ) ] for j in range ( n ) ] NEW_LINE COUNT = 0 NEW_LINE def is_valid ( x , y , matrix ) : NEW_LINE INDENT if ( x < n and y < m and x >= 0 and y >= 0 ) : NEW_LINE INDENT if ( visited [ x ] [ y ] == False and matrix [ x ] [ y ] == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT mp = { } NEW_LINE def findComponentSize ( matrix ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( visited [ i ] [ j ] == False and matrix [ i ] [ j ] == 1 ) : NEW_LINE INDENT COUNT = 0 NEW_LINE q = [ ] NEW_LINE q . append ( [ i , j ] ) NEW_LINE visited [ i ] [ j ] = True NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT p = q [ 0 ] NEW_LINE q = q [ 1 : ] NEW_LINE x = p [ 0 ] NEW_LINE y = p [ 1 ] NEW_LINE COUNT += 1 NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT newX = x + dx [ i ] NEW_LINE newY = y + dy [ i ] NEW_LINE if ( is_valid ( newX , newY , matrix ) ) : NEW_LINE INDENT q . append ( [ newX , newY ] ) NEW_LINE visited [ newX ] [ newY ] = True NEW_LINE DEDENT DEDENT DEDENT if COUNT in mp : NEW_LINE INDENT mp [ COUNT ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ COUNT ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 1 , 1 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 1 , 1 , 1 ] , [ 0 , 0 , 0 , 1 , 1 , 1 ] , [ 0 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 , 0 ] ] NEW_LINE queries = [ 6 , 1 , 8 , 2 ] NEW_LINE N = len ( queries ) NEW_LINE findComponentSize ( matrix ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if queries [ i ] in mp : NEW_LINE print ( mp [ queries [ i ] ] , end = " ▁ " ) NEW_LINE else : NEW_LINE print ( 0 , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Minimum value of X such that sum of arr [ i ] | Function to check if there exists an X that satisfies the given conditions ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver Code
def check ( a , b , k , n , x ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + pow ( max ( a [ i ] - x , 0 ) , b [ i ] ) NEW_LINE DEDENT if ( sum <= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def findMin ( a , b , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE u = max ( a ) NEW_LINE while ( l < u ) : NEW_LINE INDENT m = ( l + u ) // 2 NEW_LINE if ( check ( a , b , k , n , m ) ) : NEW_LINE INDENT u = m NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 3 , 5 ] NEW_LINE brr = [ 4 , 3 , 2 , 3 , 1 ] NEW_LINE K = 12 NEW_LINE N = len ( arr ) NEW_LINE print ( findMin ( arr , brr , N , K ) ) NEW_LINE DEDENT
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code
def getPairsCount ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT s = arr [ i ] - ( i % arr [ i ] ) NEW_LINE for j in range ( s , n ) : NEW_LINE INDENT if ( i < j and ( arr [ i ] * arr [ j ] ) == abs ( i - j ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getPairsCount ( arr , N ) ) NEW_LINE
Find frequency of each character with positions in given Array of Strings | Function to print every occurence of every characters in every string ; Iterate over the vector arr [ ] ; Traverse the string arr [ i ] ; Push the pair of { i + 1 , j + 1 } in mp [ arr [ i ] [ j ] ] ; Print the occurences of every character ; Driver Code ; Input ; Function call
def printOccurences ( arr , N ) : NEW_LINE INDENT mp = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( len ( arr [ i ] ) ) : NEW_LINE INDENT mp [ ord ( arr [ i ] [ j ] ) - ord ( ' a ' ) ] . append ( ( i + 1 , j + 1 ) ) NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if len ( mp [ i ] ) == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT print ( " Occurences ▁ of : " , chr ( i + ord ( ' a ' ) ) , " = " , end = " ▁ " ) NEW_LINE for j in mp [ i ] : NEW_LINE INDENT print ( " [ " + str ( j [ 0 ] ) + " ▁ " + str ( j [ 1 ] ) + " ] ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " geeksforgeeks " , " gfg " ] NEW_LINE N = len ( arr ) NEW_LINE printOccurences ( arr , N ) NEW_LINE DEDENT
Count of subarrays with X as the most frequent element , for each value of X from 1 to N | Function to calculate the number of subarrays where X ( 1 <= X <= N ) is the most frequent element ; array to store the final answers ; Array to store current frequencies ; Variable to store the current most frequent element ; Update frequency array ; Update answer ; Pranswer ; Driver code ; Input ; Function call
def mostFrequent ( arr , N ) : NEW_LINE INDENT ans = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = [ 0 ] * N NEW_LINE best = 0 NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT count [ arr [ j ] - 1 ] += 1 NEW_LINE if ( count [ arr [ j ] - 1 ] > count [ best - 1 ] or ( count [ arr [ j ] - 1 ] == count [ best - 1 ] and arr [ j ] < best ) ) : NEW_LINE INDENT best = arr [ j ] NEW_LINE DEDENT ans [ best - 1 ] += 1 NEW_LINE DEDENT DEDENT print ( * ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE mostFrequent ( arr , N ) NEW_LINE DEDENT
Count array elements whose highest power of 2 less than or equal to that number is present in the given array | Python program for the above approach ; Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
from math import log2 NEW_LINE def countElement ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE m = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT lg = int ( log2 ( arr [ i ] ) ) NEW_LINE p = pow ( 2 , lg ) NEW_LINE if ( p in m ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 6 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countElement ( arr , N ) ) NEW_LINE DEDENT
Maximum number of intersections possible for any of the N given segments | Python 3 program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending points ; Sort arrays points in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not intersecting with the current segment ; Store the count of segments that intersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code
from bisect import bisect_left , bisect_right NEW_LINE def lower_bound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = low + ( high - low ) // 2 NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = middle NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT def maximumIntersections ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE L = [ 0 ] * N NEW_LINE R = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT L [ i ] = arr [ i ] [ 0 ] NEW_LINE R [ i ] = arr [ i ] [ 1 ] NEW_LINE DEDENT L . sort ( ) NEW_LINE R . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT l = arr [ i ] [ 0 ] NEW_LINE r = arr [ i ] [ 1 ] NEW_LINE x = lower_bound ( L , 0 , N , l ) NEW_LINE y = N - lower_bound ( R , 0 , N , r + 1 ) NEW_LINE cnt = x + y NEW_LINE cnt = N - cnt - 1 NEW_LINE count = max ( count , cnt ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 6 ] , [ 5 , 5 ] , [ 2 , 3 ] ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumIntersections ( arr , N ) ) NEW_LINE DEDENT
Program to find the Nth Composite Number | Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ;  ; Iterate over the range [ 2 , 1000005 ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , 1000005 ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , 1000005 ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
def NthComposite ( N ) : NEW_LINE INDENT IsPrime = [ True ] * 1000005 NEW_LINE DEDENT / * Initialize the array to true * / NEW_LINE INDENT for p in range ( 2 , 1000005 ) : NEW_LINE INDENT if p * p > 1000005 : NEW_LINE INDENT break NEW_LINE DEDENT if ( IsPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 1000005 , p ) : NEW_LINE INDENT IsPrime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT Composites = [ ] NEW_LINE for p in range ( 4 , 1000005 ) : NEW_LINE INDENT if ( not IsPrime [ p ] ) : NEW_LINE INDENT Composites . append ( p ) NEW_LINE DEDENT DEDENT return Composites [ N - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( NthComposite ( N ) ) NEW_LINE DEDENT
Intersection point of two Linked Lists | Set 3 | Structure of a node of a Linked List ;  ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a . data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE DEDENT DEDENT / * Constructor * / def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . next = None NEW_LINE DEDENT def intersectingNode ( headA , headB ) : NEW_LINE INDENT a = headA NEW_LINE while ( a ) : NEW_LINE INDENT a . data *= - 1 NEW_LINE a = a . next NEW_LINE DEDENT b = headB NEW_LINE while ( b ) : NEW_LINE INDENT if ( b . data < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT b = b . next NEW_LINE DEDENT return b NEW_LINE DEDENT def formLinkList ( head1 , head2 ) : NEW_LINE INDENT head1 = Node ( 3 ) NEW_LINE head1 . next = Node ( 6 ) NEW_LINE head1 . next . next = Node ( 9 ) NEW_LINE head1 . next . next . next = Node ( 15 ) NEW_LINE head1 . next . next . next . next = Node ( 30 ) NEW_LINE head2 = Node ( 10 ) NEW_LINE head2 . next = head1 . next . next . next NEW_LINE return head1 , head2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head1 , head2 = formLinkList ( None , None ) NEW_LINE print ( abs ( intersectingNode ( head1 , head2 ) . data ) ) NEW_LINE DEDENT
Modify array of strings by replacing characters repeating in the same or remaining strings | Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Driver Code ; Given array of strings ; Function Call to modify the given array of strings
def removeDuplicateCharacters ( arr ) : NEW_LINE INDENT cset = set ( [ ] ) NEW_LINE n = len ( arr ) NEW_LINE out = [ ] NEW_LINE for st in arr : NEW_LINE INDENT out_curr = " " NEW_LINE for ch in st : NEW_LINE INDENT if ( ch in cset ) : NEW_LINE INDENT continue NEW_LINE DEDENT out_curr += ch NEW_LINE cset . add ( ch ) NEW_LINE DEDENT if ( len ( out_curr ) ) : NEW_LINE INDENT out . append ( out_curr ) NEW_LINE DEDENT DEDENT for i in range ( len ( out ) ) : NEW_LINE INDENT print ( out [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " Geeks " , " For " , " Geeks " , " Post " ] NEW_LINE removeDuplicateCharacters ( arr ) NEW_LINE DEDENT