codeparrot/xlcost-text-to-code huggingface dataset:
Check if given Preorder , Inorder and Postorder traversals are of same tree | Python3 program to check if all three given traversals are of the same tree ; A Binary Tree Node ; Function to find index of value in arr [ start ... end ] . The function assumes that value is present in in ; Recursive function to construct binary tree of size lenn from Inorder traversal in and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and lenn - 1. The function doesn 't do any error checking for cases where inorder and preorder do not form a tree ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; function to compare Postorder traversal on constructed tree and given Postorder ; first recur on left child ; now recur on right child ; Compare if data at current index in both Postorder traversals are same ; Driver code ; build tree from given Inorder and Preorder traversals ; compare postorder traversal on constructed tree with given Postorder traversal ; If both postorder traversals are same
preIndex = 0 NEW_LINE class node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def search ( arr , strt , end , value ) : NEW_LINE INDENT for i in range ( strt , end + 1 ) : NEW_LINE INDENT if ( arr [ i ] == value ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT def buildTree ( inn , pre , inStrt , inEnd ) : NEW_LINE INDENT global preIndex NEW_LINE if ( inStrt > inEnd ) : NEW_LINE INDENT return None NEW_LINE DEDENT tNode = node ( pre [ preIndex ] ) NEW_LINE preIndex += 1 NEW_LINE if ( inStrt == inEnd ) : NEW_LINE INDENT return tNode NEW_LINE DEDENT inIndex = search ( inn , inStrt , inEnd , tNode . data ) NEW_LINE tNode . left = buildTree ( inn , pre , inStrt , inIndex - 1 ) NEW_LINE tNode . right = buildTree ( inn , pre , inIndex + 1 , inEnd ) NEW_LINE return tNode NEW_LINE DEDENT def checkPostorder ( node , postOrder , index ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return index NEW_LINE DEDENT index = checkPostorder ( node . left , postOrder , index ) NEW_LINE index = checkPostorder ( node . right , postOrder , index ) NEW_LINE if ( node . data == postOrder [ index ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return index NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT inOrder = [ 4 , 2 , 5 , 1 , 3 ] NEW_LINE preOrder = [ 1 , 2 , 4 , 5 , 3 ] NEW_LINE postOrder = [ 4 , 5 , 2 , 3 , 1 ] NEW_LINE lenn = len ( inOrder ) NEW_LINE root = buildTree ( inOrder , preOrder , 0 , lenn - 1 ) NEW_LINE index = checkPostorder ( root , postOrder , 0 ) NEW_LINE if ( index == lenn ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if X can give change to every person in the Queue | Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; queue of customers with available notes . ; Calling function
def isChangeable ( notes , n ) : NEW_LINE INDENT fiveCount = 0 NEW_LINE tenCount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( notes [ i ] == 5 ) : NEW_LINE INDENT fiveCount += 1 NEW_LINE DEDENT elif ( notes [ i ] == 10 ) : NEW_LINE INDENT if ( fiveCount > 0 ) : NEW_LINE INDENT fiveCount -= 1 NEW_LINE tenCount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( fiveCount > 0 and tenCount > 0 ) : NEW_LINE INDENT fiveCount -= 1 NEW_LINE tenCount -= 1 NEW_LINE DEDENT elif ( fiveCount >= 3 ) : NEW_LINE INDENT fiveCount -= 3 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT a = [ 5 , 5 , 5 , 10 , 20 ] NEW_LINE n = len ( a ) NEW_LINE if ( isChangeable ( a , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Index Mapping ( or Trivial Hashing ) with negatives allowed | Python3 program to implement direct index mapping with negative values allowed . ; Since array is global , it is initialized as 0. ; Searching if X is Present in the given array or not . ; if X is negative take the absolute value of X . ; Driver code ; Since array is global , it is initialized as 0.
MAX = 1000 NEW_LINE has = [ [ 0 for i in range ( 2 ) ] for j in range ( MAX + 1 ) ] NEW_LINE def search ( X ) : NEW_LINE INDENT if X >= 0 : NEW_LINE INDENT return has [ X ] [ 0 ] == 1 NEW_LINE DEDENT X = abs ( X ) NEW_LINE return has [ X ] [ 1 ] == 1 NEW_LINE DEDENT def insert ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if a [ i ] >= 0 : NEW_LINE INDENT has [ a [ i ] ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT has [ abs ( a [ i ] ) ] [ 1 ] = 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ - 1 , 9 , - 5 , - 8 , - 5 , - 2 ] NEW_LINE n = len ( a ) NEW_LINE insert ( a , n ) NEW_LINE X = - 5 NEW_LINE if search ( X ) == True : NEW_LINE INDENT print ( " Present " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Present " ) NEW_LINE DEDENT DEDENT
Given level order traversal of a Binary Tree , check if the Tree is a Min | Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code
def isMinHeap ( level , n ) : NEW_LINE INDENT for i in range ( int ( n / 2 ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if level [ i ] > level [ 2 * i + 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT if 2 * i + 2 < n : NEW_LINE INDENT if level [ i ] > level [ 2 * i + 2 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT level = [ 10 , 15 , 14 , 25 , 30 ] NEW_LINE n = len ( level ) NEW_LINE if isMinHeap ( level , n ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT
Minimum delete operations to make all elements of array same | Function to get minimum number of elements to be deleted from array to make array elements equal ; Create an dictionary and store frequencies of all array elements in it using element as key and frequency as value ; Find maximum frequency among all frequencies . ; To minimize delete operations , we remove all elements but the most frequent element . ; Driver code
def minDelete ( arr , n ) : NEW_LINE INDENT freq = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in freq : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ arr [ i ] ] = 1 ; NEW_LINE DEDENT DEDENT max_freq = 0 ; NEW_LINE for i , j in freq . items ( ) : NEW_LINE INDENT max_freq = max ( max_freq , j ) ; NEW_LINE DEDENT return n - max_freq ; NEW_LINE DEDENT arr = [ 4 , 3 , 4 , 4 , 2 , 4 ] ; NEW_LINE n = len ( arr ) NEW_LINE print ( minDelete ( arr , n ) ) ; NEW_LINE
Minimum operation to make all elements equal in array | Python3 program to find the minimum number of operations required to make all elements of array equal ; Function for min operation ; Insert all elements in hash . ; find the max frequency ; return result ; Driver Code
from collections import defaultdict NEW_LINE def minOperation ( arr , n ) : NEW_LINE INDENT Hash = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT max_count = 0 NEW_LINE for i in Hash : NEW_LINE INDENT if max_count < Hash [ i ] : NEW_LINE INDENT max_count = Hash [ i ] NEW_LINE DEDENT DEDENT return n - max_count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minOperation ( arr , n ) ) NEW_LINE DEDENT
Maximum distance between two occurrences of same element in array | Function to find maximum distance between equal elements ; Used to store element to first index mapping ; Traverse elements and find maximum distance between same occurrences with the help of map . ; If this is first occurrence of element , insert its index in map ; Else update max distance ; Driver Program
def maxDistance ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE maxDict = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] not in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] = i NEW_LINE DEDENT else : NEW_LINE INDENT maxDict = max ( maxDict , i - mp [ arr [ i ] ] ) NEW_LINE DEDENT DEDENT return maxDict NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print maxDistance ( arr , n ) NEW_LINE DEDENT
Check if a given array contains duplicate elements within k distance from each other | Python 3 program to Check if a given array contains duplicate elements within k distance from each other ; Creates an empty list ; Traverse the input array ; If already present n hash , then we found a duplicate within k distance ; Add this item to hashset ; Remove the k + 1 distant item ; Driver Code
def checkDuplicatesWithinK ( arr , n , k ) : NEW_LINE INDENT myset = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in myset : NEW_LINE INDENT return True NEW_LINE DEDENT myset . append ( arr [ i ] ) NEW_LINE if ( i >= k ) : NEW_LINE INDENT myset . remove ( arr [ i - k ] ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkDuplicatesWithinK ( arr , n , 3 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find duplicates in a given array when elements are not limited to a range | Function to find the Duplicates , if duplicate occurs 2 times or more than 2 times in array so , it will print duplicate value only once at output ; Initialize ifPresent as false ; ArrayList to store the output ; Checking if element is present in the ArrayList or not if present then break ; If element is not present in the ArrayList then add it to ArrayList and make ifPresent at true ; If duplicates is present then print ArrayList ; Driver Code
def findDuplicates ( arr , Len ) : NEW_LINE INDENT ifPresent = False NEW_LINE a1 = [ ] NEW_LINE for i in range ( Len - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , Len ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT if arr [ i ] in a1 : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT a1 . append ( arr [ i ] ) NEW_LINE ifPresent = True NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ifPresent ) : NEW_LINE INDENT print ( a1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ duplicates ▁ present ▁ in ▁ arrays " ) NEW_LINE DEDENT DEDENT arr = [ 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE findDuplicates ( arr , n ) NEW_LINE
Check if leaf traversal of two Binary Trees is same ? | Binary Tree node ; checks if a given node is leaf or not . ; Returns true of leaf traversal of two trees is same , else false ; Create empty stacks . These stacks are going to be used for iterative traversals . ; Loop until either of two stacks is not empty ; If one of the stacks is empty means other stack has extra leaves so return false ; append right and left children of temp1 . Note that right child is inserted before left ; same for tree2 ; If one is None and other is not , then return false ; If both are not None and data is not same return false ; If control reaches this point , all leaves are matched ; Driver Code ; Let us create trees in above example 1
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = self . right = None NEW_LINE DEDENT def isLeaf ( self ) : NEW_LINE INDENT return ( self . left == None and self . right == None ) NEW_LINE DEDENT DEDENT def isSame ( root1 , root2 ) : NEW_LINE INDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE s1 . append ( root1 ) NEW_LINE s2 . append ( root2 ) NEW_LINE while ( len ( s1 ) != 0 or len ( s2 ) != 0 ) : NEW_LINE INDENT if ( len ( s1 ) == 0 or len ( s2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT temp1 = s1 . pop ( - 1 ) NEW_LINE while ( temp1 != None and not temp1 . isLeaf ( ) ) : NEW_LINE INDENT if ( temp1 . right != None ) : NEW_LINE INDENT s1 . append ( temp1 . right ) NEW_LINE DEDENT if ( temp1 . left != None ) : NEW_LINE INDENT s1 . append ( temp1 . left ) NEW_LINE temp1 = s1 . pop ( - 1 ) NEW_LINE DEDENT DEDENT temp2 = s2 . pop ( - 1 ) NEW_LINE while ( temp2 != None and not temp2 . isLeaf ( ) ) : NEW_LINE INDENT if ( temp2 . right != None ) : NEW_LINE INDENT s2 . append ( temp2 . right ) NEW_LINE DEDENT if ( temp2 . left != None ) : NEW_LINE INDENT s2 . append ( temp2 . left ) NEW_LINE DEDENT temp2 = s2 . pop ( - 1 ) NEW_LINE DEDENT if ( temp1 == None and temp2 != None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( temp1 != None and temp2 == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( temp1 != None and temp2 != None ) : NEW_LINE INDENT if ( temp1 . data != temp2 . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = Node ( 1 ) NEW_LINE root1 . left = Node ( 2 ) NEW_LINE root1 . right = Node ( 3 ) NEW_LINE root1 . left . left = Node ( 4 ) NEW_LINE root1 . right . left = Node ( 6 ) NEW_LINE root1 . right . right = Node ( 7 ) NEW_LINE root2 = Node ( 0 ) NEW_LINE root2 . left = Node ( 1 ) NEW_LINE root2 . right = Node ( 5 ) NEW_LINE root2 . left . right = Node ( 4 ) NEW_LINE root2 . right . left = Node ( 6 ) NEW_LINE root2 . right . right = Node ( 7 ) NEW_LINE if ( isSame ( root1 , root2 ) ) : NEW_LINE INDENT print ( " Same " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Same " ) NEW_LINE DEDENT DEDENT
Most frequent element in an array | Python3 program to find the most frequent element in an array . ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver Code
def mostFrequent ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE max_count = 1 ; res = arr [ 0 ] ; curr_count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT curr_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ i - 1 ] NEW_LINE DEDENT curr_count = 1 NEW_LINE DEDENT DEDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ n - 1 ] NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( mostFrequent ( arr , n ) ) NEW_LINE
Most frequent element in an array | Python3 program to find the most frequent element in an array . ; Insert all elements in Hash . ; find the max frequency ; Driver Code
import math as mt NEW_LINE def mostFrequent ( arr , n ) : NEW_LINE INDENT Hash = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in Hash . keys ( ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Hash [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT max_count = 0 NEW_LINE res = - 1 NEW_LINE for i in Hash : NEW_LINE INDENT if ( max_count < Hash [ i ] ) : NEW_LINE INDENT res = i NEW_LINE max_count = Hash [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( mostFrequent ( arr , n ) ) NEW_LINE
Smallest subarray with all occurrences of a most frequent element | Python3 implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver code
def smallestSubsegment ( a , n ) : NEW_LINE INDENT left = dict ( ) NEW_LINE count = dict ( ) NEW_LINE mx = 0 NEW_LINE mn , strindex = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = a [ i ] NEW_LINE if ( x not in count . keys ( ) ) : NEW_LINE INDENT left [ x ] = i NEW_LINE count [ x ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ x ] += 1 NEW_LINE DEDENT if ( count [ x ] > mx ) : NEW_LINE INDENT mx = count [ x ] NEW_LINE mn = i - left [ x ] + 1 NEW_LINE strindex = left [ x ] NEW_LINE DEDENT elif ( count [ x ] == mx and i - left [ x ] + 1 < mn ) : NEW_LINE INDENT mn = i - left [ x ] + 1 NEW_LINE strindex = left [ x ] NEW_LINE DEDENT DEDENT for i in range ( strindex , strindex + mn ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ 1 , 2 , 2 , 2 , 1 ] NEW_LINE n = len ( A ) NEW_LINE smallestSubsegment ( A , n ) NEW_LINE
Given an array of pairs , find all symmetric pairs in it | Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver Code
def findSymPairs ( arr , row ) : NEW_LINE INDENT hM = dict ( ) NEW_LINE for i in range ( row ) : NEW_LINE INDENT first = arr [ i ] [ 0 ] NEW_LINE sec = arr [ i ] [ 1 ] NEW_LINE if ( sec in hM . keys ( ) and hM [ sec ] == first ) : NEW_LINE INDENT print ( " ( " , sec , " , " , first , " ) " ) NEW_LINE DEDENT else : NEW_LINE INDENT hM [ first ] = sec NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 0 for i in range ( 2 ) ] for i in range ( 5 ) ] NEW_LINE arr [ 0 ] [ 0 ] , arr [ 0 ] [ 1 ] = 11 , 20 NEW_LINE arr [ 1 ] [ 0 ] , arr [ 1 ] [ 1 ] = 30 , 40 NEW_LINE arr [ 2 ] [ 0 ] , arr [ 2 ] [ 1 ] = 5 , 10 NEW_LINE arr [ 3 ] [ 0 ] , arr [ 3 ] [ 1 ] = 40 , 30 NEW_LINE arr [ 4 ] [ 0 ] , arr [ 4 ] [ 1 ] = 10 , 5 NEW_LINE findSymPairs ( arr , 5 ) NEW_LINE DEDENT
Find any one of the multiple repeating elements in read only array | Python 3 program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver Code ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5
from math import sqrt NEW_LINE def findRepeatingNumber ( arr , n ) : NEW_LINE INDENT sq = sqrt ( n ) NEW_LINE range__ = int ( ( n / sq ) + 1 ) NEW_LINE count = [ 0 for i in range ( range__ ) ] NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT count [ int ( ( arr [ i ] - 1 ) / sq ) ] += 1 NEW_LINE DEDENT selected_block = range__ - 1 NEW_LINE for i in range ( 0 , range__ - 1 , 1 ) : NEW_LINE INDENT if ( count [ i ] > sq ) : NEW_LINE INDENT selected_block = i NEW_LINE break NEW_LINE DEDENT DEDENT m = { i : 0 for i in range ( n ) } NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT if ( ( ( selected_block * sq ) < arr [ i ] ) and ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 NEW_LINE if ( m [ arr [ i ] ] > 1 ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 5 , 4 ] NEW_LINE n = 5 NEW_LINE print ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : " , findRepeatingNumber ( arr , n ) ) NEW_LINE DEDENT
Group multiple occurrence of array elements ordered by first occurrence | A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , print it and all subsequent occurrences ; Driver Code
def groupElements ( arr , n ) : NEW_LINE INDENT visited = [ False ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT visited [ i ] = False NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( visited [ i ] == False ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE visited [ j ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT arr = [ 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE groupElements ( arr , n ) NEW_LINE
Group multiple occurrence of array elements ordered by first occurrence | A hashing based method to group all occurrences of individual elements ; Creates an empty hashmap ; Traverse the array elements , and store count for every element in HashMap ; Increment count of elements in HashMap ; Traverse array again ; Check if this is first occurrence ; If yes , then print the element ' count ' times ; And remove the element from HashMap . ; Driver Code
def orderedGroup ( arr ) : NEW_LINE INDENT hM = { } NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT hM [ arr [ i ] ] = hM . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT count = hM . get ( arr [ i ] , None ) NEW_LINE if count != None : NEW_LINE INDENT for j in range ( 0 , count ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT del hM [ arr [ i ] ] NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 3 , 10 , 10 , 4 , 1 , 3 ] NEW_LINE orderedGroup ( arr ) NEW_LINE DEDENT
How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver program
def areDisjoint ( set1 , set2 , m , n ) : NEW_LINE INDENT for i in range ( 0 , m ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( set1 [ i ] == set2 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT set1 = [ 12 , 34 , 11 , 9 , 3 ] NEW_LINE set2 = [ 7 , 2 , 1 , 5 ] NEW_LINE m = len ( set1 ) NEW_LINE n = len ( set2 ) NEW_LINE print ( " yes " ) if areDisjoint ( set1 , set2 , m , n ) else ( " ▁ No " ) NEW_LINE
How to check if two given sets are disjoint ? | Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver Code
def areDisjoint ( set1 , set2 , m , n ) : NEW_LINE INDENT set1 . sort ( ) NEW_LINE set2 . sort ( ) NEW_LINE i = 0 ; j = 0 NEW_LINE while ( i < m and j < n ) : NEW_LINE INDENT if ( set1 [ i ] < set2 [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif ( set2 [ j ] < set1 [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT set1 = [ 12 , 34 , 11 , 9 , 3 ] NEW_LINE set2 = [ 7 , 2 , 1 , 5 ] NEW_LINE m = len ( set1 ) NEW_LINE n = len ( set2 ) NEW_LINE print ( " Yes " ) if areDisjoint ( set1 , set2 , m , n ) else print ( " No " ) NEW_LINE