Muennighoff/xP3x-sample huggingface dataset:
inputs: "Summation of GCD of all the pairs up to N | PHP approach of finding sum of GCD of all pairs ; phi [ i ] stores euler totient function for i result [ j ] stores result for value j ; Precomputation of phi [ ] numbers . Refer link for details : https : goo . gl / LUqdtY ; Refer https : goo . gl / LUqdtY ; Precomputes result for all numbers till MAX ; Precompute all phi value ; Iterate throght all the divisors of i . ; Add summation of previous calculated sum ; Function to calculate sum of all the GCD pairs"
How can the above be solved in PHP?
targets: < ? php $ MAX = 100001 ; $ phi = array_fill ( 0 , $ MAX , 0 ) ; $ result = array_fill ( 0 , $ MAX , 0 ) ; function computeTotient ( ) { global $ MAX , $ phi ; $ phi [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( ! $ phi [ $ i ] ) { $ phi [ $ i ] = $ i - 1 ; for ( $ j = ( $ i << 1 ) ; $ j < $ MAX ; $ j += $ i ) { if ( ! $ phi [ $ j ] ) $ phi [ $ j ] = $ j ; $ phi [ $ j ] = ( $ phi [ $ j ] / $ i ) * ( $ i - 1 ) ; } } } } function sumOfGcdPairs ( ) { global $ MAX , $ phi , $ result ; computeTotient ( ) ; for ( $ i = 1 ; $ i < $ MAX ; ++ $ i ) { for ( $ j = 2 ; $ i * $ j < $ MAX ; ++ $ j ) $ result [ $ i * $ j ] += $ i * $ phi [ $ j ] ; } for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ result [ $ i ] += $ result [ $ i - 1 ] ; } sumOfGcdPairs ( ) ; $ N = 4 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " 
 " ; $ N = 12 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " 
 " ; $ N = 5000 ; echo " Summation ▁ of ▁ " . $ N . " ▁ = ▁ " . $ result [ $ N ] . " 
 " ; ? >
inputs: "Assembly Line Scheduling | DP | A C # program to find minimum possible time by the car chassis to complete ; Utility function to find minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and retutn minimum ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int NUM_STATION = 4 ; static int min ( int a , int b ) { return a < b ? a : b ; } static int carAssembly ( int [ , ] a , int [ , ] t , int [ ] e , int [ ] x ) { int [ ] T1 = new int [ NUM_STATION ] ; int [ ] T2 = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 , 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 , 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 , i ] , T2 [ i - 1 ] + t [ 1 , i ] + a [ 0 , i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 , i ] , T1 [ i - 1 ] + t [ 0 , i ] + a [ 1 , i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } public static void Main ( ) { int [ , ] a = { { 4 , 5 , 3 , 2 } , { 2 , 10 , 1 , 4 } } ; int [ , ] t = { { 0 , 7 , 4 , 5 } , { 0 , 9 , 2 , 8 } } ; int [ ] e = { 10 , 12 } ; int [ ] x = { 18 , 7 } ; Console . Write ( carAssembly ( a , t , e , x ) ) ; } }
inputs: "Count all possible groups of size 2 or 3 that have sum as multiple of 3 | C Program to count all possible groups of size 2 or 3 that have sum as multiple of 3 ; Returns count of all possible groups that can be formed from elements of a [ ] . ; Create an array C [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i ; To store the result ; Count elements with remainder 0 , 1 and 2 ; Case 3. a : Count groups of size 2 from 0 remainder elements ; Case 3. b : Count groups of size 2 with one element with 1 remainder and other with 2 remainder ; Case 4. a : Count groups of size 3 with all 0 remainder elements ; Case 4. b : Count groups of size 3 with all 1 remainder elements ; Case 4. c : Count groups of size 3 with all 2 remainder elements ; Case 4. c : Count groups of size 3 with different remainders ; Return total count stored in res ; Driver program to test above functions"
Solution in C:
targets: #include <stdio.h>
int findgroups ( int arr [ ] , int n ) { int c [ 3 ] = { 0 } , i ; int res = 0 ; for ( i = 0 ; i < n ; i ++ ) c [ arr [ i ] % 3 ] ++ ; res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) ; res += c [ 1 ] * c [ 2 ] ; res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 ; res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c [ 1 ] - 2 ) ) / 6 ; res += ( ( c [ 2 ] * ( c [ 2 ] - 1 ) * ( c [ 2 ] - 2 ) ) / 6 ) ; res += c [ 0 ] * c [ 1 ] * c [ 2 ] ; return res ; } int main ( ) { int arr [ ] = { 3 , 6 , 7 , 2 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Required ▁ number ▁ of ▁ groups ▁ are ▁ % d 
 " , findgroups ( arr , n ) ) ; return 0 ; }
inputs: "Sum of numbers from 1 to N which are divisible by 3 or 4 | C # program to find sum of numbers from 1 to N which are divisible by 3 or 4 ; Function to calculate the sum of numbers divisible by 3 or 4 ; Driver code"
Solution in C#:
targets: using System ; class GFG { static int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 3 ) ) * ( 2 * 3 + ( N / 3 - 1 ) * 3 ) / 2 ; S2 = ( ( N / 4 ) ) * ( 2 * 4 + ( N / 4 - 1 ) * 4 ) / 2 ; S3 = ( ( N / 12 ) ) * ( 2 * 12 + ( N / 12 - 1 ) * 12 ) / 2 ; return S1 + S2 - S3 ; } public static void Main ( ) { int N = 20 ; Console . WriteLine ( sum ( 12 ) ) ; } }
inputs: "Reverse String according to the number of words | C ++ program to reverse string according to the number of words ; Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void reverse ( char str [ ] , int start , int end ) { char temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } void reverseletter ( char str [ ] , int start , int end ) { int wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ▁ ' ) continue ; while ( str [ wend ] != ' ▁ ' && wend <= end ) wend ++ ; wend -- ; reverse ( str , wstart , wend ) ; } } int main ( ) { char str [ 1000 ] = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " ; reverseletter ( str , 0 , strlen ( str ) - 1 ) ; cout << str ; return 0 ; }
inputs: "Sort a Bitonic Array | C ++ program for the above approach ; Function to Sort a Bitonic array in constant space ; Initialize the value of k ; In each iteration compare elements k distance apart and swap if they are not in order ; k is reduced to half after every iteration ; Print the array elements ; Driver Code ; Given array arr [ ] ; Function Call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; void sortArr ( int a [ ] , int n ) { int i , k ; k = ( int ) log2 ( n ) ; k = pow ( 2 , k ) ; while ( k > 0 ) { for ( i = 0 ; i + k < n ; i ++ ) if ( a [ i ] > a [ i + k ] ) swap ( a [ i ] , a [ i + k ] ) ; k = k / 2 ; } for ( i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
inputs: "Find pairs in array whose sums already exist in array | A simple C # program to find pair whose sum already exists in array ; Function to find pair whose sum exists in arr [ ] ; Driver code"
Solution in C#:
targets: using System ; public class GFG { static void findPair ( int [ ] arr , int n ) { bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { Console . WriteLine ( arr [ i ] + " ▁ " + arr [ j ] ) ; found = true ; } } } } if ( found == false ) Console . WriteLine ( " Not ▁ exist " ) ; } static public void Main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 8 , 13 , 5 } ; int n = arr . Length ; findPair ( arr , n ) ; } }
inputs: "Program to find the sum of the series ( 1 / a + 2 / a ^ 2 + 3 / a ^ 3 + ... + n / a ^ n ) | C ++ program to find the sum of the given series ; Function to return the sum of the series ; variable to store the answer ; Math . pow ( x , y ) returns x ^ y ; Driver code ; Print the sum of the series"
How can the above be solved in C++?
targets: #include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std ; float getSum ( int a , int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { sum += ( i / pow ( a , i ) ) ; } return sum ; } int main ( ) { int a = 3 , n = 3 ; cout << ( getSum ( a , n ) ) ; return 0 ; }
inputs: Write a program to reverse digits of a number | C program to reverse digits of a number ; Recursive function to reverse digits of num ; Driver program to test reversDigits
How can the above be solved in C?
targets: #include <stdio.h> ; int reversDigits ( int num ) { static int rev_num = 0 ; static int base_pos = 1 ; if ( num > 0 ) { reversDigits ( num / 10 ) ; rev_num += ( num % 10 ) * base_pos ; base_pos *= 10 ; } return rev_num ; } int main ( ) { int num = 4562 ; printf ( " Reverse ▁ of ▁ no . ▁ is ▁ % d " , reversDigits ( num ) ) ; getchar ( ) ; return 0 ; }
inputs: "Program to find parity | C program to find parity of an integer ; Function to get parity of number n . It returns 1 if n has odd parity , and returns 0 if n has even parity ; Driver program to test getParity ( )"
Solution in C:
targets: # include <stdio.h>
# define bool  int
bool getParity ( unsigned int n ) { bool parity = 0 ; while ( n ) { parity = ! parity ; n = n & ( n - 1 ) ; } return parity ; } int main ( ) { unsigned int n = 7 ; printf ( " Parity ▁ of ▁ no ▁ % d ▁ = ▁ % s " , n , ( getParity ( n ) ? " odd " : " even " ) ) ; getchar ( ) ; return 0 ; }
inputs: "Interesting facts about Fibonacci numbers | C # program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers"
Solution in C#:
targets: using System ; class GFG { static int MAX = 100 ; static void Main ( ) { long [ ] arr = new long [ MAX ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; Console . Write ( " Fibonacci ▁ numbers ▁ divisible ▁ by ▁ " + " their ▁ indexes ▁ are ▁ : 
 " ) ; for ( int i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) System . Console . Write ( i + " ▁ " ) ; } }
inputs: "Print the middle nodes of each level of a Binary Tree | Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Driver Code ; Given Tree ; Function Call"
Solution in Python:
targets: class node :
	def __init__ ( self , data ) :
		self . data = data
		self . left = None
		self . right = None
def newnode ( d ) :
	temp = node ( d )
	return temp
def dfs ( root , l , M ) :
	if ( root == None ) :
		return
	if l not in M :
		M [ l ] = [ ]
	M [ l ] . append ( root . data )
	dfs ( root . left , l + 1 , M )
	dfs ( root . right , l + 1 , M )
def printMidNodes ( root ) :
	M = dict ( )
	dfs ( root , 0 , M )
	for it in M . values ( ) :
		size = len ( it )
		if ( size & 1 ) :
			print ( it [ ( size - 1 ) // 2 ] )
		else :
			print ( str ( it [ ( size - 1 ) // 2 ] ) + ' ▁ ' + str ( it [ ( size - 1 ) // 2 + 1 ] ) )
if __name__ == " _ _ main _ _ " :
	root = newnode ( 1 )
	root . left = newnode ( 2 )
	root . right = newnode ( 3 )
	root . left . left = newnode ( 4 )
	root . left . right = newnode ( 5 )
	root . left . right . left = newnode ( 11 )
	root . left . right . right = newnode ( 6 )
	root . left . right . right . left = newnode ( 7 )
	root . left . right . right . right = newnode ( 9 )
	root . right . left = newnode ( 10 )
	root . right . right = newnode ( 8 )
	printMidNodes ( root )
inputs: "Count numbers having GCD with N equal to the number itself | Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code"
Solution in Python:
targets: def countNumbers ( N ) :
	count = 0
	for i in range ( 1 , N + 1 ) :
		if i * i > N :
			break
		if ( N % i == 0 ) :
			count += 1
			if ( N // i != i ) :
				count += 1
	return count
if __name__ == ' _ _ main _ _ ' :
	N = 10
	print ( countNumbers ( N ) )
inputs: "Date after adding given number of days to the given date | Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Find values of day and month from offset of result year . ; Driven Program"
How can the above be solved in Python?
targets: def isLeap ( y ) :
	if ( y % 100 != 0 and y % 4 == 0 or y % 400 == 0 ) :
		return True
	return False
def offsetDays ( d , m , y ) :
	offset = d
	switcher = { 10 : 30 , 9 : 31 , 8 : 30 , 7 : 31 , 6 : 31 , 5 : 30 , 4 : 31 , 3 : 30 , 2 : 31 , 1 : 28 , 0 : 31 }
	if ( isLeap ( y ) and m > 1 ) :
		offset += 1
	offset += switcher . get ( m )
	return offset
def revoffsetDays ( offset , y , d , m ) :
	month = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
	if ( isLeap ( y ) ) :
		month [ 2 ] = 29
	for i in range ( 1 , 13 ) :
		if ( offset <= month [ i ] ) :
			break
		offset = offset - month [ i ]
	d [ 0 ] = offset
	m [ 0 ] = i + 1
def addDays ( d1 , m1 , y1 , x ) :
	offset1 = offsetDays ( d1 , m1 , y1 )
	if isLeap ( y1 ) :
		remDays = 366 - offset1
	else :
		remDays = 365 - offset1
	if ( x <= remDays ) :
		y2 = y1
		offset2 = offset1 + x
	else :
		x -= remDays
		y2 = y1 + 1
		if isLeap ( y2 ) :
			y2days = 366
		else :
			y2days = 365
		while ( x >= y2days ) :
			x -= y2days
			y2 += 1
			if isLeap ( y2 ) :
				y2days = 366
			else :
				y2days = 365
		offset2 = x
	m2 = [ 0 ]
	d2 = [ 0 ]
	revoffsetDays ( offset2 , y2 , d2 , m2 )
	print ( " d2 ▁ = ▁ " , * d2 , " , ▁ m2 ▁ = ▁ " , * m2 , " , ▁ y2 ▁ = ▁ " , y2 , sep = " " )
d = 14
m = 3
y = 2015
x = 366
addDays ( d , m , y , x )
inputs: "Distinct powers of a number N such that the sum is equal to K | C ++ implementation to find out the powers of N that add upto K ; Initializing the PowerArray with all 0 's ; Function to find the powers of N that add up to K ; Initializing the counter ; Executing the while loop until K is greater than 0 ; If K % N == 1 , then the power array is incremented by 1 ; Checking if any power is occurred more than once ; For any other value , the sum of powers cannot be added up to K ; Printing the powers of N that sum up to K ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int b [ 50 ] = { 0 } ; int PowerArray ( int n , int k ) { int count = 0 ; while ( k ) { if ( k % n == 0 ) { k /= n ; count ++ ; } else if ( k % n == 1 ) { k -= 1 ; b [ count ] ++ ; if ( b [ count ] > 1 ) { cout << -1 ; return 0 ; } } else { cout << -1 ; return 0 ; } } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] ) { cout << i << " , ▁ " ; } } } int main ( ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; return 0 ; }
inputs: "Decimal to Binary using recursion and without using power operator | Java implementation of the approach ; Recursive function to convert n to its binary equivalent ; Base case ; Recursive call ; Driver code"
Solution in Java:
targets: import java . io . * ; class GFG { static void decimalToBinary ( int n ) { if ( n == 0 ) { System . out . print ( "0" ) ; return ; } decimalToBinary ( n / 2 ) ; System . out . print ( n % 2 ) ; } public static void main ( String [ ] args ) { int n = 13 ; decimalToBinary ( n ) ; } }
inputs: "Maximum length of consecutive 1 s or 0 s after flipping at most K characters | Java program for the above approach ; Function to find the maximum length continuos segment of character c after flipping at most K characters ; Stores the maximum length ; Stores the count of char ' c ' ; Start of window ; Remove the extra ' c ' from left ; Increment the value of the left ; Update the resultant maximum length of character ch ; Function to find the maximum length of consecutive 0 s or 1 s by flipping at most K characters of the string ; Print the maximum of the maximum length of 0 s or 1 s ; Driver Code"
How can the above be solved in Java?
targets: public class GFG { static int maxLength ( String str , int n , char c , int k ) { int ans = - 1 ; int cnt = 0 ; int left = 0 ; for ( int right = 0 ; right < n ; right ++ ) { if ( str . charAt ( right ) == c ) { cnt ++ ; } while ( cnt > k ) { if ( str . charAt ( left ) == c ) { cnt -- ; } left ++ ; } ans = Math . max ( ans , right - left + 1 ) ; } return ans ; } static int maxConsecutiveSegment ( String S , int K ) { int N = S . length ( ) ; return Math . max ( maxLength ( S , N , '0' , K ) , maxLength ( S , N , '1' , K ) ) ; } int main ( ) { return 0 ; } public static void main ( String [ ] args ) { String S = "1001" ; int K = 1 ; System . out . println ( maxConsecutiveSegment ( S , K ) ) ; } }
inputs: "Print characters and their frequencies in order of occurrence | PHP implementation to print the character and its frequency in order of its occurrence ; function to print the character and its frequency in order of its occurrence ; size of the string ' str ' ; ' freq [ ] ' implemented as hash table ; accumulate frequency of each character in ' str ' ; traverse ' str ' from left to right ; if frequency of character str [ i ] is not equal to 0 ; print the character along with its frequency ; update frequency of str [ i ] to 0 so that the same character is not printed again ; Driver Code"
Solution in php:
targets: < ? php $ SIZE = 26 ; function printCharWithFreq ( $ str ) { global $ SIZE ; $ n = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ SIZE , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] != 0 ) { echo $ str [ $ i ] . $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] . " " ; $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] = 0 ; } } } $ str = " geeksforgeeks " ; printCharWithFreq ( $ str ) ; ? >
inputs: "Maximize sum of second minimums in all quadruples of a given array | C # program for the above approach ; Function to find maximum possible sum of second minimums in each quadruple ; Sort the array ; Add the second minimum ; Print maximum possible sum ; Driver Code ; Given array ; Size of the array"
How can the above be solved in C-Sharp?
targets: using System ; public class GFG { public static void maxPossibleSum ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int sum = 0 ; int j = N - 3 ; while ( j >= 0 ) { sum += arr [ j ] ; j -= 3 ; } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 4 , 5 , 2 , 3 , 1 , 5 , 9 } ; int N = arr . Length ; maxPossibleSum ( arr , N ) ; } }
inputs: "Check if the string contains consecutive letters and each letter occurs exactly once | C # program to implement the above approach ; for all the characters of the string ; find the ascii value of the character ; check if if its a valid character , if not then return false ; calculate sum of all the characters ascii values ; find minimum ascii value from the string ; find maximum ascii value from the string ; To get the previous element of the minimum ASCII value ; take the expected sum from the above equation ; check if the expected sum is equals to the calculated sum or not ; Driver code ; 1 st example ; 2 nd example"
Solution in C#:
targets: using System ; class GFG { static bool check ( string str ) { int min = Int32 . MaxValue ; int max = Int32 . MinValue ; int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { int ascii = ( int ) str [ i ] ; if ( ascii < 96 ascii > 122 ) return false ; sum += ascii ; if ( min > ascii ) min = ascii ; if ( max < ascii ) max = ascii ; } min -= 1 ; int eSum = ( ( max * ( max + 1 ) ) / 2 ) - ( ( min * ( min + 1 ) ) / 2 ) ; return sum == eSum ; } static void Main ( ) { string str = " dcef " ; if ( check ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; string str1 = " xyza " ; if ( check ( str1 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
inputs: "Generate all rotations of a number | C # implementation of the approach ; Function to return the count of digits of n ; Function to print the left shift numbers ; Formula to calculate left shift from previous number ; Update the original number ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; public class GFG { static int numberOfDigits ( int n ) { int cnt = 0 ; while ( n > 0 ) { cnt ++ ; n /= 10 ; } return cnt ; } static void cal ( int num ) { int digits = numberOfDigits ( num ) ; int powTen = ( int ) Math . Pow ( 10 , digits - 1 ) ; for ( int i = 0 ; i < digits - 1 ; i ++ ) { int firstDigit = num / powTen ; int left = ( ( num * 10 ) + firstDigit ) - ( firstDigit * powTen * 10 ) ; Console . Write ( left + " ▁ " ) ; num = left ; } } static public void Main ( ) { int num = 1445 ; cal ( num ) ; } }
inputs: "Count numbers with unit digit k in given range | Efficient Java program to count numbers with last digit as k in given range . ; Returns count of numbers with k as last digit . ; Driver function"
Solution in C#:
targets: using System ; public class GfG { public static int counLastDigitK ( int low , int high , int k ) { int mlow = 10 * Convert . ToInt32 ( Math . Ceiling ( low / 10.0 ) ) ; int mhigh = 10 * Convert . ToInt32 ( Math . Floor ( high / 10.0 ) ) ; int count = ( mhigh - mlow ) / 10 ; if ( high % 10 >= k ) count ++ ; if ( low % 10 <= k && ( low % 10 ) > 0 ) count ++ ; return count ; } public static void Main ( ) { int low = 3 , high = 35 , k = 3 ; Console . WriteLine ( counLastDigitK ( low , high , k ) ) ; } }
inputs: "Legendre 's formula (Given p and n, find the largest x such that p^x divides n!) | C program to find largest x such that p * x divides n ! ; Returns largest power of p that divides n ! ; Initialize result ; Calculate x = n / p + n / ( p ^ 2 ) + n / ( p ^ 3 ) + ... . ; Driver program"
Solution in C:
targets: #include <stdio.h>
int largestPower ( int n , int p ) { int x = 0 ; while ( n ) { n /= p ; x += n ; } return x ; } int main ( ) { int n = 10 , p = 3 ; printf ( " The ▁ largest ▁ power ▁ of ▁ % d ▁ that ▁ divides ▁ % d ! ▁ is ▁ % d 
 " , p , n , largestPower ( n , p ) ) ; return 0 ; }
inputs: "A Space Optimized Solution of LCS | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Find lengths of two strings ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ]"
How can the above be solved in Java?
targets: class GFG { public static int lcs ( String X , String Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } }
inputs: "Average of first n odd naturals numbers | CPP Program to find the average of sum of first n odd numbers ; Return the average of sum of first n odd numbers ; Driver Code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int avg_of_odd_num ( int n ) { return n ; } int main ( ) { int n = 8 ; cout << avg_of_odd_num ( n ) ; return 0 ; }
inputs: "Find the last two missing digits of the given phone number | Function to find the last two digits of the number and print the complete number ; Sum of the first eight digits of the number ; if sum < 10 , then the two digits are '0' and the value of sum ; if sum > 10 , then the two digits are the value of sum ; Driver code"
How can the above be solved in Python?
targets: def findPhoneNumber ( n ) :
	temp = n
	sum = 0
	while ( temp != 0 ) :
		sum += temp % 10
		temp = temp // 10
	if ( sum < 10 ) :
		print ( n , "0" , sum )
	else :
		n = str ( n )
		sum = str ( sum )
		n += sum
		print ( n )
if __name__ == ' _ _ main _ _ ' :
	n = 98765432
	findPhoneNumber ( n )
inputs: "Find the sum of the first Nth Centered Pentadecagonal Number | C # program to find the sum of the first N centered pentadecagonal number ; Function to find the centered pentadecagonal number ; Formula to calculate N - th centered pentadecagonal number ; Function to find the sum of the first N centered pentadecagonal numbers ; Variable to store the sum ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int Centered_Pentadecagonal_num ( int n ) { return ( 15 * n * n - 15 * n + 2 ) / 2 ; } static int sum_Centered_Pentadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentadecagonal_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( sum_Centered_Pentadecagonal_num ( n ) ) ; } }
inputs: "Minimum number of subsets with distinct elements | A hashing based solution to find the minimum number of subsets of a set such that every subset contains distinct elements . ; Function to count subsets such that all subsets have distinct elements . ; Traverse the input array and store frequencies of elements ; Find the maximum value in map . ; Driver code"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { static int subset ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) res = Math . Max ( res , entry . Value ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 9 , 3 , 4 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( subset ( arr , n ) ) ; } }
inputs: "Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | C ++ program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver Code ; Function Call"
Solution in C++:
targets: #include " bits / stdc + + . h "
using namespace std ; void assignValues ( int Edges [ ] [ 2 ] , int n ) { vector < int > tree [ n + 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; tree [ u ] . push_back ( v ) ; tree [ v ] . push_back ( u ) ; } vector < bool > visited ( n + 1 , false ) ; vector < int > answer ( n + 1 ) ; int K = 1 ; queue < int > q ; q . push ( 1 ) ; answer [ 1 ] = K ; while ( ! q . empty ( ) ) { int node = q . front ( ) ; q . pop ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; for ( auto child : tree [ node ] ) { if ( ! visited [ child ] ) { q . push ( child ) ; answer [ child ] = K ; } } } for ( int i = 1 ; i <= n ; i ++ ) { cout << answer [ i ] << " ▁ " ; } } int main ( ) { int N = 11 ; int Edges [ ] [ 2 ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 1 , 5 } , { 2 , 6 } , { 2 , 10 } , { 10 , 11 } , { 3 , 7 } , { 4 , 8 } , { 5 , 9 } } ; assignValues ( Edges , N ) ; return 0 ; }
inputs: "Largest palindromic number by permuting digits | JAVA program to print the largest palindromic number by permuting digits of a number ; Function to check if a number can be permuted to form a palindrome number ; counts the occurrence of number which is odd ; if occurrence is odd ; if number exceeds 1 ; function to print the largest palindromic number by permuting digits of a number ; String length ; map that marks the occurrence of a number ; check the possibility of a palindromic number ; String array that stores the largest permuted palindromic number ; pointer of front ; greedily start from 9 to 0 and place the greater number in front and odd in the middle ; if the occurrence of number is odd ; place one odd occurring number in the middle ; decrease the count ; place the rest of numbers greedily ; if all numbers occur even times , then place greedily ; place greedily at front ; 2 numbers are placed , so decrease the count ; increase placing position ; print the largest String thus formed ; Driver Code"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static boolean possibility ( HashMap < Integer , Integer > m , int length , String s ) { int countodd = 0 ; for ( int i = 0 ; i < length ; i ++ ) { if ( m . get ( s . charAt ( i ) - '0' ) % 2 == 1 ) countodd ++ ; if ( countodd > 1 ) return false ; } return true ; } static void largestPalindrome ( String s ) { int l = s . length ( ) ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < l ; i ++ ) if ( m . containsKey ( s . charAt ( i ) - '0' ) ) m . put ( s . charAt ( i ) - '0' , m . get ( s . charAt ( i ) - '0' ) + 1 ) ; else m . put ( s . charAt ( i ) - '0' , 1 ) ; if ( possibility ( m , l , s ) == false ) { System . out . print ( " Palindrome ▁ cannot ▁ be ▁ formed " ) ; return ; } char [ ] largest = new char [ l ] ; int front = 0 ; for ( int i = 9 ; i >= 0 ; i -- ) { if ( m . containsKey ( i ) && m . get ( i ) % 2 == 1 ) { largest [ l / 2 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 1 ) ; while ( m . get ( i ) > 0 ) { largest [ front ] = ( char ) ( i + 48 ) ; largest [ l - front - 1 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 2 ) ; front ++ ; } } else { while ( m . containsKey ( i ) && m . get ( i ) > 0 ) { largest [ front ] = ( char ) ( i + 48 ) ; largest [ l - front - 1 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 2 ) ; front ++ ; } } } for ( int i = 0 ; i < l ; i ++ ) System . out . print ( largest [ i ] ) ; } public static void main ( String [ ] args ) { String s = "313551" ; largestPalindrome ( s ) ; } }
inputs: "Maximize boxes required to keep at least one black and one white shirt | Java program for the above approach ; Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print the result ; Driver Code"
Solution in Java:
targets: import java . util . * ; class GFG { static void numberofBoxes ( int W , int B , int O ) { int low = 0 , high = Math . min ( W , B ) ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( ( ( W >= mid ) && ( B >= mid ) ) && ( ( W - mid ) + ( B - mid ) + O ) >= mid ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int W = 3 , B = 3 , O = 1 ; numberofBoxes ( W , B , O ) ; } }
inputs: "Middle of three using minimum comparisons | C # program to find middle of three distinct numbers ; Function to find the middle of three number ; x is positive if a is greater than b . x is negative if b is greater than a . ; Similar to x ; Similar to x and y . ; Checking if b is middle ( x and y both are positive ) ; Checking if c is middle ( x and z both are positive ) ; Driver code"
Solution in C#:
targets: using System ; class Middle { public static int middleOfThree ( int a , int b , int c ) { int x = a - b ; int y = b - c ; int z = a - c ; if ( x * y > 0 ) return b ; else if ( x * z > 0 ) return c ; else return a ; } public static void Main ( ) { int a = 20 , b = 30 , c = 40 ; Console . WriteLine ( middleOfThree ( a , b , c ) ) ; } }
inputs: "Length of diagonals of a Rhombus using length of Side and vertex Angle | Python Program to implement the above approach ; Function to calculate the length of diagonals of a rhombus using length of sides and vertex angle ; Driver Code"
Solution in Python:
targets: import math
def Length_Diagonals ( a , theta ) :
	p = a * math . sqrt ( 2 + ( 2 * math . cos ( math . radians ( theta ) ) ) )
	q = a * math . sqrt ( 2 - ( 2 * math . cos ( math . radians ( theta ) ) ) )
	return [ p , q ]
A = 6
theta = 45
ans = Length_Diagonals ( A , theta )
print ( round ( ans [ 0 ] , 2 ) , round ( ans [ 1 ] , 2 ) )
inputs: "Sum of absolute differences of indices of occurrences of each array element | C ++ program for the above approach ; Function to find sum of differences of indices of occurrences of each unique array element ; Stores indices of each array element ; Store the indices ; Stores the sums ; Traverse the array ; Find sum for each element ; Iterate over the Map ; Calculate sum of occurrences of arr [ i ] ; Store sum for current element ; Print answer for each element ; Driver Code ; Given array ; Given size ; Function call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; void sum ( int arr [ ] , int n ) { map < int , vector < int > > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] . push_back ( i ) ; } int ans [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( auto it : mp [ arr [ i ] ] ) { sum += abs ( it - i ) ; } ans [ i ] = sum ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } return ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sum ( arr , n ) ; return 0 ; }
inputs: "Counting Sort | The main function that sort the given string arr [ ] in alphabetical order ; The output character array that will have sorted arr ; Create a count array to store count of inidividul characters and initialize count array as 0 ; Store count of each character ; Change count [ i ] so that count [ i ] now contains actual position of this character in output array ; Build the output character array ; Copy the output array to arr , so that arr now contains sorted characters ; Driver program to test above function"
How can the above be solved in Python?
targets: def countSort ( arr ) :
	output = [ 0 for i in range ( len ( arr ) ) ]
	count = [ 0 for i in range ( 256 ) ]
	for i in arr :
		count [ ord ( i ) ] += 1
	for i in range ( 256 ) :
		count [ i ] += count [ i - 1 ]
	for i in range ( len ( arr ) ) :
		output [ count [ ord ( arr [ i ] ) ] - 1 ] = arr [ i ]
		count [ ord ( arr [ i ] ) ] -= 1
	ans = [ " " for _ in arr ]
	for i in range ( len ( arr ) ) :
		ans [ i ] = output [ i ]
	return ans
arr = " geeksforgeeks "
ans = countSort ( arr )
print ( " Sorted ▁ character ▁ array ▁ is ▁ % ▁ s " % ( " " . join ( ans ) ) )
inputs: "Program to find remainder when large number is divided by 11 | Function to return remainder ; len is variable to store the length of number string . ; loop that find remainder ; Driver code"
How can the above be solved in Python?
targets: def remainder ( st ) :
	ln = len ( st )
	rem = 0
	for i in range ( 0 , ln ) :
		num = rem * 10 + ( int ) ( st [ i ] )
		rem = num % 11
	return rem
st = "3435346456547566345436457867978"
print ( remainder ( st ) )
inputs: Find a Fixed Point ( Value equal to index ) in a given array | C program to check fixed point in an array using binary search ; low + ( high - low ) / 2 ; ; Return - 1 if there is no Fixed Point ; Driver program to check above functions
How can the above be solved in C?
targets: #include <stdio.h>
int binarySearch ( int arr [ ] , int low , int high ) { if ( high >= low ) { int mid = ( low + high ) / 2 ; if ( mid == arr [ mid ] ) return mid ; if ( mid > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high ) ; else return binarySearch ( arr , low , ( mid - 1 ) ) ; } return -1 ; } int main ( ) { int arr [ 10 ] = { -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Fixed ▁ Point ▁ is ▁ % d " , binarySearch ( arr , 0 , n - 1 ) ) ; getchar ( ) ; return 0 ; }
inputs: "Program for Goldbachâ €™ s Conjecture ( Two Primes with given Sum ) | PHP program to implement Goldbach 's conjecture ; Array to store all prime less than and equal to 10 ^ 6 ; Utility function for Sieve of Sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half . This array is used to separate numbers of the form i + j + 2 * i * j from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to perform Goldbach 's conjecture ; Return if number is not even or less than 3 ; Check only upto half of number ; find difference by subtracting current prime from n ; Search if the difference is also a prime number ; Express as a sum of primes ; Finding all prime numbers before limit ; Express number as a sum of two primes"
Solution in php:
targets: < ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ MAX , $ primes ; $ marked = array_fill ( 0 , ( int ) ( $ MAX / 2 ) + 100 , false ) ; for ( $ i = 1 ; $ i <= ( sqrt ( $ MAX ) - 1 ) / 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= $ MAX / 2 ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ; array_push ( $ primes , 2 ) ; for ( $ i = 1 ; $ i <= $ MAX / 2 ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; } function findPrimes ( $ n ) { global $ MAX , $ primes ; if ( $ n <= 2 $ n % 2 != 0 ) { print ( " Invalid ▁ Input ▁ 
 " ) ; return ; } for ( $ i = 0 ; $ primes [ $ i ] <= $ n / 2 ; $ i ++ ) { $ diff = $ n - $ primes [ $ i ] ; if ( in_array ( $ diff , $ primes ) ) { print ( $ primes [ $ i ] . " + " ▁ . ▁ $ diff ▁ . ▁ " = " ▁ . ▁ $ n ▁ . ▁ " " return ; } } } sieveSundaram ( ) ; findPrimes ( 4 ) ; findPrimes ( 38 ) ; findPrimes ( 100 ) ; ? >
inputs: Write a program to calculate pow ( x , n ) | Extended version of power function that can work for float x and negative y ; Program to test function power
How can the above be solved in C?
targets: #include <stdio.h>
float power ( float x , int y ) { float temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else { if ( y > 0 ) return x * temp * temp ; else return ( temp * temp ) / x ; } } int main ( ) { float x = 2 ; int y = -3 ; printf ( " % f " , power ( x , y ) ) ; return 0 ; }
inputs: "Count words in a given string | Python3 program to count words in a given string ; Returns number of words in string ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Return the number of words ; Driver Code"
Solution in Python:
targets: OUT = 0
IN = 1
def countWords ( string ) :
	state = OUT
	wc = 0
	for i in range ( len ( string ) ) :
		if ( string [ i ] == ' ▁ ' or string [ i ] ==   ' ' ▁ or ▁ string [ i ] ▁ = = ▁ ' 	 ' ) :
			state = OUT
		elif state == OUT :
			state = IN
			wc += 1
	return wc
string =   " One two three
	four five   "
print ( " No . ▁ of ▁ words ▁ : ▁ " + str ( countWords ( string ) ) )
inputs: "Majority Element | Function to find the candidate for Majority ; Function to check if the candidate occurs more than n / 2 times ; Function to print Majority Element ; Find the candidate for Majority ; Print the candidate if it is Majority ; Driver code ; Function call"
How can the above be solved in Python?
targets: def findCandidate ( A ) :
	maj_index = 0
	count = 1
	for i in range ( len ( A ) ) :
		if A [ maj_index ] == A [ i ] :
			count += 1
		else :
			count -= 1
		if count == 0 :
			maj_index = i
			count = 1
	return A [ maj_index ]
def isMajority ( A , cand ) :
	count = 0
	for i in range ( len ( A ) ) :
		if A [ i ] == cand :
			count += 1
	if count > len ( A ) / 2 :
		return True
	else :
		return False
def printMajority ( A ) :
	cand = findCandidate ( A )
	if isMajority ( A , cand ) == True :
		print ( cand )
	else :
		print ( " No ▁ Majority ▁ Element " )
A = [ 1 , 3 , 3 , 1 , 2 ]
printMajority ( A )
inputs: "Largest sub | C ++ implementation of the approach ; Function to return the largest substring divisible by 2 ; While the last character of the string is '1' , pop it ; If the original string had no '0' ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; string largestSubStr ( string s ) { while ( s . size ( ) and s [ s . size ( ) - 1 ] == '1' ) s . pop_back ( ) ; if ( s . size ( ) == 0 ) return " - 1" ; else return s ; } int main ( ) { string s = "11001" ; cout << largestSubStr ( s ) ; return 0 ; }
inputs: "Maximum profit after buying and selling the stocks with transaction fees | Set 2 | Function to find the maximum profit with transaction fee ; Traversing the stocks for each day ; Update buy and sell ; Return the maximum profit ; Given Input ; Function Call"
Solution in Javascript:
targets: function MaxProfit ( arr , n , transactionFee ) { let buy = - arr [ 0 ] ; let sell = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let temp = buy ; buy = Math . max ( buy , sell - arr [ i ] ) ; sell = Math . max ( sell , temp + arr [ i ] - transactionFee ) ; } return Math . max ( sell , buy ) ; } let arr = [ 6 , 1 , 7 , 2 , 8 , 4 ] ; let n = arr . length ; let transactionFee = 2 ; document . write ( MaxProfit ( arr , n , transactionFee ) ) ;
inputs: "Rearrange an array to maximize sum of Bitwise AND of same | C # program for the above approach ; Function to implement recursive DP ; If i is equal to N ; If dp [ i ] [ mask ] is not equal to - 1 ; Iterate over the array B [ ] ; If current element is not yet selected ; Update dp [ i ] [ mask ] ; Return dp [ i ] [ mask ] ; Function to obtain maximum sum of Bitwise AND of same - indexed elements from the arrays A [ ] and B [ ] ; Stores all dp - states ; Returns the maximum value returned by the function maximizeAnd ( ) ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int maximizeAnd ( int i , int mask , int [ ] A , int [ ] B , int N , int [ , ] dp ) { if ( i == N ) return 0 ; if ( dp [ i , mask ] != - 1 ) return dp [ i , mask ] ; for ( int j = 0 ; j < N ; ++ j ) { if ( ( mask & ( 1 << j ) ) == 0 ) { dp [ i , mask ] = Math . Max ( dp [ i , mask ] , ( A [ i ] & B [ j ] ) + maximizeAnd ( i + 1 , mask | ( 1 << j ) , A , B , N , dp ) ) ; } } return dp [ i , mask ] ; } static int maximizeAndUtil ( int [ ] A , int [ ] B , int N ) { int [ , ] dp = new int [ N , ( 1 << N ) + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < ( 1 << N ) + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } return maximizeAnd ( 0 , 0 , A , B , N , dp ) ; } static void Main ( ) { int [ ] A = { 3 , 5 , 7 , 11 } ; int [ ] B = { 2 , 6 , 10 , 12 } ; int N = A . Length ; Console . Write ( maximizeAndUtil ( A , B , N ) ) ; } }
inputs: "Program for array rotation | C program to rotate an array by d elements ; Function to left Rotate arr [ ] of size n by 1 ; Function to left rotate arr [ ] of size n by d ; utility function to print an array ; Driver program to test above functions"
Solution in C:
targets: #include <stdio.h>
void leftRotatebyOne ( int arr [ ] , int n ) ; void leftRotatebyOne ( int arr [ ] , int n ) { int temp = arr [ 0 ] , i ; for ( i = 0 ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; arr [ n - 1 ] = temp ; } void leftRotate ( int arr [ ] , int d , int n ) { int i ; for ( i = 0 ; i < d ; i ++ ) leftRotatebyOne ( arr , n ) ; } void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; return 0 ; }
inputs: "Distance between two parallel lines | C # program find the distance between two parallel lines ; Function to find the distance between parallel lines ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static double dist ( double m , double b1 , double b2 ) { double d = Math . Abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; return d ; } public static void Main ( ) { double m = 2 , b1 = 4 , b2 = 3 ; Console . Write ( dist ( m , b1 , b2 ) ) ; } }
inputs: "Find most significant set bit of a number | CPP program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code"
Solution in C++:
targets: #include <iostream>
using namespace std ; int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } int main ( ) { int n = 273 ; cout << setBitNumber ( n ) ; return 0 ; }
inputs: "Sum of products of all combination taken ( 1 to n ) at a time | Find the postfix sum array ; Modify the array such that we don 't have to compute the products which are obtained before ; Finding sum of all combination taken 1 to N at a time ; sum taken 1 at time is simply sum of 1 - N ; for sum of products for all combination ; finding postfix array ; sum of products taken i + 1 at a time ; modify the array for overlapping problem ; Driver 's Code ; storing numbers from 1 to N ; calling allCombination"
Solution in Python:
targets: def postfix ( a , n ) :
	for i in range ( n - 1 , 1 , - 1 ) :
		a [ i - 1 ] = a [ i - 1 ] + a [ i ]
def modify ( a , n ) :
	for i in range ( 1 , n ) :
		a [ i - 1 ] = i * a [ i ] ;
def allCombination ( a , n ) :
	sum = 0
	for i in range ( 1 , n + 1 ) :
		sum += i
	print ( " f ( 1 ) ▁ - - > ▁ " , sum )
	for i in range ( 1 , n ) :
		postfix ( a , n - i + 1 )
		sum = 0
		for j in range ( 1 , n - i + 1 ) :
			sum += ( j * a [ j ] )
		print ( " f ( " , i + 1 , " ) ▁ - - > ▁ " , sum )
		modify ( a , n )
if __name__ == " _ _ main _ _ " :
	n = 5
	a = [ 0 ] * n
	for i in range ( n ) :
		a [ i ] = i + 1
	allCombination ( a , n )
inputs: "Minimum number of replacement done of substring "01" with "110" to remove it completely | Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code"
Solution in Python:
targets: def minimumOperations ( S , N ) :
	ans = 0
	cntOne = 0
	i = N - 1
	while ( i >= 0 ) :
		if ( S [ i ] == '0' ) :
			ans += cntOne
			cntOne *= 2
		else :
			cntOne += 1
		i -= 1
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	S = "001"
	N = len ( S )
	minimumOperations ( S , N )