codeparrot/xlcost-text-to-code huggingface dataset:
Find the coordinates of a triangle whose Area = ( S / 2 ) | Python3 implementation of the approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code
MAX = 1000000000 ; NEW_LINE def findTriangle ( S ) : NEW_LINE INDENT X1 = 0 ; Y1 = 0 ; NEW_LINE X2 = MAX ; Y2 = 1 ; NEW_LINE X3 = ( MAX - S % MAX ) % MAX ; NEW_LINE Y3 = ( S + X3 ) / MAX ; NEW_LINE print ( " ( " , X1 , " , " , Y1 , " ) " ) ; NEW_LINE print ( " ( " , X2 , " , " , Y2 , " ) " ) ; NEW_LINE print ( " ( " , X3 , " , " , Y3 , " ) " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = 4 ; NEW_LINE findTriangle ( S ) ; NEW_LINE DEDENT
Rearrange array elements such that Bitwise AND of first N | Utility function to print the elements of an array ; Function to find the required arrangement ; There has to be atleast 2 elements ; Minimum element from the array ; Swap any occurrence of the minimum element with the last element ; Find the bitwise AND of the first ( n - 1 ) elements ; If the bitwise AND is equal to the last element then print the arrangement ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def findArrangement ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" , end = " " ) ; NEW_LINE return ; NEW_LINE DEDENT minVal = min ( arr ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == minVal ) : NEW_LINE INDENT arr [ i ] , arr [ n - 1 ] = arr [ n - 1 ] , arr [ i ] ; NEW_LINE break ; NEW_LINE DEDENT DEDENT andVal = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT andVal &= arr [ i ] ; NEW_LINE DEDENT if ( andVal == arr [ n - 1 ] ) : NEW_LINE INDENT printArr ( arr , n ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findArrangement ( arr , n ) ; NEW_LINE DEDENT
Maximum prime moves to convert X to Y | Function to return the maximum operations required to convert X to Y ; X cannot be converted to Y ; If the difference is 1 ; If the difference is even ; Add 3 to X and the new difference will be even ; Driver code
def maxOperations ( X , Y ) : NEW_LINE INDENT if ( X > Y ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT diff = Y - X ; NEW_LINE if ( diff == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( diff % 2 == 0 ) : NEW_LINE INDENT return ( diff // 2 ) ; NEW_LINE DEDENT return ( 1 + ( ( diff - 3 ) // 2 ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 5 ; Y = 16 ; NEW_LINE print ( maxOperations ( X , Y ) ) ; NEW_LINE DEDENT
Sum of Digits of the Good Strings | Python3 implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
DIGITS = 10 ; NEW_LINE MAX = 10000 ; NEW_LINE MOD = 1000000007 ; NEW_LINE dp = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ; NEW_LINE cnt = [ [ 0 for i in range ( DIGITS ) ] for i in range ( MAX ) ] ; NEW_LINE def precompute ( ) : NEW_LINE INDENT for i in range ( DIGITS ) : NEW_LINE INDENT dp [ 1 ] [ i ] = i ; NEW_LINE cnt [ 1 ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT for j in range ( DIGITS ) : NEW_LINE INDENT for k in range ( DIGITS ) : NEW_LINE INDENT if ( j != k ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; NEW_LINE dp [ i ] [ j ] %= MOD ; NEW_LINE cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; NEW_LINE cnt [ i ] [ j ] %= MOD ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT x = 6 ; y = 4 ; NEW_LINE precompute ( ) ; NEW_LINE print ( dp [ x ] [ y ] ) ; NEW_LINE
Count non | Python implementation of the approach ; Function to pre - compute the sequence ; For N = 1 the answer will be 2 ; Starting two terms of the sequence ; Compute the rest of the sequence with the relation F [ i ] = F [ i - 1 ] + F [ i - 2 ] ; Driver code ; Pre - compute the sequence
N = 10000 ; NEW_LINE MOD = 1000000007 ; NEW_LINE F = [ 0 ] * N ; NEW_LINE def precompute ( ) : NEW_LINE INDENT F [ 1 ] = 2 ; NEW_LINE F [ 2 ] = 3 ; NEW_LINE F [ 3 ] = 4 ; NEW_LINE for i in range ( 4 , N ) : NEW_LINE INDENT F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; NEW_LINE DEDENT DEDENT n = 8 ; NEW_LINE precompute ( ) ; NEW_LINE print ( F [ n ] ) ; NEW_LINE
Minimize sum by dividing all elements of a subarray by K | Python3 implementation of the approach ; Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code
import sys NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - ( sys . maxsize - 1 ) ; NEW_LINE max_ending_here = 0 ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here ; NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE DEDENT DEDENT return max_so_far ; NEW_LINE DEDENT def minimizedSum ( a , n , K ) : NEW_LINE INDENT sum = maxSubArraySum ( a , n ) ; NEW_LINE totalSum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalSum += a [ i ] ; NEW_LINE DEDENT if ( sum < 0 ) : NEW_LINE INDENT return totalSum ; NEW_LINE DEDENT totalSum = totalSum - sum + sum / K ; NEW_LINE return totalSum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , - 2 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE K = 2 ; NEW_LINE print ( minimizedSum ( a , n , K ) ) ; NEW_LINE DEDENT
Minimum numbers with one 's place as 9 to be added to get N | Function to find minimum count of numbers ( with one 's digit 9) that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver Code
def findMin ( N : int ) : NEW_LINE INDENT digit = N % 10 NEW_LINE if digit == 0 and N >= 90 : NEW_LINE INDENT return 10 NEW_LINE DEDENT elif digit == 1 and N >= 81 : NEW_LINE INDENT return 9 NEW_LINE DEDENT elif digit == 2 and N >= 72 : NEW_LINE INDENT return 8 NEW_LINE DEDENT elif digit == 3 and N >= 63 : NEW_LINE INDENT return 7 NEW_LINE DEDENT elif digit == 4 and N >= 54 : NEW_LINE INDENT return 6 NEW_LINE DEDENT elif digit == 5 and N >= 45 : NEW_LINE INDENT return 5 NEW_LINE DEDENT elif digit == 6 and N >= 36 : NEW_LINE INDENT return 4 NEW_LINE DEDENT elif digit == 7 and N >= 27 : NEW_LINE INDENT return 3 NEW_LINE DEDENT elif digit == 8 and N >= 18 : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif digit == 9 and N >= 9 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 27 NEW_LINE print ( findMin ( N ) ) NEW_LINE DEDENT
Random list of M non | Python3 implementation of the approach ; Utility function to print the elements of an array ; Function to generate a list of m random non - negative integers whose sum is n ; Create an array of size m where every element is initialized to 0 ; To make the sum of the final list as n ; Increment any random element from the array by 1 ; Print the generated list ; Driver code
from random import randint NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def randomList ( m , n ) : NEW_LINE INDENT arr = [ 0 ] * m ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ randint ( 0 , n ) % m ] += 1 ; NEW_LINE DEDENT printArr ( arr , m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 4 ; n = 8 ; NEW_LINE randomList ( m , n ) ; NEW_LINE DEDENT
Maximum String Partition | Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
def maxPartition ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE P = 0 NEW_LINE current = " " NEW_LINE previous = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT current += s [ i ] NEW_LINE if ( current != previous ) : NEW_LINE INDENT previous = current NEW_LINE current = " " NEW_LINE P += 1 NEW_LINE DEDENT DEDENT return P NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE ans = maxPartition ( s ) NEW_LINE print ( ans ) NEW_LINE
How to learn Pattern printing easily ? |
N = 4 ; NEW_LINE print ( " Value ▁ of ▁ N : ▁ " , N ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT min = i if i < j else j ; NEW_LINE print ( N - min + 1 , end = " " ) ; NEW_LINE DEDENT for j in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT min = i if i < j else j ; NEW_LINE print ( N - min + 1 , end = " " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT
Find the smallest positive number missing from an unsorted array | Set 3 | Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call
def findMissingPositive ( arr , n ) : NEW_LINE INDENT m = 1 NEW_LINE x = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( m < arr [ i ] ) : NEW_LINE INDENT x . append ( arr [ i ] ) NEW_LINE DEDENT elif ( m == arr [ i ] ) : NEW_LINE INDENT m = m + 1 NEW_LINE while ( x . count ( m ) ) : NEW_LINE INDENT x . remove ( m ) NEW_LINE m = m + 1 NEW_LINE DEDENT DEDENT DEDENT return m NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMissingPositive ( arr , n ) ) NEW_LINE DEDENT
Sum of all second largest divisors after splitting a number into one or more parts | Python 3 program to find sum of all second largest divisor after splitting a number into one or more parts ; Function to find a number is prime or not ; If there is any divisor ; Function to find the sum of all second largest divisor after splitting a number into one or more parts ; If number is prime ; If n is even ; If the number is odd ; If N - 2 is prime ; There exists 3 primes x1 , x2 , x3 such that x1 + x2 + x3 = n ; Driver code ; Function call
from math import sqrt NEW_LINE def 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 INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Min_Sum ( n ) : NEW_LINE INDENT if ( prime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT if ( prime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 27 NEW_LINE print ( Min_Sum ( n ) ) NEW_LINE DEDENT
Count pairs of strings that satisfy the given conditions | Function that returns true if c is vowel ; Function to return the count of required pairs ; For every of the array ; Vector to store the vowels of the current string ; If current character is a vowel ; If current contains vowels ; Create tuple ( first vowel , last vowel , total vowels ) ; v stores the indices for which the given condition satisfies Total valid pairs will be half the size ; Driver code
def is_vowel ( c ) : NEW_LINE INDENT return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) NEW_LINE DEDENT def count ( s , n ) : NEW_LINE INDENT map = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT vowel = [ ] NEW_LINE for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT if ( is_vowel ( s [ i ] [ j ] ) ) : NEW_LINE INDENT vowel . append ( s [ i ] [ j ] ) NEW_LINE DEDENT DEDENT if ( len ( vowel ) > 0 ) : NEW_LINE INDENT Len = len ( vowel ) NEW_LINE if ( vowel [ 0 ] , vowel [ Len - 1 ] , Len ) in map . keys ( ) : NEW_LINE INDENT map [ ( vowel [ 0 ] , vowel [ Len - 1 ] , Len ) ] . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT map [ ( vowel [ 0 ] , vowel [ Len - 1 ] , Len ) ] = [ i , ] NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in map : NEW_LINE INDENT v = map [ i ] NEW_LINE count += len ( v ) // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT s = [ " geeks " , " for " , " geeks " ] NEW_LINE n = len ( s ) NEW_LINE print ( count ( s , n ) ) NEW_LINE
Find maximum value of the last element after reducing the array with given operations | Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
def find_maximum_value ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE minimum = 10 ** 9 NEW_LINE pos = 0 NEW_LINE neg = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT minimum = min ( minimum , abs ( a [ i ] ) ) NEW_LINE sum += abs ( a [ i ] ) NEW_LINE if ( a [ i ] >= 0 ) : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT else : NEW_LINE INDENT neg += 1 NEW_LINE DEDENT DEDENT if ( pos > 0 and neg > 0 ) : NEW_LINE INDENT return sum NEW_LINE DEDENT return ( sum - 2 * minimum ) NEW_LINE DEDENT a = [ 5 , 4 , 6 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( find_maximum_value ( a , n ) ) NEW_LINE
Find Nth smallest number that is divisible by 100 exactly K times | Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code
def find_number ( N , K ) : NEW_LINE INDENT r = " " NEW_LINE if ( N % 100 == 0 ) : NEW_LINE INDENT N += 1 ; NEW_LINE r = str ( N ) NEW_LINE DEDENT else : NEW_LINE INDENT r = str ( N ) NEW_LINE DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT r += "00" NEW_LINE DEDENT return r NEW_LINE DEDENT N = 1000 NEW_LINE K = 2 ; NEW_LINE ans = find_number ( N , K ) NEW_LINE print ( ans ) NEW_LINE
Count of adjacent Vowel Consonant Pairs | Function to count the adjacent pairs of consonant and vowels in the string ; Using a set to store the vowels so that checking each character becomes easier ; Variable to store number of consonant - vowel pairs ; If the ith character is not found in the set , means it is a consonant And if the ( i + 1 ) th character is found in the set , means it is a vowel We increment the count of such pairs ; Driver Code
def countPairs ( s ) : NEW_LINE INDENT st = set ( ) ; NEW_LINE st . add ( ' a ' ) ; NEW_LINE st . add ( ' e ' ) ; NEW_LINE st . add ( ' i ' ) ; NEW_LINE st . add ( ' o ' ) ; NEW_LINE st . add ( ' u ' ) ; NEW_LINE count = 0 ; NEW_LINE n = len ( s ) ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( s [ i ] not in st and s [ i + 1 ] in st ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " ; NEW_LINE print ( countPairs ( s ) ) ; NEW_LINE DEDENT
Find the largest interval that contains exactly one of the given N integers . | Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
def maxSize ( v , n ) : NEW_LINE INDENT v . append ( 0 ) NEW_LINE v . append ( 100001 ) NEW_LINE n += 2 NEW_LINE v = sorted ( v ) NEW_LINE mx = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT L = v [ i - 1 ] + 1 NEW_LINE R = v [ i + 1 ] - 1 NEW_LINE cnt = R - L + 1 NEW_LINE mx = max ( mx , cnt ) NEW_LINE DEDENT return mx NEW_LINE DEDENT v = [ 200 , 10 , 5 ] NEW_LINE n = len ( v ) NEW_LINE print ( maxSize ( v , n ) ) NEW_LINE
Maximum length of subarray such that sum of the subarray is even | Function to find Length of the longest subarray such that Sum of the subarray is even ; Check if Sum of complete array is even ; if ( Sum % 2 == 0 ) : total Sum is already even ; Find an index i such the a [ i ] is odd and compare Length of both halfs excluding a [ i ] to find max Length subarray ; Driver Code
def maxLength ( a , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE Len = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE return n NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT Len = max ( Len , max ( n - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT a = [ 1 , 2 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( maxLength ( a , n ) ) NEW_LINE
Find a Symmetric matrix of order N that contain integers from 0 to N | Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code
def solve ( n ) : NEW_LINE INDENT initial_array = [ [ 0 for i in range ( n - 1 ) ] for j in range ( n - 1 ) ] NEW_LINE final_array = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT initial_array [ 0 ] [ i ] = i + 1 NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] NEW_LINE DEDENT DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT final_array [ i ] [ j ] = initial_array [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT t0 = final_array [ i ] [ i ] NEW_LINE t1 = final_array [ i ] [ n - 1 ] NEW_LINE temp = final_array [ i ] [ i ] NEW_LINE final_array [ i ] [ i ] = final_array [ i ] [ n - 1 ] NEW_LINE final_array [ i ] [ n - 1 ] = temp NEW_LINE final_array [ n - 1 ] [ i ] = t0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( final_array [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE solve ( n ) NEW_LINE DEDENT
Generate original array from difference between every two consecutive elements | Function to print the required permutation ; Take x = 0 for simplicity ; Calculate athe differences and store it in a vector ; Preserve the original array ; Check if athe consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
def findPerm ( n , differences ) : NEW_LINE INDENT ans = [ ] NEW_LINE ans . append ( 0 ) NEW_LINE x = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT diff = differences [ i ] NEW_LINE x = x + diff NEW_LINE ans . append ( x ) NEW_LINE DEDENT anss = ans NEW_LINE ans = sorted ( ans ) NEW_LINE flag = - 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = ans [ i ] - ans [ i - 1 ] NEW_LINE if ( res != 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT mpp = dict ( ) NEW_LINE j = 1 NEW_LINE value_at_index = [ ] NEW_LINE for x in ans : NEW_LINE INDENT mpp [ x ] = j NEW_LINE j += 1 NEW_LINE DEDENT for x in anss : NEW_LINE INDENT value_at_index . append ( mpp [ x ] ) NEW_LINE DEDENT for x in value_at_index : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT differences = [ ] NEW_LINE differences . append ( 2 ) NEW_LINE differences . append ( - 3 ) NEW_LINE differences . append ( 2 ) NEW_LINE n = len ( differences ) + 1 NEW_LINE findPerm ( n , differences ) NEW_LINE
Find smallest number K such that K % p = 0 and q % K = 0 | Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code
def getMinVal ( p , q ) : NEW_LINE INDENT if q % p == 0 : NEW_LINE INDENT return p NEW_LINE DEDENT return - 1 NEW_LINE DEDENT p = 24 ; q = 48 NEW_LINE print ( getMinVal ( p , q ) ) NEW_LINE
Sort the numbers according to their sum of digits | Function to return the sum of the digits of n ; Function to sort the array according to the sum of the digits of elements ; Vector to store digit sum with respective element ; Inserting digit sum with element in the vector pair ; Quick sort the vector , this will sort the pair according to sum of the digits of elements ; Print the sorted vector content ; Driver code
def sumOfDigit ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( [ sumOfDigit ( arr [ i ] ) , arr [ i ] ] ) ; NEW_LINE DEDENT vp . sort ( ) NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 14 , 1101 , 10 , 35 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sortArr ( arr , n ) ; NEW_LINE DEDENT
Count all Prime Length Palindromic Substrings | Python3 implementation of the approach ; Function that returns True if sub - str1ing starting at i and ending at j in str1 is a palindrome ; Function to count all palindromic substr1ing whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - str1ings ; Starting from the smallest prime till the largest Length of the sub - str1ing possible ; If j is prime ; Check all the sub - str1ings of Length j ; If current sub - str1ing is a palindrome ; Driver Code
import math as mt NEW_LINE def isPalindrome ( str1 , i , j ) : NEW_LINE INDENT while ( i < j ) : NEW_LINE INDENT if ( str1 [ i ] != str1 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def countPrimePalindrome ( str1 , Len ) : NEW_LINE INDENT prime = [ True for i in range ( Len + 1 ) ] NEW_LINE prime [ 0 ] , prime [ 1 ] = False , False NEW_LINE for p in range ( 2 , mt . ceil ( mt . sqrt ( Len + 1 ) ) ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , Len + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for j in range ( 2 , Len + 1 ) : NEW_LINE INDENT if ( prime [ j ] ) : NEW_LINE INDENT for i in range ( Len + 1 - j ) : NEW_LINE INDENT if ( isPalindrome ( str1 , i , i + j - 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE Len = len ( s ) NEW_LINE print ( countPrimePalindrome ( s , Len ) ) NEW_LINE