codeparrot/xlcost-text-to-code huggingface dataset:
Reduce given array by replacing subarrays with values less than K with their sum | Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Print the result ; Driver Code
def updateArray ( arr , K ) : NEW_LINE INDENT sum = 0 NEW_LINE res = [ ] NEW_LINE for i in range ( 0 , int ( len ( arr ) ) ) : NEW_LINE INDENT if ( arr [ i ] < K ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( sum != 0 ) : NEW_LINE INDENT res . append ( sum ) NEW_LINE DEDENT sum = 0 NEW_LINE res . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT if ( sum != 0 ) : NEW_LINE INDENT res . append ( sum ) NEW_LINE DEDENT for it in res : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 ] NEW_LINE K = 100 NEW_LINE updateArray ( arr , K ) NEW_LINE DEDENT
Find if path length is even or odd between given Tree nodes for Q queries | Python program for the above approach ; Stores the input tree ; Stores the set number of all nodes ; Function to add an edge in the tree ; Function to convert the given tree into a bipartite graph using BFS ; Set the set number to - 1 for all node of the given tree ; Stores the current node during the BFS traversal of the tree ; Initialize the set number of 1 st node and enqueue it ; BFS traversal of the given tree ; Current node ; Traverse over all neighbours of the current node ; If the set is not assigned ; Assign set number to node u ; Function to find if the path length between node A and B is even or odd ; If the set number of both nodes is same , path length is odd else even ; Driver Code ; Function to convert tree into bipartite
from queue import Queue NEW_LINE adj = [ [ 0 ] * 100000 ] * 100000 NEW_LINE setNum = [ 0 ] * 100000 NEW_LINE def addEdge ( a1 , a2 ) : NEW_LINE INDENT adj [ a1 ] . append ( a2 ) ; NEW_LINE adj [ a2 ] . append ( a1 ) ; NEW_LINE DEDENT def toBipartite ( N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT setNum [ i ] = - 1 NEW_LINE DEDENT q = Queue ( ) ; NEW_LINE q . put ( 0 ) ; NEW_LINE setNum [ 0 ] = 0 ; NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT v = q . queue [ 0 ] ; NEW_LINE q . get ( ) ; NEW_LINE for u in adj [ v ] : NEW_LINE INDENT if ( setNum [ u ] == - 1 ) : NEW_LINE INDENT setNum [ u ] = setNum [ v ] ^ 1 ; NEW_LINE q . put ( u ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def pathLengthQuery ( A , B ) : NEW_LINE INDENT if ( setNum [ A ] == setNum [ B ] ) : NEW_LINE INDENT print ( " Odd " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even " ) ; NEW_LINE DEDENT DEDENT N = 7 ; NEW_LINE addEdge ( 0 , 1 ) ; NEW_LINE addEdge ( 0 , 2 ) ; NEW_LINE addEdge ( 1 , 3 ) ; NEW_LINE addEdge ( 3 , 4 ) ; NEW_LINE addEdge ( 3 , 5 ) ; NEW_LINE addEdge ( 2 , 6 ) ; NEW_LINE toBipartite ( N ) ; NEW_LINE pathLengthQuery ( 4 , 2 ) ; NEW_LINE pathLengthQuery ( 0 , 4 ) ; NEW_LINE
Minimum size of set having either element in range [ 0 , X ] or an odd power of 2 with sum N | Python program for the above approach ; Function to find the highest odd power of 2 in the range [ 0 , N ] ; If P is even , subtract 1 ; Function to find the minimum operations to make N ; If N is odd and X = 0 , then no valid set exist ; Stores the minimum possible size of the valid set ; Loop to subtract highest odd power of 2 while X < N , step 2 ; If N > 0 , then increment the value of answer by 1 ; Return the resultant size of set ; Driver Code
import math NEW_LINE def highestPowerof2 ( n ) : NEW_LINE INDENT p = int ( math . log ( n , 2 ) ) NEW_LINE if p % 2 == 0 : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT return int ( pow ( 2 , p ) ) NEW_LINE DEDENT def minStep ( N , X ) : NEW_LINE INDENT if N % 2 and X == 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT size = 0 NEW_LINE while X < N : NEW_LINE INDENT N -= highestPowerof2 ( N ) NEW_LINE size += 1 NEW_LINE DEDENT if N : NEW_LINE INDENT size += 1 NEW_LINE DEDENT return size NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE X = 2 NEW_LINE print ( minStep ( N , X ) ) NEW_LINE DEDENT
Check if all the digits of the given number are same | Function to check if all the digits in the number N is the same or not ; Find the last digit ; Find the current last digit ; Update the value of N ; If there exists any distinct digit , then return No ; Otherwise , return Yes ; Driver Code
def checkSameDigits ( N ) : NEW_LINE INDENT digit = N % 10 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT current_digit = N % 10 ; NEW_LINE N = N // 10 ; NEW_LINE if ( current_digit != digit ) : NEW_LINE INDENT return " No " ; NEW_LINE DEDENT DEDENT return " Yes " ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 222 ; NEW_LINE print ( checkSameDigits ( N ) ) ; NEW_LINE DEDENT
Find number formed by K times alternatively reducing X and adding Y to 0 | Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code
def positionAfterKJumps ( X , Y , K ) : NEW_LINE INDENT addY = Y * ( K // 2 ) NEW_LINE reduceX = - 1 * X * ( K // 2 + K % 2 ) NEW_LINE return addY + reduceX NEW_LINE DEDENT X = 2 NEW_LINE Y = 5 NEW_LINE K = 3 NEW_LINE print ( positionAfterKJumps ( X , Y , K ) ) NEW_LINE
Maximize the largest number K such that bitwise and of K till N is 0 | Function to find maximum value of k which makes bitwise AND zero . ; Take k = N initially ; Start traversing from N - 1 till 0 ; Whenever we get AND as 0 we stop and return ; Driver Code
def findMaxK ( N ) : NEW_LINE INDENT K = N NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT K &= i NEW_LINE if ( K == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( findMaxK ( N ) ) NEW_LINE DEDENT
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all possible pairs ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( arr [ j ] % arr [ i ] == 0 ) and ( j + 1 ) % ( i + 1 ) == 0 and ( arr [ j ] // arr [ i ] == ( j + 1 ) // ( i + 1 ) ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE DEDENT
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices | Function of find the count of unordered pairs ( i , j ) in the array such that arr [ j ] / arr [ i ] = j / i . ; Stores the count of valid pairs ; Iterating over all values of x in range [ 1 , N ] . ; Iterating over all values of y that are divisible by x in the range [ 1 , N ] . ; Check if the pair is valid ; Return answer ; Driver Code ; Function Call
def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT for y in range ( 2 * x , n + 1 , x ) : NEW_LINE INDENT if ( ( arr [ y - 1 ] % arr [ x - 1 ] == 0 ) and ( arr [ y - 1 ] // arr [ x - 1 ] == y // x ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE DEDENT
Number of minimum length paths between 1 to N including each node | Function to calculate the distances from node 1 to N ; Stores the number of edges ; Storing the edges in vector ; Initialize queue ; BFS from 1 st node using queue ; Pop from queue ; Traversing the adjacency list ; Initialize queue ; BFS from last node ; Pop from queue ; Traverse the adjacency list ; Print the count of minimum distance ; Driver Code
def countMinDistance ( n , m , edges ) : NEW_LINE INDENT g = [ [ ] for i in range ( 10005 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT a = edges [ i ] [ 0 ] - 1 NEW_LINE b = edges [ i ] [ 1 ] - 1 NEW_LINE g [ a ] . append ( b ) NEW_LINE g [ b ] . append ( a ) NEW_LINE DEDENT queue1 = [ ] NEW_LINE queue1 . append ( [ 0 , 0 ] ) NEW_LINE dist = [ 1e9 for i in range ( n ) ] NEW_LINE ways1 = [ 0 for i in range ( n ) ] NEW_LINE dist [ 0 ] = 0 NEW_LINE ways1 [ 0 ] = 1 NEW_LINE while ( len ( queue1 ) > 0 ) : NEW_LINE INDENT up = queue1 [ 0 ] NEW_LINE queue1 = queue1 [ : - 1 ] NEW_LINE x = up [ 0 ] NEW_LINE dis = up [ 1 ] NEW_LINE if ( dis > dist [ x ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( x == n - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for y in g [ x ] : NEW_LINE INDENT if ( dist [ y ] > dis + 1 ) : NEW_LINE INDENT dist [ y ] = dis + 1 NEW_LINE ways1 [ y ] = ways1 [ x ] NEW_LINE queue1 . append ( [ y , dis + 1 ] ) NEW_LINE DEDENT elif ( dist [ y ] == dis + 1 ) : NEW_LINE INDENT ways1 [ y ] += ways1 [ x ] NEW_LINE DEDENT DEDENT DEDENT queue2 = [ ] NEW_LINE queue2 . append ( [ n - 1 , 0 ] ) NEW_LINE dist1 = [ 1e9 for i in range ( n ) ] NEW_LINE ways2 = [ 0 for i in range ( n ) ] NEW_LINE dist1 [ n - 1 ] = 0 NEW_LINE ways2 [ n - 1 ] = 1 NEW_LINE while ( len ( queue2 ) > 0 ) : NEW_LINE INDENT up = queue2 [ 0 ] NEW_LINE queue2 = queue2 [ : - 1 ] NEW_LINE x = up [ 0 ] NEW_LINE dis = up [ 1 ] NEW_LINE if ( dis > dist1 [ x ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( x == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for y in g [ x ] : NEW_LINE INDENT if ( dist1 [ y ] > dis + 1 ) : NEW_LINE INDENT dist1 [ y ] = dis + 1 NEW_LINE ways2 [ y ] = ways2 [ x ] NEW_LINE queue2 . append ( [ y , dis + 1 ] ) NEW_LINE DEDENT elif ( dist1 [ y ] == 1 + dis ) : NEW_LINE INDENT ways2 [ y ] += ways2 [ x ] NEW_LINE DEDENT DEDENT DEDENT ways1 [ n - 1 ] = 1 NEW_LINE ways2 [ n - 1 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( ways1 [ i ] * ways2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 5 NEW_LINE edges = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 1 , 3 ] , [ 2 , 5 ] , [ 2 , 4 ] ] NEW_LINE countMinDistance ( N , M , edges ) NEW_LINE DEDENT
Count of N | Python program for the above approach ; Function to find the count of odd and even integers having N bits and K set bits ; Find the count of even integers ; Find the count of odd integers ; Print the total count ; Driver Code
import math NEW_LINE def Binary_Num ( N , K ) : NEW_LINE INDENT if N - K - 1 >= 0 and K - 1 >= 0 : NEW_LINE INDENT num_even = math . factorial ( N - 2 ) / ( math . factorial ( K - 1 ) * math . factorial ( N - K - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT num_even = 0 NEW_LINE DEDENT if K - 2 >= 0 : NEW_LINE INDENT num_odd = math . factorial ( N - 2 ) / ( math . factorial ( K - 2 ) * math . factorial ( N - K ) ) NEW_LINE DEDENT else : NEW_LINE INDENT num_odd = 0 NEW_LINE DEDENT print ( int ( num_even ) , int ( num_odd ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 NEW_LINE K = 6 NEW_LINE Binary_Num ( N , K ) NEW_LINE DEDENT
Sum of all subsets of a given size ( = K ) | Function to find the sum of all sub - sets of size K ; Frequency of each array element in summation equation . ; calculate factorial of n - 1 ; calculate factorial of k - 1 ; calculate factorial of n - k ; Calculate sum of array . ; Sum of all subsets of size k . ; Driver Code
def findSumOfAllSubsets ( arr , n , k ) : NEW_LINE INDENT factorial_N = 1 NEW_LINE factorial_d = 1 NEW_LINE factorial_D = 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT factorial_N *= i NEW_LINE DEDENT for i in range ( 1 , k , 1 ) : NEW_LINE INDENT factorial_d *= i NEW_LINE DEDENT for i in range ( 1 , n - k + 1 , 1 ) : NEW_LINE INDENT factorial_D *= i NEW_LINE DEDENT freq = factorial_N // ( factorial_d * factorial_D ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT sum = sum * freq NEW_LINE print ( " Sum ▁ of ▁ all ▁ subsets ▁ of ▁ size ▁ = ▁ " , k , " ▁ is ▁ = > " , sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 5 ] NEW_LINE n = 4 NEW_LINE k = 2 NEW_LINE findSumOfAllSubsets ( arr , n , k ) NEW_LINE DEDENT
Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code
def maximumTripletXOR ( A , B , C ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 30 , - 1 , - 1 ) : NEW_LINE INDENT cur = 1 << i NEW_LINE if ( A >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE A -= cur NEW_LINE DEDENT elif ( B >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE B -= cur NEW_LINE DEDENT elif ( C >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE C -= cur NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = 6 NEW_LINE B = 2 NEW_LINE C = 10 NEW_LINE print ( maximumTripletXOR ( A , B , C ) ) NEW_LINE
Count of integers in given range having their last K digits are equal | Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call
def intCount ( X , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for z in range ( 0 , int ( pow ( 10 , K ) ) , int ( ( pow ( 10 , K ) - 1 ) / 9 ) ) : NEW_LINE INDENT if ( z > X ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += int ( ( X - z ) / int ( pow ( 10 , K ) ) + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def intCountInRange ( L , R , K ) : NEW_LINE INDENT return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) NEW_LINE DEDENT L = 49 NEW_LINE R = 101 NEW_LINE K = 2 NEW_LINE print ( intCountInRange ( L , R , K ) ) NEW_LINE
Count subsequence of length 4 having product of the first three elements equal to the fourth element | Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code
def countQuadruples ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if A [ i ] in freq : NEW_LINE INDENT ans += freq [ A [ i ] ] NEW_LINE DEDENT else : NEW_LINE INDENT freq [ A [ i ] ] = 0 NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT for k in range ( j ) : NEW_LINE INDENT if A [ i ] * A [ j ] * A [ k ] in freq : NEW_LINE INDENT freq [ A [ i ] * A [ j ] * A [ k ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ A [ i ] * A [ j ] * A [ k ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 2 , 2 , 7 , 40 , 160 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countQuadruples ( arr , N ) ) NEW_LINE DEDENT
Maximum times X and Y can be reduced to near 0 using numbers A or B | Helper function to check if we can perform Mid number of moves ; Remove atleast Mid * B from both X and Y ; If any value is negative return false . ; Calculate remaining values ; If val >= Mid then it is possible to perform this much moves ; else return false ; Initialize a variable to store the answer ; Fix the left and right range ; Binary Search over the answer ; Check for the middle value as the answer ; It is possible to perform this much moves ; Maximise the answer ; Return answer ; Driver Code ; Generalise that A >= B
MAXN = 10000000 NEW_LINE def can ( Mid , X , Y , A , B ) : NEW_LINE INDENT p1 = X - Mid * B NEW_LINE p2 = Y - Mid * B NEW_LINE if ( p1 < 0 or p2 < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT k = A - B NEW_LINE if ( k == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT val = p1 // k + p2 // k NEW_LINE if ( val >= Mid ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def maxPossibleMoves ( X , Y , A , B ) : NEW_LINE INDENT ans = 0 NEW_LINE L = 1 NEW_LINE R = MAXN NEW_LINE while ( L <= R ) : NEW_LINE INDENT Mid = ( L + R ) // 2 NEW_LINE if ( can ( Mid , X , Y , A , B ) ) : NEW_LINE INDENT L = Mid + 1 NEW_LINE ans = max ( ans , Mid ) NEW_LINE DEDENT else : NEW_LINE INDENT R = Mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT X = 10 NEW_LINE Y = 12 NEW_LINE A = 2 NEW_LINE B = 5 NEW_LINE if ( A < B ) : NEW_LINE INDENT tmp = A NEW_LINE A = B NEW_LINE B = tmp NEW_LINE DEDENT print ( maxPossibleMoves ( X , Y , A , B ) ) NEW_LINE
Find two proper factors of N such that their sum is coprime with N | Python Program for the above approach ; Function to find two proper factors of N such that their sum is coprime with N ; Find factors in sorted order ; Find largest value of d2 such that d1 and d2 are co - prime ; Check if d1 and d2 are proper factors of N ; Print answer ; No such factors exist ; Driver code ; Function Call
import math NEW_LINE def printFactors ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT d1 = i NEW_LINE d2 = n NEW_LINE while ( d2 % d1 == 0 ) : NEW_LINE INDENT d2 = d2 // d1 NEW_LINE DEDENT if ( d1 > 1 and d2 > 1 ) : NEW_LINE INDENT print ( d1 , d2 , sep = " , ▁ " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT N = 10 NEW_LINE printFactors ( N ) NEW_LINE
Count of triplets in an Array with odd sum | Function to count the number of unordered triplets such that their sum is an odd integer ; Count the number of odd and even integers in the array ; Number of ways to create triplets using one odd and two even integers ; Number of ways to create triplets using three odd integers ; Return answer ; Driver Code ; Function Call ; Print Answer
def countTriplets ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT c1 = odd * ( even * ( even - 1 ) ) // 2 NEW_LINE c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) // 6 NEW_LINE return c1 + c2 NEW_LINE DEDENT arr = [ 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE ans = countTriplets ( arr , n ) NEW_LINE print ( ans ) NEW_LINE
Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
def maxUniqueElements ( A , N ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if A [ i ] in mp : NEW_LINE INDENT mp [ A [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ A [ i ] ] = 1 NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT if ( value % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ans = len ( mp ) NEW_LINE if ( cnt % 2 == 1 ) : NEW_LINE INDENT ans -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE A = [ 1 , 2 , 1 , 3 , 7 ] NEW_LINE print ( maxUniqueElements ( A , N ) ) NEW_LINE DEDENT
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps | Function to count the number of cells in the grid such that maximum cell is reachable with a jump of K ; Maximum reachable rows from the current row ; Stores the count of cell that are reachable from the current row ; Count of reachable rows ; Update the maximum value ; Add it to the count ; Maximum reachable columns from the current column ; Stores the count of cell that are reachable from the current column ; Count of rechable columns ; Update the maximum value ; Add it to the count ; Return the total count of cells ; Driver Code
def countCells ( n , m , s ) : NEW_LINE INDENT mx1 = - 1 NEW_LINE cont1 = 0 NEW_LINE i = 0 NEW_LINE while ( i < s and i < n ) : NEW_LINE INDENT aux = ( n - ( i + 1 ) ) // s + 1 NEW_LINE if ( aux > mx1 ) : NEW_LINE INDENT mx1 = cont1 = aux NEW_LINE DEDENT elif ( aux == mx1 ) : NEW_LINE INDENT cont1 += aux NEW_LINE DEDENT i += 1 NEW_LINE DEDENT mx2 = - 1 NEW_LINE cont2 = 0 NEW_LINE i = 0 NEW_LINE while ( i < s and i < m ) : NEW_LINE INDENT aux = ( m - ( i + 1 ) ) // s + 1 NEW_LINE if ( aux > mx2 ) : NEW_LINE INDENT mx2 = cont2 = aux NEW_LINE DEDENT elif ( aux == mx2 ) : NEW_LINE INDENT cont2 += aux NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return cont1 * cont2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 5 NEW_LINE K = 2 NEW_LINE print ( countCells ( N , M , K ) ) NEW_LINE DEDENT
Count of pairs from first N natural numbers with remainder at least K | Function to count the number of pairs ( a , b ) such that a % b is at least K ; Base Case ; Stores resultant count of pairs ; Iterate over the range [ K + 1 , N ] ; Find the cycled elements ; Find the remaining elements ; Return the resultant possible count of pairs ; Driver Code
def countTotalPairs ( N , K ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return N * N NEW_LINE DEDENT ans = 0 NEW_LINE for b in range ( K + 1 , N + 1 ) : NEW_LINE INDENT ans += ( N // b ) * ( b - K ) NEW_LINE ans += max ( N % b - K + 1 , 0 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 5 NEW_LINE K = 2 NEW_LINE print ( countTotalPairs ( N , K ) ) NEW_LINE
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code
def minSumDifference ( ar , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ar [ i ] NEW_LINE DEDENT rem = sum % n NEW_LINE return rem * ( n - rem ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minSumDifference ( arr , N ) ) NEW_LINE DEDENT