codeparrot/xlcost-text-to-code huggingface dataset:
Maximize subsequences having array elements not exceeding length of the subsequence | Python3 program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of arr [ ] ; Update the frequency ; Print the number of subsequences ; Driver Code ; Given array arr [ ] ; Function Call
from collections import defaultdict NEW_LINE def No_Of_subsequences ( mp ) : NEW_LINE INDENT count = 0 NEW_LINE left = 0 NEW_LINE for x in mp : NEW_LINE INDENT mp [ x ] += left NEW_LINE count += ( mp [ x ] // x ) NEW_LINE left = mp [ x ] % x NEW_LINE DEDENT return count NEW_LINE DEDENT def maximumsubsequences ( 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 print ( No_Of_subsequences ( mp ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE maximumsubsequences ( arr , N ) NEW_LINE DEDENT
Check if concatenation of any permutation of given list of arrays generates the given array | Python3 program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function call
from array import * NEW_LINE def check ( arr , pieces ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT m [ arr [ i ] ] = i + 1 NEW_LINE DEDENT for i in range ( 0 , len ( pieces ) ) : NEW_LINE INDENT if ( len ( pieces [ i ] ) == 1 and m [ pieces [ i ] [ 0 ] ] != 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( len ( pieces [ i ] ) > 1 and m [ pieces [ i ] [ 0 ] ] != 0 ) : NEW_LINE INDENT idx = m [ pieces [ i ] [ 0 ] ] - 1 NEW_LINE idx = idx + 1 NEW_LINE if idx >= len ( arr ) : NEW_LINE INDENT return False NEW_LINE DEDENT for j in range ( 1 , len ( pieces [ i ] ) ) : NEW_LINE INDENT if arr [ idx ] == pieces [ i ] [ j ] : NEW_LINE INDENT idx = idx + 1 NEW_LINE if ( idx >= len ( arr ) and j < len ( pieces [ i ] ) - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 3 ] NEW_LINE pieces = [ [ 1 ] , [ 4 , 3 ] , [ 2 ] ] NEW_LINE if check ( arr , pieces ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Modify given array to make sum of odd and even indexed elements same | Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Given array arr [ ] ; Function call
def makeArraySumEqual ( a , N ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE odd_sum = 0 NEW_LINE even_sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if ( ( i + 1 ) % 2 == 0 ) : NEW_LINE INDENT even_sum += a [ i ] NEW_LINE DEDENT elif ( ( i + 1 ) % 2 > 0 ) : NEW_LINE INDENT odd_sum += a [ i ] NEW_LINE DEDENT DEDENT if ( odd_sum == even_sum ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( count_0 >= N / 2 ) : NEW_LINE INDENT for i in range ( count_0 ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT is_Odd = count_1 % 2 NEW_LINE count_1 -= is_Odd NEW_LINE for i in range ( count_1 ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 1 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE makeArraySumEqual ( arr , N ) NEW_LINE
Minimum replacement of pairs by their LCM required to reduce given array to its LCM | Python3 program for the above approach ; Boolean array to set or unset prime non - prime indices ; Stores the prefix sum of the count of prime numbers ; Function to check if a number is prime or not from 0 to N ; If p is a prime ; Set its multiples as non - prime ; Function to store the count of prime numbers ; Function to count the operations to reduce the array to one element by replacing each pair with its LCM ; Generating Prime Number ; Corner Case ; Driver code ; Given array arr ; Function call
maxm = 10001 ; NEW_LINE prime = [ True ] * ( maxm + 1 ) ; NEW_LINE prime_number = [ 0 ] * ( maxm + 1 ) ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , ( int ( maxm ** 1 / 2 ) ) ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , maxm , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT prime [ 0 ] = False ; NEW_LINE prime [ 1 ] = False ; NEW_LINE DEDENT def num_prime ( ) : NEW_LINE INDENT prime_number [ 0 ] = 0 ; NEW_LINE for i in range ( 1 , maxm + 1 ) : NEW_LINE INDENT tmp = - 1 ; NEW_LINE if ( prime [ i ] == True ) : NEW_LINE INDENT tmp = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT tmp = 0 ; NEW_LINE DEDENT prime_number [ i ] = prime_number [ i - 1 ] + tmp ; NEW_LINE DEDENT DEDENT def min_steps ( arr , n ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE num_prime ( ) ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT print ( "0" ) ; NEW_LINE DEDENT elif ( n == 2 ) : NEW_LINE INDENT print ( "1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( prime_number [ n ] - 1 + ( n - 2 ) ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 3 , 2 , 1 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE min_steps ( arr , N ) ; NEW_LINE DEDENT
Find the winner of a game of removing any number of stones from the least indexed non | Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Driver Code ; Given piles of stone ; Function call
def findWinner ( a , n ) : NEW_LINE INDENT win = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT win = 1 NEW_LINE DEDENT else : NEW_LINE INDENT win = 0 NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT if ( win == 0 and a [ i ] > 1 ) : NEW_LINE INDENT win = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( win == 1 and a [ i ] > 1 ) : NEW_LINE INDENT win = 0 NEW_LINE DEDENT DEDENT DEDENT if ( win == 0 ) : NEW_LINE INDENT print ( " A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE findWinner ( arr , N ) NEW_LINE DEDENT
Check if a destination is reachable from source with two movements allowed | Set 2 | Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code
def isReachable ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT while ( x2 > x1 and y2 > y1 ) : NEW_LINE INDENT if ( x2 > y2 ) : NEW_LINE INDENT x2 %= y2 NEW_LINE DEDENT else : NEW_LINE INDENT y2 %= x2 NEW_LINE DEDENT DEDENT if ( x2 == x1 ) : NEW_LINE INDENT return ( y2 - y1 ) >= 0 and ( y2 - y1 ) % x1 == 0 NEW_LINE DEDENT elif ( y2 == y1 ) : NEW_LINE INDENT return ( x2 - x1 ) >= 0 and ( x2 - x1 ) % y1 == 0 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT source_x = 2 NEW_LINE source_y = 10 NEW_LINE dest_x = 26 NEW_LINE dest_y = 12 NEW_LINE if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the player who wins the game by removing the last of given N cards | Function to check which player can win the game ; Driver Code
def checkWinner ( N , K ) : NEW_LINE INDENT if ( N % ( K + 1 ) ) : NEW_LINE INDENT print ( " A " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " ) NEW_LINE DEDENT DEDENT N = 50 NEW_LINE K = 10 NEW_LINE checkWinner ( N , K ) NEW_LINE
Length of longest subarray having only K distinct Prime Numbers | Python3 program to implement the above approach ; Function to precalculate all the prime up to 10 ^ 6 ; Initialize prime to true ; Iterate [ 2 , sqrt ( N ) ] ; If p is prime ; Mark all multiple of p as true ; Function that finds the length of longest subarray K distinct primes ; Precompute all prime up to 2 * 10 ^ 6 ; Keep track occurrence of prime ; Initialize result to - 1 ; If number is prime then increment its count and decrease k ; Decrement K ; Remove required elements till k become non - negative ; Decrease count so that it may appear in another subarray appearing after this present subarray ; Increment K ; Take the max value as length of subarray ; Return the final length ; Given array arr [ ] ; Function call
from collections import defaultdict NEW_LINE isprime = [ True ] * 2000010 NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT isprime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isprime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT isprime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def KDistinctPrime ( arr , n , k ) : NEW_LINE INDENT SieveOfEratosthenes ( 2000000 ) NEW_LINE cnt = defaultdict ( lambda : 0 ) NEW_LINE result = - 1 NEW_LINE j = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE if ( isprime [ x ] ) : NEW_LINE INDENT cnt [ x ] += 1 NEW_LINE if ( cnt [ x ] == 1 ) : NEW_LINE INDENT k -= 1 NEW_LINE DEDENT DEDENT DEDENT while ( k < 0 ) : NEW_LINE INDENT j += 1 NEW_LINE x = arr [ j ] NEW_LINE if ( isprime [ x ] ) : NEW_LINE INDENT cnt [ x ] -= 1 NEW_LINE if ( cnt [ x ] == 0 ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT if ( k == 0 ) : NEW_LINE INDENT result = max ( result , i - j ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( KDistinctPrime ( arr , N , K ) ) NEW_LINE
Smallest composite number not divisible by first N prime numbers | Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
MAX_SIZE = 1000005 NEW_LINE def SieveOfEratosthenes ( StorePrimes ) : NEW_LINE INDENT IsPrime = [ True for i in range ( MAX_SIZE ) ] NEW_LINE p = 2 NEW_LINE while ( p * p < MAX_SIZE ) : NEW_LINE INDENT if ( IsPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , MAX_SIZE , p ) : NEW_LINE INDENT IsPrime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , MAX_SIZE ) : NEW_LINE INDENT if ( IsPrime [ p ] ) : NEW_LINE INDENT StorePrimes . append ( p ) NEW_LINE DEDENT DEDENT DEDENT def Smallest_non_Prime ( StorePrimes , N ) : NEW_LINE INDENT x = StorePrimes [ N ] NEW_LINE return x * x NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE StorePrimes = [ ] NEW_LINE SieveOfEratosthenes ( StorePrimes ) NEW_LINE print ( Smallest_non_Prime ( StorePrimes , N ) ) NEW_LINE DEDENT
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Python3 program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; Update N ; Print the subset ; Driver Code
import math NEW_LINE def printSubset ( N , K ) : NEW_LINE INDENT answer = " " NEW_LINE while ( N > 0 ) : NEW_LINE INDENT p = int ( math . log ( N , 2 ) ) NEW_LINE answer = str ( K ** p ) + " ▁ " + answer NEW_LINE N = N % ( 2 ** p ) NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT N = 5 NEW_LINE K = 4 NEW_LINE printSubset ( N , K ) NEW_LINE
Count total set bits in all numbers from range L to R | Returns position of leftmost set bit The rightmost position is taken as 0 ; Function that gives the position of previous leftmost set bit in n ; Function that returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n ; Use the position ; Function to count the set bits between the two numbers N and M ; Base Case ; Get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 ; Update n for next recursive call ; Function that counts the set bits between L and R ; Driver Code ; Given L and R ; Function Call
def getLeftmostBit ( n ) : NEW_LINE INDENT m = 0 ; NEW_LINE while ( n > 1 ) : NEW_LINE INDENT n = n >> 1 ; NEW_LINE m += 1 ; NEW_LINE DEDENT return m ; NEW_LINE DEDENT def getNextLeftmostBit ( n , m ) : NEW_LINE INDENT temp = 1 << m ; NEW_LINE while ( n < temp ) : NEW_LINE INDENT temp = temp >> 1 ; NEW_LINE m -= 1 ; NEW_LINE DEDENT return m ; NEW_LINE DEDENT def countSetBit ( n ) : NEW_LINE INDENT m = getLeftmostBit ( n ) ; NEW_LINE return _countSetBit ( n , m ) ; NEW_LINE DEDENT def _countSetBit ( n , m ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT m = getNextLeftmostBit ( n , m ) ; NEW_LINE if ( n == int ( 1 << ( m + 1 ) ) - 1 ) : NEW_LINE INDENT return int ( m + 1 ) * ( 1 << m ) ; NEW_LINE DEDENT n = n - ( 1 << m ) ; NEW_LINE return ( ( n + 1 ) + countSetBit ( n ) + m * ( 1 << ( m - 1 ) ) ) ; NEW_LINE DEDENT def countSetBits ( L , R ) : NEW_LINE INDENT return abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 3 ; NEW_LINE R = 5 ; NEW_LINE print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " , countSetBits ( L , R ) ) ; NEW_LINE DEDENT
Permutation of Array such that products of all adjacent elements are even | Function to print the required permutation ; push odd elements in ' odd ' and even elements in 'even ; Check if it possible to arrange the elements ; else print the permutation ; Print remaining odds are even . and even elements ; Driver Code
def printPermutation ( arr , n ) : NEW_LINE INDENT odd , even = [ ] , [ ] NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT odd . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT size_odd = len ( odd ) NEW_LINE size_even = len ( even ) NEW_LINE if ( size_odd > size_even + 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE while ( i < size_odd and j < size_even ) : NEW_LINE INDENT print ( odd [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE print ( even [ j ] , end = " ▁ " ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while ( i < size_odd ) : NEW_LINE INDENT print ( odd [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT while ( j < size_even ) : NEW_LINE INDENT print ( even [ j ] , end = " ▁ " ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT arr = [ 6 , 7 , 9 , 8 , 10 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE printPermutation ( arr , N ) NEW_LINE
Maximize GCD of all possible pairs from 1 to N | Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver Code ; Function call
def find ( n ) : NEW_LINE INDENT print ( n // 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE find ( n ) NEW_LINE DEDENT
Count of pairs of Array elements which are divisible by K when concatenated | Python3 program to count pairs of array elements which are divisible by K when concatenated ; Function to calculate and return the count of pairs ; Compute power of 10 modulo k ; Calculate length of a [ i ] ; Increase count of remainder ; Calculate ( a [ i ] * 10 ^ lenj ) % k ; Calculate ( k - ( a [ i ] * 10 ^ lenj ) % k ) % k ; Increase answer by count ; If a pair ( a [ i ] , a [ i ] ) is counted ; Return the count of pairs ; Driver Code
rem = [ [ 0 for x in range ( 11 ) ] for y in range ( 11 ) ] NEW_LINE def countPairs ( a , n , k ) : NEW_LINE INDENT l = [ 0 ] * n NEW_LINE p = [ 0 ] * ( 11 ) NEW_LINE p [ 0 ] = 1 NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT p [ i ] = ( p [ i - 1 ] * 10 ) % k NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT x = a [ i ] NEW_LINE while ( x > 0 ) : NEW_LINE INDENT l [ i ] += 1 NEW_LINE x //= 10 NEW_LINE DEDENT rem [ l [ i ] ] [ a [ i ] % k ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , 11 ) : NEW_LINE INDENT r = ( a [ i ] * p [ j ] ) % k NEW_LINE xr = ( k - r ) % k NEW_LINE ans += rem [ j ] [ xr ] NEW_LINE if ( l [ i ] == j and ( r + a [ i ] % k ) % k == 0 ) : NEW_LINE INDENT ans -= 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 4 , 5 , 2 ] NEW_LINE n = len ( a ) NEW_LINE k = 2 NEW_LINE print ( countPairs ( a , n , k ) ) NEW_LINE
Count of N digit Numbers whose sum of every K consecutive digits is equal | Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Driver Code ; Given integer N and K
def countDigitSum ( N , K ) : NEW_LINE INDENT l = pow ( 10 , N - 1 ) ; NEW_LINE r = pow ( 10 , N ) - 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT num = i ; NEW_LINE digits = [ 0 ] * ( N ) ; NEW_LINE for j in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT digits [ j ] = num % 10 ; NEW_LINE num //= 10 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE flag = 0 ; NEW_LINE for j in range ( 0 , K ) : NEW_LINE INDENT sum += digits [ j ] ; NEW_LINE DEDENT for j in range ( K , N ) : NEW_LINE INDENT if ( sum - digits [ j - K ] + digits [ j ] != sum ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE K = 1 ; NEW_LINE print ( countDigitSum ( N , K ) ) ; NEW_LINE DEDENT
Array formed using sum of absolute differences of that element with all other elements | Function to return the new array private static List < Integer > ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function call
def calculate ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE ans = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( len ( arr ) ) : NEW_LINE INDENT sum += abs ( arr [ i ] - arr [ j ] ) NEW_LINE DEDENT ans . append ( sum ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 6 ] NEW_LINE print ( calculate ( arr ) ) NEW_LINE DEDENT
Number of pair of positions in matrix which are not accessible | Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driver Code
def dfs ( graph , visited , x , k ) : NEW_LINE INDENT for i in range ( len ( graph [ x ] ) ) : NEW_LINE INDENT if ( not visited [ graph [ x ] [ i ] ] ) : NEW_LINE INDENT k [ 0 ] += 1 NEW_LINE visited [ graph [ x ] [ i ] ] = True NEW_LINE dfs ( graph , visited , graph [ x ] [ i ] , k ) NEW_LINE DEDENT DEDENT DEDENT def countNonAccessible ( graph , N ) : NEW_LINE INDENT visited = [ False ] * ( N * N + N ) NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , N * N + 1 ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT visited [ i ] = True NEW_LINE k = [ 1 ] NEW_LINE dfs ( graph , visited , i , k ) NEW_LINE ans += k [ 0 ] * ( N * N - k [ 0 ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def insertpath ( graph , N , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT a = ( x1 - 1 ) * N + y1 NEW_LINE b = ( x2 - 1 ) * N + y2 NEW_LINE graph [ a ] . append ( b ) NEW_LINE graph [ b ] . append ( a ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE graph = [ [ ] for i in range ( N * N + 1 ) ] NEW_LINE insertpath ( graph , N , 1 , 1 , 1 , 2 ) NEW_LINE insertpath ( graph , N , 1 , 2 , 2 , 2 ) NEW_LINE print ( countNonAccessible ( graph , N ) ) NEW_LINE DEDENT
Minimum number of distinct powers of 2 required to express a given binary number | Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; Driver Code
def findMinimum ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE x = [ 0 ] * ( n + 1 ) NEW_LINE s = s [ : : - 1 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT if ( x [ i ] == 1 ) : NEW_LINE INDENT x [ i + 1 ] = 1 NEW_LINE x [ i ] = 0 NEW_LINE DEDENT elif ( i and x [ i - 1 ] == 1 ) : NEW_LINE INDENT x [ i + 1 ] = 1 NEW_LINE x [ i - 1 ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT x [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT c = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( x [ i ] != 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT print ( c ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "111" NEW_LINE findMinimum ( str ) NEW_LINE DEDENT