text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Add 1 , 2 , 3 , ... with corresponding values | for i in range ( k ) : NEW_LINE INDENT array [ i ] += i + 1 NEW_LINE DEDENT if k - 1 != remaining or k == 1 : NEW_LINE INDENT print ( * array ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT |
There is no solution for below cases | elif k == 2 or k == 3 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT else : NEW_LINE |
Modify A [ 1 ] and A [ k - 1 ] to get the required array | array [ 1 ] -= 1 NEW_LINE array [ k - 1 ] += 1 NEW_LINE print ( * array ) NEW_LINE sys . exit ( ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 26 , 6 NEW_LINE generateArray ( n , k ) NEW_LINE DEDENT |
Function to return the maximized number | def get_maximum ( s , a ) : NEW_LINE INDENT s = list ( s ) NEW_LINE n = len ( s ) NEW_LINE DEDENT |
Iterate till the end of the string | for i in range ( n ) : NEW_LINE |
Check if it is greater or not | if ( ord ( s [ i ] ) - ord ( '0' ) < a [ ord ( s [ i ] ) - ord ( '0' ) ] ) : NEW_LINE INDENT j = i NEW_LINE DEDENT |
Replace with the alternate till smaller | while ( j < n and ( ord ( s [ j ] ) - ord ( '0' ) <= a [ ord ( s [ j ] ) - ord ( '0' ) ] ) ) : NEW_LINE INDENT s [ j ] = chr ( ord ( '0' ) + a [ ord ( s [ j ] ) - ord ( '0' ) ] ) NEW_LINE j += 1 NEW_LINE DEDENT return " " . join ( s ) ; NEW_LINE |
Return original s in case no change took place | return s NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "1337" NEW_LINE a = [ 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ] NEW_LINE print ( get_maximum ( s , a ) ) NEW_LINE DEDENT |
Python3 implementation of the approach | from math import sqrt NEW_LINE |
Function to return the count of steps | def countSteps ( n ) : NEW_LINE |
Variable to store the count of steps | steps = 0 ; NEW_LINE |
Iterate while N > 0 | while ( n ) : NEW_LINE |
Get the largest perfect square and subtract it from N | largest = int ( sqrt ( n ) ) ; NEW_LINE n -= ( largest * largest ) ; NEW_LINE |
Increment steps | steps += 1 ; NEW_LINE |
Return the required count | return steps ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 85 ; NEW_LINE print ( countSteps ( n ) ) ; NEW_LINE DEDENT |
Utility function to return the sum of the array elements | def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT |
Function to return the maximized sum of the array after performing the given operation exactly k times | def maxSum ( arr , n , k ) : NEW_LINE |
Sort the array elements | arr . sort ( reverse = False ) NEW_LINE i = 0 NEW_LINE |
Change signs of the negative elements starting from the smallest | while ( i < n and k > 0 and arr [ i ] < 0 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE k -= 1 NEW_LINE i += 1 NEW_LINE DEDENT |
If a single operation has to be performed then it must be performed on the smallest positive element | if ( k % 2 == 1 ) : NEW_LINE |
To store the index of the minimum element | min = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE |
Update the minimum index | if ( arr [ min ] > arr [ i ] ) : NEW_LINE INDENT min = i NEW_LINE DEDENT |
Perform remaining operation on the smallest element | arr [ min ] *= - 1 NEW_LINE |
Return the sum of the updated array | return sumArr ( arr , n ) NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 5 , 4 , 1 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE print ( maxSum ( arr , n , k ) ) NEW_LINE DEDENT |
Python3 implementation of the approach | from typing import List NEW_LINE |
par and rank will store the parent and rank of particular node in the Union Find Algorithm | par = [ ] NEW_LINE rnk = [ ] NEW_LINE |
Find function of Union Find Algorithm | def find ( x : int ) -> int : NEW_LINE INDENT global par NEW_LINE if ( par [ x ] != x ) : NEW_LINE INDENT par [ x ] = find ( par [ x ] ) NEW_LINE DEDENT return par [ x ] NEW_LINE DEDENT |
Union function of Union Find Algorithm | def Union ( u : int , v : int ) -> None : NEW_LINE INDENT global par , rnk NEW_LINE x = find ( u ) NEW_LINE y = find ( v ) NEW_LINE if ( x == y ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( rnk [ x ] > rnk [ y ] ) : NEW_LINE INDENT par [ y ] = x NEW_LINE DEDENT elif ( rnk [ x ] < rnk [ y ] ) : NEW_LINE INDENT par [ x ] = y NEW_LINE DEDENT else : NEW_LINE INDENT par [ x ] = y NEW_LINE rnk [ y ] += 1 NEW_LINE DEDENT DEDENT |
Function to find the required spanning tree | def findSpanningTree ( deg : List [ int ] , n : int , m : int , g : List [ List [ int ] ] ) -> None : NEW_LINE INDENT global rnk , par NEW_LINE DEDENT |
Initialising parent of a node by itself | par = [ i for i in range ( n + 1 ) ] NEW_LINE rnk = [ 0 ] * ( n + 1 ) NEW_LINE |
Variable to store the node with maximum degree | max = 1 NEW_LINE |
Finding the node with maximum degree | for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( deg [ i ] > deg [ max ] ) : NEW_LINE INDENT max = i NEW_LINE DEDENT DEDENT |
Union of all edges incident on vertex with maximum degree | for v in g [ max ] : NEW_LINE INDENT print ( " { } β { } " . format ( max , v ) ) NEW_LINE Union ( max , v ) NEW_LINE DEDENT |
Carrying out normal Kruskal Algorithm | for u in range ( 1 , n + 1 ) : NEW_LINE INDENT for v in g [ u ] : NEW_LINE INDENT x = find ( u ) NEW_LINE y = find ( v ) NEW_LINE if ( x == y ) : NEW_LINE INDENT continue NEW_LINE DEDENT Union ( x , y ) NEW_LINE print ( " { } β { } " . format ( u , v ) ) NEW_LINE DEDENT DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Number of nodes | n = 5 NEW_LINE |
Number of edges | m = 5 NEW_LINE |
ArrayList to store the graph | g = [ [ ] for _ in range ( n + 1 ) ] NEW_LINE |
Array to store the degree of each node in the graph | deg = [ 0 ] * ( n + 1 ) NEW_LINE |
Add edges and update degrees | g [ 1 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 1 ) NEW_LINE deg [ 1 ] += 1 NEW_LINE deg [ 2 ] += 1 NEW_LINE g [ 1 ] . append ( 5 ) NEW_LINE g [ 5 ] . append ( 1 ) NEW_LINE deg [ 1 ] += 1 NEW_LINE deg [ 5 ] += 1 NEW_LINE g [ 2 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 2 ) NEW_LINE deg [ 2 ] += 1 NEW_LINE deg [ 3 ] += 1 NEW_LINE g [ 5 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 5 ) NEW_LINE deg [ 3 ] += 1 NEW_LINE deg [ 5 ] += 1 NEW_LINE g [ 3 ] . append ( 4 ) NEW_LINE g [ 4 ] . append ( 3 ) NEW_LINE deg [ 3 ] += 1 NEW_LINE deg [ 4 ] += 1 NEW_LINE findSpanningTree ( deg , n , m , g ) NEW_LINE |
Function to find the maximum possible sum | def Maxsum ( c1 , c2 , c3 , c4 ) : NEW_LINE |
To store required sum | sum = 0 NEW_LINE |
Number of 234 's can be formed | two34 = min ( c2 , min ( c3 , c4 ) ) NEW_LINE |
Sum obtained with 234 s | sum = two34 * 234 NEW_LINE |
Remaining 2 's | c2 -= two34 NEW_LINE |
Sum obtained with 12 s | sum += min ( c2 , c1 ) * 12 NEW_LINE |
Return the required sum | return sum NEW_LINE |
Driver Code | c1 = 5 ; c2 = 2 ; c3 = 3 ; c4 = 4 NEW_LINE print ( Maxsum ( c1 , c2 , c3 , c4 ) ) NEW_LINE |
Function to print the array elements | def printArray ( N , arr ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT |
Function to replace all elements with absolute difference of absolute sums of positive and negative elements | def replacedArray ( N , arr ) : NEW_LINE INDENT pos_sum = 0 NEW_LINE neg_sum = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE DEDENT |
calculate difference of both sums | diff = abs ( pos_sum ) - abs ( neg_sum ) NEW_LINE |
if i - th element is positive , add it to positive sum | if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_sum = pos_sum + arr [ i ] NEW_LINE DEDENT |
if i - th element is negative , add it to negative sum | else : NEW_LINE INDENT neg_sum = neg_sum + arr [ i ] NEW_LINE DEDENT |
replace i - th elements with absolute difference | arr [ i ] = abs ( diff ) NEW_LINE |
Driver Code | N = 5 NEW_LINE arr = [ 1 , - 1 , 2 , 3 , - 2 ] NEW_LINE replacedArray ( N , arr ) NEW_LINE printArray ( N , arr ) NEW_LINE N = 6 NEW_LINE arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] NEW_LINE replacedArray ( N , arr1 ) NEW_LINE printArray ( N , arr1 ) NEW_LINE |
Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . | def findCountOfPairs ( a , b , n ) : NEW_LINE INDENT ans = 0 NEW_LINE DEDENT |
pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) | ans += n * int ( a / n ) * int ( b / n ) NEW_LINE |
pairs from 1 to n * ( a / n ) and n * ( b / n ) to b | ans += int ( a / n ) * ( b % n ) NEW_LINE |
pairs from n * ( a / n ) to a and 1 to n * ( b / n ) | ans += ( a % n ) * int ( b / n ) NEW_LINE |
pairs from n * ( a / n ) to a and n * ( b / n ) to b | ans += int ( ( ( a % n ) + ( b % n ) ) / n ) ; NEW_LINE |
Return answer | return ans NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE b = 13 NEW_LINE n = 3 NEW_LINE print ( findCountOfPairs ( a , b , n ) ) NEW_LINE DEDENT |
Function to find the required array | def findArray ( N , P ) : NEW_LINE |
calculating minimum possible sum | ans = ( P * ( P + 1 ) ) // 2 + ( N - P ) ; NEW_LINE |
Array | arr = [ 0 ] * ( N + 1 ) ; NEW_LINE |
place first P natural elements | for i in range ( 1 , P + 1 ) : NEW_LINE INDENT arr [ i ] = i ; NEW_LINE DEDENT |
Fill rest of the elements with 1 | for i in range ( P + 1 , N + 1 ) : NEW_LINE INDENT arr [ i ] = 1 ; NEW_LINE DEDENT print ( " The β Minimum β Possible β Sum β is : β " , ans ) ; NEW_LINE print ( " The β Array β Elements β are : β " ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) ; NEW_LINE DEDENT |
Driver Code | N = 5 ; NEW_LINE P = 3 ; NEW_LINE findArray ( N , P ) ; NEW_LINE |
Function to print the intersection | def findIntersection ( intervals , N ) : NEW_LINE |
First interval | l = intervals [ 0 ] [ 0 ] NEW_LINE r = intervals [ 0 ] [ 1 ] NEW_LINE |
Check rest of the intervals and find the intersection | for i in range ( 1 , N ) : NEW_LINE |
If no intersection exists | if ( intervals [ i ] [ 0 ] > r or intervals [ i ] [ 1 ] < l ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT |
Else update the intersection | else : NEW_LINE INDENT l = max ( l , intervals [ i ] [ 0 ] ) NEW_LINE r = min ( r , intervals [ i ] [ 1 ] ) NEW_LINE DEDENT print ( " [ " , l , " , β " , r , " ] " ) NEW_LINE |
Driver code | intervals = [ [ 1 , 6 ] , [ 2 , 8 ] , [ 3 , 10 ] , [ 5 , 8 ] ] NEW_LINE N = len ( intervals ) NEW_LINE findIntersection ( intervals , N ) NEW_LINE |
Function that compares a and b | def cmp ( a , b ) : NEW_LINE INDENT return ( a > b ) - ( a < b ) NEW_LINE DEDENT |
Function to return length of longest subarray that satisfies one of the given conditions | def maxSubarraySize ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE ans = 1 NEW_LINE anchor = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT c = cmp ( arr [ i - 1 ] , arr [ i ] ) NEW_LINE if c == 0 : NEW_LINE INDENT anchor = i NEW_LINE DEDENT elif i == N - 1 or c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 : NEW_LINE INDENT ans = max ( ans , i - anchor + 1 ) NEW_LINE anchor = i NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT |
Driver program | arr = [ 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ] NEW_LINE |
Print the required answer | print ( maxSubarraySize ( arr ) ) NEW_LINE |
Function to return the count of the required sub - strings | def maxSubStrings ( s , k ) : NEW_LINE INDENT maxSubStr = 0 NEW_LINE n = len ( s ) NEW_LINE DEDENT |
Iterate over all characters | for c in range ( 27 ) : NEW_LINE INDENT ch = chr ( ord ( ' a ' ) + c ) NEW_LINE DEDENT |
Count with current character | curr = 0 NEW_LINE for i in range ( n - k ) : NEW_LINE INDENT if ( s [ i ] != ch ) : NEW_LINE INDENT continue NEW_LINE DEDENT cnt = 0 NEW_LINE while ( i < n and s [ i ] == ch and cnt != k ) : NEW_LINE INDENT i += 1 NEW_LINE cnt += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT |
If the substring has a length k then increment count with current character | if ( cnt == k ) : NEW_LINE INDENT curr += 1 NEW_LINE DEDENT |
Update max count | maxSubStr = max ( maxSubStr , curr ) NEW_LINE return maxSubStr NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaacaabbaa " NEW_LINE k = 2 NEW_LINE print ( maxSubStrings ( s , k ) ) NEW_LINE DEDENT |
Function to return total valid pairs | def ValidPairs ( arr ) : NEW_LINE |
Initialize count of all the elements | count = [ 0 ] * 121 NEW_LINE |
frequency count of all the elements | for ele in arr : NEW_LINE INDENT count [ ele ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for eleX , countX in enumerate ( count ) : NEW_LINE INDENT for eleY , countY in enumerate ( count ) : NEW_LINE INDENT if eleX < eleY : NEW_LINE INDENT continue NEW_LINE DEDENT if ( abs ( eleX - eleY ) % 2 == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT |
Add total valid pairs | ans += countX * countY NEW_LINE if eleX == eleY : NEW_LINE |
Exclude pairs made with a single element i . e . ( x , x ) | ans -= countX NEW_LINE return ans NEW_LINE |
Driver Code | arr = [ 16 , 17 , 18 ] NEW_LINE |
Function call to print required answer | print ( ValidPairs ( arr ) ) NEW_LINE |
Function to check The validity of distribution | def check_distribution ( n , k , age , candy ) : NEW_LINE |
Stroring the max age of all students + 1 | mxage = max ( age ) + 1 NEW_LINE |
Stroring the max candy + 1 | mxcandy = max ( candy ) + 1 NEW_LINE fr1 = [ 0 ] * mxage NEW_LINE fr2 = [ 0 ] * mxcandy NEW_LINE |
creating the frequency array of the age of students | for j in range ( n ) : NEW_LINE INDENT fr1 [ age [ j ] ] += 1 NEW_LINE DEDENT |
Creating the frequency array of the packets of candies | for j in range ( k ) : NEW_LINE INDENT fr2 [ candy [ j ] ] += 1 NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.