codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from an array having equal sum and quotient | Function to count all pairs ( i , j ) such that a [ i ] + [ j ] = a [ i ] / a [ j ] ; Stores total count of pairs ; Generate all possible pairs ; If a valid pair is found ; Increment count ; Return the final count ; Driver Code
def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( a [ j ] != 0 and a [ i ] % a [ j ] == 0 ) : NEW_LINE INDENT if ( ( a [ i ] + a [ j ] ) == ( a [ i ] // a [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 4 , - 3 , 0 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Count pairs from an array having equal sum and quotient | Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Prthe final count ; Driver Code ; Function Call
def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT y = a [ i ] NEW_LINE if ( y != 0 and y != 1 ) : NEW_LINE INDENT x = ( ( ( y * 1.0 ) // ( 1 - y ) ) * y ) NEW_LINE count += mp . get ( x , 0 ) NEW_LINE DEDENT mp [ y ] = mp . get ( y , 0 ) + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ - 4 , - 3 , 0 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver Code ; Function Call
def contains ( num , K , base ) : NEW_LINE INDENT isThere = 0 NEW_LINE while ( num ) : NEW_LINE INDENT remainder = num % base NEW_LINE if ( remainder == K ) : NEW_LINE INDENT isThere = 1 NEW_LINE DEDENT num //= base NEW_LINE DEDENT return isThere NEW_LINE DEDENT def count ( n , k , v ) : NEW_LINE INDENT pref = [ 0 ] * 1000005 NEW_LINE for i in range ( 1 , 10 ** 6 + 5 ) : NEW_LINE INDENT present = contains ( i , k , 10 ) or contains ( i , k , 8 ) NEW_LINE pref [ i ] += pref [ i - 1 ] + present NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( v [ i ] [ 1 ] - v [ i ] [ 0 ] + 1 - ( pref [ v [ i ] [ 1 ] ] - pref [ v [ i ] [ 0 ] - 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 7 NEW_LINE Q = [ [ 2 , 5 ] , [ 1 , 15 ] ] NEW_LINE N = len ( Q ) NEW_LINE count ( N , K , Q ) NEW_LINE DEDENT
Count tiles of dimensions 2 * 1 that can be placed in an M * N rectangular board that satisfies the given conditions | Function to count tiles of dimensions 2 x 1 that can be placed in a grid of dimensions M * N as per given conditions ; Number of tiles required ; Driver Code
def numberOfTiles ( N , M ) : NEW_LINE INDENT if ( N % 2 == 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( N * M ) // 2 NEW_LINE DEDENT N = 2 NEW_LINE M = 4 NEW_LINE print ( numberOfTiles ( N , M ) ) NEW_LINE
Check if an array can be converted to another given array by swapping pairs of unequal elements | Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Driver Code ; Given input a ; Size of the array ; Function Call
def canMakeEqual ( arr1 , arr2 , N ) : NEW_LINE INDENT count = 0 NEW_LINE arr1_one = 0 NEW_LINE arr1_zero = 0 NEW_LINE arr2_one = 0 NEW_LINE arr2_zero = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr1 [ i ] == 1 ) : NEW_LINE INDENT arr1_one += 1 NEW_LINE DEDENT elif ( arr1 [ i ] == 0 ) : NEW_LINE INDENT arr1_zero += 1 NEW_LINE DEDENT if ( arr2 [ i ] == 1 ) : NEW_LINE INDENT arr2_one += 1 NEW_LINE DEDENT elif ( arr2 [ i ] == 0 ) : NEW_LINE INDENT arr2_zero += 1 NEW_LINE DEDENT DEDENT if ( arr1_one != arr2_one or arr1_zero != arr2_zero ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT count = count + ( arr1 [ i ] - arr2 [ i ] ) NEW_LINE if ( count < 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 0 , 1 , 1 , 0 ] NEW_LINE arr2 = [ 0 , 0 , 1 , 1 ] NEW_LINE N = len ( arr1 ) NEW_LINE canMakeEqual ( arr1 , arr2 , N ) NEW_LINE DEDENT
Check for each subarray whether it consists of all natural numbers up to its length or not | Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver Code
def checksubarrayExist1_N ( arr , N ) : NEW_LINE INDENT pos = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT pos [ arr [ i ] ] = i NEW_LINE DEDENT st = { } NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT st [ pos [ i ] ] = 1 NEW_LINE Min = sorted ( list ( st . keys ( ) ) ) [ 0 ] NEW_LINE Max = sorted ( list ( st . keys ( ) ) ) [ - 1 ] NEW_LINE if ( Max - Min + 1 == i ) : NEW_LINE INDENT print ( " True " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE checksubarrayExist1_N ( arr , N ) NEW_LINE DEDENT
Check if a pair of integers A and B can coincide by shifting them by distances arr [ ( A % N + N ) % N ] and arr [ ( B % N + N ) % N ] | Function to check if two integers coincide at a point ; Store the final position of integers A and B ; Iterate over the range [ 0 , n ] ; Store the final position of the integer i ; If temp is present in the Map ; Print Yes and return ; Mark its final position as visited ; If every integer stored in the Map is unique ; Driver Code
def checkSamePosition ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = ( ( i + arr [ i ] ) % n + n ) % n NEW_LINE if temp in mp : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT if ( temp in mp ) : NEW_LINE INDENT mp [ temp ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ temp ] = mp . get ( temp , 0 ) + 1 NEW_LINE DEDENT DEDENT print ( " No " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE checkSamePosition ( arr , N ) NEW_LINE DEDENT
Sum of Fibonacci Numbers | Set 2 | Python program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Prthe result ; Driver Code
import math NEW_LINE def sumFib ( N ) : NEW_LINE INDENT num = round ( pow ( ( pow ( 5 , 1 / 2 ) + 1 )   \ / 2.0 , N + 2 ) / pow ( 5 , 1 / 2 ) ) ; NEW_LINE print ( num - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; NEW_LINE sumFib ( N ) ; NEW_LINE DEDENT
Sum of Fibonacci Numbers | Set 2 | Pyhton3 program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code ; Function Call
import math NEW_LINE def sumFib ( N ) : NEW_LINE INDENT num = ( 1 - math . sqrt ( 5 ) ) / 2 NEW_LINE val = round ( abs ( 1 / ( pow ( num , N + 2 ) + pow ( num , N + 1 ) + pow ( num , N ) + pow ( num , N - 1 ) ) ) - 1 ) NEW_LINE print ( val ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE sumFib ( N ) NEW_LINE DEDENT
Minimum number of pigs required to find the poisonous bucket | Python program for the above approach ; Function to find the minimum number of pigs required to find the poisonous bucket ; Prthe result ; Driver Code
import math NEW_LINE def poorPigs ( buckets , minutesToDie , minutesToTest ) : NEW_LINE INDENT print ( math . ceil ( math . log ( buckets ) // math . log ( ( minutesToTest   \ // minutesToDie ) + 1 ) ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1000 ; NEW_LINE M = 15 ; NEW_LINE P = 60 ; NEW_LINE poorPigs ( N , M , P ) ; NEW_LINE DEDENT
Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | Python program for the above approach ; Function to find the maximum product possible after repeatedly replacing pairs of adjacent array elements with their sum ; Store the maximum product ; Store the prefix sum ; Store the total sum of array ; Traverse the array to find the total sum ; Iterate in the range [ 0 , N - 2 ] ; Add arr [ i ] to prefix_sum ; Store the value of prefix_sum ; Store the value of ( total sum - prefix sum ) ; Update the maximum product ; Print the answer ; Driver Code
import sys NEW_LINE def maxProduct ( arr , N ) : NEW_LINE INDENT max_product = - sys . maxsize ; NEW_LINE prefix_sum = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT for i in range ( N - 1 ) : NEW_LINE INDENT prefix_sum += arr [ i ] ; NEW_LINE X = prefix_sum ; NEW_LINE Y = sum - prefix_sum ; NEW_LINE max_product = max ( max_product , X * Y ) ; NEW_LINE DEDENT print ( max_product ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 6 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxProduct ( arr , N ) ; NEW_LINE DEDENT
Find array elements with rightmost set bit at the position of the rightmost set bit in K | Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K
def findMask ( K ) : NEW_LINE INDENT mask = 1 ; NEW_LINE while ( ( K & mask ) == 0 ) : NEW_LINE INDENT mask = mask << 1 ; NEW_LINE DEDENT return mask ; NEW_LINE DEDENT def sameRightSetBitPos ( arr , N , K ) : NEW_LINE INDENT mask = findMask ( K ) ; NEW_LINE pos = ( K & mask ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ( arr [ i ] & mask ) == pos ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 7 ; NEW_LINE sameRightSetBitPos ( arr , N , K ) ; NEW_LINE DEDENT
Count Pronic numbers from a given range | Python3 program for the above approach ; Function to check if x is a Pronic Number or not ; Check for Pronic Number by multiplying consecutive numbers ; Function to count pronic numbers in the range [ A , B ] ; Initialise count ; Iterate from A to B ; If i is pronic ; Increment count ; Prcount ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
import math NEW_LINE def checkPronic ( x ) : NEW_LINE INDENT for i in range ( int ( math . sqrt ( x ) ) + 1 ) : NEW_LINE INDENT if ( x == i * ( i + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countPronic ( A , B ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( A , B + 1 ) : NEW_LINE INDENT if ( checkPronic ( i ) != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT A = 3 NEW_LINE B = 20 NEW_LINE countPronic ( A , B ) NEW_LINE
Count Pronic numbers from a given range | Function to count pronic numbers in the range [ A , B ] ; Check upto sqrt N ; If product of consecutive numbers are less than equal to num ; Return N - 1 ; Function to count pronic numbers in the range [ A , B ] ; Subtract the count of pronic numbers which are <= ( A - 1 ) from the count f pronic numbers which are <= B ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
def pronic ( num ) : NEW_LINE INDENT N = int ( num ** ( 1 / 2 ) ) ; NEW_LINE if ( N * ( N + 1 ) <= num ) : NEW_LINE INDENT return N ; NEW_LINE DEDENT return N - 1 ; NEW_LINE DEDENT def countPronic ( A , B ) : NEW_LINE INDENT return pronic ( B ) - pronic ( A - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 3 ; NEW_LINE B = 20 ; NEW_LINE print ( countPronic ( A , B ) ) ; NEW_LINE DEDENT
Count integers from a given range with no odd divisors | Function to count integers in the range 1 to N having no odd divisors ; Traverse the array ; Stores the nearest power of two less than arr [ i ] ; Stores the count of integers with no odd divisor for each query ; Iterate until powerOfTwo is less then or equal to arr [ i ] ; Print the answer for the current element ; Driver Code ; Given array ; Size of the array
def oddDivisors ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT powerOfTwo = 2 ; NEW_LINE count = 0 ; NEW_LINE while ( powerOfTwo <= arr [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE powerOfTwo = 2 * powerOfTwo ; NEW_LINE DEDENT print ( count , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 15 , 16 , 20 , 35 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE oddDivisors ( arr , N ) ; NEW_LINE DEDENT
Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
def checkPossible ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] -= arr [ i - 1 ] ; NEW_LINE arr [ i - 1 ] = 0 ; NEW_LINE DEDENT DEDENT if ( arr [ n - 1 ] == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE checkPossible ( arr , N ) ; NEW_LINE DEDENT
Nearest smaller power of 2 for every digit of a number | Python 3 program for the above approach ; Function to find the nearest power of two for every digit of a given number ; Converting number to string ; Traverse the array ; Calculate log base 2 of the current digit s [ i ] ; Highest power of 2 <= s [ i ] ; ASCII conversion ; Driver Code
import math NEW_LINE def highestPowerOfTwo ( num ) : NEW_LINE INDENT s = str ( num ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT print ( "0" ) NEW_LINE continue NEW_LINE DEDENT lg = int ( math . log2 ( ord ( s [ i ] ) - 48 ) ) NEW_LINE p = pow ( 2 , lg ) NEW_LINE print ( chr ( p + 48 ) , end = " " ) NEW_LINE DEDENT DEDENT num = 4317 NEW_LINE highestPowerOfTwo ( num ) NEW_LINE
Maximum prefix sum after K reversals of a given array | Python3 program for the above approach ; Function to find the maximum prefix sum after K reversals of the array ; Stores the required sum ; If K is odd , reverse the array ; Store current prefix sum of array ; Traverse the array arr [ ] ; Add arr [ i ] to currsum ; Update maximum prefix sum till now ; Prthe answer ; Driver Code ; Function Call
import sys NEW_LINE def maxSumAfterKReverse ( arr , K , N ) : NEW_LINE INDENT sum = - sys . maxsize - 1 NEW_LINE if ( K & 1 ) : NEW_LINE INDENT arr . reverse ( ) NEW_LINE DEDENT currsum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT currsum += arr [ i ] NEW_LINE sum = max ( sum , currsum ) NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT arr = [ 1 , 5 , 8 , 9 , 11 , 2 ] NEW_LINE K = 1 NEW_LINE N = len ( arr ) NEW_LINE maxSumAfterKReverse ( arr , K , N ) NEW_LINE
Print all submasks of a given mask | Function to print the submasks of N ; Driven Code
def SubMasks ( N ) : NEW_LINE INDENT S = N NEW_LINE while S > 0 : NEW_LINE INDENT print ( S , end = ' ▁ ' ) NEW_LINE S = ( S - 1 ) & N NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE SubMasks ( N ) NEW_LINE DEDENT
Count pairs from a given range having equal Bitwise OR and XOR values | Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; The upper bound is 2 ^ N ; Stores the count of pairs ; Generate all possible pairs ; Find XOR of both integers ; Find OR of both integers ; If both are equal ; Increment count ; Print count % MOD ; Driver Code ; Function Call
def power ( x , y ) : NEW_LINE INDENT MOD = 1000000007 NEW_LINE res = 1 NEW_LINE x = x % MOD NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 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 countPairs ( N ) : NEW_LINE INDENT MOD = 1000000007 NEW_LINE high = power ( 2 , N ) NEW_LINE count = 0 NEW_LINE for i in range ( high ) : NEW_LINE INDENT for j in range ( high ) : NEW_LINE INDENT X = ( i ^ j ) NEW_LINE Y = ( i j ) NEW_LINE if ( X == Y ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count % MOD ) NEW_LINE DEDENT N = 10 NEW_LINE countPairs ( N ) NEW_LINE
Count pairs from a given range having equal Bitwise OR and XOR values | Python program for the above approach ; Function to find the value of ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now , then update y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; Finding 3 ^ N % 10 ^ 9 + 7 ; Driver Code ; Function Call
MOD = 1000000007 ; NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % MOD ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 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 countPairs ( N ) : NEW_LINE INDENT print ( power ( 3 , N ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 ; NEW_LINE countPairs ( N ) ; NEW_LINE DEDENT
Minimize swaps required to place largest and smallest array elements at first and last array indices | Python 3 program for the above approach ; Function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; Stores the smallest array element ; Stores the smallest array element ; Stores the last occurrence of the smallest array element ; Stores the first occurrence of the largest array element ; Traverse the array arr [ ] ; If a [ i ] is less than min_element ; Update min_element ; Update min_ind ; If a [ i ] is greater than max_element ; Update max_element ; Update max_ind ; If max_ind is equal to min_ind ; Return 0 ; If max_ind is greater than min_ind ; Otherwise ; Driver Code ; Input ; Print the result
import sys NEW_LINE def minimumMoves ( a , n ) : NEW_LINE INDENT min_element = sys . maxsize NEW_LINE max_element = - sys . maxsize - 1 NEW_LINE min_ind = - 1 NEW_LINE max_ind = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] <= min_element ) : NEW_LINE INDENT min_element = a [ i ] NEW_LINE min_ind = i NEW_LINE DEDENT if ( a [ i ] > max_element ) : NEW_LINE INDENT max_element = a [ i ] NEW_LINE max_ind = i NEW_LINE DEDENT DEDENT if ( max_ind == min_ind ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( max_ind > min_ind ) : NEW_LINE INDENT return max_ind + ( n - min_ind - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return max_ind + n - min_ind - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 35 , 46 , 17 , 23 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumMoves ( arr , N ) ) NEW_LINE DEDENT
Find the index of the smallest element to be removed to make sum of array divisible by K | Function to find index of the smallest array element required to be removed to make sum divisible by K ; Stores sum of array elements ; Stores index of the smallest element removed from the array to make sum divisible by K ; Stores the smallest element removed from the array to make sum divisible by K ; Traverse the array , arr [ ] ; Update sum ; Traverse the array arr [ ] ; Calculate remaining sum ; If sum is divisible by K ; If res == - 1 or mini is greater than arr [ i ] ; Update res and mini ; Driver Code
def findIndex ( arr , n , K ) : NEW_LINE INDENT sum = 0 NEW_LINE res = - 1 NEW_LINE mini = 1e9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT temp = sum - arr [ i ] NEW_LINE if ( temp % K == 0 ) : NEW_LINE INDENT if ( res == - 1 or mini > arr [ i ] ) : NEW_LINE INDENT res = i + 1 NEW_LINE mini = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT arr = [ 14 , 7 , 8 , 2 , 4 ] NEW_LINE K = 7 NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N , K ) ) NEW_LINE
Count subarrays having a single distinct element that can be obtained from a given array | python 3 program for the above approach ; Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
from collections import defaultdict NEW_LINE def divisionalArrays ( arr , N ) : NEW_LINE INDENT sum = N NEW_LINE mp = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for x in mp : NEW_LINE INDENT if ( mp [ x ] > 1 ) : NEW_LINE INDENT sum += mp [ x ] - 1 NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE divisionalArrays ( arr , N ) NEW_LINE DEDENT