codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the number of palindromic Strings | Python3 program for the above approach ; To check if there is any string of odd length ; If there is at least 1 string of odd length . ; If all the strings are of even length . ; Count of 0 's in all  the strings ; Count of 1 's in  all the strings ; If z is even and o is even then ans will be N . ; Otherwise ans will be N - 1. ; Driver code
def max_palindrome ( s , n ) : NEW_LINE INDENT flag = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT flag = 1 ; NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT z = 0 ; o = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT if ( s [ i ] [ j ] == '0' ) : NEW_LINE INDENT z += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT o += 1 ; NEW_LINE DEDENT DEDENT DEDENT if ( o % 2 == 0 and z % 2 == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT else : NEW_LINE INDENT return n - 1 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE s = [ "1110" , "100110" , "010101" ] ; NEW_LINE print ( max_palindrome ( s , n ) ) ; NEW_LINE DEDENT
Minimum possible travel cost among N cities | Function to return the minimum cost to travel from the first city to the last ; To store the total cost ; Start from the first city ; If found any city with cost less than that of the previous boarded bus then change the bus ; Calculate the cost to travel from the currently boarded bus till the current city ; Update the currently boarded bus ; Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city ; Driver code
def minCost ( cost , n ) : NEW_LINE INDENT totalCost = 0 NEW_LINE boardingBus = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( cost [ boardingBus ] > cost [ i ] ) : NEW_LINE INDENT totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) NEW_LINE boardingBus = i NEW_LINE DEDENT DEDENT totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) NEW_LINE return totalCost NEW_LINE DEDENT cost = [ 4 , 7 , 8 , 3 , 4 ] NEW_LINE n = len ( cost ) NEW_LINE print ( minCost ( cost , n ) ) NEW_LINE
Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | Python 3 implementation of the approach ; Function to return the minimum flips required such that the submatrix from mat [ i ] [ j ] to mat [ i + 1 ] [ j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code
import sys NEW_LINE def minFlipsSub ( mat , i , j ) : NEW_LINE INDENT cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE if ( mat [ i ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT return min ( cnt0 , cnt1 ) NEW_LINE DEDENT def minFlips ( mat , r , c ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( r - 1 ) : NEW_LINE INDENT for j in range ( c - 1 ) : NEW_LINE INDENT res = min ( res , minFlipsSub ( mat , i , j ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ "0101" , "0101" , "0101" ] NEW_LINE r = len ( mat ) NEW_LINE c = len ( mat [ 0 ] ) NEW_LINE print ( minFlips ( mat , r , c ) ) NEW_LINE DEDENT
Count set bits in the Kth number after segregating even and odd from N natural numbers | Function to return the kth element of the Odd - Even sequence of length n ; Finding the index from where the even numbers will be stored ; Return the kth element ; Function to return the count of set bits in the kth number of the odd even sequence of length n ; Required kth number ; Return the count of set bits ; Driver code
def findK ( n , k ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT pos = n // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos = ( n // 2 ) + 1 ; NEW_LINE DEDENT if ( k <= pos ) : NEW_LINE INDENT return ( k * 2 - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( k - pos ) * 2 ) ; NEW_LINE DEDENT DEDENT def countSetBits ( n , k ) : NEW_LINE INDENT kth = findK ( n , k ) ; NEW_LINE return bin ( kth ) . count ( '1' ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 18 ; k = 12 ; NEW_LINE print ( countSetBits ( n , k ) ) ; NEW_LINE DEDENT
Minimum cost to convert str1 to str2 with the given operations | Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
def minCost ( str1 , str2 , n ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT if ( i < n - 1 and str1 [ i + 1 ] != str2 [ i + 1 ] ) : NEW_LINE INDENT swap ( str1 [ i ] , str1 [ i + 1 ] ) NEW_LINE cost += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cost += 1 NEW_LINE DEDENT DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abb " NEW_LINE str2 = " bba " NEW_LINE n = len ( str1 ) NEW_LINE print ( minCost ( str1 , str2 , n ) ) NEW_LINE DEDENT
Partition first N natural number into two sets such that their sum is not coprime | Function to find the required sets ; Impossible case ; Sum of first n - 1 natural numbers ; Driver code
def find_set ( n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT sum1 = ( n * ( n - 1 ) ) / 2 ; NEW_LINE sum2 = n ; NEW_LINE print ( sum1 , " ▁ " , sum2 ) ; NEW_LINE DEDENT n = 8 ; NEW_LINE find_set ( n ) ; NEW_LINE
Count common elements in two arrays containing multiples of N and M | Recursive function to find gcd using euclidean algorithm ; Function to find lcm of two numbers using gcd ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def lcm ( n , m ) : NEW_LINE INDENT return ( n * m ) // gcd ( n , m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; m = 3 ; k = 5 ; NEW_LINE print ( k // lcm ( n , m ) ) ; NEW_LINE DEDENT
Check whether a subsequence exists with sum equal to k if arr [ i ] > 2 * arr [ i | Function to check whether sum of any set of the array element is equal to k or not ; Traverse the array from end to start ; if k is greater than arr [ i ] then subtract it from k ; If there is any subsequence whose sum is equal to k ; Driver code
def CheckForSequence ( arr , n , k ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( k >= arr [ i ] ) : NEW_LINE INDENT k -= arr [ i ] ; NEW_LINE DEDENT DEDENT if ( k != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 3 , 7 , 15 , 31 ] ; NEW_LINE n = len ( A ) ; NEW_LINE if ( CheckForSequence ( A , n , 18 ) ) : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT DEDENT
Maximum possible sub | Function to return the maximum sub - array sum after at most x swaps ; To store the required answer ; For all possible intervals ; Keep current ans as zero ; To store the integers which are not part of the sub - array currently under consideration ; To store elements which are part of the sub - array currently under consideration ; Create two sets ; Swap at most X elements ; Remove the minimum of the taken elements ; Add maximum of the discarded elements ; Update the answer ; Return the maximized sub - array sum ; Driver code
def SubarraySum ( a , n , x ) : NEW_LINE INDENT ans = - 10000 NEW_LINE for i in range ( n ) : NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT curans = 0 NEW_LINE pq = [ ] NEW_LINE pq2 = [ ] NEW_LINE for k in range ( n ) : NEW_LINE if ( k >= i and k <= j ) : NEW_LINE INDENT curans += a [ k ] NEW_LINE pq2 . append ( a [ k ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pq . append ( a [ k ] ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE pq . reverse ( ) NEW_LINE pq2 . sort ( ) NEW_LINE ans = max ( ans , curans ) NEW_LINE for k in range ( 1 , x + 1 ) : NEW_LINE if ( len ( pq ) == 0 or len ( pq2 ) == 0 or pq2 [ 0 ] >= pq [ 0 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT curans -= pq2 [ 0 ] NEW_LINE pq2 . pop ( 0 ) NEW_LINE curans += pq [ 0 ] NEW_LINE pq . pop ( 0 ) NEW_LINE ans = max ( ans , curans ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 5 , - 1 , 2 , 3 , 4 , - 2 , 5 ] NEW_LINE x = 2 ; NEW_LINE n = len ( a ) NEW_LINE print ( SubarraySum ( a , n , x ) ) NEW_LINE
Generate an array of size K which satisfies the given conditions | Python3 implementation of the approach ; Function to generate and print the required array ; Initializing the array ; Finding r ( from above approach ) ; If r < 0 ; Finding ceiling and floor values ; Fill the array with ceiling values ; Fill the array with floor values ; Add 1 , 2 , 3 , ... with corresponding values ; There is no solution for below cases ; Modify A [ 1 ] and A [ k - 1 ] to get the required array ; Driver Code
import sys NEW_LINE from math import floor , ceil NEW_LINE def generateArray ( n , k ) : NEW_LINE INDENT array = [ 0 ] * k NEW_LINE remaining = n - int ( k * ( k + 1 ) / 2 ) NEW_LINE if remaining < 0 : NEW_LINE INDENT print ( " NO " ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT right_most = remaining % k NEW_LINE high = ceil ( remaining / k ) NEW_LINE low = floor ( remaining / k ) NEW_LINE for i in range ( k - right_most , k ) : NEW_LINE INDENT array [ i ] = high NEW_LINE DEDENT for i in range ( k - right_most ) : NEW_LINE INDENT array [ i ] = low NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT array [ i ] += i + 1 NEW_LINE DEDENT if k - 1 != remaining or k == 1 : NEW_LINE INDENT print ( * array ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT elif k == 2 or k == 3 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT else : NEW_LINE INDENT array [ 1 ] -= 1 NEW_LINE array [ k - 1 ] += 1 NEW_LINE print ( * array ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 26 , 6 NEW_LINE generateArray ( n , k ) NEW_LINE DEDENT
Maximize the given number by replacing a segment of digits with the alternate digits given | Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code
def get_maximum ( s , a ) : NEW_LINE INDENT s = list ( s ) NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( s [ i ] ) - ord ( '0' ) < a [ ord ( s [ i ] ) - ord ( '0' ) ] ) : NEW_LINE INDENT j = i NEW_LINE while ( j < n and ( ord ( s [ j ] ) - ord ( '0' ) <= a [ ord ( s [ j ] ) - ord ( '0' ) ] ) ) : NEW_LINE INDENT s [ j ] = chr ( ord ( '0' ) + a [ ord ( s [ j ] ) - ord ( '0' ) ] ) NEW_LINE j += 1 NEW_LINE DEDENT return " " . join ( s ) ; NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "1337" NEW_LINE a = [ 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ] NEW_LINE print ( get_maximum ( s , a ) ) NEW_LINE DEDENT
Number of times the largest perfect square number can be subtracted from N | Python3 implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code
from math import sqrt NEW_LINE def countSteps ( n ) : NEW_LINE INDENT steps = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT largest = int ( sqrt ( n ) ) ; NEW_LINE n -= ( largest * largest ) ; NEW_LINE steps += 1 ; NEW_LINE DEDENT return steps ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 85 ; NEW_LINE print ( countSteps ( n ) ) ; NEW_LINE DEDENT
Maximum array sum that can be obtained after exactly k changes | Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code
def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def maxSum ( arr , n , k ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE i = 0 NEW_LINE while ( i < n and k > 0 and arr [ i ] < 0 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE k -= 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( k % 2 == 1 ) : NEW_LINE INDENT min = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ min ] > arr [ i ] ) : NEW_LINE INDENT min = i NEW_LINE DEDENT DEDENT arr [ min ] *= - 1 NEW_LINE DEDENT return sumArr ( arr , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 5 , 4 , 1 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE print ( maxSum ( arr , n , k ) ) NEW_LINE DEDENT
Spanning Tree With Maximum Degree ( Using Kruskal 's Algorithm) | Python3 implementation of the approach ; par and rank will store the parent and rank of particular node in the Union Find Algorithm ; Find function of Union Find Algorithm ; Union function of Union Find Algorithm ; Function to find the required spanning tree ; Initialising parent of a node by itself ; Variable to store the node with maximum degree ; Finding the node with maximum degree ; Union of all edges incident on vertex with maximum degree ; Carrying out normal Kruskal Algorithm ; Driver code ; Number of nodes ; Number of edges ; ArrayList to store the graph ; Array to store the degree of each node in the graph ; Add edges and update degrees
from typing import List NEW_LINE par = [ ] NEW_LINE rnk = [ ] NEW_LINE def find ( x : int ) -> int : NEW_LINE INDENT global par NEW_LINE if ( par [ x ] != x ) : NEW_LINE INDENT par [ x ] = find ( par [ x ] ) NEW_LINE DEDENT return par [ x ] NEW_LINE DEDENT def Union ( u : int , v : int ) -> None : NEW_LINE INDENT global par , rnk NEW_LINE x = find ( u ) NEW_LINE y = find ( v ) NEW_LINE if ( x == y ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( rnk [ x ] > rnk [ y ] ) : NEW_LINE INDENT par [ y ] = x NEW_LINE DEDENT elif ( rnk [ x ] < rnk [ y ] ) : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ x ] = y NEW_LINE rnk [ y ] += 1 NEW_LINE DEDENT DEDENT def findSpanningTree ( deg : List [ int ] , n : int , m : int , g : List [ List [ int ] ] ) -> None : NEW_LINE INDENT global rnk , par NEW_LINE par = [ i for i in range ( n + 1 ) ] NEW_LINE rnk = [ 0 ] * ( n + 1 ) NEW_LINE max = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( deg [ i ] > deg [ max ] ) : NEW_LINE INDENT max = i NEW_LINE DEDENT DEDENT for v in g [ max ] : NEW_LINE INDENT print ( " { } ▁ { } " . format ( max , v ) ) NEW_LINE Union ( max , v ) NEW_LINE DEDENT for u in range ( 1 , n + 1 ) : NEW_LINE INDENT for v in g [ u ] : NEW_LINE INDENT x = find ( u ) NEW_LINE y = find ( v ) NEW_LINE if ( x == y ) : NEW_LINE INDENT continue NEW_LINE DEDENT Union ( x , y ) NEW_LINE print ( " { } ▁ { } " . format ( u , v ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 5 NEW_LINE g = [ [ ] for _ in range ( n + 1 ) ] NEW_LINE deg = [ 0 ] * ( n + 1 ) NEW_LINE g [ 1 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 1 ) NEW_LINE deg [ 1 ] += 1 NEW_LINE deg [ 2 ] += 1 NEW_LINE g [ 1 ] . append ( 5 ) NEW_LINE g [ 5 ] . append ( 1 ) NEW_LINE deg [ 1 ] += 1 NEW_LINE deg [ 5 ] += 1 NEW_LINE g [ 2 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 2 ) NEW_LINE deg [ 2 ] += 1 NEW_LINE deg [ 3 ] += 1 NEW_LINE g [ 5 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 5 ) NEW_LINE deg [ 3 ] += 1 NEW_LINE deg [ 5 ] += 1 NEW_LINE g [ 3 ] . append ( 4 ) NEW_LINE g [ 4 ] . append ( 3 ) NEW_LINE deg [ 3 ] += 1 NEW_LINE deg [ 4 ] += 1 NEW_LINE findSpanningTree ( deg , n , m , g ) NEW_LINE DEDENT
Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver Code
def Maxsum ( c1 , c2 , c3 , c4 ) : NEW_LINE INDENT sum = 0 NEW_LINE two34 = min ( c2 , min ( c3 , c4 ) ) NEW_LINE sum = two34 * 234 NEW_LINE c2 -= two34 NEW_LINE sum += min ( c2 , c1 ) * 12 NEW_LINE return sum NEW_LINE DEDENT c1 = 5 ; c2 = 2 ; c3 = 3 ; c4 = 4 NEW_LINE print ( Maxsum ( c1 , c2 , c3 , c4 ) ) NEW_LINE
Replace all elements by difference of sums of positive and negative numbers after that element | Function to print the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; calculate difference of both sums ; if i - th element is positive , add it to positive sum ; if i - th element is negative , add it to negative sum ; replace i - th elements with absolute difference ; Driver Code
def printArray ( N , arr ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def replacedArray ( N , arr ) : NEW_LINE INDENT pos_sum = 0 NEW_LINE neg_sum = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT diff = abs ( pos_sum ) - abs ( neg_sum ) NEW_LINE if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_sum = pos_sum + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT neg_sum = neg_sum + arr [ i ] NEW_LINE DEDENT arr [ i ] = abs ( diff ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE arr = [ 1 , - 1 , 2 , 3 , - 2 ] NEW_LINE replacedArray ( N , arr ) NEW_LINE printArray ( N , arr ) NEW_LINE N = 6 NEW_LINE arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] NEW_LINE replacedArray ( N , arr1 ) NEW_LINE printArray ( N , arr1 ) NEW_LINE
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) ; pairs from 1 to n * ( a / n ) and n * ( b / n ) to b ; pairs from n * ( a / n ) to a and 1 to n * ( b / n ) ; pairs from n * ( a / n ) to a and n * ( b / n ) to b ; Return answer ; Driver code
def findCountOfPairs ( a , b , n ) : NEW_LINE INDENT ans = 0 NEW_LINE ans += n * int ( a / n ) * int ( b / n ) NEW_LINE ans += int ( a / n ) * ( b % n ) NEW_LINE ans += ( a % n ) * int ( b / n ) NEW_LINE ans += int ( ( ( a % n ) + ( b % n ) ) / n ) ; NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE b = 13 NEW_LINE n = 3 NEW_LINE print ( findCountOfPairs ( a , b , n ) ) NEW_LINE DEDENT
Generate array with minimum sum which can be deleted in P steps | Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
def findArray ( N , P ) : NEW_LINE INDENT ans = ( P * ( P + 1 ) ) // 2 + ( N - P ) ; NEW_LINE arr = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( 1 , P + 1 ) : NEW_LINE INDENT arr [ i ] = i ; NEW_LINE DEDENT for i in range ( P + 1 , N + 1 ) : NEW_LINE INDENT arr [ i ] = 1 ; NEW_LINE DEDENT print ( " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " , ans ) ; NEW_LINE print ( " The ▁ Array ▁ Elements ▁ are : ▁ " ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE P = 3 ; NEW_LINE findArray ( N , P ) ; NEW_LINE
Find Intersection of all Intervals | Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
def findIntersection ( intervals , N ) : NEW_LINE INDENT l = intervals [ 0 ] [ 0 ] NEW_LINE r = intervals [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( intervals [ i ] [ 0 ] > r or intervals [ i ] [ 1 ] < l ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT l = max ( l , intervals [ i ] [ 0 ] ) NEW_LINE r = min ( r , intervals [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT print ( " [ " , l , " , ▁ " , r , " ] " ) NEW_LINE DEDENT intervals = [ [ 1 , 6 ] , [ 2 , 8 ] , [ 3 , 10 ] , [ 5 , 8 ] ] NEW_LINE N = len ( intervals ) NEW_LINE findIntersection ( intervals , N ) NEW_LINE
Maximum size of sub | Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver program ; Print the required answer
def cmp ( a , b ) : NEW_LINE INDENT return ( a > b ) - ( a < b ) NEW_LINE DEDENT def maxSubarraySize ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE ans = 1 NEW_LINE anchor = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT c = cmp ( arr [ i - 1 ] , arr [ i ] ) NEW_LINE if c == 0 : NEW_LINE INDENT anchor = i NEW_LINE DEDENT elif i == N - 1 or c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 : NEW_LINE INDENT ans = max ( ans , i - anchor + 1 ) NEW_LINE anchor = i NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ] NEW_LINE print ( maxSubarraySize ( arr ) ) NEW_LINE
Maximum count of sub | Function to return the count of the required sub - strings ; Iterate over all characters ; Count with current character ; If the substring has a length k then increment count with current character ; Update max count ; Driver Code
def maxSubStrings ( s , k ) : NEW_LINE INDENT maxSubStr = 0 NEW_LINE n = len ( s ) NEW_LINE for c in range ( 27 ) : NEW_LINE INDENT ch = chr ( ord ( ' a ' ) + c ) NEW_LINE curr = 0 NEW_LINE for i in range ( n - k ) : NEW_LINE INDENT if ( s [ i ] != ch ) : NEW_LINE INDENT continue NEW_LINE DEDENT cnt = 0 NEW_LINE while ( i < n and s [ i ] == ch and cnt != k ) : NEW_LINE INDENT i += 1 NEW_LINE cnt += 1 NEW_LINE DEDENT i -= 1 NEW_LINE if ( cnt == k ) : NEW_LINE INDENT curr += 1 NEW_LINE DEDENT DEDENT maxSubStr = max ( maxSubStr , curr ) NEW_LINE DEDENT return maxSubStr NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaacaabbaa " NEW_LINE k = 2 NEW_LINE print ( maxSubStrings ( s , k ) ) NEW_LINE DEDENT
Count valid pairs in the array satisfying given conditions | Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer
def ValidPairs ( arr ) : NEW_LINE INDENT count = [ 0 ] * 121 NEW_LINE for ele in arr : NEW_LINE INDENT count [ ele ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for eleX , countX in enumerate ( count ) : NEW_LINE INDENT for eleY , countY in enumerate ( count ) : NEW_LINE INDENT if eleX < eleY : NEW_LINE INDENT continue NEW_LINE DEDENT if ( abs ( eleX - eleY ) % 2 == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans += countX * countY NEW_LINE if eleX == eleY : NEW_LINE INDENT ans -= countX NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 16 , 17 , 18 ] NEW_LINE print ( ValidPairs ( arr ) ) NEW_LINE