codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays consisting of first K natural numbers in descending order | Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver Code ; Function Call
def CountSubarray ( arr , n , k ) : NEW_LINE INDENT temp = k NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == temp ) : NEW_LINE INDENT if ( temp == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE temp = k NEW_LINE DEDENT else : NEW_LINE INDENT temp -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT temp = k NEW_LINE if ( arr [ i ] == k ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( CountSubarray ( arr , N , K ) ) NEW_LINE DEDENT
Smallest number not less than N which is divisible by all digits of N | Python3 program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call
import math NEW_LINE def LCM ( A , B ) : NEW_LINE INDENT return ( A * B // math . gcd ( A , B ) ) NEW_LINE DEDENT def findSmallestNumber ( X ) : NEW_LINE INDENT lcm = 1 NEW_LINE temp = X NEW_LINE while ( temp ) : NEW_LINE INDENT last = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( not last ) : NEW_LINE INDENT continue NEW_LINE DEDENT lcm = LCM ( lcm , last ) NEW_LINE DEDENT answer = ( ( X + lcm - 1 ) // lcm ) * lcm NEW_LINE print ( answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 280 NEW_LINE findSmallestNumber ( X ) NEW_LINE DEDENT
Minimum sum of two integers whose product is strictly greater than N | Function to find the minimum sum of two integers such that their product is strictly greater than N ; Initialise low as 0 and high as 1e9 ; Iterate to find the first number ; Find the middle value ; If mid ^ 2 is greater than equal to A , then update high to mid ; Otherwise update low ; Store the first number ; Again , set low as 0 and high as 1e9 ; Iterate to find the second number ; Find the middle value ; If first number * mid is greater than N then update high to mid ; Else , update low to mid ; Store the second number ; Print the result ; Driver Code ; Function Call
def minSum ( N ) : NEW_LINE INDENT low = 0 NEW_LINE high = 1000000000 NEW_LINE while ( low + 1 < high ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 NEW_LINE if ( mid * mid >= N ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT first = high NEW_LINE low = 0 NEW_LINE high = 1000000000 NEW_LINE while ( low + 1 < high ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 NEW_LINE if ( first * mid > N ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT second = high NEW_LINE print ( round ( first + second ) ) NEW_LINE DEDENT N = 10 NEW_LINE minSum ( N ) NEW_LINE
Minimum sum of two integers whose product is strictly greater than N | Python3 program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Store the answer using the AP - GP inequality ; Print the result ; Driver Code ; Function Call
import math NEW_LINE def minSum ( N ) : NEW_LINE INDENT ans = math . ceil ( 2 * math . sqrt ( N + 1 ) ) NEW_LINE print ( math . trunc ( ans ) ) NEW_LINE DEDENT N = 10 NEW_LINE minSum ( N ) NEW_LINE
Query to find length of the longest subarray consisting only of 1 s | Python 3 Program for the above approach ; Arrays to store prefix sums , suffix and MAX 's node respectively ; Function to construct Segment Tree ; MAX array for node MAX ; Array for prefix sum node ; Array for suffix sum node ; Calculate MAX node ; Function to update Segment Tree ; Update at position ; Update sums from bottom to the top of Segment Tree ; Update MAX tree ; Update pref tree ; Update suf tree ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Print longest length of subarray in [ 1 , N ] ; Flip the character ; Driver Code ; Size of array ; Given array ; Given queries ; Number of queries
INF = 1000000 NEW_LINE pref = [ 0 for i in range ( 500005 ) ] NEW_LINE suf = [ 0 for i in range ( 500005 ) ] NEW_LINE MAX = [ 0 for i in range ( 500005 ) ] NEW_LINE def build ( a , tl , tr , v ) : NEW_LINE INDENT global MAX NEW_LINE global pref NEW_LINE global suf NEW_LINE if ( tl == tr ) : NEW_LINE INDENT MAX [ v ] = a [ tl ] NEW_LINE pref [ v ] = a [ tl ] NEW_LINE suf [ v ] = a [ tl ] NEW_LINE DEDENT else : NEW_LINE INDENT tm = ( tl + tr ) // 2 NEW_LINE build ( a , tl , tm , v * 2 ) NEW_LINE build ( a , tm + 1 , tr , v * 2 + 1 ) NEW_LINE MAX [ v ] = max ( MAX [ v * 2 ] , max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) NEW_LINE pref [ v ] = max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == tm - tl + 1 ) * pref [ v * 2 + 1 ] ) NEW_LINE suf [ v ] = max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == tr - tm ) ) NEW_LINE DEDENT DEDENT def update ( a , pos , tl , tr , v ) : NEW_LINE INDENT global pref NEW_LINE global suf NEW_LINE global MAX NEW_LINE if ( tl > pos or tr < pos ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( tl == tr and tl == pos ) : NEW_LINE INDENT MAX [ v ] = a [ pos ] NEW_LINE pref [ v ] = a [ pos ] NEW_LINE suf [ v ] = a [ pos ] NEW_LINE DEDENT else : NEW_LINE INDENT tm = ( tl + tr ) // 2 NEW_LINE update ( a , pos , tl , tm , v * 2 ) NEW_LINE update ( a , pos , tm + 1 , tr , v * 2 + 1 ) NEW_LINE MAX [ v ] = max ( MAX [ v * 2 ] , max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) NEW_LINE pref [ v ] = max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == tm - tl + 1 ) * pref [ v * 2 + 1 ] ) NEW_LINE suf [ v ] = max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + ( suf [ 2 * v + 1 ] == tr - tm ) * suf [ v * 2 ] ) NEW_LINE DEDENT DEDENT def solveQueries ( arr , n , Q , k ) : NEW_LINE INDENT global MAX NEW_LINE cntQuery = len ( Q ) NEW_LINE build ( arr , 0 , n - 1 , 1 ) NEW_LINE for i in range ( cntQuery ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT print ( MAX [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 NEW_LINE update ( arr , Q [ i ] [ 1 ] - 1 , 0 , n - 1 , 1 ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE arr = [ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ] NEW_LINE Q = [ [ 1 ] , [ 2 , 3 ] , [ 1 ] ] NEW_LINE K = 3 NEW_LINE solveQueries ( arr , N , Q , K ) NEW_LINE DEDENT
Find the array element having maximum frequency of the digit K | Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Print element having max frequency of digit k ; Driver Code ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
def countFreq ( N , K ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % 10 == K ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT N = N // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def findElementUtil ( arr , N , K ) : NEW_LINE INDENT c = 0 NEW_LINE max = 0 NEW_LINE ele = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT c = countFreq ( arr [ i ] , K ) NEW_LINE if ( c > max ) : NEW_LINE INDENT max = c NEW_LINE ele = arr [ i ] NEW_LINE DEDENT DEDENT if ( max == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ele NEW_LINE DEDENT DEDENT def findElement ( arr , N , K ) : NEW_LINE INDENT ele = findElementUtil ( arr , N , K ) NEW_LINE if ( ele == - 1 ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ele ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 3 NEW_LINE arr = [ 3 , 77 , 343 , 456 ] NEW_LINE N = len ( arr ) NEW_LINE findElement ( arr , K , N ) NEW_LINE DEDENT
Minimum removals required to make any interval equal to the union of the given Set | Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
def findMinDeletions ( v , n ) : NEW_LINE INDENT minDel = 10 ** 18 NEW_LINE for i in range ( n ) : NEW_LINE INDENT L = v [ i ] [ 0 ] NEW_LINE R = v [ i ] [ 1 ] NEW_LINE Count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( v [ j ] [ 1 ] >= L and v [ j ] [ 0 ] <= R ) : NEW_LINE INDENT Count += 1 NEW_LINE DEDENT DEDENT minDel = min ( minDel , n - Count ) NEW_LINE DEDENT return minDel NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT v = [ ] NEW_LINE v . append ( [ 1 , 3 ] ) NEW_LINE v . append ( [ 4 , 12 ] ) NEW_LINE v . append ( [ 5 , 8 ] ) NEW_LINE v . append ( [ 13 , 2 ] ) NEW_LINE N = len ( v ) NEW_LINE print ( findMinDeletions ( v , N ) ) NEW_LINE DEDENT
Maximum even numbers present in any subarray of size K | Function to find the maximum count of even numbers from all the subarrays of size M ; Stores the count of even numbers in a subarray of size M ; Calculate the count of even numbers in the current subarray ; If current element is an even number ; Stores the maximum count of even numbers from all the subarrays of size M ; Traverse remaining subarrays of size M using sliding window technique ; If the first element of the subarray is even ; update curr ; If i - th element is even increment the count ; update the answer ; Return answer ; Driver Code ; Size of the input array ; Function call
def maxEvenIntegers ( arr , N , M ) : NEW_LINE INDENT curr = 0 NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT curr += 1 NEW_LINE DEDENT DEDENT ans = curr NEW_LINE for i in range ( M , N ) : NEW_LINE INDENT if ( arr [ i - M ] % 2 == 0 ) : NEW_LINE INDENT curr -= 1 NEW_LINE DEDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT curr += 1 NEW_LINE ans = max ( curr , ans ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 4 , 7 , 6 ] NEW_LINE M = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( maxEvenIntegers ( arr , N , M ) ) NEW_LINE DEDENT
Count numbers up to N having digit D in its octal representation | Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Prthe answer ; Driver Code ; Given N and D ; Counts and prints numbers up to N having D as a digit in its octal representation
def countNumbers ( n , d ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT x = i NEW_LINE while ( x > 0 ) : NEW_LINE INDENT if ( x % 8 == d ) : NEW_LINE INDENT total += 1 NEW_LINE break NEW_LINE DEDENT x = x // 8 NEW_LINE DEDENT DEDENT print ( total ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , d = 20 , 7 NEW_LINE countNumbers ( n , d ) NEW_LINE DEDENT
Count number of triplets ( a , b , c ) from first N natural numbers such that a * b + c = N | Function to find the count of triplets ( a , b , c ) with a * b + c = N ; Stores count of triplets of 1 st N natural numbers which are of the form a * b + c = N ; Iterate over the range [ 1 , N ] ; If N is divisible by i ; Update cntTriplet ; Update cntTriplet ; Driver code
def findCntTriplet ( N ) : NEW_LINE INDENT cntTriplet = 0 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( N % i != 0 ) : NEW_LINE INDENT cntTriplet += N // i ; NEW_LINE DEDENT else : NEW_LINE INDENT cntTriplet += ( N // i ) - 1 ; NEW_LINE DEDENT DEDENT return cntTriplet ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; NEW_LINE print ( findCntTriplet ( N ) ) ; NEW_LINE DEDENT
Count divisors which generates same Quotient and Remainder | Python3 program of the above approach ; Function to calculate the count of numbers such that it gives same quotient and remainder ; Stores divisor of number N . ; Iterate through numbers from 2 to sqrt ( N ) and store divisors of N ; As N is also divisor of itself ; Iterate through divisors ; Checking whether x satisfies the required condition ; Print the count ; Driver Code ; Given N ; Function Call
from math import floor , sqrt NEW_LINE def countDivisors ( n ) : NEW_LINE INDENT count = 0 NEW_LINE divisor = [ ] NEW_LINE for i in range ( 2 , floor ( sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT divisor . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT divisor . append ( i ) NEW_LINE divisor . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT divisor . append ( n ) NEW_LINE for x in divisor : NEW_LINE INDENT x -= 1 NEW_LINE if ( ( n // x ) == ( n % x ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1000000000000 NEW_LINE countDivisors ( N ) NEW_LINE DEDENT
Smallest indexed array element required to be flipped to make sum of array equal to 0 | Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
def smallestIndexArrayElementsFlip ( arr , N ) : NEW_LINE INDENT pos = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE sum = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , - 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( smallestIndexArrayElementsFlip ( arr , N ) ) NEW_LINE DEDENT
Smallest indexed array element required to be flipped to make sum of array equal to 0 | Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Stores the sum of the array ; Traverse the given array ; Update ArrSum ; Traverse the given array ; If sum of array elements double the value of the current element ; Update pos ; Driver Code
def smallestIndexArrayElementsFlip ( arr , N ) : NEW_LINE INDENT pos = - 1 NEW_LINE ArrSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT ArrSum += arr [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( 2 * arr [ i ] == ArrSum ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT arr = [ 1 , 3 , - 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( smallestIndexArrayElementsFlip ( arr , N ) ) NEW_LINE
Check if a string can be converted to another given string by removal of a substring | Function to check if S can be converted to T by removing at most one substring from S ; Check if S can be converted to T by removing at most one substring from S ; Stores length of string T ; Stores length of string S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the substring S [ 0 ] , ... , S [ i ] ; Stores Length of the substring S [ 0 ] , ... , S [ i ] ; Stores prefix substring ; Stores suffix substring ; Checking if prefix + suffix == T ; Given String S and T ; Function call
def make_string_S_to_T ( S , T ) : NEW_LINE INDENT possible = False NEW_LINE M = len ( T ) NEW_LINE N = len ( S ) NEW_LINE for i in range ( 0 , M + 1 ) : NEW_LINE INDENT prefix_length = i NEW_LINE suffix_length = M - i NEW_LINE prefix = S [ : prefix_length ] NEW_LINE suffix = S [ N - suffix_length : N ] NEW_LINE if ( prefix + suffix == T ) : NEW_LINE INDENT possible = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( possible ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT S = " ababcdcd " NEW_LINE T = " abcd " NEW_LINE print ( make_string_S_to_T ( S , T ) ) NEW_LINE
Sum of maximum of all subarrays by adding even frequent maximum twice | Function to calculate sum of maximum of all subarrays ; Stores the sum of maximums ; Traverse the array ; Store the frequency of the maximum element in subarray ; Finding maximum ; Increment frequency by 1 ; If new maximum is obtained ; If frequency of maximum is even , then add 2 * maxNumber . Otherwise , add maxNumber ; Print the sum obtained ; Driver Code ; Function Call
def findSum ( a ) : NEW_LINE INDENT ans = 0 NEW_LINE for low in range ( 0 , len ( a ) ) : NEW_LINE INDENT for high in range ( low , len ( a ) ) : NEW_LINE count = 0 NEW_LINE maxNumber = 0 NEW_LINE for i in range ( low , high + 1 ) : NEW_LINE INDENT if ( a [ i ] == maxNumber ) : NEW_LINE count += 1 NEW_LINE elif ( a [ i ] > maxNumber ) : NEW_LINE maxNumber = a [ i ] NEW_LINE count = 1 NEW_LINE DEDENT if count % 2 : NEW_LINE INDENT ans += maxNumber NEW_LINE DEDENT else : NEW_LINE INDENT ans += maxNumber * 2 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 2 , 1 , 4 , 4 , 2 ] NEW_LINE findSum ( arr ) NEW_LINE
Count pairs of indices having equal prefix and suffix sums | Function to find the count of index pairs having equal prefix and suffix sums ; Maps indices with prefix sums ; Traverse the array ; Update prefix sum ; Update frequency in Map ; Traverse the array in reverse ; Update suffix sum ; Check if any prefix sum of equal value exists or not ; Print the obtained count ; Driver code ; Given array ; Given size ; Function Call
def countPairs ( arr , n ) : NEW_LINE INDENT mp1 = { } NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE mp1 [ sum ] = mp1 . get ( sum , 0 ) + 1 NEW_LINE DEDENT sum = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE if ( sum in mp1 ) : NEW_LINE INDENT ans += mp1 [ sum ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE countPairs ( arr , n ) NEW_LINE DEDENT
Queries to count Composite Magic Numbers from a given range [ L , R ] | Check if a number is magic number or not ; Check number is composite number or not ; Corner cases ; Check if the number is a multiple of 2 or 3 ; Check for multiples of remaining primes ; Function to find Composite Magic Numbers in given ranges ; dp [ i ] : Stores the count of composite Magic numbers up to i ; Traverse in the range [ 1 , 1e5 ) ; Check if number is Composite number as well as a Magic number or not ; Print results for each query ; Driver Code
def isMagic ( num ) : NEW_LINE INDENT return ( num % 9 == 1 ) NEW_LINE DEDENT def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 5 , n + 1 , 6 ) : NEW_LINE INDENT if i * i > n + 1 : NEW_LINE INDENT break NEW_LINE DEDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def find ( L , R , q ) : NEW_LINE INDENT dp = [ 0 ] * 1000005 NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 0 NEW_LINE for i in range ( 1 , 1000005 ) : NEW_LINE INDENT if ( isComposite ( i ) and isMagic ( i ) ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT print ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = [ 10 , 3 ] NEW_LINE R = [ 100 , 2279 ] NEW_LINE Q = 2 NEW_LINE find ( L , R , Q ) NEW_LINE DEDENT
Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code
def findSmallestInteger ( arr , N , K ) : NEW_LINE INDENT left = 1 NEW_LINE right = max ( arr ) NEW_LINE while ( left < right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += ( arr [ i ] + mid - 1 ) // mid NEW_LINE DEDENT if ( sum > K ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid NEW_LINE DEDENT DEDENT return left NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE K = 6 NEW_LINE print ( findSmallestInteger ( arr , N , K ) ) NEW_LINE DEDENT
Minimum Deci | Function to find the count of minimum Deci - Binary numbers required to obtain S ; Stores the minimum count ; Iterate over the string s ; Convert the char to its equivalent integer ; If current character is the maximum so far ; Update the maximum digit ; Prthe required result ; Driver Code
def minimum_deci_binary_number ( s ) : NEW_LINE INDENT m = - 10 ** 19 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT temp = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( temp > m ) : NEW_LINE INDENT m = temp NEW_LINE DEDENT DEDENT return m NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "31" NEW_LINE print ( minimum_deci_binary_number ( S ) ) NEW_LINE DEDENT