codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increments by index value required to obtain at least two equal Array elements | Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code
def update ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] += ( i + 1 ) ; NEW_LINE DEDENT DEDENT def check ( arr , N ) : NEW_LINE INDENT f = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT if ( count >= 2 ) : NEW_LINE INDENT f = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( f == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def incrementCount ( arr , N ) : NEW_LINE INDENT min = 0 ; NEW_LINE while ( check ( arr , N ) != True ) : NEW_LINE INDENT update ( arr , N ) ; NEW_LINE min += 1 ; NEW_LINE DEDENT print ( min ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 ; NEW_LINE arr = [ 12 , 8 , 4 ] ; NEW_LINE incrementCount ( arr , N ) ; NEW_LINE DEDENT
Rearrange array such that all even | Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
def checkPossible ( a , n ) : NEW_LINE INDENT even_no_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even_no_count += 1 NEW_LINE DEDENT DEDENT if ( n // 2 > even_no_count ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT print ( " Yes " ) NEW_LINE j = 0 NEW_LINE for i in range ( 1 , n , 2 ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT while ( j < n and a [ j ] % 2 != 0 ) : NEW_LINE INDENT j += 2 NEW_LINE DEDENT a [ i ] += a [ j ] NEW_LINE a [ j ] = a [ i ] - a [ j ] NEW_LINE a [ i ] -= a [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE checkPossible ( arr , n ) NEW_LINE
Largest number M having bit count of N such that difference between their OR and XOR value is maximized | Python3 program for the above approach ; Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Driver code ; Given Number N ; Function call
import math NEW_LINE def maxORminusXOR ( N ) : NEW_LINE INDENT MSB = int ( math . log2 ( N ) ) ; NEW_LINE M = 0 NEW_LINE for i in range ( MSB + 1 ) : NEW_LINE INDENT M += ( 1 << i ) NEW_LINE DEDENT return M NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE print ( maxORminusXOR ( N ) ) NEW_LINE DEDENT
Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle | Function to find the number of equilateral triangle formed within another triangle ; Check for the valid condition ; Number of triangles having upward peak ; Number of inverted triangles ; Total no . of K sized triangle ; Driver Code ; Given N and K ; Function Call
def No_of_Triangle ( N , K ) : NEW_LINE INDENT if ( N < K ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT Tri_up = 0 ; NEW_LINE Tri_up = ( ( N - K + 1 ) * ( N - K + 2 ) ) // 2 ; NEW_LINE Tri_down = 0 ; NEW_LINE Tri_down = ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) // 2 ; NEW_LINE return Tri_up + Tri_down ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; K = 2 ; NEW_LINE print ( No_of_Triangle ( N , K ) ) ; NEW_LINE DEDENT
Check if Array forms an increasing | Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Given array arr ; Function Call
def canMake ( n , ar ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ar [ 0 ] < ar [ 1 ] ) : NEW_LINE INDENT i = 1 ; NEW_LINE while ( i < n and ar [ i - 1 ] < ar [ i ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( i + 1 < n and ar [ i ] > ar [ i + 1 ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( i >= n - 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT elif ( ar [ 0 ] > ar [ 1 ] ) : NEW_LINE INDENT i = 1 ; NEW_LINE while ( i < n and ar [ i - 1 ] > ar [ i ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( i + 1 < n and ar [ i ] < ar [ i + 1 ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( i >= n - 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( ar [ i - 1 ] <= ar [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( canMake ( n , arr ) == False ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Minimum Count of Bit flips required to make a Binary String Palindromic | Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code
def check_length ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n >> 1 NEW_LINE ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def check_ith_bit ( n , i ) : NEW_LINE INDENT if ( n & ( 1 << ( i - 1 ) ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def no_of_flips ( n ) : NEW_LINE INDENT ln = check_length ( n ) NEW_LINE ans = 0 NEW_LINE right = 1 NEW_LINE left = ln NEW_LINE while ( right < left ) : NEW_LINE INDENT if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT left -= 1 NEW_LINE right += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 12 NEW_LINE print ( no_of_flips ( n ) ) NEW_LINE
Split N into two integers whose addition to A and B makes them equal | Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver Code
def findPair ( A , B , N ) : NEW_LINE INDENT X = N - B + A NEW_LINE if ( X % 2 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT X = X // 2 NEW_LINE Y = N - X NEW_LINE print ( X , Y ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 NEW_LINE B = 3 NEW_LINE N = 4 NEW_LINE findPair ( A , B , N ) NEW_LINE DEDENT
Find the amplitude and number of waves for the given array | Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Prthe Amplitude ; Driver Code ; Given array a [ ] ; Calculate number of waves ; Function Call
def check ( a , n ) : NEW_LINE INDENT ma = a [ 1 ] - a [ 0 ] NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( ( a [ i ] > a [ i - 1 ] and a [ i + 1 ] < a [ i ] ) or ( a [ i ] < a [ i - 1 ] and a [ i + 1 ] > a [ i ] ) ) : NEW_LINE INDENT ma = max ( ma , abs ( a [ i ] - a [ i + 1 ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT print ( " Amplitude ▁ = ▁ " , ma ) NEW_LINE return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 1 , 5 , 0 , 7 , - 6 ] NEW_LINE n = len ( a ) NEW_LINE wave = ( n - 1 ) // 2 NEW_LINE if ( check ( a , n ) ) : NEW_LINE INDENT print ( " Waves ▁ = ▁ " , wave ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT
Find relative rank of each element in array | Function to find relative rank for each element in the array A [ ] ; Create Rank Array ; Stack to store numbers in non - decreasing order from right ; Push last element in stack ; Iterate from second last element to first element ; If current element is less than the top of stack and append A [ i ] in stack ; Rank is stack size - 1 for current element ; Pop elements from stack till current element is greater than the top ; Push current element in Stack ; Rank is stack size - 1 ; Print rank of all elements ; Driver Code ; Given array A [ ] ; Function call
def findRank ( A , N ) : NEW_LINE INDENT rank = [ 0 ] * N NEW_LINE s = [ ] NEW_LINE s . append ( A [ N - 1 ] ) NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] < s [ - 1 ] ) : NEW_LINE INDENT s . append ( A [ i ] ) NEW_LINE rank [ i ] = len ( s ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT while ( len ( s ) > 0 and A [ i ] >= s [ - 1 ] ) : NEW_LINE INDENT del s [ - 1 ] NEW_LINE DEDENT s . append ( A [ i ] ) NEW_LINE rank [ i ] = len ( s ) - 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( rank [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 5 , 4 ] NEW_LINE N = len ( A ) NEW_LINE findRank ( A , N ) NEW_LINE DEDENT
Largest string obtained in Dictionary order after deleting K characters | Python3 program for the above approach ; Function to find the largest string after deleting k characters ; Deque dq used to find the largest string in dictionary after deleting k characters ; Iterate till the length of the string ; Condition for popping characters from deque ; To store the resultant string ; To form resultant string ; Return the resultant string ; Driver Code ; Given String ; Function call ; Print the answer
from collections import deque NEW_LINE def largestString ( n , k , sc ) : NEW_LINE INDENT s = [ i for i in sc ] NEW_LINE deq = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( deq ) > 0 and deq [ - 1 ] < s [ i ] and k > 0 ) : NEW_LINE INDENT deq . popleft ( ) NEW_LINE k -= 1 NEW_LINE DEDENT deq . append ( s [ i ] ) NEW_LINE DEDENT st = " " NEW_LINE for c in deq : NEW_LINE INDENT st = st + c NEW_LINE DEDENT return st NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE k = 2 NEW_LINE sc = " ritz " NEW_LINE result = largestString ( n , k , sc ) NEW_LINE print ( result ) NEW_LINE DEDENT
Count total set bits in all numbers from range L to R | Function that counts the set bits from 0 to N ; To store sum of set bits from 0 - N ; Until n >= to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; Change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; Again set change to 2 ^ i ; Increment the position ; Function that counts the set bit in the range ( L , R ) ; Return the count ; Driver Code ; Given L and R ; Function Call
def countSetBit ( n ) : NEW_LINE INDENT i = 0 ; NEW_LINE ans = 0 ; NEW_LINE while ( ( 1 << i ) <= n ) : NEW_LINE INDENT k = True ; NEW_LINE change = 1 << i ; NEW_LINE for j in range ( n + 1 ) : NEW_LINE INDENT ans += 0 if k == True else 1 ; NEW_LINE if ( change == 1 ) : NEW_LINE INDENT k = False if k == True else True ; NEW_LINE change = 1 << i ; NEW_LINE DEDENT else : NEW_LINE INDENT change -= 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return ans ; 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
Count total set bits in all numbers from range L to R | Function to count set bit in range ; Count variable ; Find the set bit in Nth number ; If last bit is set ; Left sift by one bit ; Return count ; Driver Code ; Given range L and R ; Function call
def countSetBits ( L , R ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT n = i ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT count += ( n & 1 ) ; NEW_LINE n = n >> 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 3 ; R = 5 ; NEW_LINE print ( " Total ▁ set ▁ Bit ▁ count ▁ is ▁ " , countSetBits ( L , R ) ) ; NEW_LINE DEDENT
Count total set bits in all numbers from range L to R | Function to count set bit in [ L , R ] ; Variable for count set bit in range ; Count set bit for all number in range ; Use inbuilt function ; Driver Code ; Given range L and R ; Function Call
def countSetBits ( L , R ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT count += countSetBit ( i ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT def countSetBit ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT count += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return count 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
Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | Function that count the number of integers from array B such that subtracting element in the range [ 0 , K ] given any element in A ; To store the count of element ; Traverse the array B ; Traverse the array A ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Driver Code ; Given array A and B ; Given K ; Function call
def countElement ( A , N , B , M , K ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT currentElement = B [ i ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT diff = abs ( currentElement - A [ j ] ) NEW_LINE if ( diff <= K ) : NEW_LINE INDENT cnt += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( cnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 100 , 65 , 35 , 85 , 55 ] NEW_LINE B = [ 30 , 60 , 75 , 95 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE K = 5 NEW_LINE countElement ( A , N , B , M , K ) NEW_LINE DEDENT
Generate a String of having N * N distinct non | Function to construct a string having N * N non - palindromic substrings ; Driver Code
def createString ( N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( ' a ' , end = ' ' ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( ' b ' , end = ' ' ) NEW_LINE DEDENT DEDENT N = 4 NEW_LINE createString ( N ) NEW_LINE
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | Function returns maximum subset sum from the given array = ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
def maxSubset ( A , flag ) : NEW_LINE INDENT n = len ( A ) NEW_LINE sum = 0 ; NEW_LINE if ( flag ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT A [ i ] = - A [ i ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( A [ i ] > 0 ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def findBest ( A ) : NEW_LINE INDENT x = maxSubset ( A , 0 ) NEW_LINE y = maxSubset ( A , 1 ) NEW_LINE y = - y NEW_LINE y += A [ 0 ] NEW_LINE y = - y NEW_LINE return max ( x , y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 10 , 4 , - 6 , 3 ] NEW_LINE print ( findBest ( A ) ) NEW_LINE DEDENT
Construct an Array of Strings having Longest Common Prefix specified by the given Array | Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
def solve ( n , arr ) : NEW_LINE INDENT s = ' a ' * ( n ) NEW_LINE ans = [ ] NEW_LINE ans . append ( s ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if len ( s ) - 1 >= arr [ i ] : NEW_LINE ch = s [ arr [ i ] ] NEW_LINE if ( ch == ' b ' ) : NEW_LINE INDENT ch = ' a ' NEW_LINE DEDENT else : NEW_LINE INDENT ch = ' b ' NEW_LINE DEDENT p = list ( s ) NEW_LINE p [ arr [ i ] ] = ch NEW_LINE s = ' ' . join ( p ) NEW_LINE ans . append ( s ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE ans = solve ( n , arr ) NEW_LINE for i in range ( len ( ans ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( ans [ i ] ) NEW_LINE DEDENT DEDENT
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code
def countSwaps ( A , n ) : NEW_LINE INDENT A . sort ( ) NEW_LINE ind , res = 1 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( ind < n and A [ ind ] == A [ i ] ) : NEW_LINE INDENT ind += 1 NEW_LINE DEDENT if ( ind < n and A [ ind ] > A [ i ] ) : NEW_LINE INDENT res += 1 NEW_LINE ind += 1 NEW_LINE DEDENT if ( ind >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT A = [ 4 , 3 , 3 , 2 , 5 ] NEW_LINE print ( countSwaps ( A , 5 ) ) NEW_LINE
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver code ; function call
def countSwaps ( A , n ) : NEW_LINE INDENT mp = { } NEW_LINE max_frequency = 0 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 max_frequency = max ( max_frequency , mp [ A [ i ] ] ) NEW_LINE DEDENT return n - max_frequency NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 6 , 5 , 4 , 3 , 2 , 1 ] NEW_LINE print ( countSwaps ( A , 6 ) ) NEW_LINE DEDENT
Minimum number of steps required to obtain the given Array by the given operations | Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array arr [ ] ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
def min_operation ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i > 0 ) : NEW_LINE INDENT ans += abs ( a [ i ] - a [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += abs ( a [ i ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( min_operation ( arr , n ) ) NEW_LINE DEDENT
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
def FindMatrix ( n , m , x , y ) : NEW_LINE INDENT if ( n * m == 1 ) : NEW_LINE INDENT if ( x > y ) : NEW_LINE INDENT print ( y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT return NEW_LINE DEDENT a = min ( x , y ) NEW_LINE b = min ( 2 * x , y ) - a NEW_LINE flag = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT print ( a , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( b , end = " ▁ " ) NEW_LINE DEDENT flag = not flag NEW_LINE DEDENT if ( ( ( n % 2 != 0 and m % 2 == 0 ) or ( n % 2 == 0 and m % 2 == 0 ) ) ) : NEW_LINE INDENT flag = not flag NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE M = 3 NEW_LINE X = 5 NEW_LINE Y = 3 NEW_LINE FindMatrix ( N , M , X , Y ) NEW_LINE
Count distinct elements after adding each element of First Array with Second Array | Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX Count = new int [ 2 * MAX + 1 ] ; ; Size of Arr1 and Arr2 ; Find the elements of arr3 and increase count of element by 1 ; Prthe result ; Driver Code ; Given arrays arr1 and arr2 ; Function call
def findCount ( Arr1 , Arr2 ) : NEW_LINE INDENT MAX = max ( max ( Arr1 ) , max ( Arr2 ) ) ; NEW_LINE Count = [ 0 for i in range ( 2 * MAX + 1 ) ] NEW_LINE n = len ( Arr1 ) ; NEW_LINE m = len ( Arr2 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT element = Arr1 [ i ] + Arr2 [ j ] ; NEW_LINE Count [ element ] += 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , 2 * MAX + 1 ) : NEW_LINE INDENT if ( Count [ i ] > 0 ) : NEW_LINE INDENT print ( i , " - > " , Count [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 ] ; NEW_LINE arr2 = [ 1 , 2 , 1 ] ; NEW_LINE findCount ( arr1 , arr2 ) ; NEW_LINE DEDENT
Find two numbers with difference and division both same as N | Function to find two numbers with difference and division both as N ; Condition if the answer is not possible ; Calculate a and b ; Print the values ; Given N ; Function call
def findAandB ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT a = N * N / ( N - 1 ) NEW_LINE b = a / N NEW_LINE print ( " a ▁ = ▁ " , a ) NEW_LINE print ( " b ▁ = ▁ " , b ) NEW_LINE DEDENT N = 6 NEW_LINE findAandB ( N ) NEW_LINE
Maximize number of elements from Array with sum at most K | Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Driver Code ; Given array ; Given sum k ; Function call
def maxSelections ( A , n , k ) : NEW_LINE INDENT A . sort ( ) ; NEW_LINE sum = 0 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + A [ i ] ; NEW_LINE if ( sum > k ) : NEW_LINE break ; NEW_LINE count += 1 ; NEW_LINE return count ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 7 , 2 , 9 , 4 ] ; NEW_LINE k = 15 ; NEW_LINE n = len ( A ) ; NEW_LINE print ( maxSelections ( A , n , k ) ) ; NEW_LINE DEDENT
Pair of integers with difference K having an element as the K | Function to find the required pair ; No pair possible ; Driver Code
def computePair ( K ) : NEW_LINE INDENT if ( K == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT print ( K * K / ( K - 1 ) , end = " ▁ " ) NEW_LINE print ( K / ( K - 1 ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 6 NEW_LINE computePair ( K ) NEW_LINE DEDENT