codeparrot/xlcost-text-to-code huggingface dataset:
Array Queries for multiply , replacements and product | vector of 1000 elements , all set to 0 ; vector of 1000 elements , all set to 0 ; Function to check number of trailing zeros in multiple of 2 ; Function to check number of trailing zeros in multiple of 5 ; Function to solve the queries received ; If the query is of typee 1. ; Counting the number of zeros in the given value of x ; The value x has been multiplied to their respective indices ; The value obtained above has been added to their respective vectors ; If the query is of typee 2. ; Counting the number of zero in the given value of x ; The value y has been replaced to their respective indices ; The value obtained above has been added to their respective vectors ; If the query is of typee 2 ; As the number of trailing zeros for each case has been found for each array element then we simply add those to the respective index to a variable ; Compare the number of zeros obtained in the multiples of five and two consider the minimum of them and add them ; Input the Size of array and number of queries ; Running the while loop for m number of queries
twos = [ 0 ] * 1000 NEW_LINE fives = [ 0 ] * 1000 NEW_LINE sum = 0 NEW_LINE def returnTwos ( val ) : NEW_LINE INDENT count = 0 NEW_LINE while ( val % 2 == 0 and val != 0 ) : NEW_LINE INDENT val = val // 2 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def returnFives ( val ) : NEW_LINE INDENT count = 0 NEW_LINE while ( val % 5 == 0 and val != 0 ) : NEW_LINE INDENT val = val // 5 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def solve_queries ( arr , n ) : NEW_LINE INDENT global sum NEW_LINE arrr1 = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE typee = arrr1 [ 0 ] NEW_LINE if ( typee == 1 ) : NEW_LINE INDENT ql , qr , x = arrr1 [ 1 ] , arrr1 [ 2 ] , arrr1 [ 3 ] NEW_LINE temp = returnTwos ( x ) NEW_LINE temp1 = returnFives ( x ) NEW_LINE i = ql - 1 NEW_LINE while ( i < qr ) : NEW_LINE INDENT arr [ i ] = arr [ i ] * x NEW_LINE twos [ i ] += temp NEW_LINE fives [ i ] += temp1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if ( typee == 2 ) : NEW_LINE INDENT ql , qr , y = arrr1 [ 1 ] , arrr1 [ 2 ] , arrr1 [ 3 ] NEW_LINE temp = returnTwos ( y ) NEW_LINE temp1 = returnFives ( y ) NEW_LINE i = ql - 1 NEW_LINE while ( i < qr ) : NEW_LINE INDENT arr [ i ] = ( i - ql + 2 ) * y NEW_LINE twos [ i ] = returnTwos ( i - ql + 2 ) + temp NEW_LINE fives [ i ] = returnFives ( i - ql + 2 ) + temp1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if ( typee == 3 ) : NEW_LINE INDENT ql , qr = arrr1 [ 1 ] , arrr1 [ 2 ] NEW_LINE sumtwos = 0 NEW_LINE sumfives = 0 NEW_LINE i = ql - 1 NEW_LINE while ( i < qr ) : NEW_LINE INDENT sumtwos += twos [ i ] NEW_LINE sumfives += fives [ i ] NEW_LINE i += 1 NEW_LINE DEDENT sum += min ( sumtwos , sumfives ) NEW_LINE DEDENT DEDENT n , m = map ( int , input ( ) . split ( ) ) NEW_LINE arr = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT twos [ i ] = returnTwos ( arr [ i ] ) NEW_LINE fives [ i ] = returnFives ( arr [ i ] ) NEW_LINE DEDENT while ( m ) : NEW_LINE INDENT m -= 1 NEW_LINE solve_queries ( arr , n ) NEW_LINE DEDENT print ( sum ) NEW_LINE
Mean of range in array | Python 3 program to find floor value of mean in range l to r ; To find mean of range in l to r ; Both sum and count are initialize to 0 ; To calculate sum and number of elements in range l to r ; Calculate floor value of mean ; Returns mean of array in range l to r ; Driver Code
import math NEW_LINE def findMean ( arr , l , r ) : NEW_LINE INDENT sum , count = 0 , 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE count += 1 NEW_LINE DEDENT mean = math . floor ( sum / count ) NEW_LINE return mean NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE print ( findMean ( arr , 0 , 2 ) ) NEW_LINE print ( findMean ( arr , 1 , 3 ) ) NEW_LINE print ( findMean ( arr , 0 , 4 ) ) NEW_LINE
Mean of range in array | Python3 program to find floor value of mean in range l to r ; To calculate prefixSum of array ; Calculate prefix sum of array ; To return floor of mean in range l to r ; Sum of elements in range l to r is prefixSum [ r ] - prefixSum [ l - 1 ] Number of elements in range l to r is r - l + 1 ; Driver Code
import math as mt NEW_LINE MAX = 1000005 NEW_LINE prefixSum = [ 0 for i in range ( MAX ) ] NEW_LINE def calculatePrefixSum ( arr , n ) : NEW_LINE INDENT prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT DEDENT def findMean ( l , r ) : NEW_LINE INDENT if ( l == 0 ) : NEW_LINE INDENT return mt . floor ( prefixSum [ r ] / ( r + 1 ) ) NEW_LINE DEDENT return ( mt . floor ( ( prefixSum [ r ] - prefixSum [ l - 1 ] ) / ( r - l + 1 ) ) ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE calculatePrefixSum ( arr , n ) NEW_LINE print ( findMean ( 0 , 2 ) ) NEW_LINE print ( findMean ( 1 , 3 ) ) NEW_LINE print ( findMean ( 0 , 4 ) ) NEW_LINE
Print modified array after executing the commands of addition and subtraction | Update function for every command ; If q == 0 , add ' k ' and ' - k ' to ' l - 1' and ' r ' index ; If q == 1 , add ' - k ' and ' k ' to ' l - 1' and ' r ' index ; Function to generate the final array after executing all commands ; Generate final array with the help of DP concept ; Driver Code ; Generate final array ; Printing the final modified array
def updateQuery ( arr , n , q , l , r , k ) : NEW_LINE INDENT if ( q == 0 ) : NEW_LINE INDENT arr [ l - 1 ] += k NEW_LINE arr [ r ] += - k NEW_LINE DEDENT else : NEW_LINE INDENT arr [ l - 1 ] += - k NEW_LINE arr [ r ] += k NEW_LINE DEDENT return NEW_LINE DEDENT def generateArray ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT return NEW_LINE DEDENT n = 5 NEW_LINE arr = [ 0 for i in range ( n + 1 ) ] NEW_LINE q = 0 ; l = 1 ; r = 3 ; k = 2 NEW_LINE updateQuery ( arr , n , q , l , r , k ) NEW_LINE q , l , r , k = 1 , 3 , 5 , 3 NEW_LINE updateQuery ( arr , n , q , l , r , k ) ; NEW_LINE q , l , r , k = 0 , 2 , 5 , 1 NEW_LINE updateQuery ( arr , n , q , l , r , k ) NEW_LINE generateArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Products of ranges in an array | Function to calculate Product in the given range . ; As our array is 0 based and L and R are given as 1 based index . ; Driver code
def calculateProduct ( A , L , R , P ) : NEW_LINE INDENT L = L - 1 NEW_LINE R = R - 1 NEW_LINE ans = 1 NEW_LINE for i in range ( R + 1 ) : NEW_LINE INDENT ans = ans * A [ i ] NEW_LINE ans = ans % P NEW_LINE DEDENT return ans NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE P = 229 NEW_LINE L = 2 NEW_LINE R = 5 NEW_LINE print ( calculateProduct ( A , L , R , P ) ) NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE print ( calculateProduct ( A , L , R , P ) ) NEW_LINE
Products of ranges in an array | Returns modulo inverse of a with respect to m using extended Euclid Algorithm . Assumption : a and m are coprimes , i . e . , gcd ( a , m ) = 1 ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; calculating pre_product array ; Cacluating inverse_product array . ; Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver Code ; Array ; Prime P ; Calculating PreProduct and InverseProduct ; Range [ L , R ] in 1 base index
def modInverse ( a , m ) : NEW_LINE INDENT m0 , x0 , x1 = m , 0 , 1 NEW_LINE if m == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT while a > 1 : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m , a = a % m , t NEW_LINE t = x0 NEW_LINE x0 = x1 - q * x0 NEW_LINE x1 = t NEW_LINE DEDENT if x1 < 0 : NEW_LINE INDENT x1 += m0 NEW_LINE DEDENT return x1 NEW_LINE DEDENT def calculate_Pre_Product ( A , N , P ) : NEW_LINE INDENT pre_product [ 0 ] = A [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pre_product [ i ] = pre_product [ i - 1 ] * A [ i ] NEW_LINE pre_product [ i ] = pre_product [ i ] % P NEW_LINE DEDENT DEDENT def calculate_inverse_product ( A , N , P ) : NEW_LINE INDENT inverse_product [ 0 ] = modInverse ( pre_product [ 0 ] , P ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT inverse_product [ i ] = modInverse ( pre_product [ i ] , P ) NEW_LINE DEDENT DEDENT def calculateProduct ( A , L , R , P ) : NEW_LINE INDENT L = L - 1 NEW_LINE R = R - 1 NEW_LINE ans = 0 NEW_LINE if L == 0 : NEW_LINE INDENT ans = pre_product [ R ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = pre_product [ R ] * inverse_product [ L - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( A ) NEW_LINE P = 113 NEW_LINE MAX = 100 NEW_LINE pre_product = [ None ] * ( MAX ) NEW_LINE inverse_product = [ None ] * ( MAX ) NEW_LINE calculate_Pre_Product ( A , N , P ) NEW_LINE calculate_inverse_product ( A , N , P ) NEW_LINE L , R = 2 , 5 NEW_LINE print ( calculateProduct ( A , L , R , P ) ) NEW_LINE L , R = 1 , 3 NEW_LINE print ( calculateProduct ( A , L , R , P ) ) NEW_LINE DEDENT
Count Primes in Ranges | Python3 program to answer queries for count of primes in given range . ; prefix [ i ] is going to store count of primes till i ( including i ) . ; Create a boolean array value in prime [ i ] will " prime [ 0 . . n ] " . A finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Build prefix array prefix [ 0 ] = prefix [ 1 ] = 0 ; ; Returns count of primes in range from L to R ( both inclusive ) . ; Driver code
MAX = 10000 NEW_LINE prefix = [ 0 ] * ( MAX + 1 ) NEW_LINE def buildPrefix ( ) : NEW_LINE INDENT prime = [ 1 ] * ( MAX + 1 ) NEW_LINE p = 2 NEW_LINE while ( p * p <= MAX ) : NEW_LINE INDENT if ( prime [ p ] == 1 ) : NEW_LINE INDENT i = p * 2 NEW_LINE while ( i <= MAX ) : NEW_LINE INDENT prime [ i ] = 0 NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , MAX + 1 ) : NEW_LINE INDENT prefix [ p ] = prefix [ p - 1 ] NEW_LINE if ( prime [ p ] == 1 ) : NEW_LINE INDENT prefix [ p ] += 1 NEW_LINE DEDENT DEDENT DEDENT def query ( L , R ) : NEW_LINE INDENT return prefix [ R ] - prefix [ L - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT buildPrefix ( ) NEW_LINE L = 5 NEW_LINE R = 10 NEW_LINE print ( query ( L , R ) ) NEW_LINE L = 1 NEW_LINE R = 10 NEW_LINE print ( query ( L , R ) ) NEW_LINE DEDENT
Binary array after M range toggle operations | function for toggle ; function for final processing of array ; function for printing result ; Driver Code ; function call for toggle ; process array ; print result
def command ( brr , a , b ) : NEW_LINE INDENT arr [ a ] ^= 1 NEW_LINE arr [ b + 1 ] ^= 1 NEW_LINE DEDENT def process ( arr , n ) : NEW_LINE INDENT for k in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT arr [ k ] ^= arr [ k - 1 ] NEW_LINE DEDENT DEDENT def result ( arr , n ) : NEW_LINE INDENT for k in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT print ( arr [ k ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE m = 3 NEW_LINE arr = [ 0 for i in range ( n + 2 ) ] NEW_LINE command ( arr , 1 , 5 ) NEW_LINE command ( arr , 2 , 5 ) NEW_LINE command ( arr , 3 , 5 ) NEW_LINE process ( arr , n ) NEW_LINE result ( arr , n ) NEW_LINE DEDENT
Print modified array after multiple array range increment operations | function to increment values in the given range by a value d for multiple queries ; for each ( start , end ) index pair perform the following operations on 'sum[]   ; increment the value at index ' start ' by the given value ' d ' in 'sum[]   ; if the index ' ( end + 1 ) ' exists then decrement the value at index ' ( end + 1 ) ' by the given value ' d ' in 'sum[]   ; Now , perform the following operations : accumulate values in the ' sum [ ] ' array and then add them to the corresponding indexes in 'arr[]   ; function to print the elements of the given array ; Driver Code ; modifying the array for multiple queries
def incrementByD ( arr , q_arr , n , m , d ) : NEW_LINE INDENT sum = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT sum [ q_arr [ i ] [ 0 ] ] += d NEW_LINE if ( ( q_arr [ i ] [ 1 ] + 1 ) < n ) : NEW_LINE INDENT sum [ q_arr [ i ] [ 1 ] + 1 ] -= d NEW_LINE DEDENT DEDENT arr [ 0 ] += sum [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT sum [ i ] += sum [ i - 1 ] NEW_LINE arr [ i ] += sum [ i ] NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 3 , 5 , 4 , 8 , 6 , 1 ] NEW_LINE q_arr = [ [ 0 , 3 ] , [ 4 , 5 ] , [ 1 , 4 ] , [ 0 , 1 ] , [ 2 , 5 ] ] NEW_LINE n = len ( arr ) NEW_LINE m = len ( q_arr ) NEW_LINE d = 2 NEW_LINE print ( " Original ▁ Array : " ) NEW_LINE printArray ( arr , n ) NEW_LINE incrementByD ( arr , q_arr , n , m , d ) NEW_LINE print ( " Modified Array : " ) NEW_LINE printArray ( arr , n ) NEW_LINE
Queries for number of distinct elements in a subarray | Python3 code to find number of distinct numbers in a subarray ; structure to store queries ; updating the bit array ; querying the bit array ; initialising bit array ; holds the rightmost index of any number as numbers of a [ i ] are less than or equal to 10 ^ 6 ; answer for each query ; If last visit is not - 1 update - 1 at the idx equal to last_visit [ arr [ i ] ] ; Setting last_visit [ arr [ i ] ] as i and updating the bit array accordingly ; If i is equal to r of any query store answer for that query in ans [ ] ; print answer for each query ; Driver Code
MAX = 1000001 NEW_LINE class Query : NEW_LINE INDENT def __init__ ( self , l , r , idx ) : NEW_LINE INDENT self . l = l NEW_LINE self . r = r NEW_LINE self . idx = idx NEW_LINE DEDENT DEDENT def update ( idx , val , bit , n ) : NEW_LINE INDENT while idx <= n : NEW_LINE INDENT bit [ idx ] += val NEW_LINE idx += idx & - idx NEW_LINE DEDENT DEDENT def query ( idx , bit , n ) : NEW_LINE INDENT summ = 0 NEW_LINE while idx : NEW_LINE INDENT summ += bit [ idx ] NEW_LINE idx -= idx & - idx NEW_LINE DEDENT return summ NEW_LINE DEDENT def answeringQueries ( arr , n , queries , q ) : NEW_LINE INDENT bit = [ 0 ] * ( n + 1 ) NEW_LINE last_visit = [ - 1 ] * MAX NEW_LINE ans = [ 0 ] * q NEW_LINE query_counter = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if last_visit [ arr [ i ] ] != - 1 : NEW_LINE INDENT update ( last_visit [ arr [ i ] ] + 1 , - 1 , bit , n ) NEW_LINE DEDENT last_visit [ arr [ i ] ] = i NEW_LINE update ( i + 1 , 1 , bit , n ) NEW_LINE while query_counter < q and queries [ query_counter ] . r == i : NEW_LINE INDENT ans [ queries [ query_counter ] . idx ] = query ( queries [ query_counter ] . r + 1 , bit , n ) - query ( queries [ query_counter ] . l , bit , n ) NEW_LINE query_counter += 1 NEW_LINE DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT print ( ans [ i ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 1 , 2 , 1 , 3 ] NEW_LINE n = len ( a ) NEW_LINE queries = [ Query ( 0 , 4 , 0 ) , Query ( 1 , 3 , 1 ) , Query ( 2 , 4 , 2 ) ] NEW_LINE q = len ( queries ) NEW_LINE queries . sort ( key = lambda x : x . r ) NEW_LINE answeringQueries ( a , n , queries , q ) NEW_LINE DEDENT
No of pairs ( a [ j ] >= a [ i ] ) with k numbers in range ( a [ i ] , a [ j ] ) that are divisible by x | Python3 program to calculate the number pairs satisfying th condition ; function to calculate the number of pairs ; traverse through all elements ; current number 's divisor ; use binary search to find the element after k multiples of x ; use binary search to find the element after k + 1 multiples of x so that we get the answer bu subtracting ; the difference of index will be the answer ; Driver code ; function call to get the number of pairs
import bisect NEW_LINE def countPairs ( a , n , x , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT d = ( a [ i ] - 1 ) // x NEW_LINE it1 = bisect . bisect_left ( a , max ( ( d + k ) * x , a [ i ] ) ) NEW_LINE it2 = bisect . bisect_left ( a , max ( ( d + k + 1 ) * x , a [ i ] ) ) NEW_LINE ans += it2 - it1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 5 , 7 ] NEW_LINE n = len ( a ) NEW_LINE x = 2 NEW_LINE k = 1 NEW_LINE print ( countPairs ( a , n , x , k ) ) NEW_LINE DEDENT
Probability of a random pair being the maximum weighted pair |  ; Function to return probability ; Count occurrences of maximum element in A [ ] ; Count occurrences of maximum element in B [ ] ; Returning probability ; Driver code
import sys NEW_LINE def probability ( a , b , size1 , size2 ) : NEW_LINE INDENT max1 = - ( sys . maxsize - 1 ) NEW_LINE count1 = 0 NEW_LINE for i in range ( size1 ) : NEW_LINE INDENT if a [ i ] > max1 : NEW_LINE INDENT count1 = 1 NEW_LINE DEDENT elif a [ i ] == max1 : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT DEDENT max2 = - ( sys . maxsize - 1 ) NEW_LINE count2 = 0 NEW_LINE for i in range ( size2 ) : NEW_LINE INDENT if b [ i ] > max2 : NEW_LINE INDENT max2 = b [ i ] NEW_LINE count2 = 1 NEW_LINE DEDENT elif b [ i ] == max2 : NEW_LINE INDENT count2 += 1 NEW_LINE DEDENT DEDENT return round ( ( count1 * count2 ) / ( size1 * size2 ) , 6 ) NEW_LINE DEDENT a = [ 1 , 2 , 3 ] NEW_LINE b = [ 1 , 3 , 3 ] NEW_LINE size1 = len ( a ) NEW_LINE size2 = len ( b ) NEW_LINE print ( probability ( a , b , size1 , size2 ) ) NEW_LINE
Minimum De | function to count Dearrangement ; create a copy of original array ; sort the array ; traverse sorted array for counting mismatches ; reverse the sorted array ; traverse reverse sorted array for counting mismatches ; return minimum mismatch count ; Driven code
def countDe ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE v = arr . copy ( ) NEW_LINE arr . sort ( ) NEW_LINE count1 = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] != v [ i ] ) : NEW_LINE INDENT count1 = count1 + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT arr . sort ( reverse = True ) NEW_LINE count2 = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] != v [ i ] ) : NEW_LINE INDENT count2 = count2 + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return ( min ( count1 , count2 ) ) NEW_LINE DEDENT arr = [ 5 , 9 , 21 , 17 , 13 ] NEW_LINE n = 5 NEW_LINE print ( " Minimum ▁ Dearrangement ▁ = " , countDe ( arr , n ) ) NEW_LINE
Divide an array into k segments to maximize maximum of segment minimums | function to calculate the max of all the minimum segments ; if we have to divide it into 1 segment then the min will be the answer ; If k >= 3 , return maximum of all elements . ; Driver code
def maxOfSegmentMins ( a , n , k ) : NEW_LINE INDENT if k == 1 : NEW_LINE INDENT return min ( a ) NEW_LINE DEDENT if k == 2 : NEW_LINE INDENT return max ( a [ 0 ] , a [ n - 1 ] ) NEW_LINE DEDENT return max ( a ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 2 NEW_LINE print ( maxOfSegmentMins ( a , n , k ) ) NEW_LINE DEDENT
Minimum product pair an array of positive Integers | Function to calculate minimum product of pair ; Initialize first and second minimums . It is assumed that the array has at least two elements . ; Traverse remaining array and keep track of two minimum elements ( Note that the two minimum elements may be same if minimum element appears more than once ) more than once ) ; Driver code
def printMinimumProduct ( arr , n ) : NEW_LINE INDENT first_min = min ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE second_min = max ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] < first_min ) : NEW_LINE INDENT second_min = first_min NEW_LINE first_min = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < second_min ) : NEW_LINE INDENT second_min = arr [ i ] NEW_LINE DEDENT DEDENT return first_min * second_min NEW_LINE DEDENT a = [ 11 , 8 , 5 , 7 , 5 , 100 ] NEW_LINE n = len ( a ) NEW_LINE print ( printMinimumProduct ( a , n ) ) NEW_LINE
Count ways to form minimum product triplets | function to calculate number of triples ; Sort the array ; Count occurrences of third element ; If all three elements are same ( minimum element appears at l east 3 times ) . Answer is nC3 . ; If minimum element appears once . Answer is nC2 . ; Minimum two elements are distinct . Answer is nC1 . ; Driver code
def noOfTriples ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == arr [ 2 ] : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if arr [ 0 ] == arr [ 2 ] : NEW_LINE INDENT return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 NEW_LINE DEDENT elif arr [ 1 ] == arr [ 2 ] : NEW_LINE INDENT return ( count - 1 ) * ( count ) / 2 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 1 , 3 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( noOfTriples ( arr , n ) ) NEW_LINE
Check if reversing a sub array make the array sorted | Return true , if reversing the subarray will sort the array , else return false . ; Copying the array ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driver code
def checkReverse ( arr , n ) : NEW_LINE INDENT temp = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp [ i ] = arr [ i ] NEW_LINE DEDENT temp . sort ( ) NEW_LINE for front in range ( n ) : NEW_LINE INDENT if temp [ front ] != arr [ front ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for back in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if temp [ back ] != arr [ back ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if front >= back : NEW_LINE INDENT return True NEW_LINE DEDENT while front != back : NEW_LINE INDENT front += 1 NEW_LINE if arr [ front - 1 ] < arr [ front ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 2 , 5 , 4 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if checkReverse ( arr , n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if reversing a sub array make the array sorted | Python3 program to check whether reversing a sub array make the array sorted or not ; Return True , if reversing the subarray will sort the array , else return False . ; Find first increasing part ; Find reversed part ; Find last increasing part ; To handle cases like 1 , 2 , 3 , 4 , 20 , 9 , 16 , 17 ; Driver Code
import math as mt NEW_LINE def checkReverse ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i - 1 ] < arr [ i ] : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT j = i NEW_LINE while ( j < n and arr [ j ] < arr [ j - 1 ] ) : NEW_LINE INDENT if ( i > 1 and arr [ j ] < arr [ i - 2 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT k = j NEW_LINE if ( arr [ k ] < arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT while ( k > 1 and k < n ) : NEW_LINE INDENT if ( arr [ k ] < arr [ k - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT k += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 10 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE if checkReverse ( arr , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT