text
stringlengths
1
636
code
stringlengths
8
1.89k
Print the pairs
print ( c , " ▁ " , d ) ; NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array
arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE
Function call
maxProduct ( arr , N ) ; NEW_LINE
Utility function to check if a character is a vowel
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT
Function to calculate and return the count of substrings with even number of vowels
def countSubstrings ( s , n ) : NEW_LINE
Stores the count of substrings
result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE DEDENT
If the current character is a vowel
if ( isVowel ( s [ j ] ) ) : NEW_LINE
Increase count
count += 1 NEW_LINE
If substring contains even number of vowels
if ( count % 2 == 0 ) : NEW_LINE
Increase the answer
result += 1 NEW_LINE
Prthe final answer
print ( result ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE s = " abcde " NEW_LINE countSubstrings ( s , n ) NEW_LINE DEDENT
Utility function to check if a character is a vowel
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT
Function to calculate and return the count of substrings with even number of vowels
def countSubstrings ( s , n ) : NEW_LINE
Stores the count of substrings with even and odd number of vowels respectively
temp = [ 1 , 0 ] ; NEW_LINE result = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE
Update count of vowels modulo 2 in sum to obtain even or odd
sum += ( 1 if isVowel ( s [ i ] ) else 0 ) ; NEW_LINE sum %= 2 ; NEW_LINE
Increment even / odd count
temp [ sum ] += 1 ; NEW_LINE
Count substrings with even number of vowels using Handshaking Lemma
result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) // 2 ) ; NEW_LINE result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) // 2 ) ; NEW_LINE print ( result ) ; NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE s = " abcde " ; NEW_LINE countSubstrings ( s , n ) ; NEW_LINE DEDENT
Python3 program to implement the above approach
from collections import deque NEW_LINE
Structure of a Tree node
class Node : NEW_LINE
Function to create new node
def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT
Function returns required level of width k , if found else - 1
def findLevel ( root : Node , k : int , level : int ) -> int : NEW_LINE
To store the node and the label and perform traversal
qt = deque ( ) NEW_LINE qt . append ( [ root , 0 ] ) NEW_LINE count = 1 NEW_LINE b = 0 NEW_LINE a = 0 NEW_LINE while qt : NEW_LINE INDENT temp = qt . popleft ( ) NEW_LINE DEDENT
Taking the last label of each level of the tree
if ( count == 1 ) : NEW_LINE INDENT b = temp [ 1 ] NEW_LINE DEDENT if ( temp [ 0 ] . left ) : NEW_LINE INDENT qt . append ( [ temp [ 0 ] . left , 2 * temp [ 1 ] ] ) NEW_LINE DEDENT if ( temp [ 0 ] . right ) : NEW_LINE INDENT qt . append ( [ temp [ 0 ] . right , 2 * temp [ 1 ] + 1 ] ) NEW_LINE DEDENT count -= 1 NEW_LINE
Check width of current level
if ( count == 0 ) : NEW_LINE
If the width is equal to k then return that level
if ( b - a + 1 == k ) : NEW_LINE INDENT return level NEW_LINE DEDENT secondLabel = qt [ 0 ] NEW_LINE
Taking the first label of each level of the tree
a = secondLabel [ 1 ] NEW_LINE level += 1 NEW_LINE count = len ( qt ) NEW_LINE
If any level does not has width equal to k , return - 1
return - 1 NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 5 ) NEW_LINE root . left = Node ( 6 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . left . left = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE root . left . right . right = Node ( 4 ) NEW_LINE k = 4 NEW_LINE print ( findLevel ( root , k , 1 ) ) NEW_LINE DEDENT
Function to check operation can be perform or not
def possible ( arr , N , mid , K ) : NEW_LINE INDENT add = 0 NEW_LINE for i in range ( N // 2 - ( N + 1 ) % 2 , N ) : NEW_LINE INDENT if ( mid - arr [ i ] > 0 ) : NEW_LINE DEDENT DEDENT
Number of operation to perform s . t . mid is median
add += ( mid - arr [ i ] ) NEW_LINE if ( add > K ) : NEW_LINE INDENT return False NEW_LINE DEDENT
If mid is median of the array
if ( add <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT
Function to find max median of the array
def findMaxMedian ( arr , N , K ) : NEW_LINE
Lowest possible median
low = 1 NEW_LINE mx = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) NEW_LINE DEDENT
Highest possible median
high = K + mx NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( high + low ) // 2 NEW_LINE DEDENT
Checking for mid is possible for the median of array after doing at most k operation
if ( possible ( arr , N , mid , K ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT if ( N % 2 == 0 ) : NEW_LINE if ( low - 1 < arr [ N // 2 ] ) : NEW_LINE INDENT return ( arr [ N // 2 ] + low - 1 ) // 2 NEW_LINE DEDENT
Return the max possible ans
return low - 1 NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array
arr = [ 1 , 3 , 6 ] NEW_LINE
Given number of operation
K = 10 NEW_LINE
Size of array
N = len ( arr ) NEW_LINE
Sort the array
arr = sorted ( arr ) NEW_LINE
Function call
print ( findMaxMedian ( arr , N , K ) ) NEW_LINE
Function that returns true if the count of elements is less than mid
def countLessThanMid ( mid , N , M , K ) : NEW_LINE
To store count of elements less than mid
count = 0 NEW_LINE
Loop through each row
for i in range ( 1 , min ( N , mid ) + 1 ) : NEW_LINE
Count elements less than mid in the ith row
count = count + min ( mid // i , M ) NEW_LINE if ( count >= K ) : NEW_LINE return False NEW_LINE else : NEW_LINE return True NEW_LINE
Function that returns the Kth smallest element in the NxM Matrix after sorting in an array
def findKthElement ( N , M , K ) : NEW_LINE
Initialize low and high
low = 1 NEW_LINE high = N * M NEW_LINE
Perform binary search
while ( low <= high ) : NEW_LINE
Find the mid
mid = low + ( high - low ) // 2 NEW_LINE
Check if the count of elements is less than mid
if ( countLessThanMid ( mid , N , M , K ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT
Return Kth smallest element of the matrix
return high + 1 NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE M = 3 NEW_LINE K = 5 NEW_LINE print ( findKthElement ( N , M , K ) ) NEW_LINE DEDENT
Function that finds the count of substrings containing only character C in the S
def countSubString ( S , C ) : NEW_LINE
To store total count of substrings
count = 0 NEW_LINE
To store count of consecutive C 's
conCount = 0 NEW_LINE
Loop through the string
for ch in S : NEW_LINE
Increase the consecutive count of C 's
if ( ch == C ) : NEW_LINE INDENT conCount += 1 NEW_LINE DEDENT else : NEW_LINE
Add count of sub - strings from consecutive strings
count += ( ( conCount * ( conCount + 1 ) ) // 2 ) NEW_LINE
Reset the consecutive count of C 's
conCount = 0 NEW_LINE
Add count of sub - strings from consecutive strings
count += ( ( conCount * ( conCount + 1 ) ) // 2 ) NEW_LINE
Print the count of sub - strings containing only C
print ( count ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE C = ' e ' NEW_LINE countSubString ( S , C ) NEW_LINE DEDENT
Function to check if both halves of a string are palindrome or not
def checkPalindrome ( S ) : NEW_LINE
Length of string
n = len ( S ) NEW_LINE
Initialize both part as true
first_half = True NEW_LINE second_half = True NEW_LINE cnt = ( n // 2 ) - 1 NEW_LINE for i in range ( 0 , int ( ( n / 2 ) / 2 ) ) : NEW_LINE
If first half is not palindrome
if ( S [ i ] != S [ cnt ] ) : NEW_LINE INDENT first_half = False NEW_LINE break NEW_LINE DEDENT
If second half is not palindrome
if ( S [ n // 2 + i ] != S [ n // 2 + cnt ] ) : NEW_LINE INDENT second_half = False NEW_LINE break NEW_LINE DEDENT cnt -= 1 NEW_LINE
If both halves are palindrome
if ( first_half and second_half ) : NEW_LINE INDENT print ( ' Yes ' , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' , end = ' ' ) NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = ' momdad ' NEW_LINE checkPalindrome ( S ) NEW_LINE DEDENT
Python3 program to find palindromic string
from collections import defaultdict NEW_LINE def getCount ( N , s ) : NEW_LINE
Stores frequency array and its count
mp = defaultdict ( lambda : 0 ) NEW_LINE
Total number of pairs
ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE
Initializing array of size 26 to store count of character
a = [ 0 ] * 26 NEW_LINE
Counting occurrence of each character of current string
for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT a [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT
Convert each count to parity ( 0 or 1 ) on the basis of its frequency
for j in range ( 26 ) : NEW_LINE INDENT a [ j ] = a [ j ] % 2 NEW_LINE DEDENT
Adding to answer
ans += mp [ tuple ( a ) ] NEW_LINE
Frequency of single character can be possibly changed , so change its parity
for j in range ( 26 ) : NEW_LINE INDENT changedCount = a [ : ] NEW_LINE if ( a [ j ] == 0 ) : NEW_LINE INDENT changedCount [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT changedCount [ j ] = 0 NEW_LINE DEDENT ans += mp [ tuple ( changedCount ) ] NEW_LINE DEDENT mp [ tuple ( a ) ] += 1 NEW_LINE return ans NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE A = [ " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " ] NEW_LINE print ( getCount ( N , A ) ) NEW_LINE DEDENT
Python3 program to find K - th smallest element in a perfect BST
kth_smallest = 0 NEW_LINE
A BST node
class newNode : NEW_LINE
A utility function to create a new BST node
def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT
A utility function to insert a new node with given key in BST
def insert ( node , key ) : NEW_LINE
If the tree is empty
if ( node == None ) : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT
Recur down the left subtree for smaller values
if ( key < node . key ) : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE DEDENT
Recur down the right subtree for smaller values
elif ( key > node . key ) : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT
Return the ( unchanged ) node pointer
return node NEW_LINE
FUnction to find Kth Smallest element in a perfect BST
def KSmallestPerfectBST ( root , k , treeSize ) : NEW_LINE INDENT global kth_smallest NEW_LINE if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT
Find the median ( division operation is floored )
median_loc = ( treeSize // 2 ) + 1 NEW_LINE
If the element is at the median
if ( k == median_loc ) : NEW_LINE INDENT kth_smallest = root . key NEW_LINE return True NEW_LINE DEDENT
Calculate the number of nodes in the right and left sub - trees ( division operation is floored )
newTreeSize = treeSize // 2 NEW_LINE
If median is located higher
if ( k < median_loc ) : NEW_LINE INDENT return KSmallestPerfectBST ( root . left , k , newTreeSize ) NEW_LINE DEDENT
If median is located lower
newK = k - median_loc NEW_LINE return KSmallestPerfectBST ( root . right , newK , newTreeSize ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85
root = None NEW_LINE root = insert ( root , 50 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 20 ) NEW_LINE insert ( root , 40 ) NEW_LINE insert ( root , 70 ) NEW_LINE insert ( root , 60 ) NEW_LINE insert ( root , 80 ) NEW_LINE insert ( root , 14 ) NEW_LINE insert ( root , 25 ) NEW_LINE insert ( root , 35 ) NEW_LINE insert ( root , 45 ) NEW_LINE insert ( root , 55 ) NEW_LINE insert ( root , 65 ) NEW_LINE insert ( root , 75 ) NEW_LINE insert ( root , 85 ) NEW_LINE n = 15 NEW_LINE k = 5 NEW_LINE
Function call
if ( KSmallestPerfectBST ( root , k , n ) ) : NEW_LINE INDENT print ( kth_smallest , end = " ▁ " ) NEW_LINE DEDENT
Function to check if the string follows rules or not
def checkrules ( s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT