text
stringlengths
1
636
code
stringlengths
8
1.89k
check whether resultant is sorted or not
for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if A [ i ] > A [ i + 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT
Is resultant is sorted return true
return True NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 3 , 2 , 4 , 6 , 5 ] NEW_LINE n = len ( A ) NEW_LINE if almostSort ( A , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Function to find next gap .
def nextGap ( gap ) : NEW_LINE INDENT if ( gap <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( gap // 2 ) + ( gap % 2 ) NEW_LINE DEDENT def merge ( arr1 , arr2 , n , m ) : NEW_LINE INDENT gap = n + m NEW_LINE gap = nextGap ( gap ) NEW_LINE while gap > 0 : NEW_LINE DEDENT
comparing elements in the first array .
i = 0 NEW_LINE while i + gap < n : NEW_LINE INDENT if ( arr1 [ i ] > arr1 [ i + gap ] ) : NEW_LINE INDENT arr1 [ i ] , arr1 [ i + gap ] = arr1 [ i + gap ] , arr1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT
comparing elements in both arrays .
j = gap - n if gap > n else 0 NEW_LINE while i < n and j < m : NEW_LINE INDENT if ( arr1 [ i ] > arr2 [ j ] ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ j ] = arr2 [ j ] , arr1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( j < m ) : NEW_LINE
comparing elements in the second array .
j = 0 NEW_LINE while j + gap < m : NEW_LINE INDENT if ( arr2 [ j ] > arr2 [ j + gap ] ) : NEW_LINE INDENT arr2 [ j ] , arr2 [ j + gap ] = arr2 [ j + gap ] , arr2 [ j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT gap = nextGap ( gap ) NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a1 = [ 10 , 27 , 38 , 43 , 82 ] NEW_LINE a2 = [ 3 , 9 ] NEW_LINE n = len ( a1 ) NEW_LINE m = len ( a2 ) NEW_LINE DEDENT
Function Call
merge ( a1 , a2 , n , m ) NEW_LINE print ( " First ▁ Array : ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( a1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE print ( " Second ▁ Array : ▁ " , end = " " ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT print ( a2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE
Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ]
def mergeArrays ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT arr3 = [ None ] * ( n1 + n2 ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE DEDENT
Traverse both array
while i < n1 and j < n2 : NEW_LINE
Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array
if arr1 [ i ] < arr2 [ j ] : NEW_LINE INDENT arr3 [ k ] = arr1 [ i ] NEW_LINE k = k + 1 NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr3 [ k ] = arr2 [ j ] NEW_LINE k = k + 1 NEW_LINE j = j + 1 NEW_LINE DEDENT
Store remaining elements of first array
while i < n1 : NEW_LINE INDENT arr3 [ k ] = arr1 [ i ] ; NEW_LINE k = k + 1 NEW_LINE i = i + 1 NEW_LINE DEDENT
Store remaining elements of second array
while j < n2 : NEW_LINE INDENT arr3 [ k ] = arr2 [ j ] ; NEW_LINE k = k + 1 NEW_LINE j = j + 1 NEW_LINE DEDENT print ( " Array ▁ after ▁ merging " ) NEW_LINE for i in range ( n1 + n2 ) : NEW_LINE INDENT print ( str ( arr3 [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT
Driver code
arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE arr2 = [ 2 , 4 , 6 , 8 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE mergeArrays ( arr1 , arr2 , n1 , n2 ) ; NEW_LINE
Function to sort an square array
def sortSquare ( arr , n ) : NEW_LINE
First convert each array elements into its square
for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] * arr [ i ] NEW_LINE DEDENT
Sort an array using " inbuild ▁ sort ▁ function " in Arrays class
arr . sort ( ) NEW_LINE
Driver code
arr = [ - 6 , - 3 , - 1 , 2 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Before ▁ sort " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE sortSquare ( arr , n ) NEW_LINE print ( " After ▁ sort " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Merge two sorted halves of Array into single sorted array
def mergeTwoHalf ( A , n ) : NEW_LINE
Starting index of second half
half_i = 0 NEW_LINE
Temp Array store sorted resultant array
temp = [ 0 for i in range ( n ) ] NEW_LINE
First Find the point where array is divide into two half
for i in range ( n - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT half_i = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT
If Given array is all - ready sorted
if ( half_i == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT
Merge two sorted arrays in single sorted array
i = 0 NEW_LINE j = half_i NEW_LINE k = 0 NEW_LINE while ( i < half_i and j < n ) : NEW_LINE INDENT if ( A [ i ] < A [ j ] ) : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT
Copy the remaining elements of A [ i to half_ ! ]
while i < half_i : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT
Copy the remaining elements of A [ half_ ! to n ]
while ( j < n ) : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT A [ i ] = temp [ i ] NEW_LINE DEDENT
Driver code
A = [ 2 , 3 , 8 , - 1 , 7 , 10 ] NEW_LINE n = len ( A ) NEW_LINE mergeTwoHalf ( A , n ) NEW_LINE
Print sorted Array
print ( * A , sep = ' ▁ ' ) NEW_LINE
arr [ 0. . n - 1 ] represents sizes of packets m is number of students . Returns minimum difference between maximum and minimum values of distribution .
def findMinDiff ( arr , n , m ) : NEW_LINE
if there are no chocolates or number of students is 0
if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
Sort the given packets
arr . sort ( ) NEW_LINE
Number of students cannot be more than number of packets
if ( n < m ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT
Largest number of chocolates
min_diff = arr [ n - 1 ] - arr [ 0 ] NEW_LINE
Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum .
for i in range ( len ( arr ) - m + 1 ) : NEW_LINE INDENT min_diff = min ( min_diff , arr [ i + m - 1 ] - arr [ i ] ) NEW_LINE DEDENT return min_diff NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 ] NEW_LINE DEDENT
m = 7 Number of students
n = len ( arr ) NEW_LINE print ( " Minimum ▁ difference ▁ is " , findMinDiff ( arr , n , m ) ) NEW_LINE
This function returns number of distinct absolute values among the elements of the array
def distinctCount ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE DEDENT
set keeps all unique elements
for i in range ( n ) : NEW_LINE INDENT s . add ( abs ( arr [ i ] ) ) NEW_LINE DEDENT return len ( s ) NEW_LINE
Driver Code
arr = [ - 2 , - 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values : " , distinctCount ( arr , n ) ) NEW_LINE
The function returns return number of distinct absolute values among the elements of the array
def distinctCount ( arr , n ) : NEW_LINE
initialize count as number of elements
count = n ; NEW_LINE i = 0 ; j = n - 1 ; sum = 0 ; NEW_LINE while ( i < j ) : NEW_LINE
Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count
while ( i != j and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE DEDENT
Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count
while ( i != j and arr [ j ] == arr [ j - 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT
break if only one element is left
if ( i == j ) : NEW_LINE INDENT break ; NEW_LINE DEDENT
Now look for the zero sum pair in current window ( i , j )
sum = arr [ i ] + arr [ j ] ; NEW_LINE if ( sum == 0 ) : NEW_LINE
decrease the count if ( positive , negative ) pair is encountered
count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE j = j - 1 ; NEW_LINE elif ( sum < 0 ) : NEW_LINE i = i + 1 ; NEW_LINE else : NEW_LINE j = j - 1 ; NEW_LINE return count ; NEW_LINE
Driver code
arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " , distinctCount ( arr , n ) ) ; NEW_LINE
Reverses arr [ 0. . i ]
def flip ( arr , i ) : NEW_LINE INDENT start = 0 NEW_LINE while start < i : NEW_LINE INDENT temp = arr [ start ] NEW_LINE arr [ start ] = arr [ i ] NEW_LINE arr [ i ] = temp NEW_LINE start += 1 NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT
Returns index of the maximum element in arr [ 0. . n - 1 ]
def findMax ( arr , n ) : NEW_LINE INDENT mi = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] > arr [ mi ] : NEW_LINE INDENT mi = i NEW_LINE DEDENT DEDENT return mi NEW_LINE DEDENT
The main function that sorts given array using flip operations
def pancakeSort ( arr , n ) : NEW_LINE
Start from the complete array and one by one reduce current size by one
curr_size = n NEW_LINE while curr_size > 1 : NEW_LINE
Find index of the maximum element in arr [ 0. . curr_size - 1 ]
mi = findMax ( arr , curr_size ) NEW_LINE
Move the maximum element to end of current array if it 's not already at the end
if mi != curr_size - 1 : NEW_LINE
To move at the end , first move maximum number to beginning
flip ( arr , mi ) NEW_LINE
Now move the maximum number to end by reversing current array
flip ( arr , curr_size - 1 ) NEW_LINE curr_size -= 1 NEW_LINE
A utility function to print an array of size n
def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( " % d " % ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Driver program
arr = [ 23 , 10 , 20 , 11 , 12 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE pancakeSort ( arr , n ) ; NEW_LINE print ( " Sorted ▁ Array ▁ " ) NEW_LINE printArray ( arr , n ) NEW_LINE
Function to construct lexicographically smallest numeric string having an odd count of each characters
def genString ( N ) : NEW_LINE
Stores the resultant string
ans = " " NEW_LINE
If N is even
if ( N % 2 == 0 ) : NEW_LINE INDENT ans = " " . join ( "1" for i in range ( N - 1 ) ) NEW_LINE ans = ans + "2" NEW_LINE DEDENT
Otherwise
else : NEW_LINE INDENT ans = " " . join ( "1" for i in range ( N ) ) NEW_LINE DEDENT return ans NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( genString ( N ) ) NEW_LINE DEDENT
Function to modify the given string satisfying the given criteria
def performOperation ( S , N ) : NEW_LINE
Traverse the string S we cannot directly change string because it is immutable so change of list of char
S = list ( S ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE
If i is even
if ( i % 2 == 0 ) : NEW_LINE
If the S [ i ] is ' a ' , then change S [ i ] to 'b
' NEW_LINE INDENT if ( S [ i ] == ' a ' ) : NEW_LINE INDENT S [ i ] = ' b ' NEW_LINE DEDENT DEDENT
If S [ i ] is ' z ' , then change S [ i ] to 'y
' NEW_LINE INDENT if ( S [ i ] == ' z ' ) : NEW_LINE INDENT S [ i ] = ' y ' NEW_LINE DEDENT DEDENT
Otherwise , change S [ i ] to 'z
' NEW_LINE INDENT else : NEW_LINE INDENT S [ i ] = ' z ' NEW_LINE DEDENT DEDENT
Return the result join the list of char
return " " . join ( S ) NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " giad " NEW_LINE N = len ( S ) NEW_LINE print ( performOperation ( S , N ) ) NEW_LINE DEDENT
Function to check if the value of X reduces N to 0 or not
def check ( x , N ) : NEW_LINE INDENT while True : NEW_LINE DEDENT
Update the value of N as N - x
N -= x NEW_LINE
Check if x is a single digit integer
if len ( str ( x ) ) == 1 : NEW_LINE INDENT break NEW_LINE DEDENT x = sum ( list ( map ( int , str ( x ) ) ) ) NEW_LINE if len ( str ( x ) ) == 1 and N == 0 : NEW_LINE return 1 NEW_LINE return 0 NEW_LINE
Function to find the number of values X such that N can be reduced to 0 after performing the given operations
def countNoOfsuchX ( N ) : NEW_LINE
Number of digits in N
k = len ( str ( N ) ) NEW_LINE
Stores the count of value of X
count = 0 NEW_LINE
Iterate over all possible value of X
for x in range ( N - k * ( k + 1 ) * 5 , N + 1 ) : NEW_LINE
Check if x follow the conditions
if check ( x , N ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT
Return the total count
return count NEW_LINE
Driver Code
N = 9939 NEW_LINE print ( countNoOfsuchX ( N ) ) NEW_LINE
Returns the count of subarrays which contains both the maximum and minimum elements in the given vector
def proc ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE DEDENT
Initialize the low and high of array
INDENT low = v [ n - 1 ] NEW_LINE high = v [ n - 1 ] NEW_LINE p1 = n NEW_LINE p2 = n ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE DEDENT DEDENT
If current element is less than least element
if ( x < low ) : NEW_LINE low = x ; NEW_LINE ans = 0 ; NEW_LINE
If current element is more than highest element
elif ( x > high ) : NEW_LINE high = x ; NEW_LINE ans = 0 ; NEW_LINE
If current element is equal to low or high then update the pointers
if ( x == low ) : p1 = i ; NEW_LINE if ( x == high ) : p2 = i ; NEW_LINE
Update number of subarrays
ans += n - max ( p1 , p2 ) ; NEW_LINE
Return the result
INDENT return ans ; NEW_LINE DEDENT
Function to find the maximum count of subarrays
def subarray ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT ans = proc ( v ) ; NEW_LINE low = v [ 0 ] NEW_LINE pos_low = 0 NEW_LINE high = v [ 0 ] NEW_LINE pos_high = 0 NEW_LINE DEDENT
Iterate the array to find the maximum and minimum element
INDENT for i in range ( 1 , n ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE if ( x < low ) : NEW_LINE low = x ; NEW_LINE pos_low = i ; NEW_LINE elif ( x > high ) : NEW_LINE high = x ; NEW_LINE pos_high = i ; NEW_LINE DEDENT DEDENT
Vector after removing the minimum element
INDENT u = v [ : ] ; NEW_LINE DEDENT
Using assignment operator to copy one vector to other
INDENT del u [ pos_low ] ; NEW_LINE ans = max ( ans , proc ( u ) ) ; NEW_LINE DEDENT
Vector after removing the maximum element
INDENT w = v [ : ] ; NEW_LINE del w [ pos_high ] ; NEW_LINE return max ( ans , proc ( w ) ) ; NEW_LINE DEDENT
Given array
v = [ ] ; NEW_LINE v . append ( 7 ) ; NEW_LINE v . append ( 2 ) ; NEW_LINE v . append ( 5 ) ; NEW_LINE v . append ( 4 ) ; NEW_LINE v . append ( 3 ) ; NEW_LINE v . append ( 1 ) ; NEW_LINE
Function Call
print ( subarray ( v ) ) ; NEW_LINE
Function to find the minimum number of increment and decrement of pairs required to make all array elements equal
def find ( arr , N ) : NEW_LINE
Stores the sum of the array
Sum = sum ( arr ) NEW_LINE
If sum is not divisible by N
if Sum % N : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE
Update sum
k = Sum // N NEW_LINE
Store the minimum number of operations
ans = 0 NEW_LINE i = 0 NEW_LINE