codeparrot/xlcost-text-to-code huggingface dataset:
Determine the winner of a game of deleting Characters from a String | Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
def determineWinner ( str ) : NEW_LINE INDENT A = [ 0 for i in range ( 9 ) ] ; NEW_LINE sum1 = 0 ; sum2 = 0 ; NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT A [ int ( str [ i ] ) ] += 1 ; NEW_LINE DEDENT for i in range ( 0 , 9 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT sum1 = sum1 + A [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT sum2 = sum2 + A [ i ] ; NEW_LINE DEDENT DEDENT if ( sum1 == sum2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT elif ( sum1 > sum2 ) : NEW_LINE INDENT print ( " Player ▁ 1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Player ▁ 2" ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "78787" ; NEW_LINE determineWinner ( str ) ; NEW_LINE DEDENT
Generate a String from given Strings P and Q based on the given conditions | Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in freq [ ] to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
def manipulateStrings ( P , Q ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] ; NEW_LINE for i in range ( len ( P ) ) : NEW_LINE INDENT freq [ ord ( P [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( Q ) ) : NEW_LINE INDENT freq [ ord ( Q [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE DEDENT sb = " " NEW_LINE pos = ord ( Q [ 0 ] ) - ord ( ' a ' ) NEW_LINE for i in range ( pos + 1 ) : NEW_LINE INDENT while freq [ i ] > 0 : NEW_LINE INDENT sb += chr ( ord ( ' a ' ) + i ) NEW_LINE freq [ i ] -= 1 NEW_LINE DEDENT DEDENT sb += Q NEW_LINE for i in range ( pos + 1 , 26 ) : NEW_LINE INDENT while freq [ i ] > 0 : NEW_LINE INDENT sb += chr ( ord ( ' a ' ) + i ) NEW_LINE freq [ i ] -= 1 NEW_LINE DEDENT DEDENT print ( sb ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = " geeksforgeeksfor " ; NEW_LINE Q = " for " ; NEW_LINE manipulateStrings ( P , Q ) ; NEW_LINE DEDENT
Minimum moves required to type a word in QWERTY based keyboard | Function that calculates the moves required to prthe current String ; row1 has qwertyuiop , row2 has asdfghjkl , and row3 has zxcvbnm Store the row number of each character ; String length ; Initialise move to 1 ; Traverse the String ; If current row number is not equal to previous row then increment the moves ; Return the moves ; Driver Code ; Given String str ; Function Call
def numberMoves ( s ) : NEW_LINE INDENT row = [ 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 ] ; NEW_LINE n = len ( s ) ; NEW_LINE move = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE if ( row [ ord ( s [ i ] ) - ord ( ' a ' ) ] != row [ ord ( s [ i - 1 ] ) - ord ( ' a ' ) ] ) : NEW_LINE move += 1 ; NEW_LINE return move ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE print ( numberMoves ( str ) ) ; NEW_LINE DEDENT
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | Python3 program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
from collections import defaultdict NEW_LINE mod = 1000000007 NEW_LINE def countS ( string : list , n : int , m : int ) -> int : NEW_LINE INDENT counts = defaultdict ( lambda : set ( ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = string [ i ] NEW_LINE for j in range ( m ) : NEW_LINE INDENT counts [ j ] . add ( s [ j ] ) NEW_LINE DEDENT DEDENT result = 1 NEW_LINE for index in counts : NEW_LINE INDENT result = ( result * len ( counts [ index ] ) ) % mod NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = [ "112" , "211" ] NEW_LINE N = 2 NEW_LINE M = 3 NEW_LINE print ( countS ( string , N , M ) ) NEW_LINE DEDENT
Longest Subarrays having each Array element as the maximum | Function to find the maximum length of Subarrays for each element of the array having it as the maximum ; Initialise the bounds ; Iterate to find greater element on the left ; If greater element is found ; Decrement left pointer ; If boundary is exceeded ; Iterate to find greater element on the right ; If greater element is found ; Increment right pointer ; if boundary is exceeded ; Length of longest subarray where arr [ i ] is the largest ; Print the answer ; Driver code
def solve ( n , arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left = max ( i - 1 , 0 ) NEW_LINE right = min ( n - 1 , i + 1 ) NEW_LINE while left >= 0 : NEW_LINE INDENT if arr [ left ] > arr [ i ] : NEW_LINE INDENT left += 1 NEW_LINE break NEW_LINE DEDENT left -= 1 NEW_LINE DEDENT if left < 0 : NEW_LINE INDENT left += 1 NEW_LINE DEDENT while right < n : NEW_LINE INDENT if arr [ right ] > arr [ i ] : NEW_LINE INDENT right -= 1 NEW_LINE break NEW_LINE DEDENT right += 1 NEW_LINE DEDENT if right >= n : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT ans = 1 + right - left NEW_LINE print ( ans , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 4 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE solve ( n , arr ) NEW_LINE
Count of Substrings having Sum equal to their Length | Python3 program to implement the above approach ; Function to count the number of substrings with sum equal to length ; Stores the count of substrings ; Add character to sum ; Add count of substrings to result ; Increase count of subarrays ; Return count ; Driver code
from collections import defaultdict NEW_LINE def countSubstrings ( s , n ) : NEW_LINE INDENT count , sum = 0 , 0 NEW_LINE mp = defaultdict ( lambda : 0 ) NEW_LINE mp [ 0 ] += 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ord ( s [ i ] ) - ord ( '0' ) NEW_LINE count += mp [ sum - ( i + 1 ) ] NEW_LINE mp [ sum - ( i + 1 ) ] += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT str = '112112' NEW_LINE n = len ( str ) NEW_LINE print ( countSubstrings ( str , n ) ) NEW_LINE
Count of ways to split an Array into three contiguous Subarrays having increasing Sum | Function to count the number of ways to split array into three contiguous subarrays of the required type ; Stores the prefix sums ; Stores the suffix sums ; Traverse the given array ; Updating curr_subarray_sum until it is less than prefix_sum [ s - 1 ] ; Increase count ; Decrease curr_subarray_sum by arr [ s [ ] ; Return count ; Driver code
def findCount ( arr , n ) : NEW_LINE INDENT prefix_sum = [ 0 for x in range ( n ) ] NEW_LINE prefix_sum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT suffix_sum = [ 0 for x in range ( n ) ] NEW_LINE suffix_sum [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT s = 1 NEW_LINE e = 1 NEW_LINE curr_subarray_sum = 0 NEW_LINE count = 0 NEW_LINE while ( s < n - 1 and e < n - 1 ) : NEW_LINE INDENT while ( e < n - 1 and curr_subarray_sum < prefix_sum [ s - 1 ] ) : NEW_LINE INDENT curr_subarray_sum += arr [ e ] NEW_LINE e += 1 NEW_LINE DEDENT if ( curr_subarray_sum <= suffix_sum [ e ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT curr_subarray_sum -= arr [ s ] NEW_LINE s += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 2 , 3 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findCount ( arr , n ) ) NEW_LINE
Minimum number of operations required to maximize the Binary String | Function to find the number of operations required ; Count of 1 's ; Count of 0 's upto (cnt1)-th index ; Return the answer ; Driver Code
def minOperation ( s , n ) : NEW_LINE INDENT cnt1 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( s [ i ] ) == ord ( '1' ) ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE DEDENT DEDENT cnt0 = 0 ; NEW_LINE for i in range ( 0 , cnt1 ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE DEDENT DEDENT return cnt0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE s = "01001011" ; NEW_LINE ans = minOperation ( s , n ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Minimum number of operations required to maximize the Binary String | Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver code
def minOperation ( s , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE i = 0 ; j = n - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( s [ i ] == '0' and s [ j ] == '1' ) : NEW_LINE INDENT ans += 1 ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE continue ; NEW_LINE DEDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( s [ j ] == '0' ) : NEW_LINE INDENT j -= 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE s = "10100101" ; NEW_LINE print ( minOperation ( s , n ) ) ; NEW_LINE DEDENT
Count of N digit Numbers having no pair of equal consecutive Digits | Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp table ; Calculate the count of required N - digit numbers ; Driver Code
def count ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( 10 ) ; NEW_LINE return ; NEW_LINE DEDENT dp = [ [ 0 for i in range ( 10 ) ] for j in range ( N ) ] NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( 10 ) : NEW_LINE INDENT temp += dp [ i - 1 ] [ j ] ; NEW_LINE DEDENT for j in range ( 10 ) : NEW_LINE INDENT dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT ans += dp [ N - 1 ] [ i ] ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE count ( N ) ; NEW_LINE DEDENT
Count of N digit Numbers having no pair of equal consecutive Digits | Iterative Function to calculate ( x ^ y ) % mod in O ( log y ) ; Initialize result ; Update x if x >= mod ; If x is divisible by mod ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Driver Code
def power ( x , y , mod ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % mod ; NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT res = ( res * x ) % mod ; NEW_LINE DEDENT y = y >> 1 ; NEW_LINE x = ( x * x ) % mod ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def count ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( 10 ) ; NEW_LINE return ; NEW_LINE DEDENT print ( power ( 9 , N , 1000000007 ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; NEW_LINE count ( N ) ; NEW_LINE DEDENT
Smallest String consisting of a String S exactly K times as a Substring | KMP algorithm ; Function to return the required string ; Finding the longest proper prefix which is also suffix ; ans string ; Update ans appending the substring K - 1 times ; Append the original string ; Returning min length string which contain exactly k substring of given string ; Driver code
def kmp ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lps = [ None ] * n NEW_LINE lps [ 0 ] = 0 NEW_LINE i , Len = 1 , 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( s [ i ] == s [ Len ] ) : NEW_LINE INDENT Len += 1 NEW_LINE lps [ i ] = Len NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( Len != 0 ) : NEW_LINE INDENT Len = lps [ Len - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lps [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT DEDENT return lps NEW_LINE DEDENT def findString ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lps = kmp ( s ) NEW_LINE ans = " " NEW_LINE suff = s [ 0 : n - lps [ n - 1 ] : 1 ] NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT ans += suff NEW_LINE DEDENT ans += s NEW_LINE return ans NEW_LINE DEDENT k = 3 NEW_LINE s = " geeksforgeeks " NEW_LINE print ( findString ( s , k ) ) NEW_LINE
Check if an Array can be Sorted by picking only the corner Array elements | Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
def check ( arr , n ) : NEW_LINE INDENT g = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] > 0 and g == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( arr [ i ] - arr [ i ] < 0 ) : NEW_LINE INDENT g = 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 10 , 4 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Minimum Sum of a pair at least K distance apart from an Array | Python3 program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code
import sys NEW_LINE def findMinSum ( A , K ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE suffix_min = [ 0 ] * n ; NEW_LINE suffix_min [ n - 1 ] = A [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix_min [ i ] = min ( suffix_min [ i + 1 ] , A [ i ] ) ; NEW_LINE DEDENT min_sum = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i + K < n ) : NEW_LINE INDENT min_sum = min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; NEW_LINE DEDENT DEDENT print ( min_sum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE K = 2 ; NEW_LINE findMinSum ( A , K ) ; NEW_LINE DEDENT
Maximum number of envelopes that can be put inside other bigger envelopes | Function that returns the maximum number of envelopes that can be inserted into another envelopes ; Number of envelopes ; Sort the envelopes in non - decreasing order ; Initialize dp [ ] array ; To store the result ; Loop through the array ; Find envelopes count for each envelope ; Store maximum envelopes count ; Return the result ; Driver Code ; Given the envelopes ; Function Call
def maxEnvelopes ( envelopes ) : NEW_LINE INDENT N = len ( envelopes ) NEW_LINE if ( N == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT envelopes = sorted ( envelopes ) NEW_LINE dp = [ 0 ] * N NEW_LINE max_envelope = 1 NEW_LINE dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( envelopes [ i ] [ 0 ] > envelopes [ j ] [ 0 ] and envelopes [ i ] [ 1 ] > envelopes [ j ] [ 1 ] and dp [ i ] < dp [ j ] + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ j ] + 1 NEW_LINE DEDENT DEDENT max_envelope = max ( max_envelope , dp [ i ] ) NEW_LINE DEDENT return max_envelope NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT envelopes = [ [ 4 , 3 ] , [ 5 , 3 ] , [ 5 , 6 ] , [ 1 , 2 ] ] NEW_LINE print ( maxEnvelopes ( envelopes ) ) NEW_LINE DEDENT
Maximize difference between the Sum of the two halves of the Array after removal of N elements | Function to print the maximum difference possible between the two halves of the array ; Stores n maximum values from the start ; Insert first n elements ; Update sum of largest n elements from left ; For the remaining elements ; Obtain minimum value in the set ; Insert only if it is greater than minimum value ; Update sum from left ; Remove the minimum ; Insert the current element ; Clear the set ; Store n minimum elements from the end ; Insert the last n elements ; Update sum of smallest n elements from right ; For the remaining elements ; Obtain the minimum ; Insert only if it is smaller than maximum value ; Update sum from right ; Remove the minimum ; Insert the new element ; Compare the difference and store the maximum ; Return the maximum possible difference ; Driver code
def FindMaxDif ( a , m ) : NEW_LINE INDENT n = m // 3 NEW_LINE l = [ 0 ] * ( m + 5 ) NEW_LINE r = [ 0 ] * ( m + 5 ) NEW_LINE s = [ ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( i <= n ) : NEW_LINE INDENT l [ i ] = a [ i - 1 ] + l [ i - 1 ] NEW_LINE s . append ( a [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT l [ i ] = l [ i - 1 ] NEW_LINE s . sort ( ) NEW_LINE d = s [ 0 ] NEW_LINE if ( a [ i - 1 ] > d ) : NEW_LINE INDENT l [ i ] -= d NEW_LINE l [ i ] += a [ i - 1 ] NEW_LINE s . remove ( d ) NEW_LINE s . append ( a [ i - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT s . clear ( ) NEW_LINE for i in range ( m , 0 , - 1 ) : NEW_LINE INDENT if ( i >= m - n + 1 ) : NEW_LINE INDENT r [ i ] = a [ i - 1 ] + r [ i + 1 ] NEW_LINE s . append ( a [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT r [ i ] = r [ i + 1 ] NEW_LINE s . sort ( ) NEW_LINE d = s [ - 1 ] NEW_LINE if ( a [ i - 1 ] < d ) : NEW_LINE INDENT r [ i ] -= d NEW_LINE r [ i ] += a [ i - 1 ] NEW_LINE s . remove ( d ) NEW_LINE s . append ( a [ i - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT ans = - 9e18 NEW_LINE for i in range ( n , m - n + 1 ) : NEW_LINE INDENT ans = max ( ans , l [ i ] - r [ i + 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT vtr = [ 3 , 1 , 4 , 1 , 5 , 9 ] NEW_LINE n = len ( vtr ) NEW_LINE print ( FindMaxDif ( vtr , n ) ) NEW_LINE
Check if each element of an Array is the Sum of any two elements of another Array | Function to check if each element of B [ ] can be formed by adding two elements of array A [ ] ; Store each element of B [ ] ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code
def checkPossible ( A , B , n ) : NEW_LINE INDENT values = set ( [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT values . add ( B [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( A [ i ] + A [ j ] ) in values ) : NEW_LINE INDENT values . remove ( A [ i ] + A [ j ] ) NEW_LINE if ( len ( values ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( len ( values ) == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE A = [ 3 , 5 , 1 , 4 , 2 ] NEW_LINE B = [ 3 , 4 , 5 , 6 , 7 ] NEW_LINE print ( checkPossible ( A , B , N ) ) NEW_LINE DEDENT
Count of Array elements greater than or equal to twice the Median of K trailing Array elements | Python3 program to implement the above approach ; Function to find the count of array elements >= twice the median of K trailing array elements ; Stores frequencies ; Stores the array elements ; Count the frequencies of the array elements ; Iterating from d to n - 1 index means ( d + 1 ) th element to nth element ; To check the median ; Iterate over the frequencies of the elements ; Add the frequencies ; Check if the low_median value is obtained or not , if yes then do not change as it will be minimum ; Check if the high_median value is obtained or not , if yes then do not change it as it will be maximum ; Store 2 * median of K trailing elements ; If the current >= 2 * median ; Decrease the frequency for ( k - 1 ) - th element ; Increase the frequency of the current element ; Print the count ; Driver Code
import math NEW_LINE N = 200000 NEW_LINE V = 500 NEW_LINE def solve ( n , d , input1 ) : NEW_LINE INDENT a = [ 0 ] * N NEW_LINE cnt = [ 0 ] * ( V + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = input1 [ i ] NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( d ) : NEW_LINE INDENT cnt [ a [ i ] ] += 1 NEW_LINE DEDENT for i in range ( d , n ) : NEW_LINE INDENT acc = 0 NEW_LINE low_median = - 1 NEW_LINE high_median = - 1 NEW_LINE for v in range ( V + 1 ) : NEW_LINE INDENT acc += cnt [ v ] NEW_LINE if ( low_median == - 1 and acc >= int ( math . floor ( ( d + 1 ) / 2.0 ) ) ) : NEW_LINE INDENT low_median = v NEW_LINE DEDENT if ( high_median == - 1 and acc >= int ( math . ceil ( ( d + 1 ) / 2.0 ) ) ) : NEW_LINE INDENT high_median = v NEW_LINE DEDENT DEDENT double_median = low_median + high_median NEW_LINE if ( a [ i ] >= double_median ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT cnt [ a [ i - d ] ] -= 1 NEW_LINE cnt [ a [ i ] ] += 1 NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input1 = [ 1 , 2 , 2 , 4 , 5 ] NEW_LINE n = len ( input1 ) NEW_LINE k = 3 NEW_LINE solve ( n , k , input1 ) NEW_LINE DEDENT
Smallest positive integer X satisfying the given equation | Python3 program to implement the above approach ; Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code
import sys NEW_LINE def findMinSoln ( n , k ) : NEW_LINE INDENT minSoln = sys . maxsize ; NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT minSoln = min ( minSoln , ( n // i ) * k + i ) ; NEW_LINE DEDENT DEDENT return minSoln ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE k = 6 ; NEW_LINE print ( findMinSoln ( n , k ) ) ; NEW_LINE DEDENT