codeparrot/xlcost-text-to-code huggingface dataset:
Numbers of pairs from an array whose average is also present in the array | Function to count the number of pairs from the array having sum S ; Stores the total count of pairs whose sum is 2 * S ; Generate all possible pairs and check their sums ; If the sum is S , then increment the count ; Return the total count of pairs ; Function to count of pairs having whose average exists in the array ; Initialize the count ; Use set to remove duplicates ; Add elements in the set ; For every sum , count all possible pairs ; Return the total count ; Driver Code
def getCountPairs ( arr , N , S ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( arr ) ) : NEW_LINE INDENT if ( ( arr [ i ] + arr [ j ] ) == S ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def countPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE S = set ( [ ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( arr [ i ] ) NEW_LINE DEDENT for ele in S : NEW_LINE INDENT sum = 2 * ele NEW_LINE count += getCountPairs ( arr , N , sum ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2 | Python3 program for the above approach ; Function to check if N is power of 2 or not ; Base Case ; Return true if N is power of 2 ; Function to print array of strings in ascending order ; Sort strings in ascending order ; Print the array ; Function to sort the strings after modifying each string according to the given conditions ; Store the frequency of each characters of the string ; Stores the required array of strings ; Traverse the array of strings ; Temporary string ; Stores frequency of each alphabet of the string ; Update frequency of S [ i ] [ j ] ; Traverse the map freq ; Check if the frequency of i . first is a power of 2 ; Update string st ; Clear the map ; Null string ; Sort the string in descending order ; Update res ; Print the array of strings ; Driver Code
from collections import defaultdict NEW_LINE import math NEW_LINE def isPowerOfTwo ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( math . ceil ( math . log2 ( n ) ) == math . floor ( math . log2 ( n ) ) ) NEW_LINE DEDENT def printArray ( res ) : NEW_LINE INDENT res . sort ( ) NEW_LINE for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def sortedStrings ( S , N ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE res = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT st = " " NEW_LINE for j in range ( len ( S [ i ] ) ) : NEW_LINE INDENT freq [ S [ i ] [ j ] ] += 1 NEW_LINE DEDENT for i in freq : NEW_LINE INDENT if ( isPowerOfTwo ( freq [ i ] ) ) : NEW_LINE INDENT for j in range ( freq [ i ] ) : NEW_LINE INDENT st += i NEW_LINE DEDENT DEDENT DEDENT freq . clear ( ) NEW_LINE if ( len ( st ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT st = list ( st ) NEW_LINE st . sort ( reverse = True ) NEW_LINE st = ' ' . join ( st ) NEW_LINE res . append ( st ) NEW_LINE DEDENT printArray ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " aaacbb " , " geeks " , " aaa " ] NEW_LINE N = len ( arr ) NEW_LINE sortedStrings ( arr , N ) NEW_LINE DEDENT
Modify string by replacing all occurrences of given characters by specified replacing characters | Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver Code
def replaceCharacters ( s , p ) : NEW_LINE INDENT n , k = len ( s ) , len ( p ) NEW_LINE arr = [ 0 ] * 26 NEW_LINE brr = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ] NEW_LINE brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ] NEW_LINE DEDENT for j in range ( k ) : NEW_LINE INDENT a , b = p [ j ] [ 0 ] , p [ j ] [ 1 ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( arr [ i ] == a ) : NEW_LINE INDENT brr [ i ] = b NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabbgg " NEW_LINE P = [ [ ' a ' , ' b ' ] , [ ' b ' , ' g ' ] , [ ' g ' , ' a ' ] ] NEW_LINE replaceCharacters ( S , P ) NEW_LINE DEDENT
Smallest Semi | Python3 program for the above approach ; Function to find all the prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the smallest semi - prime number having a difference between any of its two divisors at least N ; Stores the prime numbers ; Fill the prime array ; Initialize the first divisor ; Find the value of the first prime number ; Initialize the second divisor ; Find the second prime number ; Print the semi - prime number ; Driver Code
MAX = 100001 NEW_LINE def SieveOfEratosthenes ( prime ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p < MAX : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def smallestSemiPrime ( n ) : NEW_LINE INDENT prime = [ True ] * MAX NEW_LINE SieveOfEratosthenes ( prime ) NEW_LINE num1 = n + 1 NEW_LINE while ( prime [ num1 ] != True ) : NEW_LINE INDENT num1 += 1 NEW_LINE DEDENT num2 = num1 + n NEW_LINE while ( prime [ num2 ] != True ) : NEW_LINE INDENT num2 += 1 NEW_LINE DEDENT print ( num1 * num2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE smallestSemiPrime ( N ) NEW_LINE DEDENT
Check if a number can be represented as product of two positive perfect cubes | Function to check if N can be represented as the product of two perfect cubes or not ; Stores the perfect cubes ; Traverse the Map ; Stores the first number ; Stores the second number ; Search the pair for the first number to obtain product N from the Map ; If N cannot be represented as the product of the two positive perfect cubes ; Driver Code
def productOfTwoPerfectCubes ( N ) : NEW_LINE INDENT cubes = { } NEW_LINE i = 1 NEW_LINE while i * i * i <= N : NEW_LINE INDENT cubes [ i * i * i ] = i NEW_LINE i += 1 NEW_LINE DEDENT for itr in cubes : NEW_LINE INDENT firstNumber = itr NEW_LINE if ( N % itr == 0 ) : NEW_LINE INDENT secondNumber = N // itr NEW_LINE if ( secondNumber in cubes ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " No " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 216 NEW_LINE productOfTwoPerfectCubes ( N ) NEW_LINE DEDENT
Check if a number can be represented as product of two positive perfect cubes | Function to check if the number N can be represented as the product of two perfect cubes or not ; If cube of cube_root is N ; Otherwise , prNo ; Driver Code
def productOfTwoPerfectCubes ( N ) : NEW_LINE INDENT cube_root = round ( ( N ) ** ( 1 / 3 ) ) NEW_LINE print ( cube_root ) NEW_LINE if ( cube_root * cube_root * cube_root == N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 216 NEW_LINE productOfTwoPerfectCubes ( N ) NEW_LINE DEDENT
Size of smallest square that contains N non | Function to check if side of square X can pack all the N rectangles or not ; Find the number of rectangle it can pack ; If val is atleast N , then return true ; Otherwise , return false ; Function to find the size of the smallest square that can contain N rectangles of dimensions W * H ; Stores the lower bound ; Stores the upper bound ; Iterate until i is less than j ; Calculate the mid value ; If the current size of square cam contain N rectangles ; Otherwise , update i ; Return the minimum size of the square required ; Driver Code ; Function Call
def bound ( w , h , N , x ) : NEW_LINE INDENT val = ( x // w ) * ( x // h ) NEW_LINE if ( val >= N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def FindSquare ( N , W , H ) : NEW_LINE INDENT i = 1 NEW_LINE j = W * H * N NEW_LINE while ( i < j ) : NEW_LINE INDENT mid = i + ( j - i ) // 2 NEW_LINE if ( bound ( W , H , N , mid ) ) : NEW_LINE INDENT j = mid NEW_LINE DEDENT else : NEW_LINE INDENT i = mid + 1 NEW_LINE DEDENT DEDENT return j NEW_LINE DEDENT W = 2 NEW_LINE H = 3 NEW_LINE N = 10 NEW_LINE print ( FindSquare ( N , W , H ) ) NEW_LINE
Count integers up to N that are equal to at least 2 nd power of any integer exceeding 1 | Python 3 program for the above approach ; Function to count the integers up to N that can be represented as a ^ b , where a & b > 1 ; Initialize a HashSet ; Iterating over the range [ 2 , sqrt ( N ) ] ; Generate all possible power of x ; Multiply x by i ; If the generated number lies in the range [ 1 , N ] then insert it in HashSet ; Print the total count ; Driver code
from math import sqrt NEW_LINE def printNumberOfPairs ( N ) : NEW_LINE INDENT st = set ( ) NEW_LINE for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT x = i NEW_LINE while ( x <= N ) : NEW_LINE INDENT x *= i NEW_LINE if ( x <= N ) : NEW_LINE INDENT st . add ( x ) NEW_LINE DEDENT DEDENT DEDENT print ( len ( st ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10000 NEW_LINE printNumberOfPairs ( N ) NEW_LINE DEDENT
Maximize product of lengths of strings having no common characters | Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver Code
def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT count += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def maximumProduct ( words ) : NEW_LINE INDENT bits = [ 0 for i in range ( len ( words ) ) ] NEW_LINE for i in range ( len ( words ) ) : NEW_LINE INDENT for j in range ( len ( words [ i ] ) ) : NEW_LINE INDENT bits [ i ] = bits [ i ] | 1 << ( ord ( words [ i ] [ j ] ) - 97 ) NEW_LINE DEDENT DEDENT result = 0 NEW_LINE for i in range ( len ( bits ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( bits ) ) : NEW_LINE INDENT if ( ( bits [ i ] & bits [ j ] ) == 0 ) : NEW_LINE INDENT L = countSetBits ( bits [ i ] ) NEW_LINE R = countSetBits ( bits [ j ] ) NEW_LINE result = max ( L * R , result ) NEW_LINE DEDENT DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " abcw " , " baz " , " foo " , " bar " , " xtfn " , " abcdef " ] NEW_LINE maximumProduct ( arr ) NEW_LINE DEDENT
Modify a sentence by reversing order of occurrences of all Palindrome Words | Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code
def palindrome ( string ) : NEW_LINE INDENT if ( string == string [ : : - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printReverse ( sentence ) : NEW_LINE INDENT newlist = [ ] NEW_LINE lis = list ( sentence . split ( ) ) NEW_LINE for i in lis : NEW_LINE INDENT if ( palindrome ( i ) ) : NEW_LINE INDENT newlist . append ( i ) NEW_LINE DEDENT DEDENT newlist . reverse ( ) NEW_LINE j = 0 NEW_LINE for i in range ( len ( lis ) ) : NEW_LINE INDENT if ( palindrome ( lis [ i ] ) ) : NEW_LINE INDENT lis [ i ] = newlist [ j ] NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT for i in lis : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " NEW_LINE printReverse ( sentence ) NEW_LINE
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code
def minCount ( A , B , N ) : NEW_LINE INDENT i = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] == B [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT return N - i NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 1 , 5 , 2 , 3 , 4 ] NEW_LINE N = len ( A ) NEW_LINE print ( minCount ( A , B , N ) ) NEW_LINE DEDENT
Minimize maximum array element possible by at most K splits on the given array | Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
def possible ( A , N , mid , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += ( A [ i ] - 1 ) // mid NEW_LINE DEDENT return count <= K NEW_LINE DEDENT def minimumMaximum ( A , N , K ) : NEW_LINE INDENT lo = 1 NEW_LINE hi = max ( A ) NEW_LINE while ( lo < hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE if ( possible ( A , N , mid , K ) ) : NEW_LINE INDENT hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT return hi NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 8 , 2 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( minimumMaximum ( arr , N , K ) ) NEW_LINE DEDENT
Maximum value of X such that difference between any array element and X does not exceed K | Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
def maximumNumber ( arr , N , K ) : NEW_LINE INDENT minimum = min ( arr ) NEW_LINE ans = minimum + K NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( arr [ i ] - ans ) > K ) : NEW_LINE INDENT ans = - 1 NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE maximumNumber ( arr , N , K ) NEW_LINE DEDENT
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Python3 program for the above approach ; Function to check if a number is prime or not ; Base Cases ; Check if n is divisible by 2 or 3 ; Iterate over every 6 number from the range [ 5 , sqrt ( N ) ] ; If n is found to be non - prime ; Otherwise , return true ; Function to check if a number can be represented as the sum of a prime number and a perfect square or not ; Stores all perfect squares less than N ; Store the perfect square in the array ; Iterate over all perfect squares ; Store the difference of perfect square from n ; If difference is prime ; Update flag ; Break out of the loop ; If N is the sum of a prime number and a perfect square ; Driver Code
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def sumOfPrimeSquare ( n ) : NEW_LINE INDENT i = 0 NEW_LINE squares = [ ] NEW_LINE while ( i * i < n ) : NEW_LINE INDENT squares . append ( i * i ) NEW_LINE i += 1 NEW_LINE DEDENT flag = False NEW_LINE for i in range ( len ( squares ) ) : NEW_LINE INDENT difference = n - squares [ i ] NEW_LINE if ( isPrime ( difference ) ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 27 NEW_LINE sumOfPrimeSquare ( N ) NEW_LINE DEDENT
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Python3 program for the above approach ; Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Code
import math NEW_LINE def SieveOfEratosthenes ( prime , n ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( n ** ( 1 / 2 ) ) ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p ** 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumOfPrimeSquare ( n ) : NEW_LINE INDENT flag = False NEW_LINE prime = [ True ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( prime , n ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( not prime [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT dif = n - i NEW_LINE if ( math . ceil ( dif ** ( 1 / 2 ) ) == math . floor ( dif ** ( 1 / 2 ) ) ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 27 NEW_LINE sumOfPrimeSquare ( N ) NEW_LINE DEDENT
Check if any subarray of length M repeats at least K times consecutively or not | Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver Code
def check ( arr , M , K , ind ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( 1 , K , 1 ) : NEW_LINE INDENT if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def SubarrayRepeatsKorMore ( arr , N , M , K ) : NEW_LINE INDENT for ind in range ( N - M * K + 1 ) : NEW_LINE INDENT if ( check ( arr , M , K , ind ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ] NEW_LINE M = 2 NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if any subarray of length M repeats at least K times consecutively or not | Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code
def checkExists ( arr , N , M , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N - M ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + M ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 NEW_LINE DEDENT if ( count == M * ( K - 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 2 , 1 , 1 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE K = 2 NEW_LINE if ( checkExists ( arr , N , M , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Number of common digits present in two given numbers | Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Driver Code ; Input
def CommonDigits ( N , M ) : NEW_LINE INDENT count = 0 NEW_LINE freq1 = [ 0 ] * 10 NEW_LINE freq2 = [ 0 ] * 10 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT freq1 [ N % 10 ] += 1 NEW_LINE N = N // 10 NEW_LINE DEDENT while ( M > 0 ) : NEW_LINE INDENT freq2 [ M % 10 ] += 1 NEW_LINE M = M // 10 NEW_LINE DEDENT for i in range ( 10 ) : NEW_LINE INDENT if ( freq1 [ i ] > 0 and freq2 [ i ] > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 748294 NEW_LINE M = 34298156 NEW_LINE print ( CommonDigits ( N , M ) ) NEW_LINE DEDENT
Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
def minimumNumber ( K , X ) : NEW_LINE INDENT if ( K > X ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT ans = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( K , X + 1 ) : NEW_LINE INDENT sum += i NEW_LINE if ( sum >= X ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT K = 5 NEW_LINE X = 13 NEW_LINE minimumNumber ( K , X ) NEW_LINE