text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given Input | A = [ 5 , 4 , 3 , 1 , 2 ] NEW_LINE K = 3 NEW_LINE N = len ( A ) NEW_LINE print ( MinimumOperations ( A , N , K ) ) NEW_LINE |
python 3 program for the above approach Function to check prime . | from math import sqrt NEW_LINE |
Function to check prime . | def is_prime ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE DEDENT DEDENT |
It means it is not a prime | return False NEW_LINE |
No factor other than 1 therefore prime number | return True NEW_LINE |
Function to find out the required consecutive primes . | def consecutive_primes ( n ) : NEW_LINE INDENT first = - 1 NEW_LINE second = - 1 NEW_LINE DEDENT |
Finding first prime just less than sqrt ( n ) . | i = int ( sqrt ( n ) ) NEW_LINE while ( i >= 2 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT first = i NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT |
Finding prime just greater than sqrt ( n ) . | for i in range ( int ( sqrt ( n ) ) + 1 , n // 2 + 1 , 1 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT second = i NEW_LINE break NEW_LINE DEDENT DEDENT |
Product of both prime is greater than n then print it | if ( first * second >= n ) : NEW_LINE INDENT print ( first , second ) NEW_LINE DEDENT |
Finding prime greater than second | else : NEW_LINE INDENT for i in range ( second + 1 , n + 1 , 1 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT print ( second , i ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT |
Driver Program | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE consecutive_primes ( n ) NEW_LINE DEDENT |
Python program to implement the above approach | from collections import Counter NEW_LINE |
Function to construct the original set of digits from the string in ascending order | def construct_digits ( s ) : NEW_LINE |
Store the unique characters corresponding to word and number | k = [ " z " , " w " , " u " , " x " , " g " , " h " , " o " , " f " , " v " , " i " ] NEW_LINE l = [ " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " ] NEW_LINE c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] NEW_LINE |
Store the required result | ans = [ ] NEW_LINE |
Store the frequency of each character of S | d = Counter ( s ) NEW_LINE |
Traverse the unique characters | for i in range ( len ( k ) ) : NEW_LINE |
Store the count of k [ i ] in S | x = d . get ( k [ i ] , 0 ) NEW_LINE |
Traverse the corresponding word | for j in range ( len ( l [ i ] ) ) : NEW_LINE |
Decrement the frequency of characters by x | d [ l [ i ] [ j ] ] -= x NEW_LINE |
Append the digit x times to ans | ans . append ( str ( c [ i ] ) * x ) NEW_LINE |
Sort the digits in ascending order | ans . sort ( ) NEW_LINE return " " . join ( ans ) NEW_LINE |
Given string , s | s = " fviefuro " NEW_LINE |
Function Call | print ( construct_digits ( s ) ) NEW_LINE |
Function to count minimum number of operations required | n = 3 NEW_LINE m = 3 NEW_LINE def countDecrements ( arr ) : NEW_LINE INDENT count_1 = 0 NEW_LINE count_2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE DEDENT DEDENT |
Case 1 : | if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT |
Case 2 : | if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) : NEW_LINE INDENT count_2 += 1 NEW_LINE DEDENT |
Print the minimum number of operations required | print ( min ( count_1 , count_2 ) ) NEW_LINE |
The given matrix | arr = [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ] NEW_LINE |
Function Call to count the minimum number of decrements required | countDecrements ( arr ) NEW_LINE |
Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) | def check ( X , Y ) : NEW_LINE |
Conditions for possible conversion | if ( X > 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 1 and Y == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 2 and Y <= 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( X == 3 and Y <= 3 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT |
Otherwise , conversion is not possible | else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 6 NEW_LINE Y = 8 NEW_LINE check ( X , Y ) NEW_LINE DEDENT |
Function to count distinct sum of pairs possible from the range [ L , R ] | def distIntegers ( L , R ) : NEW_LINE |
Return the count of distinct sum of pairs | return 2 * R - 2 * L + 1 NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 3 , 8 NEW_LINE print ( distIntegers ( L , R ) ) NEW_LINE DEDENT |
Function to count subarrays having even Bitwise XOR | def evenXorSubarray ( arr , n ) : NEW_LINE |
Store the required result | ans = 0 NEW_LINE |
Stores count of subarrays with even and odd XOR values | freq = [ 0 ] * n NEW_LINE |
Stores Bitwise XOR of current subarray | XOR = 0 NEW_LINE |
Traverse the array | for i in range ( n ) : NEW_LINE |
Update current Xor | XOR = XOR ^ arr [ i ] NEW_LINE |
If XOR is even | if ( XOR % 2 == 0 ) : NEW_LINE |
Update ans | ans += freq [ 0 ] + 1 NEW_LINE |
Increment count of subarrays with even XOR | freq [ 0 ] += 1 NEW_LINE else : NEW_LINE |
Otherwise , increment count of subarrays with odd XOR | ans += freq [ 1 ] NEW_LINE freq [ 1 ] += 1 NEW_LINE |
Print the result | print ( ans ) NEW_LINE |
Given array | arr = [ 1 , 2 , 3 , 4 ] NEW_LINE |
Stores the size of the array | N = len ( arr ) NEW_LINE evenXorSubarray ( arr , N ) NEW_LINE |
Python3 program for the above approach | from math import sqrt NEW_LINE |
Function to count the occurrences of X in the generated square matrix | def countOccurrences ( N , X ) : NEW_LINE |
Stores the required result | count = 0 NEW_LINE |
Iterate upto square root of X | for i in range ( 1 , int ( sqrt ( X ) ) + 1 ) : NEW_LINE |
Check if i divides X | if X % i == 0 : NEW_LINE |
Store the quotient obtained on dividing X by i | a = i NEW_LINE b = X // i NEW_LINE |
If both the numbers fall in the range , update count | if a <= N and b <= N : NEW_LINE INDENT if a == b : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 2 NEW_LINE DEDENT DEDENT |
Return the result | return count NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given N and X | N = 7 NEW_LINE X = 12 NEW_LINE |
Function Call | print ( countOccurrences ( N , X ) ) NEW_LINE |
Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 | def findArrayWithMaxProduct ( arr , N ) : NEW_LINE |
Traverse the array | for i in range ( N ) : NEW_LINE |
Applying the operation on all the positive elements | if ( arr [ i ] >= 0 ) : NEW_LINE INDENT arr [ i ] = - arr [ i ] - 1 NEW_LINE DEDENT if ( N % 2 == 1 ) : NEW_LINE |
Stores maximum element in array | max_element = - 1 NEW_LINE |
Stores index of maximum element | index = - 1 NEW_LINE for i in range ( N ) : NEW_LINE |
Check if current element is greater than the maximum element | if ( abs ( arr [ i ] ) > max_element ) : NEW_LINE INDENT max_element = abs ( arr [ i ] ) NEW_LINE DEDENT |
Find index of the maximum element | index = i NEW_LINE |
Perform the operation on the maximum element | arr [ index ] = - arr [ index ] - 1 NEW_LINE |
Prthe elements of the array | for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 3 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE DEDENT |
Function Call | findArrayWithMaxProduct ( arr , N ) NEW_LINE |
Function to print the lexicographically smallest permutation with K perfect indices | def findPerfectIndex ( N , K ) : NEW_LINE |
Iterator to traverse the array | i = 0 NEW_LINE |
Traverse first K array indices | for i in range ( K ) : NEW_LINE INDENT print ( ( N - K + 1 ) + i , end = " ▁ " ) NEW_LINE DEDENT |
Traverse remaining indices | for i in range ( 3 , N ) : NEW_LINE INDENT print ( i - K + 1 , end = " ▁ " ) NEW_LINE DEDENT |
Driver Code | N = 10 NEW_LINE K = 3 NEW_LINE findPerfectIndex ( N , K ) NEW_LINE |
Function to count pairs made up of elements from the range [ L , R ] having distinct sum | def countPairs ( L , R ) : NEW_LINE |
Stores the least sum which can be formed by the pairs | firstNum = 2 * L NEW_LINE |
Stores the highest sum which can be formed by the pairs | lastNum = 2 * R NEW_LINE |
Stores the count of pairs having distinct sum | Cntpairs = lastNum - firstNum + 1 NEW_LINE |
Print the count of pairs | print ( Cntpairs ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 2 , 3 NEW_LINE DEDENT |
Function call to count the number of pairs having distinct sum in the range [ L , R ] | countPairs ( L , R ) NEW_LINE |
Python 3 program for the above approach | from collections import defaultdict NEW_LINE |
Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND | def countPairs ( A , n ) : NEW_LINE |
Total number of pairs possible from the array | count = ( n * ( n - 1 ) ) // 2 NEW_LINE |
Stores frequency of each array element | ump = defaultdict ( int ) NEW_LINE |
Traverse the array A [ ] | for i in range ( n ) : NEW_LINE |
Increment ump [ A [ i ] ] by 1 | ump [ A [ i ] ] += 1 NEW_LINE |
Traverse the Hashmap ump | for it in ump . keys ( ) : NEW_LINE |
Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) | c = ump [ it ] NEW_LINE count = count - ( c * ( c - 1 ) ) // 2 NEW_LINE |
Print the result | print ( count ) NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 4 , 7 ] NEW_LINE N = len ( A ) NEW_LINE DEDENT |
Function Call | countPairs ( A , N ) NEW_LINE |
Function to construct binary string according to the given conditions | def constructBinaryString ( arr , N , K ) : NEW_LINE |
Initialize with 1 | bit = 1 NEW_LINE |
Traverse the array | for i in range ( 0 , N ) : NEW_LINE |
To check if the i - th eger needs to be considered or not | bit |= bit << arr [ i ] NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.