codeparrot/xlcost-text-to-code huggingface dataset:
Minimize swaps of same | Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of S ; Sum of T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If ord ( S [ i ] ) anord ( ' a ) rd ( T [ i ] ) ▁ haord ( ' a ) different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists pr1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
def countSwaps ( S , T ) : NEW_LINE INDENT value = [ 0 ] * 26 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT value [ i ] = i + 1 NEW_LINE DEDENT N = len ( S ) NEW_LINE sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE flag = False NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += value [ ord ( S [ i ] ) - ord ( ' a ' ) ] NEW_LINE sum2 += value [ ord ( T [ i ] ) - ord ( ' a ' ) ] NEW_LINE if ( value [ ord ( S [ i ] ) - ord ( ' a ' ) ] % 2 == 0 and value [ ord ( T [ i ] ) - ord ( ' a ' ) ] % 2 == 1 or value [ ord ( S [ i ] ) - ord ( ' a ' ) ] % 2 == 1 and value [ ord ( T [ i ] ) - ord ( ' a ' ) ] % 2 == 0 ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT DEDENT if ( sum1 % 2 == 1 and sum2 % 2 == 1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( sum1 % 2 == 0 and sum2 % 2 == 0 ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " acd " NEW_LINE T = " dbf " NEW_LINE countSwaps ( S , T ) NEW_LINE DEDENT
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | Python3 program for the above approach ; Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Prthe count obtained ; Driver Code
import math NEW_LINE def isPrime ( N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( math . sqrt ( N ) + 1 ) ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countPrimeTuples ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) and isPrime ( i - 2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT N = 6 NEW_LINE countPrimeTuples ( N ) NEW_LINE
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Store the required answer ; Check for all possible pairs ; If the condition satisfy then increment count by 1 ; Prthe answer ; Driver Code ; Function Call
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 [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 7 ] ; NEW_LINE N = len ( A ) ; NEW_LINE countPairs ( A , N ) ; NEW_LINE DEDENT
Count maximum number of consumable candies | Function to find the count of maximum consumable candies ; Store the count of total candies ; Stores the count of maximum consumable candies ; Checks if it is safe to counsume all candies ; Traverse the array arr ; If A [ i ] + M is greater than B [ i ] ; Mark all_safe as false ; Update ans ; Update ans ; Increment total by A [ i ] ; If all_safe is true ; Otherwise , ; Driver Code ; Function call to find maximum consumable candies
def maximumCandy ( candies , safety , N , M ) : NEW_LINE INDENT total = 0 NEW_LINE ans = 10 ** 8 NEW_LINE all_safe = True NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( candies [ i ] + M > safety [ i ] ) : NEW_LINE INDENT all_safe = False NEW_LINE ans = min ( ans , safety [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = min ( ans , candies [ i ] + M ) NEW_LINE DEDENT total += candies [ i ] NEW_LINE DEDENT if ( all_safe ) : NEW_LINE INDENT return total NEW_LINE DEDENT else : NEW_LINE INDENT return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 5 , 2 , 3 ] NEW_LINE B = [ 8 , 13 , 6 , 4 ] NEW_LINE M = 5 NEW_LINE N = len ( A ) NEW_LINE print ( maximumCandy ( A , B , N , M ) ) NEW_LINE DEDENT
Check if two arrays can be made equal by swapping pairs of one of the arrays | Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , pr " Yes " . Otherwise " No " ; Driver Code ; Given arrays ; Size of the array
def checkArrays ( arr1 , arr2 , N ) : NEW_LINE INDENT count = 0 NEW_LINE flag = True NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr1 [ i ] != arr2 [ i ] ) : NEW_LINE INDENT if ( arr1 [ i ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count -= 1 NEW_LINE if ( count < 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( flag and count == 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 arr1 = [ 0 , 0 , 1 , 1 ] NEW_LINE arr2 = [ 1 , 1 , 0 , 0 ] NEW_LINE N = len ( arr1 ) NEW_LINE checkArrays ( arr1 , arr2 , N ) NEW_LINE DEDENT
Generate an N | Function to construct an array with sum of each subarray divisible by K ; Traverse a loop from 1 to N ; Pri - th multiple of K ; Driver Code
def construct_Array ( N , K ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( K * i , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE K = 3 NEW_LINE construct_Array ( N , K ) NEW_LINE
Generate an N | Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
def Print ( N ) : NEW_LINE INDENT if ( ( N / 2 ) % 2 or ( N % 2 ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT CurEven = 2 NEW_LINE CurOdd = 1 NEW_LINE SumOdd = 0 NEW_LINE SumEven = 0 NEW_LINE for i in range ( N // 2 ) : NEW_LINE INDENT print ( CurEven , end = " ▁ " ) NEW_LINE SumEven += CurEven NEW_LINE CurEven += 2 NEW_LINE DEDENT for i in range ( ( N // 2 ) - 1 ) : NEW_LINE INDENT print ( CurOdd , end = " ▁ " ) NEW_LINE SumOdd += CurOdd NEW_LINE CurOdd += 2 NEW_LINE DEDENT CurOdd = SumEven - SumOdd NEW_LINE print ( CurOdd ) NEW_LINE DEDENT N = 12 NEW_LINE Print ( N ) NEW_LINE
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | Function to print the last remaining array element after after performing given operations ; If size of the array is 1 ; Check for the condition ; If condition is not satisfied ; Driver Code ; Function Call
def canArrayBeReduced ( arr , N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( arr [ 0 ] ) NEW_LINE return NEW_LINE DEDENT if ( arr [ 0 ] < arr [ N - 1 ] ) : NEW_LINE INDENT print ( arr [ N - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 5 , 2 , 4 , 1 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE canArrayBeReduced ( arr , N ) NEW_LINE DEDENT
Minimum prefix increments required to make all elements of an array multiples of another array | Python3 program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays
from math import ceil , floor NEW_LINE def MinimumMoves ( A , B , N ) : NEW_LINE INDENT totalOperations = 0 NEW_LINE carry = 0 NEW_LINE K = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT nearestMultiple = ceil ( ( A [ i ] + carry ) / B [ i ] ) * B [ i ] NEW_LINE K = nearestMultiple - ( A [ i ] + carry ) NEW_LINE totalOperations += K NEW_LINE carry += K NEW_LINE DEDENT return totalOperations NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 4 , 5 , 2 , 5 , 5 , 9 ] NEW_LINE B = [ 1 , 1 , 9 , 6 , 3 , 8 , 7 ] NEW_LINE N = len ( A ) NEW_LINE print ( MinimumMoves ( A , B , N ) ) NEW_LINE DEDENT
Minimize deviation of an array by given operations | Function to find the minimum deviation of the array A [ ] ; Store all array elements in sorted order ; Odd number are transformed using 2 nd operation ; ( Maximum - Minimum ) ; Check if the size of set is > 0 and the maximum element is divisible by 2 ; Maximum element of the set ; Erase the maximum element ; Using operation 1 ; ( Maximum - Minimum ) ; Print the Minimum Deviation Obtained ; Driver Code ; Function Call to find Minimum Deviation of A [ ]
def minimumDeviation ( A , N ) : NEW_LINE INDENT s = set ( [ ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % 2 == 0 ) : NEW_LINE INDENT s . add ( A [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT s . add ( 2 * A [ i ] ) NEW_LINE DEDENT DEDENT s = list ( s ) NEW_LINE diff = s [ - 1 ] - s [ 0 ] NEW_LINE while ( len ( s ) and s [ - 1 ] % 2 == 0 ) : NEW_LINE INDENT maxEl = s [ - 1 ] NEW_LINE s . remove ( maxEl ) NEW_LINE s . append ( maxEl // 2 ) NEW_LINE diff = min ( diff , s [ - 1 ] - s [ 0 ] ) NEW_LINE DEDENT print ( diff ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 4 , 1 , 5 , 20 , 3 ] NEW_LINE N = len ( A ) NEW_LINE minimumDeviation ( A , N ) NEW_LINE DEDENT
Maximize given integer by swapping pairs of unequal bits | Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver Code ; Given "Input ; Function call to find the Maximum Possible Number
def findMaxNum ( num ) : NEW_LINE INDENT binaryNumber = bin ( num ) [ 2 : ] NEW_LINE maxBinaryNumber = " " NEW_LINE count0 , count1 = 0 , 0 NEW_LINE N = len ( binaryNumber ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( binaryNumber [ i ] == '1' ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT for i in range ( count1 ) : NEW_LINE INDENT maxBinaryNumber += '1' NEW_LINE DEDENT for i in range ( count0 ) : NEW_LINE INDENT maxBinaryNumber += '0' NEW_LINE DEDENT return int ( maxBinaryNumber , 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE print ( findMaxNum ( N ) ) NEW_LINE DEDENT
Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input String ; Function call
def minOperations ( N ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE for it in N : NEW_LINE INDENT if ( int ( ord ( it ) - ord ( '0' ) ) % 2 == 0 ) : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 ; NEW_LINE DEDENT DEDENT if ( odd == 0 or odd == 1 ) : NEW_LINE INDENT print ( " Not ▁ Possible " + " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 ; NEW_LINE for it in N : NEW_LINE INDENT if ( int ( ord ( it ) - ord ( '0' ) ) % 2 == 0 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 ; NEW_LINE DEDENT DEDENT if ( odd % 2 != 0 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT print ( ans , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "12345" ; NEW_LINE minOperations ( N ) ; NEW_LINE DEDENT
Find the player with least 0 s after emptying a Binary String by removing non | Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
def FindwinnerOfGame ( S ) : NEW_LINE INDENT cntZero = 0 NEW_LINE cntConOne = 0 NEW_LINE nimSum = 0 NEW_LINE N = len ( S ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT cntConOne += 1 NEW_LINE DEDENT else : NEW_LINE INDENT nimSum ^= cntConOne NEW_LINE cntConOne = 0 NEW_LINE cntZero += 1 NEW_LINE DEDENT DEDENT nimSum ^= cntConOne NEW_LINE if ( cntZero % 2 == 0 ) : NEW_LINE INDENT print ( " Tie " ) NEW_LINE DEDENT elif ( nimSum ) : 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 S = "0110011" NEW_LINE FindwinnerOfGame ( S ) NEW_LINE DEDENT
Place first N natural numbers at indices not equal to their values in an array | Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code
def generatepermutation ( N ) : NEW_LINE INDENT answer = [ ] NEW_LINE answer . append ( N ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT answer . append ( i ) NEW_LINE DEDENT print ( * answer ) NEW_LINE DEDENT N = 4 NEW_LINE generatepermutation ( N ) NEW_LINE
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Prthe answer ; Driver Code ; Given N , K & M
def maxValueAtIndexK ( N , K , M ) : NEW_LINE INDENT S1 = 0 ; S2 = 0 ; NEW_LINE S1 = K * ( K + 1 ) // 2 ; NEW_LINE S2 = ( N - K - 1 ) * ( N - K ) // 2 ; NEW_LINE X = ( M + S1 + S2 ) // N ; NEW_LINE print ( X ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; K = 1 ; M = 7 ; NEW_LINE maxValueAtIndexK ( N , K , M ) ; NEW_LINE DEDENT
Farthest cell from a given cell in a Matrix | Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Prthe answer ; Driver Code
def farthestCellDistance ( N , M , R , C ) : NEW_LINE INDENT d1 = N + M - R - C ; NEW_LINE d2 = R + C - 2 ; NEW_LINE d3 = N - R + C - 1 ; NEW_LINE d4 = M - C + R - 1 ; NEW_LINE maxDistance = max ( d1 , max ( d2 , max ( d3 , d4 ) ) ) ; NEW_LINE print ( maxDistance ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 ; NEW_LINE M = 12 ; NEW_LINE R = 1 ; NEW_LINE C = 6 ; NEW_LINE farthestCellDistance ( N , M , R , C ) ; NEW_LINE DEDENT
Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Prthe array ; Driver Code ; Given array ; Size of the array
def maxSum ( arr , N ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT t = i ; NEW_LINE if ( t + arr [ i ] < N ) : NEW_LINE INDENT arr [ i ] += arr [ t + arr [ i ] ] ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 7 , 1 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxSum ( arr , N ) ; NEW_LINE DEDENT
Path from the root node to a given node in an N | Python3 program for the above approach ; Function to find the path from root to N ; Stores the number of nodes at ( i + 1 ) - th level ; Stores the number of nodes ; Stores if the current level is even or odd ; If level is odd ; If level is even ; If level with node N is reached ; Push into vector ; Compute prefix sums of count of nodes in each level ; Stores the level in which node N s present ; Store path ; Insert temp into path ; Print path ; Driver Code ; Function Call
from bisect import bisect_left NEW_LINE def PrintPathNodes ( N ) : NEW_LINE INDENT arr = [ ] NEW_LINE arr . append ( 1 ) NEW_LINE k = 1 NEW_LINE flag = True NEW_LINE while ( k < N ) : NEW_LINE INDENT if ( flag == True ) : NEW_LINE INDENT k *= 2 NEW_LINE flag = False NEW_LINE DEDENT else : NEW_LINE INDENT k *= 4 NEW_LINE flag = True NEW_LINE DEDENT if ( k > N ) : NEW_LINE INDENT break NEW_LINE DEDENT arr . append ( k ) NEW_LINE DEDENT lenn = len ( arr ) NEW_LINE prefix = [ 0 ] * ( lenn ) NEW_LINE prefix [ 0 ] = 1 NEW_LINE for i in range ( 1 , lenn ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] + prefix [ i - 1 ] NEW_LINE DEDENT it = bisect_left ( prefix , N ) NEW_LINE ind = it NEW_LINE temp = N NEW_LINE path = [ ] NEW_LINE path . append ( N ) NEW_LINE while ( ind > 1 ) : NEW_LINE INDENT val = temp - prefix [ ind - 1 ] NEW_LINE if ( ind % 2 != 0 ) : NEW_LINE INDENT temp = prefix [ ind - 2 ] + ( val + 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT temp = prefix [ ind - 2 ] + ( val + 3 ) // 4 NEW_LINE DEDENT ind -= 1 NEW_LINE path . append ( temp ) NEW_LINE DEDENT if ( N != 1 ) : NEW_LINE INDENT path . append ( 1 ) NEW_LINE DEDENT for i in range ( len ( path ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( path [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 14 NEW_LINE PrintPathNodes ( N ) NEW_LINE DEDENT
Generate an N | Python3 program for the above approach ; Function to construct an array with given conditions ; Traverse the array arr [ ] ; Stores closest power of 2 less than or equal to arr [ i ] ; Stores R into brr [ i ] ; Prarray elements ; Driver Code ; Given array ; Size of the array ; Function Call
from math import log NEW_LINE def constructArray ( arr , N ) : NEW_LINE INDENT brr = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT K = int ( log ( arr [ i ] ) / log ( 2 ) ) NEW_LINE R = pow ( 2 , K ) NEW_LINE brr [ i ] = R NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( brr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 5 , 7 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE constructArray ( arr , N ) NEW_LINE DEDENT
Find the element at specified index in a Spiral Matrix | Function to the find element at ( i , j ) index ; Driver Code ; Function Call
def findInGrid ( i , j ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT return ( i * i - ( i - 1 ) ) NEW_LINE DEDENT elif ( i > j ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT return i * i - ( j - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( j % 2 == 0 ) : NEW_LINE INDENT return ( ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return j * j - ( i - 1 ) NEW_LINE DEDENT DEDENT DEDENT i = 3 NEW_LINE j = 4 NEW_LINE print ( findInGrid ( i , j ) ) NEW_LINE
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements | Function to find minimum count of operations required to convert all array elements to zero by replacing subsequence of equal elements to 0 ; Store distinct elements present in the array ; Traverse the array ; If arr [ i ] is already present in the Set or arr [ i ] is equal to 0 ; Otherwise , increment ans by 1 and insert current element ; Given array ; Size of the given array
def minOpsToTurnArrToZero ( arr , N ) : NEW_LINE INDENT st = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i in st . keys ( ) or arr [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT st [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT print ( len ( st ) ) NEW_LINE DEDENT arr = [ 3 , 7 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minOpsToTurnArrToZero ( arr , N ) NEW_LINE
Split an array into equal length subsequences consisting of equal elements only | Python3 program for the above approach ; Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Store the GCD of frequencies of all array elements ; Traverse the map ; Update GCD ; If the GCD is greater than 1 , print YES otherwise print NO ; Driver Code ; Given array ; Store the size of the array
from collections import defaultdict NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def splitArray ( arr , N ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT G = 0 NEW_LINE for i in mp : NEW_LINE INDENT G = gcd ( G , mp [ i ] ) NEW_LINE DEDENT if ( G > 1 ) : 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 = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE splitArray ( arr , n ) NEW_LINE DEDENT
Minimize count of given operations required to be performed to make all array elements equal to 1 | Python 3 program to implement the above approach ; Function to check if all array elements are equal or not ; Traverse the array ; If all array elements are not equal ; Function to find minimum count of operation to make all the array elements equal to 1 ; Stores largest element of the array ; Check if a number is a power of 2 or not ; If Max is a power of 2 and all array elements are equal ; Driver Code
import math NEW_LINE def CheckAllEqual ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ 0 ] != arr [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def minCntOperations ( arr , N ) : NEW_LINE INDENT Max = max ( arr ) NEW_LINE isPower2 = not ( Max and ( Max & ( Max - 1 ) ) ) NEW_LINE if ( isPower2 and CheckAllEqual ( arr , N ) ) : NEW_LINE INDENT return log2 ( Max ) NEW_LINE DEDENT else : NEW_LINE INDENT return math . ceil ( math . log2 ( Max ) ) + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minCntOperations ( arr , N ) ) NEW_LINE DEDENT