codeparrot/xlcost-text-to-code huggingface dataset:
Minimum decrements required to make all pairs of adjacent matrix elements distinct | Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; The given matrix ; Function Call to count the minimum number of decrements required
n = 3 NEW_LINE m = 3 NEW_LINE def countDecrements ( arr ) : NEW_LINE INDENT count_1 = 0 NEW_LINE count_2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) : NEW_LINE INDENT count_2 += 1 NEW_LINE DEDENT DEDENT DEDENT print ( min ( count_1 , count_2 ) ) NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ] NEW_LINE countDecrements ( arr ) NEW_LINE
Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code
def check ( X , Y ) : NEW_LINE INDENT if ( X > 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 1 and Y == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 2 and Y <= 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 3 and Y <= 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 6 NEW_LINE Y = 8 NEW_LINE check ( X , Y ) NEW_LINE DEDENT
Count distinct sum of pairs possible from a given range | Function to count distinct sum of pairs possible from the range [ L , R ] ; Return the count of distinct sum of pairs ; Driver Code
def distIntegers ( L , R ) : NEW_LINE INDENT return 2 * R - 2 * L + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 3 , 8 NEW_LINE print ( distIntegers ( L , R ) ) NEW_LINE DEDENT
Count subarrays having even Bitwise XOR | Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Given array ; Stores the size of the array
def evenXorSubarray ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE freq = [ 0 ] * n NEW_LINE XOR = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT XOR = XOR ^ arr [ i ] NEW_LINE if ( XOR % 2 == 0 ) : NEW_LINE INDENT ans += freq [ 0 ] + 1 NEW_LINE freq [ 0 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += freq [ 1 ] NEW_LINE freq [ 1 ] += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE evenXorSubarray ( arr , N ) NEW_LINE
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Python3 program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Driver Code ; Given N and X ; Function Call
from math import sqrt NEW_LINE def countOccurrences ( N , X ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , int ( sqrt ( X ) ) + 1 ) : NEW_LINE INDENT if X % i == 0 : NEW_LINE INDENT a = i NEW_LINE b = X // i NEW_LINE if a <= N and b <= N : NEW_LINE INDENT if a == b : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 2 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE X = 12 NEW_LINE print ( countOccurrences ( N , X ) ) NEW_LINE DEDENT
Maximize array product by changing any array element arr [ i ] to ( | Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Prthe elements of the array ; Driver Code ; Function Call
def findArrayWithMaxProduct ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= 0 ) : NEW_LINE INDENT arr [ i ] = - arr [ i ] - 1 NEW_LINE DEDENT DEDENT if ( N % 2 == 1 ) : NEW_LINE INDENT max_element = - 1 NEW_LINE index = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) > max_element ) : NEW_LINE INDENT max_element = abs ( arr [ i ] ) NEW_LINE index = i NEW_LINE DEDENT DEDENT arr [ index ] = - arr [ index ] - 1 NEW_LINE DEDENT for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 3 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE findArrayWithMaxProduct ( arr , N ) NEW_LINE DEDENT
Lexicographically smallest permutation of first N natural numbers having K perfect indices | Function to print the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
def findPerfectIndex ( N , K ) : NEW_LINE INDENT i = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT print ( ( N - K + 1 ) + i , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 3 , N ) : NEW_LINE INDENT print ( i - K + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE K = 3 NEW_LINE findPerfectIndex ( N , K ) NEW_LINE
Count pairs having distinct sum from a given range | Function to count pairs made up of elements from the range [ L , R ] having distinct sum ; Stores the least sum which can be formed by the pairs ; Stores the highest sum which can be formed by the pairs ; Stores the count of pairs having distinct sum ; Print the count of pairs ; Driver Code ; Function call to count the number of pairs having distinct sum in the range [ L , R ]
def countPairs ( L , R ) : NEW_LINE INDENT firstNum = 2 * L NEW_LINE lastNum = 2 * R NEW_LINE Cntpairs = lastNum - firstNum + 1 NEW_LINE print ( Cntpairs ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 2 , 3 NEW_LINE countPairs ( L , R ) NEW_LINE DEDENT
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Python 3 program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE def countPairs ( A , n ) : NEW_LINE INDENT count = ( n * ( n - 1 ) ) // 2 NEW_LINE ump = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ump [ A [ i ] ] += 1 NEW_LINE DEDENT for it in ump . keys ( ) : NEW_LINE INDENT c = ump [ it ] NEW_LINE count = count - ( c * ( c - 1 ) ) // 2 NEW_LINE 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
Construct a K | Function to construct binary string according to the given conditions ; Initialize with 1 ; Traverse the array ; To check if the i - th eger needs to be considered or not ; Print the binary string for i in range ( 1 , K ) : print ( bit [ i ] ) ; Given array ; Size of the array ; Given K
def constructBinaryString ( arr , N , K ) : NEW_LINE INDENT bit = 1 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT bit |= bit << arr [ i ] NEW_LINE DEDENT bit = bin ( bit ) . replace ( "0b " , " " ) NEW_LINE print ( bit [ 1 : K + 1 ] ) NEW_LINE DEDENT arr = [ 1 , 6 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 8 NEW_LINE constructBinaryString ( arr , N , K ) NEW_LINE
Check if a Rook can reach the given destination in a single move | Function to check if it is possible to reach destination in a single move by a rook ; Given arrays
def check ( current_row , current_col , destination_row , destination_col ) : NEW_LINE INDENT if ( current_row == destination_row ) : NEW_LINE INDENT return ( " POSSIBLE " ) NEW_LINE DEDENT elif ( current_col == destination_col ) : NEW_LINE INDENT return ( " POSSIBLE " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " NOT ▁ POSSIBLE " ) NEW_LINE DEDENT DEDENT current_row = 8 NEW_LINE current_col = 8 NEW_LINE destination_row = 8 NEW_LINE destination_col = 4 NEW_LINE output = check ( current_row , current_col , destination_row , destination_col ) NEW_LINE print ( output ) NEW_LINE
Append X digits to the end of N to make it divisible by M | Base case ; If N is divisible by M ; global variable to store result ; Iterate over the range [ 0 , 9 ] ; if N is Divisible by M upon appending X digits ; Driver Code ; Stores the number by appending X digits on the right side of N
if ( X == 0 ) : NEW_LINE INDENT if ( N % M == 0 ) : NEW_LINE INDENT global res NEW_LINE res = N NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT res = - 1 NEW_LINE def isDiv ( N , X , M ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT if ( isDiv ( N * 10 + i , X - 1 , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , X = 4 , 50 , 2 NEW_LINE if ( isDiv ( N , X , M ) ) : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | Function to count minimum moves ; Stores sum of given array ; Stores maximum array element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver Code ; Given array ; Size of given array ; Function Call
def minimumMoves ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE maxelement = - 1 NEW_LINE if ( N == 2 ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE maxelement = max ( maxelement , arr [ i ] ) NEW_LINE DEDENT K = ( sum + N - 2 ) // ( N - 1 ) NEW_LINE K = max ( maxelement , K ) NEW_LINE ans = K * ( N - 1 ) - sum NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 7 ] NEW_LINE N = 3 NEW_LINE minimumMoves ( arr , N ) NEW_LINE DEDENT
Generate an array with product of all subarrays of length exceeding one divisible by K | Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
def array_divisbleby_k ( N , K ) : NEW_LINE INDENT flag = False NEW_LINE d1 , d2 = 0 , 0 NEW_LINE for i in range ( 2 , int ( K ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( K % i == 0 ) : NEW_LINE INDENT flag = True NEW_LINE d1 = i NEW_LINE d2 = K // i NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT print ( d2 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( d1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE K = 21 NEW_LINE array_divisbleby_k ( N , K ) NEW_LINE DEDENT
Minimum characters to be replaced to make a string concatenation of a K | Python3 program for the above approach ; Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given string ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver Code ; Function Call
import sys NEW_LINE sys . setrecursionlimit ( 1500 ) NEW_LINE def addEdge ( u , v ) : NEW_LINE INDENT global adj NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT def DFS ( u , fre , S ) : NEW_LINE INDENT global visited , adj , cnt NEW_LINE visited [ u ] = 1 NEW_LINE cnt += 1 NEW_LINE fre [ ord ( S [ u ] ) - ord ( ' a ' ) ] += 1 NEW_LINE for i in adj [ u ] : NEW_LINE INDENT if ( visited [ i ] == 0 ) : NEW_LINE INDENT DFS ( i , fre , S ) NEW_LINE DEDENT DEDENT DEDENT def minimumOperations ( S , m ) : NEW_LINE INDENT global adj , visited , cnt NEW_LINE total , N = 0 , len ( S ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT addEdge ( i , N - i - 1 ) NEW_LINE addEdge ( N - i - 1 , i ) NEW_LINE DEDENT for i in range ( N - m ) : NEW_LINE INDENT addEdge ( i , i + m ) NEW_LINE addEdge ( i + m , i ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT fre = [ 0 ] * 26 NEW_LINE cnt , maxx = 0 , - 1 NEW_LINE DFS ( i , fre , S ) NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT maxx = max ( maxx , fre [ j ] ) NEW_LINE DEDENT total += cnt - maxx NEW_LINE DEDENT DEDENT print ( total ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT adj = [ [ ] for i in range ( 101 ) ] NEW_LINE visited , cnt = [ 0 for i in range ( 101 ) ] , 0 NEW_LINE S = " abaaba " NEW_LINE K = 2 NEW_LINE minimumOperations ( S , K ) NEW_LINE DEDENT
Replace specified matrix elements such that no two adjacent elements are equal | Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Given N and M ; Given matrix ; Function call
def Print ( arr , n , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT a = arr [ i ] [ j ] NEW_LINE if ( ( i + j ) % 2 == 0 and a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '1' NEW_LINE DEDENT elif ( a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '2' NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n , m = 4 , 4 NEW_LINE arr = [ [ ' F ' , ' F ' , ' F ' , ' F ' ] , [ ' F ' , ' O ' , ' F ' , ' F ' ] , [ ' F ' , ' F ' , ' O ' , ' F ' ] , [ ' F ' , ' F ' , ' F ' , ' F ' ] ] NEW_LINE Print ( arr , n , m ) NEW_LINE
Lexicographically smallest string formed by removing duplicates | Function that finds lexicographically smallest after removing the duplicates from the given string ; Stores the frequency of characters ; Mark visited characters ; Stores count of each character ; Stores the resultant string ; Decrease the count of current character ; If character is not already in answer ; Last character > S [ i ] and its count > 0 ; Mark letter unvisited ; Add s [ i ] in res and mark it visited ; Return the resultant string ; Driver Code ; Given S ; Function Call
def removeDuplicateLetters ( s ) : NEW_LINE INDENT cnt = [ 0 ] * 26 NEW_LINE vis = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in s : NEW_LINE INDENT cnt [ ord ( i ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT res = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE if ( not vis [ ord ( s [ i ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT while ( len ( res ) > 0 and res [ - 1 ] > s [ i ] and cnt [ ord ( res [ - 1 ] ) - ord ( ' a ' ) ] > 0 ) : NEW_LINE INDENT vis [ ord ( res [ - 1 ] ) - ord ( ' a ' ) ] = 0 NEW_LINE del res [ - 1 ] NEW_LINE DEDENT res += s [ i ] NEW_LINE vis [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 NEW_LINE DEDENT DEDENT return " " . join ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " acbc " NEW_LINE print ( removeDuplicateLetters ( S ) ) NEW_LINE DEDENT
Number of pairs whose product is a power of 2 | Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function call
def countPairs ( arr , N ) : NEW_LINE INDENT countPowerof2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( bin ( arr [ i ] ) . count ( '1' ) == 1 ) : NEW_LINE INDENT countPowerof2 += 1 NEW_LINE DEDENT DEDENT desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) // 2 NEW_LINE print ( desiredPairs ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 7 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Count ways to split N ! into two distinct co | Python3 program for the above approach ; Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; Mark all its multiples as non - prime ; Count primes <= MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into co - prime factors ; Driver Code ; Calling sieve function ; Given N ; Function call
import math NEW_LINE MAXN = 1000000 NEW_LINE is_prime = [ 0 ] * MAXN NEW_LINE count_of_primes = [ 0 ] * MAXN NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 3 , MAXN , 2 ) : NEW_LINE INDENT is_prime [ i ] = 1 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( MAXN ) ) , 2 ) : NEW_LINE INDENT if is_prime [ i ] : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT is_prime [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT is_prime [ 2 ] = 1 NEW_LINE for i in range ( 1 , MAXN ) : NEW_LINE INDENT count_of_primes [ i ] = ( count_of_primes [ i - 1 ] + is_prime [ i ] ) NEW_LINE DEDENT DEDENT def power ( x , y , p ) : NEW_LINE INDENT result = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if y & 1 == 1 : NEW_LINE INDENT result = ( result * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE y >>= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def numberOfWays ( N ) : NEW_LINE INDENT count = count_of_primes [ N ] - 1 NEW_LINE mod = 1000000007 NEW_LINE answer = power ( 2 , count , mod ) NEW_LINE if N == 1 : NEW_LINE INDENT answer = 0 NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) NEW_LINE N = 7 NEW_LINE numberOfWays ( N ) NEW_LINE DEDENT
Maximum possible sum of squares of stack elements satisfying the given properties | Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Print the result ; Driver Code ; Function call
def maxSumOfSquares ( N , S ) : NEW_LINE INDENT res = 0 NEW_LINE if ( S < N or S > 9 * N ) : NEW_LINE INDENT cout << - 1 ; NEW_LINE return NEW_LINE DEDENT S = S - N NEW_LINE c = 0 NEW_LINE while ( S > 0 ) : NEW_LINE INDENT c += 1 NEW_LINE if ( S // 8 > 0 ) : NEW_LINE INDENT res += 9 * 9 NEW_LINE S -= 8 NEW_LINE DEDENT else : NEW_LINE INDENT res += ( S + 1 ) * ( S + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT res = res + ( N - c ) NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE S = 12 NEW_LINE maxSumOfSquares ( N , S ) NEW_LINE DEDENT
Unset least significant K bits of a given number | Function to return the value after unsetting K LSBs ; Create a mask ; Bitwise AND operation with the number and the mask ; Given N and K ; Function call
def clearLastBit ( N , K ) : NEW_LINE INDENT mask = ( - 1 << K + 1 ) NEW_LINE N = N & mask NEW_LINE return N NEW_LINE DEDENT N = 730 NEW_LINE K = 3 NEW_LINE print ( clearLastBit ( N , K ) ) NEW_LINE
Check if quantities of 3 distinct colors can be converted to a single color by given merge | Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Given colors ; Function call
def isPossible ( r , b , g ) : NEW_LINE INDENT r = r % 3 NEW_LINE b = b % 3 NEW_LINE g = g % 3 NEW_LINE if ( r == b or b == g or g == r ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT R = 1 NEW_LINE B = 3 NEW_LINE G = 6 NEW_LINE if ( isPossible ( R , B , G ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Minimize steps required to make all array elements same by adding 1 , 2 or 5 | Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code
def calculate_steps ( arr , n , minimum ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = arr [ i ] NEW_LINE if ( arr [ i ] > minimum ) : NEW_LINE INDENT arr [ i ] = arr [ i ] - minimum NEW_LINE count += arr [ i ] // 5 NEW_LINE arr [ i ] = arr [ i ] % 5 NEW_LINE count += arr [ i ] // 2 NEW_LINE arr [ i ] = arr [ i ] % 2 NEW_LINE if ( arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT arr [ i ] = val NEW_LINE DEDENT return count NEW_LINE DEDENT def solve ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE minimum = arr [ n - 1 ] NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE count3 = 0 NEW_LINE count1 = calculate_steps ( arr , n , minimum ) NEW_LINE count2 = calculate_steps ( arr , n , minimum - 1 ) NEW_LINE count3 = calculate_steps ( arr , n , minimum - 2 ) NEW_LINE return min ( count1 , min ( count2 , count3 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( solve ( arr , N ) ) NEW_LINE DEDENT