codeparrot/xlcost-text-to-code huggingface dataset:
Make all the elements of array odd by incrementing odd | Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of arr [ ] ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Print the number of operations ; Driver Code ; Function Call
def minOperations ( arr , n ) : NEW_LINE INDENT flips = 0 ; NEW_LINE i = 0 ; NEW_LINE while i < n : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT i += 2 ; NEW_LINE continue ; NEW_LINE DEDENT while ( i < n and arr [ i ] % 2 == 0 ) : NEW_LINE INDENT i += 2 ; NEW_LINE DEDENT flips += 1 ; NEW_LINE i += 2 ; NEW_LINE DEDENT i = 1 NEW_LINE while i < n : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT i += 2 ; NEW_LINE continue ; NEW_LINE DEDENT while ( i < n and arr [ i ] % 2 == 0 ) : NEW_LINE INDENT i += 2 ; NEW_LINE DEDENT flips += 1 ; NEW_LINE i += 2 ; NEW_LINE DEDENT print ( flips ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 3 , 5 , 3 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE minOperations ( arr , N ) ; NEW_LINE DEDENT
Minimum substring reversals required to make given Binary String alternating | Function to count the minimum number of substrings required to be reversed to make the string S alternating ; Store count of consecutive pairs ; Stores the count of 1 s and 0 s ; Traverse through the string ; Increment 1 s count ; Increment 0 s count ; Increment K if consecutive same elements are found ; Increment 1 s count ; else : Increment 0 s count ; Check if it is possible or not ; Otherwise , print the number of required operations ; Driver code ; Function Call
def minimumReverse ( s , n ) : NEW_LINE INDENT k = 0 ; NEW_LINE l = 0 ; NEW_LINE sum1 = 0 ; NEW_LINE sum0 = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT sum1 += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT sum0 += 1 ; NEW_LINE DEDENT if ( s [ i ] == s [ i - 1 ] and s [ i ] == '0' ) : NEW_LINE INDENT k += 1 ; NEW_LINE DEDENT elif ( s [ i ] == s [ i - 1 ] and s [ i ] == '1' ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT DEDENT if ( s [ 0 ] == '1' ) : NEW_LINE INDENT sum1 += 1 ; NEW_LINE sum0 += 1 ; NEW_LINE DEDENT if ( abs ( sum1 - sum0 ) > 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return max ( k , l ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "10001" ; NEW_LINE N = len ( S ) ; NEW_LINE print ( minimumReverse ( S , N ) ) ; NEW_LINE DEDENT
Count pairs with Even Product from two given arrays | Function to count pairs ( arr [ i ] , brr [ j ] ) whose product is an even number ; Stores count of odd numbers in arr [ ] ; Stores count of odd numbers in brr [ ] ; Traverse the array , arr [ ] ; If arr [ i ] is an odd number ; Update cntOddArr ; Traverse the array , brr [ ] ; If brr [ i ] is an odd number ; Update cntOddArr ; Return pairs whose product is an even number ; Driver Code
def cntPairsInTwoArray ( arr , brr , N , M ) : NEW_LINE INDENT cntOddArr = 0 NEW_LINE cntOddBrr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT cntOddArr += 1 NEW_LINE DEDENT DEDENT for i in range ( M ) : NEW_LINE INDENT if ( brr [ i ] & 1 ) : NEW_LINE INDENT cntOddBrr += 1 NEW_LINE DEDENT DEDENT return ( N * M ) - ( cntOddArr * cntOddBrr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE brr = [ 1 , 2 ] NEW_LINE M = len ( brr ) NEW_LINE print ( cntPairsInTwoArray ( arr , brr , N , M ) ) NEW_LINE DEDENT
Maximize count of persons receiving a chocolate | Python3 program for the above approach ; Function to count the maximum number of persons receiving a chocolate ; Initialize count as 0 ; Sort the given arrays ; Initialize a multiset ; Insert B [ ] array values into the multiset ; Traverse elements in array A [ ] ; Search for the lowest value in B [ ] ; If found , increment count , and delete from set ; Return the number of people ; Driver Code ; Function Call
from bisect import bisect_left NEW_LINE def countMaxPersons ( A , n , B , m , x ) : NEW_LINE INDENT count = 0 NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE s = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT s . append ( B [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT val = A [ i ] - x NEW_LINE it = bisect_left ( s , val ) NEW_LINE if ( it != len ( s ) and it <= A [ i ] + x ) : NEW_LINE INDENT count += 1 NEW_LINE del s [ it ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 90 , 49 , 20 , 39 , 49 ] NEW_LINE B = [ 14 , 24 , 82 ] NEW_LINE X = 15 NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE print ( countMaxPersons ( A , N , B , M , X ) ) NEW_LINE DEDENT
Maximize frequency of an element by at most one increment or decrement of all array elements | Function to maximize the frequency of an array element by incrementing or decrementing array elements at most once ; Stores the largest array element ; Stores the smallest array element ; freq [ i ] : Stores frequency of ( i + Min ) in the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores maximum frequency of an array element by incrementing or decrementing array elements ; Iterate all the numbers over the range [ Min , Max ] ; Stores sum of three consecutive numbers ; Update maxSum ; Print maxSum ; Driver Code ; Function call
def max_freq ( arr , N ) : NEW_LINE INDENT Max = max ( arr ) NEW_LINE Min = min ( arr ) NEW_LINE freq = [ 0 ] * ( Max - Min + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] - Min ] += 1 NEW_LINE DEDENT maxSum = 0 NEW_LINE for i in range ( Max - Min - 1 ) : NEW_LINE INDENT val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] NEW_LINE maxSum = max ( maxSum , val ) NEW_LINE DEDENT print ( maxSum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE max_freq ( arr , N ) NEW_LINE DEDENT
Longest increasing subsequence consisting of elements from indices divisible by previously selected indices | Function to find length of longest subsequence generated that satisfies the specified conditions ; Stores the length of longest subsequences of all lengths ; Iterate through the given array ; Iterate through the multiples i ; Update dp [ j ] as maximum of dp [ j ] and dp [ i ] + 1 ; Return the maximum element in dp [ ] as the length of longest subsequence ; Driver Code ; Function Call
def findMaxLength ( N , arr ) : NEW_LINE INDENT dp = [ 1 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , N + 1 , i ) : NEW_LINE INDENT if ( arr [ i - 1 ] < arr [ j - 1 ] ) : NEW_LINE INDENT dp [ j ] = max ( dp [ j ] , dp [ i ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return max ( dp ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaxLength ( N , arr ) ) NEW_LINE DEDENT
Queries to check if any pair exists in an array having values at most equal to the given pair | Function that performs binary search to find value less than or equal to first value of the pair ; Perform binary search ; Find the mid ; Update the high ; Else update low ; Return the low index ; Function to modify the second value of each pair ; start from index 1 ; Function to evaluate each query ; Find value less than or equal to the first value of pair ; check if we got the required pair or not ; Function to find a pair whose values is less than the given pairs in query ; Find the size of the vector ; sort the vector based on the first value ; Function Call to modify the second value of each pair ; Traverse each queries ; Evaluate each query ; Print the result ; Driver Code ; Function Call
def binary_search ( vec , n , a ) : NEW_LINE INDENT low , high , mid = 0 , 0 , 0 NEW_LINE low = 0 NEW_LINE high = n - 1 NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = low + ( high - low + 1 ) // 2 NEW_LINE if ( vec [ mid ] [ 0 ] > a ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT elif vec [ mid ] [ 0 ] <= a : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT def modify_vec ( v , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT v [ i ] [ 1 ] = min ( v [ i ] [ 1 ] , v [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT return v NEW_LINE DEDENT def evaluate_query ( v , n , m1 , m2 ) : NEW_LINE INDENT temp = binary_search ( v , n , m1 ) NEW_LINE if ( ( v [ temp ] [ 0 ] <= m1 ) and ( v [ temp ] [ 1 ] <= m2 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def checkPairs ( v , queries ) : NEW_LINE INDENT n = len ( v ) NEW_LINE v = sorted ( v ) NEW_LINE v = modify_vec ( v , n ) NEW_LINE k = len ( queries ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT m1 = queries [ i ] [ 0 ] NEW_LINE m2 = queries [ i ] [ 1 ] NEW_LINE result = evaluate_query ( v , n , m1 , m2 ) NEW_LINE if ( result > 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 3 , 5 ] , [ 2 , 7 ] , [ 2 , 3 ] , [ 4 , 9 ] ] NEW_LINE queries = [ [ 3 , 4 ] , [ 3 , 2 ] , [ 4 , 1 ] , [ 3 , 7 ] ] NEW_LINE checkPairs ( arr , queries ) NEW_LINE DEDENT
Sort a stream of integers | Function to sort a stream of integers ; Stores the position of array elements ; Traverse through the array ; If any element is found to be greater than the current element ; If an element > num is not present ; Otherwise , place the number at its right position ; Function to print the sorted stream of integers ; Stores the sorted stream of integers ; Traverse the array ; Function Call ; Print the array after every insertion ; Driver Code
def Sort ( ans , num ) : NEW_LINE INDENT pos = - 1 ; NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT if ( ans [ i ] >= num ) : NEW_LINE INDENT pos = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( pos == - 1 ) : NEW_LINE INDENT ans . append ( num ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ans . insert ( pos , num ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def sortStream ( arr , N ) : NEW_LINE INDENT ans = list ( ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans = Sort ( ans , arr [ i ] ) ; NEW_LINE for j in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 7 , 6 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE sortStream ( arr , N ) ; NEW_LINE DEDENT
Count ways to split array into two equal sum subarrays by changing sign of any one array element | Function to count ways of spliting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call
def countSubArraySignChange ( arr , N ) : NEW_LINE INDENT prefixCount = { } NEW_LINE suffixCount = { } NEW_LINE total = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE suffixCount [ arr [ i ] ] = suffixCount . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT prefixSum = 0 NEW_LINE suffixSum = 0 NEW_LINE count = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT prefixSum += arr [ i ] NEW_LINE prefixCount [ arr [ i ] ] = prefixCount . get ( arr [ i ] , 0 ) + 1 NEW_LINE suffixSum = total - prefixSum NEW_LINE suffixCount [ arr [ i ] ] -= 1 NEW_LINE diff = prefixSum - suffixSum NEW_LINE if ( diff % 2 == 0 ) : NEW_LINE INDENT y , z = 0 , 0 NEW_LINE if - diff // 2 in suffixCount : NEW_LINE INDENT y = suffixCount [ - dff // 2 ] NEW_LINE DEDENT if diff // 2 in prefixCount : NEW_LINE INDENT z = prefixCount NEW_LINE DEDENT x = z + y NEW_LINE count = count + x NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , - 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countSubArraySignChange ( arr , N ) ) NEW_LINE DEDENT
Partition string into two substrings having maximum number of common non | Function to count maximum common non - repeating characters that can be obtained by partitioning the string into two non - empty substrings ; Stores count of non - repeating characters present in both the substrings ; Stores distinct characters in left substring ; Stores distinct characters in right substring ; Traverse left substring ; Insert S [ i ] into ls ; Traverse right substring ; Insert S [ i ] into rs ; Traverse distinct characters of left substring ; If current character is present in right substring ; Update cnt ; Return count ; Function to partition the string into two non - empty substrings in all possible ways ; Stores maximum common distinct characters present in both the substring partitions ; Traverse the string ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code
def countCommonChar ( ind , S ) : NEW_LINE INDENT cnt = 0 NEW_LINE ls = set ( ) NEW_LINE rs = set ( ) NEW_LINE for i in range ( ind ) : NEW_LINE INDENT ls . add ( S [ i ] ) NEW_LINE DEDENT for i in range ( ind , len ( S ) ) : NEW_LINE INDENT rs . add ( S [ i ] ) NEW_LINE DEDENT for v in ls : NEW_LINE INDENT if v in rs : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT def partitionStringWithMaxCom ( S ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT ans = max ( ans , countCommonChar ( i , S ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " aabbca " NEW_LINE partitionStringWithMaxCom ( string ) NEW_LINE DEDENT
Longest Substring of 1 's after removing one character | Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Add '0' at the end ; Iterator to traverse the string ; Stores maximum length of required substring ; Stores length of substring of '1' preceding the current character ; Stores length of substring of '1' succeeding the current character ; Counts number of '0' s ; Traverse the string S ; If current character is '1 ; Increase curr_one by one ; Otherwise ; Increment numberofZeros by one ; Count length of substring obtained y concatenating preceding and succeeding substrings of '1 ; Store maximum size in res ; Assign curr_one to prev_one ; Reset curr_one ; If string contains only one '0 ; Return the answer ; Driver Code
def longestSubarray ( s ) : NEW_LINE INDENT s += '0' NEW_LINE i = 0 NEW_LINE res = 0 NEW_LINE prev_one = 0 NEW_LINE curr_one = 0 NEW_LINE numberOfZeros = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT curr_one += 1 NEW_LINE DEDENT else : NEW_LINE INDENT numberOfZeros += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT prev_one += curr_one NEW_LINE res = max ( res , prev_one ) NEW_LINE prev_one = curr_one NEW_LINE curr_one = 0 NEW_LINE DEDENT ' NEW_LINE INDENT if ( numberOfZeros == 1 ) : NEW_LINE INDENT res -= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "1101" NEW_LINE print ( longestSubarray ( S ) ) NEW_LINE DEDENT
Lexicographically smallest string possible by inserting given character | Function to obtain lexicographically smallest string possible by inserting character c in the string s ; Traverse the string ; Check if current character is greater than the given character ; Insert the character before the first greater character ; Return the string ; Append the character at the end ; Return the string ; Function call
def SmallestString ( s , c ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < len ( s ) ) : NEW_LINE INDENT if s [ i ] > c : NEW_LINE INDENT s = s [ : i ] + c + s [ i : ] NEW_LINE return s NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT s = s + c NEW_LINE return s NEW_LINE DEDENT S = ' abd ' NEW_LINE C = ' c ' NEW_LINE print ( SmallestString ( S , C ) ) NEW_LINE
Largest Non | Python program to implement the above approach ; Function to find the largest unique element of the array ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest non - repeating array element ; Traverse the array ; If frequency of arr [ i ] is equal to 1 and arr [ i ] exceeds LNRElem ; Update ind ; Update LNRElem ; If no array element is found with frequency equal to 1 ; Prlargest non - repeating element ; Driver Code
import sys NEW_LINE def LarUnEl ( arr , N ) : NEW_LINE INDENT map = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT map [ arr [ i ] ] += 1 ; NEW_LINE DEDENT LNRElem = - sys . maxsize ; NEW_LINE ind = - 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( map . get ( arr [ i ] ) == 1 and arr [ i ] > LNRElem ) : NEW_LINE INDENT ind = i ; NEW_LINE LNRElem = arr [ i ] ; NEW_LINE DEDENT DEDENT if ( ind == - 1 ) : NEW_LINE INDENT print ( ind ) ; NEW_LINE return ; NEW_LINE DEDENT print ( arr [ ind ] ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 8 , 8 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE LarUnEl ( arr , N ) ; NEW_LINE DEDENT
Minimum shifts of substrings of 1 s required to group all 1 s together in a given Binary string | Function to count indices substrings of 1 s need to be shifted such that all 1 s in the string are grouped together ; Stores first occurrence of '1 ; Stores last occurrence of '1 ; Count of 0 s between firstOne and lastOne ; Traverse the string to find the first and last occurrences of '1 ; Count number of 0 s present between firstOne and lastOne ; Print minimum operations ; Given string
def countShifts ( str ) : NEW_LINE ' NEW_LINE INDENT firstOne = - 1 NEW_LINE DEDENT ' NEW_LINE INDENT lastOne = - 1 NEW_LINE count = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT if ( firstOne == - 1 ) : NEW_LINE INDENT firstOne = i NEW_LINE DEDENT lastOne = i NEW_LINE DEDENT DEDENT if ( ( firstOne == - 1 ) or ( firstOne == lastOne ) ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT for i in range ( firstOne , lastOne + 1 , 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT str = "00110111011" NEW_LINE countShifts ( str ) NEW_LINE
Check if a non | Utility Function to check whether a subsequence same as the given subarray exists or not ; Check if first element of the subarray is also present before ; Check if last element of the subarray is also present later ; If above two conditions are not satisfied , then no such subsequence exists ; Function to check and prif a subsequence which is same as the given subarray is present or not ; Driver Code ; Function Call
def checkSubsequenceUtil ( arr , L , R , N ) : NEW_LINE INDENT for i in range ( L ) : NEW_LINE INDENT if ( arr [ i ] == arr [ L ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT for i in range ( R + 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ R ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def checkSubsequence ( arr , L , R , N ) : NEW_LINE INDENT if ( checkSubsequenceUtil ( arr , L , R , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 7 , 12 , 1 , 7 , 5 , 10 , 11 , 42 ] NEW_LINE N = len ( arr ) NEW_LINE L = 3 NEW_LINE R = 6 NEW_LINE checkSubsequence ( arr , L , R , N ) NEW_LINE
Longest subarray in which all elements are smaller than K | Function to find the length of the longest subarray with all elements smaller than K ; Stores the length of maximum consecutive sequence ; Stores maximum length of subarray ; Iterate through the array ; Check if array element smaller than K ; Store the maximum of length and count ; Reset the counter ; Print the maximum length ; Driver Code ; Given array arr [ ] ; Size of the array ; Given K ; Function Call
def largestSubarray ( arr , N , K ) : NEW_LINE INDENT count = 0 NEW_LINE length = 0 NEW_LINE for i in range ( N ) : NEW_LINE if ( arr [ i ] < K ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT length = max ( length , count ) NEW_LINE count = 0 NEW_LINE DEDENT if ( count ) : NEW_LINE INDENT length = max ( length , count ) NEW_LINE DEDENT print ( length , end = " " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 8 , 3 , 5 , 2 , 2 , 1 , 13 ] NEW_LINE N = len ( arr ) NEW_LINE K = 6 NEW_LINE largestSubarray ( arr , N , K ) NEW_LINE DEDENT
Smallest pair of indices with product of subarray co | Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then pr - 1 ; Driver Code ; Function Call
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def findPair ( A , N ) : NEW_LINE INDENT right_prod = [ 0 ] * N NEW_LINE flag = 0 NEW_LINE right_prod [ N - 1 ] = A [ N - 1 ] NEW_LINE for i in range ( N - 2 , 0 , - 1 ) : NEW_LINE INDENT right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] NEW_LINE DEDENT total_prod = right_prod [ 0 ] NEW_LINE product = 1 NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , N - 1 ) : NEW_LINE INDENT product *= A [ j ] NEW_LINE if ( gcd ( product , right_prod [ j + 1 ] ) == 1 or gcd ( product , total_prod / right_prod [ i ] ) == 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE print ( " ( " , ( i - 1 ) , " , ▁ " , ( j + 1 ) , " ) " ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findPair ( arr , N ) NEW_LINE DEDENT
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' | Function to count the strings consisting of at least 1 set bit ; Initialize count ; Iterate through string ; The answer is 2 ^ N - 1 ; Driver Code ; Given string ; Function call
def countString ( S ) : NEW_LINE INDENT count = 0 NEW_LINE for i in S : NEW_LINE INDENT if ( i == '0' and count > 0 ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( ( 1 << count ) - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1001" NEW_LINE countString ( S ) NEW_LINE DEDENT