codeparrot/xlcost-text-to-code huggingface dataset:
Maximum length palindromic substring for every index such that it starts and ends at that index | Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code
def isPalindrome ( S , i , j ) : NEW_LINE INDENT while ( i < j ) : NEW_LINE INDENT if ( S [ i ] != S [ j ] ) : NEW_LINE return False NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def printLongestPalindrome ( S , N ) : NEW_LINE INDENT palLength = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT maxlength = 1 NEW_LINE for j in range ( i ) : NEW_LINE if ( S [ j ] == S [ i ] ) : NEW_LINE INDENT if ( isPalindrome ( S , j , i ) ) : NEW_LINE maxlength = i - j + 1 NEW_LINE break NEW_LINE DEDENT j = N - 1 NEW_LINE while ( j > i ) : NEW_LINE if ( S [ j ] == S [ i ] ) : NEW_LINE INDENT if ( isPalindrome ( S , i , j ) ) : NEW_LINE maxlength = max ( j - i + 1 , maxlength ) NEW_LINE break NEW_LINE DEDENT j -= 1 NEW_LINE palLength [ i ] = maxlength NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( palLength [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " bababa " NEW_LINE N = len ( S ) NEW_LINE printLongestPalindrome ( S , N ) NEW_LINE DEDENT
Sum of array elements which are multiples of a given number | Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the array ; Print total sum ; Driver Code ; Given arr [ ] ; Function call
def mulsum ( arr , n , N ) : NEW_LINE INDENT sums = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] % N == 0 : NEW_LINE INDENT sums = sums + arr [ i ] NEW_LINE DEDENT DEDENT print ( sums ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE N = 3 NEW_LINE mulsum ( arr , n , N ) NEW_LINE DEDENT
Farthest index that can be reached from the Kth index of given array by given operations | Function to find the farthest index that can be reached ; Declare a priority queue ; Iterate the array ; If current element is greater than the next element ; Otherwise , store their difference ; Push diff into pq ; If size of pq exceeds Y ; Decrease X by the top element of pq ; Remove top of pq ; If X is exhausted ; Current index is the farthest possible ; Print N - 1 as farthest index ; Driver Code ; Function Call
def farthestHill ( arr , X , Y , N , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( K , N - 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] >= arr [ i + 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT diff = arr [ i + 1 ] - arr [ i ] NEW_LINE pq . append ( diff ) NEW_LINE if ( len ( pq ) > Y ) : NEW_LINE INDENT X -= pq [ - 1 ] NEW_LINE pq [ - 1 ] NEW_LINE DEDENT if ( X < 0 ) : NEW_LINE INDENT print ( i ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( N - 1 ) NEW_LINE DEDENT arr = [ 4 , 2 , 7 , 6 , 9 , 14 , 12 ] NEW_LINE X = 5 NEW_LINE Y = 1 NEW_LINE K = 0 NEW_LINE N = len ( arr ) NEW_LINE farthestHill ( arr , X , Y , N , K ) NEW_LINE
Lexicographically largest string possible consisting of at most K consecutive similar characters | Function to return nearest lower character ; Traverse charset from start - 1 ; If no character can be appended ; Function to find largest string ; Stores the frequency of characters ; Traverse the string ; Append larger character ; Decrease count in charset ; Increase count ; Check if count reached to charLimit ; Find nearest lower char ; If no character can be appended ; Append nearest lower character ; Reset count for next calculation ; Return new largest string ; Driver code ; Given string s
def nextAvailableChar ( charset , start ) : NEW_LINE INDENT for i in range ( start - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( charset [ i ] > 0 ) : NEW_LINE INDENT charset [ i ] -= 1 NEW_LINE return chr ( i + ord ( ' a ' ) ) NEW_LINE DEDENT DEDENT return ' \0' NEW_LINE DEDENT def newString ( originalLabel , limit ) : NEW_LINE INDENT n = len ( originalLabel ) NEW_LINE charset = [ 0 ] * ( 26 ) NEW_LINE newStrings = " " NEW_LINE for i in originalLabel : NEW_LINE INDENT charset [ ord ( i ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 25 , - 1 , - 1 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( charset [ i ] > 0 ) : NEW_LINE INDENT newStrings += chr ( i + ord ( ' a ' ) ) NEW_LINE charset [ i ] -= 1 NEW_LINE count += 1 NEW_LINE if ( charset [ i ] > 0 and count == limit ) : NEW_LINE INDENT next = nextAvailableChar ( charset , i ) NEW_LINE if ( next == ' \0' ) : NEW_LINE INDENT return newStrings NEW_LINE DEDENT newStrings += next NEW_LINE count = 0 NEW_LINE DEDENT DEDENT DEDENT return newStrings NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ccbbb " NEW_LINE K = 2 NEW_LINE print ( newString ( S , K ) ) NEW_LINE DEDENT
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace odd elements with even elements and vice versa ; Length of the array ; Traverse the given array ; If arr [ i ] is visited ; Find the next odd element ; Find next even element ; Mark them visited ; Swap them ; Print the final array ; Driver Code ; Given array arr ; Function Call
def swapEvenOdd ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE o = - 1 NEW_LINE e = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT r = - 1 NEW_LINE if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT o += 1 NEW_LINE while ( arr [ o ] % 2 == 0 or arr [ o ] < 0 ) : NEW_LINE INDENT o += 1 NEW_LINE DEDENT r = o NEW_LINE DEDENT else : NEW_LINE INDENT e += 1 NEW_LINE while ( arr [ e ] % 2 == 1 or arr [ e ] < 0 ) : NEW_LINE INDENT e += 1 NEW_LINE DEDENT r = e NEW_LINE DEDENT arr [ i ] *= - 1 NEW_LINE arr [ r ] *= - 1 NEW_LINE tmp = arr [ i ] NEW_LINE arr [ i ] = arr [ r ] NEW_LINE arr [ r ] = tmp NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ( - 1 * arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 4 ] NEW_LINE swapEvenOdd ( arr ) NEW_LINE DEDENT
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Python3 program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ ▁ + b ' s right part in a variable a and a ' s ▁ ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False
def check ( a , b , n ) : NEW_LINE INDENT i , j = 0 , n - 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] != b [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE xa = a [ i : j + 1 ] NEW_LINE xb = b [ i : j + 1 ] NEW_LINE if ( xa == xa [ : : - 1 ] or xb == xb [ : : - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT a = " xbdef " NEW_LINE b = " cabex " NEW_LINE if check ( a , b , len ( a ) ) == True or check ( b , a , len ( a ) ) == True : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT
Maximize length of subarray having equal elements by adding at most K | Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Driver Code ; Function call
def maxEqualIdx ( arr , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE prefixSum = [ 0 ] * ( len ( arr ) + 1 ) NEW_LINE prefixSum [ 1 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , len ( prefixSum ) - 1 , 1 ) : NEW_LINE INDENT prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] NEW_LINE DEDENT max = len ( arr ) NEW_LINE min = 1 NEW_LINE ans = 1 NEW_LINE while ( min <= max ) : NEW_LINE INDENT mid = ( max + min ) // 2 NEW_LINE if ( check ( prefixSum , mid , k , arr ) ) : NEW_LINE INDENT ans = mid NEW_LINE min = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT max = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def check ( pSum , lenn , k , a ) : NEW_LINE INDENT i = 0 NEW_LINE j = lenn NEW_LINE while ( j <= len ( a ) ) : NEW_LINE INDENT maxSize = a [ j - 1 ] NEW_LINE totalNumbers = maxSize * lenn NEW_LINE currNumbers = pSum [ j ] - pSum [ i ] NEW_LINE if ( currNumbers + k >= totalNumbers ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 1 , 1 , 1 ] NEW_LINE k = 7 NEW_LINE print ( maxEqualIdx ( arr , k ) ) NEW_LINE
Sum of product of all pairs of a Binary Array | Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; Stores the size of the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code
def productSum ( arr ) : NEW_LINE INDENT cntOne = 0 NEW_LINE N = len ( arr ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cntOne += 1 NEW_LINE DEDENT DEDENT return cntOne * ( cntOne - 1 ) // 2 NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 , 1 ] NEW_LINE print ( productSum ( arr ) ) NEW_LINE
Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | ''Python program for the above approach ; ''Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ;  ;  ; '' Add the digits of num2 ; ''If the sum of the digits of N  is less than or equal to K ; '' No number needs to  be added ; '' Otherwise ; '' Calculate the sum of digits  from least significant digit ; '' If sum exceeds K ; '' Increase previous  digit by 1 ; '' Add zeros to the end ; '' Calculate difference  between the result and N ; '' Return the result ; ''Driver Code
import math ; NEW_LINE def minDigits ( N , K ) : NEW_LINE ' ▁ Find ▁ the ▁ number ▁ of ▁ digits ' ' ' NEW_LINE digits_num = int ( math . floor ( math . log ( N ) + 1 ) ) ; NEW_LINE INDENT temp_sum = 0 ; NEW_LINE temp = digits_num ; NEW_LINE result = 0 ; NEW_LINE X = 0 ; var = 0 ; NEW_LINE sum1 = 0 ; NEW_LINE num2 = N ; NEW_LINE DEDENT ' Calculate ▁ sum ▁ of ▁ the ▁ digits ' ' ' NEW_LINE INDENT while ( num2 != 0 ) : NEW_LINE sum1 += num2 % 10 ; NEW_LINE num2 /= 10 ; NEW_LINE DEDENT if ( sum1 <= K ) : NEW_LINE INDENT X = 0 ; NEW_LINE else : NEW_LINE DEDENT while ( temp > 0 ) : NEW_LINE var = int ( N // ( pow ( 10 , temp - 1 ) ) ) ; NEW_LINE INDENT temp_sum += var % 10 ; NEW_LINE if ( temp_sum >= K ) : NEW_LINE var = var // 10 ; NEW_LINE var += 1 ; NEW_LINE result = var * int ( pow ( 10 , temp ) ) ; NEW_LINE break ; NEW_LINE temp -= 1 ; NEW_LINE X = result - N ; NEW_LINE return X ; NEW_LINE DEDENT return - 1 ; NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 ; NEW_LINE K = 1 ; NEW_LINE DEDENT print ( minDigits ( N , K ) ) ; NEW_LINE
Count of Ways to obtain given Sum from the given Array elements | Python3 program to implement the above approach ; Function to call dfs to calculate the number of ways ; Iterate till the length of array ; Initialize the memorization table ; Function to perform the DFS to calculate the number of ways ; Base case : Reached the end of array ; If current sum is obtained ; Otherwise ; If previously calculated subproblem occurred ; Check if the required sum can be obtained by adding current element or by subtracting the current index element ; Store the count of ways ; Driver Code
import sys NEW_LINE def findWays ( nums , S ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT sum += nums [ i ] NEW_LINE DEDENT memo = [ [ - sys . maxsize - 1 for i in range ( 2 * sum + 1 ) ] for j in range ( len ( nums ) + 1 ) ] NEW_LINE return dfs ( memo , nums , S , 0 , 0 , sum ) NEW_LINE DEDENT def dfs ( memo , nums , S , curr_sum , index , sum ) : NEW_LINE INDENT if ( index == len ( nums ) ) : NEW_LINE INDENT if ( S == curr_sum ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( memo [ index ] [ curr_sum + sum ] != - sys . maxsize - 1 ) : NEW_LINE INDENT return memo [ index ] [ curr_sum + sum ] NEW_LINE DEDENT ans = ( dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) ) NEW_LINE memo [ index ] [ curr_sum + sum ] = ans NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = 3 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE answer = findWays ( arr , S ) NEW_LINE print ( answer ) NEW_LINE DEDENT
Longest subarray with sum not divisible by X | Function to print the longest subarray with sum of elements not divisible by X ; Pref [ ] stores the prefix sum Suff [ ] stores the suffix sum ; If array element is divisibile by x ; Increase count ; If all the array elements are divisible by x ; No subarray possible ; Reverse v to calculate the suffix sum ; Calculate the suffix sum ; Reverse to original form ; Reverse the suffix sum array ; Calculate the prefix sum ; Stores the starting index of required subarray ; Stores the ending index of required subarray ; If suffix sum till i - th index is not divisible by x ; Update the answer ; If prefix sum till i - th index is not divisible by x ; Update the answer ; Print the longest subarray ; Driver code
def max_length ( N , x , v ) : NEW_LINE INDENT preff , suff = [ ] , [ ] NEW_LINE ct = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT a = v [ i ] NEW_LINE if a % x == 0 : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT if ct == N : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT v . reverse ( ) NEW_LINE suff . append ( v [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT suff . append ( v [ i ] + suff [ i - 1 ] ) NEW_LINE DEDENT v . reverse ( ) NEW_LINE suff . reverse ( ) NEW_LINE preff . append ( v [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preff . append ( v [ i ] + preff [ i - 1 ] ) NEW_LINE DEDENT ans = 0 NEW_LINE lp = 0 NEW_LINE rp = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if suff [ i ] % x != 0 and ans < N - 1 : NEW_LINE INDENT lp = i NEW_LINE rp = N - 1 NEW_LINE ans = max ( ans , N - i ) NEW_LINE DEDENT if preff [ i ] % x != 0 and ans < i + 1 : NEW_LINE INDENT lp = 0 NEW_LINE rp = i NEW_LINE ans = max ( ans , i + 1 ) NEW_LINE DEDENT DEDENT for i in range ( lp , rp + 1 ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT x = 3 NEW_LINE v = [ 1 , 3 , 2 , 6 ] NEW_LINE N = len ( v ) NEW_LINE max_length ( N , x , v ) NEW_LINE
Number of times Maximum and minimum value updated during traversal of array | Function to find the count the number of times maximum and minimum value updated ; Update the maximum and minimum values during traversal ; Driver code
def maximumUpdates ( arr ) : NEW_LINE INDENT min = arr [ 0 ] NEW_LINE max = arr [ 0 ] NEW_LINE minCount , maxCount = 1 , 1 NEW_LINE for arr in arr : NEW_LINE INDENT if arr > max : NEW_LINE INDENT maxCount += 1 NEW_LINE max = arr NEW_LINE DEDENT if arr < min : NEW_LINE INDENT minCount += 1 ; NEW_LINE min = arr NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ times ▁ maximum ▁ " , end = " " ) NEW_LINE print ( " value ▁ updated ▁ = ▁ " , maxCount ) NEW_LINE print ( " Number ▁ of ▁ times ▁ minimum ▁ " , end = " " ) NEW_LINE print ( " value ▁ updated ▁ = ▁ " , minCount ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 20 , 22 ] NEW_LINE maximumUpdates ( arr ) NEW_LINE DEDENT
Find the farthest smaller number in the right side | Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code
def farthest_min ( a , n ) : NEW_LINE INDENT suffix_min = [ 0 for i in range ( n ) ] NEW_LINE suffix_min [ n - 1 ] = a [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix_min [ i ] = min ( suffix_min [ i + 1 ] , a [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT low = i + 1 NEW_LINE high = n - 1 NEW_LINE ans = - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( suffix_min [ mid ] < a [ i ] ) : NEW_LINE INDENT ans = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT print ( ans , end = " ▁ " ) NEW_LINE DEDENT DEDENT a = [ 3 , 1 , 5 , 2 , 4 ] NEW_LINE n = len ( a ) NEW_LINE farthest_min ( a , n ) NEW_LINE
Print numbers in descending order along with their frequencies | Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
def printElements ( a , n ) : NEW_LINE INDENT a . sort ( reverse = True ) NEW_LINE cnt = 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] != a [ i + 1 ] ) : NEW_LINE INDENT print ( a [ i ] , " ▁ occurs ▁ " , cnt , " times " ) NEW_LINE cnt = 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT print ( a [ n - 1 ] , " occurs " , cnt , " times " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ] NEW_LINE n = len ( a ) NEW_LINE printElements ( a , n ) NEW_LINE DEDENT
Find element position in given monotonic sequence | Python 3 implementation of the approach ; Function to return the value of f ( n ) for given values of a , b , c , n ; if c is 0 , then value of n can be in order of 10 ^ 15. if c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; for efficient searching , use binary search . ; Driver code
from math import log2 , floor NEW_LINE SMALL_N = 1000000 NEW_LINE LARGE_N = 1000000000000000 NEW_LINE def func ( a , b , c , n ) : NEW_LINE INDENT res = a * n NEW_LINE logVlaue = floor ( log2 ( n ) ) NEW_LINE res += b * n * logVlaue NEW_LINE res += c * ( n * n * n ) NEW_LINE return res NEW_LINE DEDENT def getPositionInSeries ( a , b , c , k ) : NEW_LINE INDENT start = 1 NEW_LINE end = SMALL_N NEW_LINE if ( c == 0 ) : NEW_LINE INDENT end = LARGE_N NEW_LINE DEDENT ans = 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE val = func ( a , b , c , mid ) NEW_LINE if ( val == k ) : NEW_LINE INDENT ans = mid NEW_LINE break NEW_LINE DEDENT elif ( val > k ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 1 NEW_LINE c = 1 NEW_LINE k = 12168587437017 NEW_LINE print ( getPositionInSeries ( a , b , c , k ) ) NEW_LINE DEDENT
Check if array can be sorted with one swap | A linear Python 3 program to check if array becomes sorted after one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Code
def checkSorted ( n , arr ) : NEW_LINE INDENT b = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b . append ( arr [ i ] ) NEW_LINE DEDENT b . sort ( ) NEW_LINE ct = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] != b [ i ] : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT if ct == 0 or ct == 2 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if checkSorted ( n , arr ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check whether ( i , j ) exists such that arr [ i ] != arr [ j ] and arr [ arr [ i ] ] is equal to arr [ arr [ j ] ] | Function that will tell whether such Indices present or Not . ; Checking 1 st condition i . e whether Arr [ i ] equal to Arr [ j ] or not ; Checking 2 nd condition i . e whether Arr [ Arr [ i ] ] equal to Arr [ Arr [ j ] ] or not . ; Driver Code ; Calling function .
def checkIndices ( Arr , N ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( Arr [ i ] != Arr [ j ] ) : NEW_LINE INDENT if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Arr = [ 3 , 2 , 1 , 1 , 4 ] NEW_LINE N = len ( Arr ) NEW_LINE if checkIndices ( Arr , N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find two non | Function to find two non - overlapping with same Sum in an array ; first create an empty Map key -> which is Sum of a pair of elements in the array value -> vector storing index of every pair having that Sum ; consider every pair ( arr [ i ] , arr [ j ] ) and where ( j > i ) ; calculate Sum of current pair ; if Sum is already present in the Map ; check every pair having equal Sum ; if pairs don 't overlap,  print them and return ; Insert current pair into the Map ; If no such pair found ; Driver Code
def findPairs ( arr , size ) : NEW_LINE INDENT Map = { } NEW_LINE for i in range ( 0 , size - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT Sum = arr [ i ] + arr [ j ] NEW_LINE if Sum in Map : NEW_LINE INDENT for pair in Map [ Sum ] : NEW_LINE INDENT m , n = pair NEW_LINE if ( ( m != i and m != j ) and ( n != i and n != j ) ) : NEW_LINE INDENT print ( " Pair ▁ First ▁ ( { } , ▁ { } ) " . format ( arr [ i ] , arr [ j ] ) ) NEW_LINE print ( " Pair ▁ Second ▁ ( { } , ▁ { } ) " . format ( arr [ m ] , arr [ n ] ) ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if Sum not in Map : NEW_LINE INDENT Map [ Sum ] = [ ] NEW_LINE DEDENT Map [ Sum ] . append ( ( i , j ) ) NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 4 , 7 , 8 , 4 ] NEW_LINE size = len ( arr ) NEW_LINE findPairs ( arr , size ) NEW_LINE DEDENT
Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to 'sum ; Consider all possible pairs and check their sums ; Driver Code
' NEW_LINE def printPairs ( arr , n , sum ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] == sum ) : NEW_LINE INDENT print ( " ( " , arr [ i ] , " , ▁ " , arr [ j ] , " ) " , sep = " " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 1 , 5 , 7 , - 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 6 NEW_LINE printPairs ( arr , n , sum ) NEW_LINE