codeparrot/xlcost-text-to-code huggingface dataset:
Number from a given range that requires Kth smallest number of steps to get reduced to 1 | Python3 program for the above approach ; Function to count the number of steps required to reduce val to 1 by the given operations ; Base Case ; If val is even , divide by 2 ; Otherwise , multiply it by 3 and increment by 1 ; Function to find Kth smallest count of steps required for numbers from the range [ L , R ] ; Stores numbers and their respective count of steps ; Count the number of steps for anumbers from the range [ L , R ] ; Insert in the vector ; Sort the vector in ascending order w . r . t . to power value ; Print the K - th smallest number ; Driver Code
v = [ - 1 ] * ( 1000000 ) NEW_LINE def power_value ( val ) : NEW_LINE INDENT if ( val == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( val % 2 == 0 ) : NEW_LINE INDENT v [ val ] = power_value ( val // 2 ) + 1 NEW_LINE return v [ val ] NEW_LINE DEDENT else : NEW_LINE INDENT temp = val * 3 NEW_LINE temp += 1 NEW_LINE v [ val ] = power_value ( temp ) + 1 NEW_LINE return v [ val ] NEW_LINE DEDENT DEDENT def getKthNumber ( l , r , k ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( v [ i ] == - 1 ) : NEW_LINE INDENT power_value ( i ) NEW_LINE DEDENT DEDENT j = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT ans . append ( [ v [ i ] , i ] ) NEW_LINE j += 1 NEW_LINE DEDENT ans = sorted ( ans ) NEW_LINE print ( ans [ k - 1 ] [ 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R , K = 7 , 10 , 4 NEW_LINE getKthNumber ( L , R , K ) NEW_LINE DEDENT
Sum of decimal equivalents of binary node values in each level of a Binary Tree | Structure of a Tree Node ; Function to convert binary number to its equivalent decimal value ; Function to calculate sum of decimal equivalent of binary numbers of node values present at each level ; Push root node into queue ; Connect nodes at the same level to form a binary number ; Stores the front element of the queue ; Append the value of the current node to eachLvl ; Insert the Left child to queue , if its not NULL ; Insert the Right child to queue , if its not NULL ; Decrement length by one ; Add decimal equivalent of the binary number formed on the current level to ans ; Finally print ans ; Given Tree ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = 0 , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def convertBinaryToDecimal ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in arr : NEW_LINE INDENT ans = ( ans << 1 ) | i NEW_LINE DEDENT return ans NEW_LINE DEDENT def decimalEquilvalentAtEachLevel ( root ) : NEW_LINE INDENT ans = 0 NEW_LINE que = [ root ] NEW_LINE while True : NEW_LINE INDENT length = len ( que ) NEW_LINE if not length : NEW_LINE INDENT break NEW_LINE DEDENT eachLvl = [ ] NEW_LINE while length : NEW_LINE INDENT temp = que . pop ( 0 ) NEW_LINE eachLvl . append ( temp . val ) NEW_LINE if temp . left : NEW_LINE INDENT que . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT que . append ( temp . right ) NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT ans += convertBinaryToDecimal ( eachLvl ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT root = TreeNode ( 0 ) NEW_LINE root . left = TreeNode ( 1 ) NEW_LINE root . right = TreeNode ( 0 ) NEW_LINE root . left . left = TreeNode ( 0 ) NEW_LINE root . left . right = TreeNode ( 1 ) NEW_LINE root . right . left = TreeNode ( 1 ) NEW_LINE root . right . right = TreeNode ( 1 ) NEW_LINE decimalEquilvalentAtEachLevel ( root ) NEW_LINE
Minimize increments required to make differences between all pairs of array elements even | Function to find the minimum increments required to difference between all pairs of array elements even ; Store the count of odd and even numbers ; Traverse the array ; Increment evenCnt by 1 ; Increment eveCnt by 1 ; Prthe minimum of oddCnt and eveCnt ; Driver Code
def minimumOperations ( arr , N ) : NEW_LINE INDENT oddCnt = 0 NEW_LINE evenCnt = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT evenCnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT oddCnt += 1 NEW_LINE DEDENT DEDENT print ( min ( oddCnt , evenCnt ) ) NEW_LINE DEDENT arr = [ 4 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minimumOperations ( arr , N ) NEW_LINE
Minimum increments required to make array elements alternately even and odd | Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array arr [ ] ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
def minIncr ( arr ) : NEW_LINE INDENT forEven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT if not arr [ i ] % 2 : NEW_LINE INDENT forEven += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr [ i ] % 2 : NEW_LINE INDENT forEven += 1 NEW_LINE DEDENT DEDENT DEDENT return min ( forEven , len ( arr ) - forEven ) NEW_LINE DEDENT arr = [ 1 , 4 , 6 , 8 , 9 , 5 ] NEW_LINE print ( minIncr ( arr ) ) NEW_LINE
Queries to find the minimum array sum possible by removing elements from either end | Function to find the minimum sum for each query after removing element from either ends till each value Q [ i ] ; Stores the prefix sum from both the ends of the array ; Traverse the array from front ; Insert it into the map m1 ; Traverse the array in reverse ; Insert it into the map m2 ; Traverse the query array ; Print the minimum of the two values as the answer ; Driver Code ; Function Call
def minOperations ( arr , N , Q , M ) : NEW_LINE INDENT m1 = { } NEW_LINE m2 = { } NEW_LINE front = 0 NEW_LINE rear = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT front += arr [ i ] NEW_LINE m1 [ arr [ i ] ] = front NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT rear += arr [ i ] NEW_LINE m2 [ arr [ i ] ] = rear NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT print ( min ( m1 [ Q [ i ] ] , m2 [ Q [ i ] ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ 7 , 6 ] NEW_LINE M = len ( Q ) NEW_LINE minOperations ( arr , N , Q , M ) NEW_LINE
Check if a string contains an anagram of another string as its substring | Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code
def checkAnagram ( s1 , s2 ) : NEW_LINE INDENT s2hash = [ 0 for i in range ( 26 ) ] NEW_LINE s1hash = [ 0 for i in range ( 26 ) ] NEW_LINE s1len = len ( s1 ) NEW_LINE s2len = len ( s2 ) NEW_LINE if ( s1len > s2len ) : NEW_LINE INDENT return False NEW_LINE DEDENT left = 0 NEW_LINE right = 0 NEW_LINE while ( right < s1len ) : NEW_LINE INDENT s1hash [ ord ( s1 [ right ] ) - 97 ] += 1 NEW_LINE s2hash [ ord ( s2 [ right ] ) - 97 ] += 1 NEW_LINE right += 1 NEW_LINE DEDENT right -= 1 NEW_LINE while ( right < s2len ) : NEW_LINE INDENT if ( s1hash == s2hash ) : NEW_LINE INDENT return True NEW_LINE DEDENT right += 1 NEW_LINE if ( right != s2len ) : NEW_LINE INDENT s2hash [ ord ( s2 [ right ] ) - 97 ] += 1 NEW_LINE DEDENT s2hash [ ord ( s2 [ left ] ) - 97 ] -= 1 NEW_LINE left += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " ab " NEW_LINE s2 = " bbpobac " NEW_LINE if ( checkAnagram ( s1 , s2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Maximum number of intervals that an interval can intersect | Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver code ; Function Call
def findMaxIntervals ( v , n ) : NEW_LINE INDENT maxi = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT c = n NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( v [ i ] [ 1 ] < v [ j ] [ 0 ] or v [ i ] [ 0 ] > v [ j ] [ 1 ] ) : NEW_LINE INDENT c -= 1 NEW_LINE DEDENT DEDENT maxi = max ( c , maxi ) NEW_LINE DEDENT print ( maxi ) NEW_LINE DEDENT arr = [ ] NEW_LINE arr . append ( ( 1 , 2 ) ) NEW_LINE arr . append ( ( 3 , 4 ) ) NEW_LINE arr . append ( ( 2 , 5 ) ) NEW_LINE N = len ( arr ) NEW_LINE findMaxIntervals ( arr , N ) NEW_LINE
Check if all disks can be placed at a single rod based on given conditions | Function to check if it is possible to move all disks to a single rod ; Stores if it is possible to move all disks to a single rod ; Traverse the array ; If i - th element is smaller than both its adjacent elements ; If flag is true ; Otherwise ; Driver Code
def check ( a , n ) : NEW_LINE INDENT flag = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( a [ i + 1 ] > a [ i ] and a [ i ] < a [ i - 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( flag != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 5 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE if ( check ( arr , N ) != 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Smallest positive integer that does not divide any elements of the given array | Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Prthe answer ; Driver Code ; Function Call
def smallestNumber ( arr , lenn ) : NEW_LINE INDENT maxi = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT maxi = max ( maxi , arr [ i ] ) NEW_LINE DEDENT ans = - 1 NEW_LINE for i in range ( 2 , maxi + 2 ) : NEW_LINE INDENT flag = True NEW_LINE for j in range ( lenn ) : NEW_LINE INDENT if ( arr [ j ] % i == 0 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 6 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE smallestNumber ( arr , N ) NEW_LINE DEDENT
Find the duplicate characters in a string in O ( 1 ) space | Function to find duplicate characters in str1ing without using any additional data str1ucture ; Check if ( i + ' a ' ) is present in str1 at least once or not . ; Check if ( i + ' a ' ) is present in str1 at least twice or not . ; Iterate over the characters of the str1ing str1 ; If str1 [ i ] has already occurred in str1 ; Set ( str1 [ i ] - ' a ' ) - th bit of second ; Set ( str1 [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code
def findDuplicate ( str1 , N ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( first & ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) ) : NEW_LINE INDENT second = second | ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT first = first | ( 1 << ( ord ( str1 [ i ] ) - 97 ) ) NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( ( first & ( 1 << i ) ) and ( second & ( 1 << i ) ) ) : NEW_LINE INDENT print ( chr ( i + 97 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE N = len ( str1 ) NEW_LINE findDuplicate ( str1 , N ) NEW_LINE DEDENT
Minimize insertions required to make all characters of a given string equal | Function to calculate the minimum number of operations required to make all characters of the string same ; Stores count of operations ; Traverse the string ; Check if adjacent characters are same or not ; Increment count ; Prthe count obtained ; Driver Code
def minOperations ( S ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT if ( S [ i ] != S [ i - 1 ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "0101010101" ; NEW_LINE minOperations ( S ) ; NEW_LINE DEDENT
Count pairs made up of an element divisible by the other from an array consisting of powers of 2 | Function to count the number of pairs as per the given conditions ; Initialize array set_bits as 0 ; Store the total number of required pairs ; Traverse the array arr [ ] ; Store arr [ i ] in x ; Store the position of the leftmost set bit in arr [ i ] ; Increase bit position ; Divide by 2 to shift bits in right at each step ; Count of pairs for index i till its set bit position ; Increasing count of set bit position of current elelement ; Prthe answer ; Driver Code ; Function Call
def numberOfPairs ( arr , N ) : NEW_LINE INDENT set_bits = [ 0 ] * 31 NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE bitpos = - 1 NEW_LINE while ( x > 0 ) : NEW_LINE INDENT bitpos += 1 NEW_LINE x //= 2 NEW_LINE DEDENT for j in range ( bitpos + 1 ) : NEW_LINE INDENT count += set_bits [ j ] NEW_LINE DEDENT set_bits [ bitpos ] += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 16 , 8 , 64 ] NEW_LINE N = len ( arr ) NEW_LINE numberOfPairs ( arr , N ) NEW_LINE DEDENT
Queries to find minimum sum of array elements from either end of an array | Function to calculate the minimum sum from either end of the arrays for the given queries ; Traverse the query [ ] array ; Stores sum from start and end of the array ; Calculate distance from start ; Calculate distance from end ; Driver Code
def calculateQuery ( arr , N , query , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT X = query [ i ] NEW_LINE sum_start = 0 NEW_LINE sum_end = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT sum_start += arr [ j ] NEW_LINE if ( arr [ j ] == X ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum_end += arr [ j ] NEW_LINE if ( arr [ j ] == X ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( min ( sum_end , sum_start ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 30 ] NEW_LINE queries = [ 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( queries ) NEW_LINE calculateQuery ( arr , N , queries , M ) NEW_LINE DEDENT
Queries to find minimum sum of array elements from either end of an array | Function to find the minimum sum for the given queries ; Stores prefix and suffix sums ; Stores pairs of prefix and suffix sums ; Traverse the array ; Add element to prefix ; Store prefix for each element ; Traverse the array in reverse ; Add element to suffix ; Storing suffix for each element ; Traverse the array queries [ ] ; Minimum of suffix and prefix sums ; Driver Code
def calculateQuery ( arr , N , query , M ) : NEW_LINE INDENT prefix = 0 NEW_LINE suffix = 0 NEW_LINE mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT prefix += arr [ i ] NEW_LINE mp [ arr [ i ] ] = [ prefix , 0 ] NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT suffix += arr [ i ] NEW_LINE mp [ arr [ i ] ] = [ mp [ arr [ i ] ] [ 0 ] , suffix ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT X = query [ i ] NEW_LINE print ( min ( mp [ X ] [ 0 ] , mp [ X ] [ 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 30 ] NEW_LINE queries = [ 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( queries ) NEW_LINE calculateQuery ( arr , N , queries , M ) NEW_LINE
Queries to find the maximum array element after removing elements from a given range | Function to find the maximum element after removing elements in range [ l , r ] ; Store prefix maximum element ; Store suffix maximum element ; Prefix max till first index is first index itself ; Traverse the array to fill prefix max array ; Store maximum till index i ; Suffix max till last index is last index itself ; Traverse the array to fill suffix max array ; Store maximum till index i ; Traverse all queries ; Store the starting and the ending index of the query ; Edge Cases ; Otherwise ; Driver Code
def findMaximum ( arr , N , Q , queries ) : NEW_LINE INDENT prefix_max = [ 0 ] * ( N + 1 ) NEW_LINE suffix_max = [ 0 ] * ( N + 1 ) NEW_LINE prefix_max [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT prefix_max [ i ] = max ( prefix_max [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT suffix_max [ N - 1 ] = arr [ N - 1 ] NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix_max [ i ] = max ( suffix_max [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT for i in range ( Q ) : NEW_LINE INDENT l = queries [ i ] [ 0 ] NEW_LINE r = queries [ i ] [ 1 ] NEW_LINE if ( l == 0 and r == ( N - 1 ) ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( l == 0 ) : NEW_LINE INDENT print ( suffix_max [ r + 1 ] ) NEW_LINE DEDENT elif ( r == ( N - 1 ) ) : NEW_LINE INDENT print ( prefix_max [ l - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( max ( prefix_max [ l - 1 ] , suffix_max [ r + 1 ] ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 6 , 8 , 10 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE queries = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 4 ] ] NEW_LINE Q = len ( queries ) NEW_LINE findMaximum ( arr , N , Q , queries ) NEW_LINE DEDENT
Queries to find first occurrence of a character in a given range | Python3 implementation for the above approach ; Function to find the first occurence for a given range ; N = length of string ; M = length of queries ; Stores the indices of a character ; Traverse the string ; Push the index i into the vector [ s [ i ] ] ; Traverse the query ; Stores the value L ; Stores the value R ; Stores the character C ; Find index >= L in the vector v ; If there is no index of C >= L ; Stores the value at idx ; If idx > R ; Otherwise ; Driver Code
from bisect import bisect_left NEW_LINE def firstOccurence ( s , Q ) : NEW_LINE INDENT N = len ( s ) NEW_LINE M = len ( Q ) NEW_LINE v = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT v [ ord ( s [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT left = Q [ i ] [ 0 ] NEW_LINE right = Q [ i ] [ 1 ] NEW_LINE c = Q [ i ] [ 2 ] NEW_LINE if ( len ( v [ ord ( c ) - ord ( ' a ' ) ] ) == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE continue NEW_LINE DEDENT idx = bisect_left ( v [ ord ( c ) - ord ( ' a ' ) ] , left ) NEW_LINE if ( idx == len ( v [ ord ( c ) - ord ( ' a ' ) ] ) ) : NEW_LINE INDENT print ( " - 1 ▁ " ) NEW_LINE continue NEW_LINE DEDENT idx = v [ ord ( c ) - ord ( ' a ' ) ] [ idx ] NEW_LINE if ( idx > right ) : NEW_LINE INDENT print ( " - 1 ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( idx , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcabcabc " ; NEW_LINE Q = [ [ 0 , 3 , ' a ' ] , [ 0 , 2 , ' b ' ] , [ 2 , 4 , ' z ' ] ] NEW_LINE firstOccurence ( S , Q ) NEW_LINE DEDENT
Find index of the element differing in parity with all other array elements | Function to prthe element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Given array ; Size of the array
def oddOneOut ( arr , N ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE lastOdd = 0 NEW_LINE lastEven = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE lastEven = i NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE lastOdd = i NEW_LINE DEDENT DEDENT if ( odd == 1 ) : NEW_LINE INDENT print ( lastOdd ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( lastEven ) NEW_LINE DEDENT DEDENT arr = [ 2 , 4 , 7 , 8 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE oddOneOut ( arr , N ) NEW_LINE
Reduce a string to a valid email address of minimum length by replacing specified substrings | Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver Code ; To display the result
def minEmail ( email ) : NEW_LINE INDENT ans = ' ' NEW_LINE ans += email [ 0 ] NEW_LINE i = 1 NEW_LINE notAt = True NEW_LINE while i < len ( email ) : NEW_LINE INDENT if ( i < len ( email ) - 3 and notAt and email [ i : i + 2 ] == ' at ' ) : NEW_LINE INDENT ans += ' @ ' NEW_LINE i += 1 NEW_LINE notAt = False NEW_LINE DEDENT elif i < len ( email ) - 4 and email [ i : i + 3 ] == ' dot ' : NEW_LINE INDENT ans += ' . ' NEW_LINE i += 2 NEW_LINE DEDENT else : NEW_LINE INDENT ans += email [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT email = ' geeksforgeeksatgmaildotcom ' NEW_LINE print ( minEmail ( email ) ) NEW_LINE DEDENT
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
def isGreaterEqual ( N , X ) : NEW_LINE INDENT return ( N * ( N + 1 ) // 2 ) >= X NEW_LINE DEDENT def minimumPossible ( X ) : NEW_LINE INDENT for i in range ( 1 , X + 1 ) : NEW_LINE INDENT if ( isGreaterEqual ( i , X ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 14 NEW_LINE print ( minimumPossible ( X ) ) NEW_LINE DEDENT
Queries to count numbers from given range which are divisible by all its digits | Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
def CheckDivByAllDigits ( number ) : NEW_LINE INDENT n = number NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n % 10 ) : NEW_LINE INDENT if ( number % ( n % 10 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def cntNumInRang ( arr , N ) : NEW_LINE INDENT global Max NEW_LINE prefCntDiv = [ 0 ] * Max NEW_LINE for i in range ( 1 , Max ) : NEW_LINE INDENT prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 5 ] , [ 12 , 14 ] ] NEW_LINE Max = 1000005 NEW_LINE N = len ( arr ) NEW_LINE cntNumInRang ( arr , N ) NEW_LINE DEDENT