text
stringlengths
1
636
code
stringlengths
8
1.89k
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array
arr = [ 1 , 0 , 0 ] NEW_LINE
Store the size of the array
N = len ( arr ) NEW_LINE
Function Call
maximumMex ( arr , N ) NEW_LINE
Function to implement bubble sort without using loops
def bubble_sort ( ar ) : NEW_LINE
Base Case : If array contains a single element
if len ( ar ) <= 1 : NEW_LINE INDENT return ar NEW_LINE DEDENT
Base Case : If array contains two elements
if len ( ar ) == 2 : NEW_LINE INDENT return ar if ar [ 0 ] < ar [ 1 ] else [ ar [ 1 ] , ar [ 0 ] ] NEW_LINE DEDENT
Store the first two elements of the list in variables a and b
a , b = ar [ 0 ] , ar [ 1 ] NEW_LINE
Store remaining elements in the list bs
bs = ar [ 2 : ] NEW_LINE
Store the list after each recursive call
res = [ ] NEW_LINE
If a < b
if a < b : NEW_LINE INDENT res = [ a ] + bubble_sort ( [ b ] + bs ) NEW_LINE DEDENT
Otherwise , if b >= a
else : NEW_LINE INDENT res = [ b ] + bubble_sort ( [ a ] + bs ) NEW_LINE DEDENT
Recursively call for the list less than the last element and and return the newly formed list
return bubble_sort ( res [ : - 1 ] ) + res [ - 1 : ] NEW_LINE
Driver Code
arr = [ 1 , 3 , 4 , 5 , 6 , 2 ] NEW_LINE res = bubble_sort ( arr ) NEW_LINE
Print the array
print ( * res ) NEW_LINE
Function to print the elements of the matrix in row - wise manner
def printMatrix ( a ) : NEW_LINE INDENT for x in a : NEW_LINE INDENT for y in x : NEW_LINE INDENT print ( y , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT
Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner
def sortBoundaryWise ( a ) : NEW_LINE
k - starting row index m - ending row index l - starting column index n - ending column index i - iterator
k = 0 NEW_LINE l = 0 NEW_LINE m = len ( a ) NEW_LINE n = len ( a [ 0 ] ) NEW_LINE n_k = 0 NEW_LINE n_l = 0 NEW_LINE n_m = m NEW_LINE n_n = n NEW_LINE while ( k < m and l < n ) : NEW_LINE
Stores the current boundary elements
boundary = [ ] NEW_LINE
Push the first row
for i in range ( l , n ) : NEW_LINE INDENT boundary . append ( a [ k ] [ i ] ) NEW_LINE DEDENT k += 1 NEW_LINE
Push the last column
for i in range ( k , m ) : NEW_LINE INDENT boundary . append ( a [ i ] [ n - 1 ] ) NEW_LINE DEDENT n -= 1 NEW_LINE
Push the last row
if ( k < m ) : NEW_LINE INDENT for i in range ( n - 1 , l - 1 , - 1 ) : NEW_LINE INDENT boundary . append ( a [ m - 1 ] [ i ] ) NEW_LINE DEDENT m -= 1 NEW_LINE DEDENT
Push the first column
if ( l < n ) : NEW_LINE INDENT for i in range ( m - 1 , k - 1 , - 1 ) : NEW_LINE INDENT boundary . append ( a [ i ] [ l ] ) NEW_LINE DEDENT l += 1 NEW_LINE DEDENT
Sort the boundary elements
boundary . sort ( ) NEW_LINE ind = 0 NEW_LINE
Update the first row
for i in range ( n_l , n_n ) : NEW_LINE INDENT a [ n_k ] [ i ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_k += 1 NEW_LINE
Update the last column
for i in range ( n_k , n_m ) : NEW_LINE INDENT a [ i ] [ n_n - 1 ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_n -= 1 NEW_LINE
Update the last row
if ( n_k < n_m ) : NEW_LINE INDENT for i in range ( n_n - 1 , n_l - 1 , - 1 ) : NEW_LINE INDENT a [ n_m - 1 ] [ i ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_m -= 1 NEW_LINE DEDENT
Update the first column
if ( n_l < n_n ) : NEW_LINE INDENT for i in range ( n_m - 1 , n_k - 1 , - 1 ) : NEW_LINE INDENT a [ i ] [ n_l ] = boundary [ ind ] NEW_LINE ind += 1 NEW_LINE DEDENT n_l += 1 NEW_LINE DEDENT
Print the resultant matrix
printMatrix ( a ) NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Given matrix
matrix = [ [ 9 , 7 , 4 , 5 ] , [ 1 , 6 , 2 , - 6 ] , [ 12 , 20 , 2 , 0 ] , [ - 5 , - 6 , 7 , - 2 ] ] NEW_LINE sortBoundaryWise ( matrix ) NEW_LINE
Python 3 Program for the above approach
import sys NEW_LINE
Function to find minimum sum of absolute differences of pairs of a triplet
def minimum_sum ( A , N ) : NEW_LINE
Sort the array
A . sort ( reverse = False ) NEW_LINE
Stores the minimum sum
sum = sys . maxsize NEW_LINE
Traverse the array
for i in range ( N - 2 ) : NEW_LINE
Update the minimum sum
sum = min ( sum , abs ( A [ i ] - A [ i + 1 ] ) + abs ( A [ i + 1 ] - A [ i + 2 ] ) ) NEW_LINE
Print the minimum sum
print ( sum ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Input
A = [ 1 , 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE
Function call to find minimum sum of absolute differences of pairs in a triplet
minimum_sum ( A , N ) NEW_LINE
Function to check if all characters of the string can be made the same
def sameChar ( S , N ) : NEW_LINE
Sort the string
S = ' ' . join ( sorted ( S ) ) NEW_LINE
Calculate ASCII value of the median character
mid = ord ( S [ N // 2 ] ) NEW_LINE
Stores the minimum number of operations required to make all characters equal
total_operations = 0 NEW_LINE
Traverse the string
for i in range ( N ) : NEW_LINE
Calculate absolute value of current character and median character
total_operations += abs ( ord ( S [ i ] ) - mid ) NEW_LINE
Print the minimum number of operations required
print ( total_operations ) NEW_LINE
Driver Code
S = " geeks " NEW_LINE N = len ( S ) NEW_LINE sameChar ( S , N ) NEW_LINE
Python program for the above approach
import sys NEW_LINE
Function to count the maximum score of an index
def maxScore ( i , A , K , N , dp ) : NEW_LINE
Base Case
if ( i >= N - 1 ) : NEW_LINE INDENT return A [ N - 1 ] ; NEW_LINE DEDENT
If the value for the current index is pre - calculated
if ( dp [ i ] != - 1 ) : NEW_LINE INDENT return dp [ i ] ; NEW_LINE DEDENT score = 1 - sys . maxsize ; NEW_LINE
Calculate maximum score for all the steps in the range from i + 1 to i + k
for j in range ( 1 , K + 1 ) : NEW_LINE
Score for index ( i + j )
score = max ( score , maxScore ( i + j , A , K , N , dp ) ) ; NEW_LINE
Update dp [ i ] and return the maximum value
dp [ i ] = score + A [ i ] ; NEW_LINE return dp [ i ] ; NEW_LINE
Function to get maximum score possible from the array A
def getScore ( A , N , K ) : NEW_LINE
Array to store memoization
dp = [ 0 ] * N ; NEW_LINE
Initialize dp with - 1
for i in range ( N ) : NEW_LINE INDENT dp [ i ] = - 1 ; NEW_LINE DEDENT print ( maxScore ( 0 , A , K , N , dp ) ) ; NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 100 , - 30 , - 50 , - 15 , - 20 , - 30 ] ; NEW_LINE K = 3 ; NEW_LINE N = len ( A ) ; NEW_LINE getScore ( A , N , K ) ; NEW_LINE DEDENT
Function to find the count of triplets ( a , b , c ) Such that the equations ax ^ 2 + bx + c = 0 has real roots
def getcount ( arr , N ) : NEW_LINE
store count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots
count = 0 NEW_LINE
base case
if ( N < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
Generate all possible triplets ( a , b , c )
for b in range ( 0 , N ) : NEW_LINE INDENT for a in range ( 0 , N ) : NEW_LINE DEDENT
if the coefficient of X ^ 2 and x are equal
if ( a == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT for c in range ( 0 , N ) : NEW_LINE
if coefficient of X ^ 2 or x are equal to the constant
if ( c == a or c == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT d = arr [ b ] * arr [ b ] // 4 NEW_LINE
condition for having real roots
if ( arr [ a ] * arr ) <= d : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE
Driver code
arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE
Function Call
print ( getcount ( arr , N ) ) NEW_LINE
Function to check if all array elements can be reduced to less than X or not
def findAns ( A , N , X ) : NEW_LINE
Checks if all array elements are already a X or not
INDENT if ( check ( A , X ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT
Traverse every possible pair
INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE DEDENT DEDENT
Calculate GCD of two array elements
gcd = GCD ( A [ i ] , A [ j ] ) NEW_LINE
If gcd is a 1
if ( gcd != 1 ) : NEW_LINE
If gcd is a X , then a pair is present to reduce all array elements to a X
if ( gcd <= X ) : NEW_LINE return True NEW_LINE
If no pair is present with gcd is a X
INDENT return False NEW_LINE DEDENT
Function to check if all array elements area X
def check ( A , X ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT if ( A [ i ] > X ) : NEW_LINE return False NEW_LINE DEDENT return True NEW_LINE DEDENT
Function to calculate gcd of two numbers
def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT
Driver Code
X = 4 NEW_LINE A = [ 2 , 1 , 5 , 3 , 6 ] NEW_LINE N = 5 NEW_LINE print ( findAns ( A , N , X ) ) NEW_LINE
Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements
def check ( a , b , Na , Nb , k , m ) : NEW_LINE
Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ]
INDENT if ( Na < k or Nb < m ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT
Sort arrays in ascending order
INDENT a . sort ( ) NEW_LINE a . sort ( ) NEW_LINE DEDENT
Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ]
INDENT if ( a [ k - 1 ] < b [ Nb - m ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT
Return false
INDENT return " No " NEW_LINE DEDENT
Driver Code
arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 3 , 4 , 5 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE X = 2 NEW_LINE Y = 1 NEW_LINE
Function Call
print ( check ( arr1 , arr2 , N , M , X , Y ) ) NEW_LINE
Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum
def splitArray ( arr , N ) : NEW_LINE
Sort the array in increasing order
arr = sorted ( arr ) NEW_LINE result = 10 ** 9 NEW_LINE
Calculating the min Bitwise XOR between consecutive elements
for i in range ( 1 , N ) : NEW_LINE INDENT result = min ( result , arr [ i ] ^ arr [ i - 1 ] ) NEW_LINE DEDENT
Return the final minimum Bitwise XOR
return result NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array arr [ ]
arr = [ 3 , 1 , 2 , 6 , 4 ] NEW_LINE
Size of array
N = len ( arr ) NEW_LINE
Function Call
print ( splitArray ( arr , N ) ) NEW_LINE
Function to check if an array is sorted in increasing order or not
def isIncreasing ( arr ) : NEW_LINE
Traverse the array
for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE
Function to sort the array by left shifting digits of array elements
def sortArr ( arr ) : NEW_LINE
Stores previous array element
prev = - 1 NEW_LINE
Traverse the array arr [ ]
for i in range ( len ( arr ) ) : NEW_LINE
Stores current element
optEle = arr [ i ] NEW_LINE