Muennighoff/xP3x-sample huggingface dataset:
inputs: "Sum of even values and update queries on an array | Function to return the sum of even elements after updating value at given index ; Add given value to A [ index ] ; To store the sum of even elements ; If current element is even ; Function to print result for every query ; Resultant vector that stores the result for every query ; Get sum of even elements after updating value at given index ; Store sum for each query ; Print the result for every query ; Driver code"
Solution in Python:
targets: def EvenSum ( A , index , value ) :
	A [ index ] = A [ index ] + value
	sum = 0
	for i in A :
		if ( i % 2 == 0 ) :
			sum = sum + i
	return sum
def BalanceArray ( A , Q ) :
	ANS = [ ]
	i , sum = 0 , 0
	for i in range ( len ( Q ) ) :
		index = Q [ i ] [ 0 ]
		value = Q [ i ] [ 1 ]
		sum = EvenSum ( A , index , value )
		ANS . append ( sum )
	for i in ANS :
		print ( i , end = " ▁ " )
A = [ 1 , 2 , 3 , 4 ]
Q = [ [ 0 , 1 ] , [ 1 , - 3 ] , [ 0 , - 4 ] , [ 3 , 2 ] ]
BalanceArray ( A , Q )
inputs: "Minimize cost of removals required to make all remaining characters of the string unique | Function to find the minimum cost of removing characters to make the string unique ; Store the minimum cost required ; Create a dictionary to store the maximum cost of removal a character ; Create a dictionary to store the total deletion cost of a character ; Traverse the string , S ; Keep track of maximum cost of each character ; Update the maximum deletion cost ; Keep track of the total cost of each character ; Update the total deletion cost ; Traverse through all the unique characters ; Keep the maximum cost character and delete the rest ; Return the answer ; Given string ; Given cost array ; Function Call"
Solution in Javascript:
targets: function delCost ( s , cost ) { var ans = 0 ; var forMax = new Map ( ) ; var forTot = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! forMax . has ( s [ i ] ) ) { forMax . set ( s [ i ] , cost [ i ] ) ; } else { forMax . set ( s [ i ] , Math . max ( forMax . get ( s [ i ] ) , cost [ i ] ) ) } if ( ! forTot . has ( s [ i ] ) ) { forTot . set ( s [ i ] , cost [ i ] ) ; } else { forTot . set ( s [ i ] , forTot . get ( s [ i ] ) + cost [ i ] ) } } forMax . forEach ( ( value , key ) => { ans += forTot . get ( key ) - value ; } ) ; return ans ; } var s = " " ; var cost = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; document . write ( delCost ( s , cost ) ) ;
inputs: Maximum Sum Increasing Subsequence | DP | Dynamic Programming implementation of Maximum Sum Increasing Subsequence ( MSIS ) problem ; maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
int maxSumIS ( int arr [ ] , int n ) { int i , j , max = 0 ; int msis [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( max < msis [ i ] ) max = msis [ i ] ; return max ; } int main ( ) { int arr [ ] = { 1 , 101 , 2 , 3 , 100 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ " " subsequence ▁ is ▁ % d 
 " , maxSumIS ( arr , n ) ) ; return 0 ; }
inputs: "Subtract two numbers without using arithmetic operators | PHP Program to subtract two Number without using arithmetic operator ; Iterate till there is no carry ; borrow contains common set bits of y and unset bits of x ; Subtraction of bits of x and y where at least one of the bits is not set ; Borrow is shifted by one so that subtracting it from x gives the required sum ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function subtract ( $ x , $ y ) { while ( $ y != 0 ) { $ borrow = ( ~ $ x ) & $ y ; $ x = $ x ^ $ y ; $ y = $ borrow << 1 ; } return $ x ; } $ x = 29 ; $ y = 13 ; echo " x ▁ - ▁ y ▁ is ▁ " , subtract ( $ x , $ y ) ; ? >
inputs: "Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; If not possible ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; Return minimum operations required ; Driver code"
Solution in C#:
targets: using System ; class GFG { static int minOperations ( int n , int m , int k , int [ , ] matrix ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 , 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i , j ] ; if ( matrix [ i , j ] % k != mod ) { return - 1 ; } } } Array . Sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void Main ( ) { int [ , ] matrix = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . GetLength ( 0 ) ; int m = matrix . GetLength ( 1 ) ; int k = 2 ; Console . WriteLine ( minOperations ( n , m , k , matrix ) ) ; } }
inputs: "Print list items containing all characters of a given word | C program to print all strings that contain all characters of a word ; prints list items having all characters of word ; Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length of given word ; Check each item of list if has all characters of word ; unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver program to test to pront printDups"
Solution in C:
targets: # include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define NO_OF_CHARS  256
void print ( char * list [ ] , char * word , int list_size ) { int * map = ( int * ) calloc ( sizeof ( int ) , NO_OF_CHARS ) ; int i , j , count , word_size ; for ( i = 0 ; * ( word + i ) ; i ++ ) map [ * ( word + i ) ] = 1 ; word_size = strlen ( word ) ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; * ( list [ i ] + j ) ; j ++ ) { if ( map [ * ( list [ i ] + j ) ] ) { count ++ ; map [ * ( list [ i ] + j ) ] = 0 ; } } if ( count == word_size ) printf ( " % s " , list [ i ] ) ; for ( j = 0 ; * ( word + j ) ; j ++ ) map [ * ( word + j ) ] = 1 ; } } int main ( ) { char str [ ] = " sun " ; char * list [ ] = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; getchar ( ) ; return 0 ; }
inputs: "Print all possible combinations of r elements in a given array of size n | Program to print all combination of size r in an array of size n ; The main function that prints all combinations of size r in arr [ ] of size n . This function mainly uses combinationUtil ( ) ; A temporary array to store all combination one by one ; Print all combination using temprary array ' data [ ] ' ; arr [ ] -- -> Input Array data [ ] -- -> Temporary array to store current combination start & end -- -> Staring and Ending indexes in arr [ ] index -- -> Current index in data [ ] r -- -> Size of a combination to be printed ; Current combination is ready to be printed , print it ; replace index with all possible elements . The condition " end - i + 1 ▁ > = ▁ r - index " makes sure that including one element at index will make a combination with remaining elements at remaining positions ; Driver program to test above functions"
Solution in C:
targets: #include <stdio.h>
void combinationUtil ( int arr [ ] , int data [ ] , int start , int end , int index , int r ) ; void printCombination ( int arr [ ] , int n , int r ) { int data [ r ] ; combinationUtil ( arr , data , 0 , n - 1 , 0 , r ) ; } void combinationUtil ( int arr [ ] , int data [ ] , int start , int end , int index , int r ) { if ( index == r ) { for ( int j = 0 ; j < r ; j ++ ) printf ( " % d ▁ " , data [ j ] ) ; printf ( " 
 " ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { data [ index ] = arr [ i ] ; combinationUtil ( arr , data , i + 1 , end , index + 1 , r ) ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int r = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printCombination ( arr , n , r ) ; }
inputs: "Minimize cost to reach end of an array by two forward jumps or one backward jump in each move | C # Program to implement the above approach ; Function to find the minimum cost to reach the end of an array ; Base Case : When N < 3 ; Store the results in table ; Initialize base cases ; Iterate over the range [ 2 , N - 2 ] to construct the dp array ; Handle case for the last index , i . e . N - 1 ; Print the answer ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; public class GFG { static void minCost ( int [ ] arr , int n ) { if ( n < 3 ) { Console . WriteLine ( arr [ 0 ] ) ; return ; } int [ ] dp = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = dp [ 0 ] + arr [ 1 ] + arr [ 2 ] ; for ( int i = 2 ; i < n - 1 ; i ++ ) dp [ i ] = Math . Min ( dp [ i - 2 ] + arr [ i ] , dp [ i - 1 ] + arr [ i ] + arr [ i + 1 ] ) ; dp [ n - 1 ] = Math . Min ( dp [ n - 2 ] , dp [ n - 3 ] + arr [ n - 1 ] ) ; Console . WriteLine ( dp [ n - 1 ] ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 9 , 4 , 6 , 8 , 5 } ; int N = arr . Length ; minCost ( arr , N ) ; } }
inputs: "Count Negative Numbers in a Column | JavaScript implementation of Naive method to count of negative numbers in M [ n ] [ m ] ; Follow the path shown using arrows above ; no more negative numbers in this row ; Driver program to test above functions"
Solution in Javascript:
targets: function countNegative ( M , n , m ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; } let M = [ [ - 3 , - 2 , - 1 , 1 ] , [ - 2 , 2 , 3 , 4 ] , [ 4 , 5 , 7 , 8 ] ] ; document . write ( countNegative ( M , 3 , 4 ) ) ;
inputs: "Minimize cost of placing tiles of dimensions 2 * 1 over a Matrix | Function to find the minimum cost in placing N tiles in a grid M [ ] [ ] ; Stores the minimum profit after placing i tiles ; Traverse the grid [ ] [ ] ; Update the orig_cost ; Traverse over the range [ 2 , N ] ; Place tiles horizentally or vertically ; Print the answer ; Driver Code"
Solution in Python:
targets: def tile_placing ( grid , N ) :
	dp = [ 0 ] * ( N + 5 ) ;
	orig_cost = 0 ;
	for i in range ( 2 ) :
		for j in range ( N ) :
			orig_cost += grid [ i ] [ j ] ;
	dp [ 0 ] = 0 ;
	dp [ 1 ] = abs ( grid [ 0 ] [ 0 ] - grid [ 1 ] [ 0 ] ) ;
	for i in range ( 2 , N + 1 ) :
		dp [ i ] = max ( dp [ i - 1 ] + abs ( grid [ 0 ] [ i - 1 ] - grid [ 1 ] [ i - 1 ] ) , dp [ i - 2 ] + abs ( grid [ 0 ] [ i - 2 ] - grid [ 0 ] [ i - 1 ] ) + abs ( grid [ 1 ] [ i - 2 ] - grid [ 1 ] [ i - 1 ] ) ) ;
	print ( orig_cost - dp [ N ] , end = " " ) ;
if __name__ == " _ _ main _ _ " :
	M = [ [ 7 , 5 , 1 , 3 ] , [ 8 , 6 , 0 , 2 ] ] ;
	N = len ( M [ 0 ] ) ;
	tile_placing ( M , N ) ;
inputs: "Count subarrays having sum modulo K same as the length of the subarray | Java program for the above approach ; Function that counts the subarrays having sum modulo k equal to the length of subarray ; Stores the count of ( pref [ i ] - i ) % k ; Stores the count of subarray ; Stores prefix sum of the array ; Find prefix sum array ; Base Condition ; Remove the index at present after K indices from the current index ; Update the answer for subarrays ending at the i - th index ; Add the calculated value of current index to count ; Print the count of subarrays ; Driver Code ; Given arr [ ] ; Size of the array ; Given K ; Function call"
How can the above be solved in Java?
targets: import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void countSubarrays ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > cnt = new HashMap < > ( ) ; long ans = 0 ; ArrayList < Integer > pref = new ArrayList < > ( ) ; pref . add ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref . add ( ( a [ i ] + pref . get ( i ) ) % k ) ; cnt . put ( 0 , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int remIdx = i - k ; if ( remIdx >= 0 ) { if ( cnt . containsKey ( ( pref . get ( remIdx ) - remIdx % k + k ) % k ) ) cnt . put ( ( pref . get ( remIdx ) - remIdx % k + k ) % k , cnt . get ( ( pref . get ( remIdx ) - remIdx % k + k ) % k ) - 1 ) ; else cnt . put ( ( pref . get ( remIdx ) - remIdx % k + k ) % k , - 1 ) ; } if ( cnt . containsKey ( ( pref . get ( i ) - i % k + k ) % k ) ) ans += cnt . get ( ( pref . get ( i ) - i % k + k ) % k ) ; if ( cnt . containsKey ( ( pref . get ( i ) - i % k + k ) % k ) ) cnt . put ( ( pref . get ( i ) - i % k + k ) % k , cnt . get ( ( pref . get ( i ) - i % k + k ) % k ) + 1 ) ; else cnt . put ( ( pref . get ( i ) - i % k + k ) % k , 1 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 2 , 3 , 5 , 3 , 1 , 5 } ; int N = arr . length ; int K = 4 ; countSubarrays ( arr , N , K ) ; } }
inputs: Angle between two Planes in 3D | C program to find the Angle between two Planes in 3 D . ; Function to find Angle ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
#include <math.h>
void distance ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { float d = ( a1 * a2 + b1 * b2 + c1 * c2 ) ; float e1 = sqrt ( a1 * a1 + b1 * b1 + c1 * c1 ) ; float e2 = sqrt ( a2 * a2 + b2 * b2 + c2 * c2 ) ; d = d / ( e1 * e2 ) ; float pi = 3.14159 ; float A = ( 180 / pi ) * ( acos ( d ) ) ; printf ( " Angle ▁ is ▁ % .2f ▁ degree " , A ) ; } int main ( ) { float a1 = 1 ; float b1 = 1 ; float c1 = 2 ; float d1 = 1 ; float a2 = 2 ; float b2 = -1 ; float c2 = 1 ; float d2 = -4 ; distance ( a1 , b1 , c1 , a2 , b2 , c2 ) ; return 0 ; }
inputs: "Check if a number starts with another number or not | C ++ program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into strings ; Check if s2 is a prefix of s1 or not using starts_with ( ) function ; If result is true , print " Yes " ; Driver Code ; Given numbers ; Function Call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std ; void checkprefix ( int A , int B ) { string s1 = to_string ( A ) ; string s2 = to_string ( B ) ; bool result ; result = boost :: algorithm :: starts_with ( s1 , s2 ) ; if ( result ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int A = 12345 , B = 12 ; checkprefix ( A , B ) ; return 0 ; }
inputs: "Longest substring such that no three consecutive characters are same | Java implementation of the approach ; Function to return the length of the longest substring such that no three consecutive characters are same ; If the length of the given string is less than 3 ; Initialize temporary and final ans to 2 as this is the minimum length of substring when length of the given string is greater than 2 ; Traverse the string from the third character to the last ; If no three consecutive characters are same then increment temporary count ; Else update the final ans and reset the temporary count ; Driver code"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static int maxLenSubStr ( String s ) { if ( s . length ( ) < 3 ) return s . length ( ) ; int temp = 2 ; int ans = 2 ; for ( int i = 2 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) || s . charAt ( i ) != s . charAt ( i - 2 ) ) temp ++ ; else { ans = Math . max ( temp , ans ) ; temp = 2 ; } } ans = Math . max ( temp , ans ) ; return ans ; } public static void main ( String [ ] args ) { String s = " baaabbabbb " ; System . out . println ( maxLenSubStr ( s ) ) ; } }
inputs: "Check whether a given number is Polydivisible or Not | C # program to check whether a number is polydivisible or not ; function to check polydivisible number ; digit extraction of input number ; store the digits in an array ; n contains first i digits ; n should be divisible by i ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { static void check_polydivisible ( int n ) { int N = n ; List < int > digit = new List < int > ( ) ; while ( n > 0 ) { digit . Add ( ( int ) n % 10 ) ; n /= 10 ; } digit . Reverse ( ) ; bool flag = true ; n = digit [ 0 ] ; for ( int i = 1 ; i < digit . Count ; i ++ ) { n = n * 10 + digit [ i ] ; if ( n % ( i + 1 ) != 0 ) { flag = false ; break ; } } if ( flag ) Console . WriteLine ( N + " ▁ is ▁ Polydivisible ▁ number . " ) ; else Console . WriteLine ( N + " ▁ is ▁ Not ▁ Polydivisible ▁ number . " ) ; } public static void Main ( String [ ] args ) { int n = 345654 ; check_polydivisible ( n ) ; } }
inputs: "Smallest number greater than or equal to N having sum of digits not exceeding S | Java program for the above approach ; Function to calculate sum digits of n ; Function to find the smallest possible integer satisfying the given condition ; If the sum of digits is already smaller than S ; Initialize variables ; Finding last kth digit ; Add remaining to make digit 0 ; If sum of digits does not exceed S ; Update k ; Driver Code ; Given N and S ; Function call"
Solution in Java:
targets: import java . io . * ; class GFG { static int sum ( int n ) { int res = 0 ; while ( n > 0 ) { res += n % 10 ; n /= 10 ; } return res ; } static int smallestNumber ( int n , int s ) { if ( sum ( n ) <= s ) { return n ; } int ans = n , k = 1 ; for ( int i = 0 ; i < 9 ; ++ i ) { int digit = ( ans / k ) % 10 ; int add = k * ( ( 10 - digit ) % 10 ) ; ans += add ; if ( sum ( ans ) <= s ) { break ; } k *= 10 ; } return ans ; } public static void main ( String [ ] args ) { int N = 3 , S = 2 ; System . out . println ( smallestNumber ( N , S ) ) ; } }
inputs: "Index of character depending on frequency count in string | C # implementation of the approach ; Function to perform the queries ; L [ i , j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i , j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ , ] and R [ , ] as it is cnt [ j ] th occurrence of character k ; Only update L [ , ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int MAX = 26 ; static void performQueries ( String str , int q , int [ ] type , char [ ] ch , int [ ] freq ) { int n = str . Length ; int [ , ] L = new int [ MAX , n ] ; int [ , ] F = new int [ MAX , n ] ; int [ ] cnt = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = str [ i ] - ' a ' ; cnt [ k ] ++ ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j , cnt [ j ] ] = i ; F [ j , cnt [ j ] ] = i ; } else L [ j , cnt [ j ] ] = L [ j , cnt [ j ] ] + 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { Console . Write ( L [ ch [ i ] - ' a ' , freq [ i ] ] ) ; } else { Console . Write ( F [ ch [ i ] - ' a ' , freq [ i ] ] ) ; } Console . Write ( " 
 " ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int [ ] type = { 1 , 2 } ; char [ ] ch = { ' e ' , ' k ' } ; int [ ] freq = { 2 , 2 } ; int q = type . Length ; performQueries ( str , q , type , ch , freq ) ; } }
inputs: "Largest number that divides x and is co | java program to find the Largest Coprime Divisor ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; divisor code"
Solution in Java:
targets: import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int cpFact ( int x , int y ) { while ( gcd ( x , y ) != 1 ) { x = x / gcd ( x , y ) ; } return x ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 3 ; System . out . println ( cpFact ( x , y ) ) ; x = 14 ; y = 28 ; System . out . println ( cpFact ( x , y ) ) ; x = 7 ; y = 3 ; System . out . println ( cpFact ( x , y ) ) ; } }
inputs: Find all factors of a natural number | Set 1 | C implementation of Naive method to print all divisors ; function to print the divisors ; Driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
void printDivisors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) if ( n % i == 0 ) printf ( " % d ▁ " , i ) ; } int main ( ) { printf ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ 
 " ) ; printDivisors ( 100 ) ; return 0 ; }
inputs: "Number of quadruples where the first three terms are in AP and last three terms are in GP | C # implementation of the approach ; Function to return the count of quadruples ; Hash table to count the number of occurrences ; Traverse and increment the count ; Run two nested loop for second and third element ; If they are same ; Initially decrease the count ; Find the first element using common difference ; Find the fourth element using GP y ^ 2 = x * z property ; If it is an integer ; If not equal ; Same elements ; Later increase the value for future calculations ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { static int countQuadruples ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . ContainsKey ( a [ i ] ) ) { mp [ a [ i ] ] = mp [ a [ i ] ] + 1 ; } else { mp . Add ( a [ i ] , 1 ) ; } int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( j == k ) continue ; mp [ a [ j ] ] = mp [ a [ j ] ] - 1 ; mp [ a [ k ] ] = mp [ a [ k ] ] - 1 ; int first = a [ j ] - ( a [ k ] - a [ j ] ) ; int fourth = ( a [ k ] * a [ k ] ) / a [ j ] ; if ( ( a [ k ] * a [ k ] ) % a [ j ] == 0 ) { if ( a [ j ] != a [ k ] ) { if ( mp . ContainsKey ( first ) && mp . ContainsKey ( fourth ) ) count += mp [ first ] * mp [ fourth ] ; } else if ( mp . ContainsKey ( first ) && mp . ContainsKey ( fourth ) ) count += mp [ first ] * ( mp [ fourth ] - 1 ) ; } if ( mp . ContainsKey ( a [ j ] ) ) { mp [ a [ j ] ] = mp [ a [ j ] ] + 1 ; } else { mp . Add ( a [ j ] , 1 ) ; } if ( mp . ContainsKey ( a [ k ] ) ) { mp [ a [ k ] ] = mp [ a [ k ] ] + 1 ; } else { mp . Add ( a [ k ] , 1 ) ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 6 , 4 , 9 , 2 } ; int n = a . Length ; Console . Write ( countQuadruples ( a , n ) ) ; } }
inputs: "Find the sum of all Betrothed numbers up to N | C ++ program to find the sum of the all betrothed numbers up to N ; Function to find the sum of the all betrothed numbers ; To store the betrothed numbers ; Calculate sum of number_1 's divisors  1 is always a divisor ; i = 2 because we don 't  want to include  1 as a divisor. ; Sum all betrothed numbers up to N ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int Betrothed_Sum ( int n ) { vector < int > Set ; for ( int number_1 = 1 ; number_1 < n ; number_1 ++ ) { int sum_divisor_1 = 1 ; int i = 2 ; while ( i * i <= number_1 ) { if ( number_1 % i == 0 ) { sum_divisor_1 = sum_divisor_1 + i ; if ( i * i != number_1 ) sum_divisor_1 += number_1 / i ; } i ++ ; } if ( sum_divisor_1 > number_1 ) { int number_2 = sum_divisor_1 - 1 ; int sum_divisor_2 = 1 ; int j = 2 ; while ( j * j <= number_2 ) { if ( number_2 % j == 0 ) { sum_divisor_2 += j ; if ( j * j != number_2 ) sum_divisor_2 += number_2 / j ; } j = j + 1 ; } if ( sum_divisor_2 == number_1 + 1 and number_1 <= n && number_2 <= n ) { Set . push_back ( number_1 ) ; Set . push_back ( number_2 ) ; } } } int Summ = 0 ; for ( auto i : Set ) { if ( i <= n ) Summ += i ; } return Summ ; } int main ( ) { int n = 78 ; cout << Betrothed_Sum ( n ) ; return 0 ; }
inputs: "Program to calculate GST from original and net prices | Python3 Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver program to test above functions"
How can the above be solved in Python?
targets: def Calculate_GST ( org_cost , N_price ) :
	return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ;
org_cost = 100
N_price = 120
print ( " GST ▁ = ▁ " , end = ' ' )
print ( round ( Calculate_GST ( org_cost , N_price ) ) , end = ' ' )
print ( " % " )
inputs: "Area of a square inscribed in a circle which is inscribed in an equilateral triangle | Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code"
Solution in php:
targets: < ? php function area ( $ a ) { if ( $ a < 0 ) return -1 ; $ area = sqrt ( $ a ) / 6 ; return $ area ; } $ a = 10 ; echo area ( $ a ) ; ? >
inputs: "Length of the longest substring with every character appearing even number of times | Function to find length of the longest substring with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required substring ; Traverse the string ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Driver Code ; Given string ; : Length of the given string ; Function Call"
How can the above be solved in Python?
targets: def lenOfLongestReqSubstr ( s , N ) :
	ind = { }
	mask = 0
	ind [ 0 ] = - 1
	ans = 0
	for i in range ( N ) :
		val = ord ( s [ i ] ) - ord ( '0' )
		mask ^= ( 1 << val )
		if ( mask in ind ) :
			ans = max ( ans , i - ind [ mask ] )
		else :
			ind [ mask ] = i
	return ans
if __name__ == " _ _ main _ _ " :
	s = "223015150"
	N = len ( s )
	print ( lenOfLongestReqSubstr ( s , N ) )
inputs: "Ways to split array into two groups of same XOR value | CPP Program to count number of ways to split array into two groups such that each group has equal XOR value ; Return the count number of ways to split array into two groups such that each group has equal XOR value . ; We can split only if XOR is 0. Since XOR of all is 0 , we can consider all subsets as one group . ; Driver Program"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int countgroup ( int a [ ] , int n ) { int xs = 0 ; for ( int i = 0 ; i < n ; i ++ ) xs = xs ^ a [ i ] ; if ( xs == 0 ) return ( 1 << ( n - 1 ) ) - 1 ; return 0 ; } int main ( ) { int a [ ] = { 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countgroup ( a , n ) << endl ; return 0 ; }
inputs: "Temple Offerings | Returns minimum offerings required ; Go through all templs one by one ; Go to left while height keeps increasing ; Go to right while height keeps increasing ; This temple should offer maximum of two values to follow the rule . ; Driver code"
How can the above be solved in PHP?
targets: < ? php function offeringNumber ( $ n , $ templeHeight ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ left = 0 ; $ right = 0 ; for ( $ j = $ i - 1 ; $ j >= 0 ; -- $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j + 1 ] ) ++ $ left ; else break ; } for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j - 1 ] ) ++ $ right ; else break ; } $ sum += max ( $ right , $ left ) + 1 ; } return $ sum ; } $ arr1 = array ( 1 , 2 , 2 ) ; echo offeringNumber ( 3 , $ arr1 ) , " 
 " ; $ arr2 = array ( 1 , 4 , 3 , 6 , 2 , 1 ) ; echo offeringNumber ( 6 , $ arr2 ) , " 
 " ; ? >
inputs: "Search an element in a sorted and rotated array | Java program to search an element in sorted and rotated array using single pass of Binary Search ; Returns index of key in arr [ l . . h ] if key is present , otherwise returns - 1 ; If arr [ l ... mid ] first subarray is sorted ; As this subarray is sorted , we can quickly check if key lies in half or other half ; If key not lies in first half subarray , Divide other half into two subarrays , such that we can quickly check if key lies in other half ; If arr [ l . . mid ] first subarray is not sorted , then arr [ mid ... h ] must be sorted subarry ; main function"
How can the above be solved in Java?
targets: class Main { static int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; return search ( arr , mid + 1 , h , key ) ; } if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 } ; int n = arr . length ; int key = 6 ; int i = search ( arr , 0 , n - 1 , key ) ; if ( i != - 1 ) System . out . println ( " Index : ▁ " + i ) ; else System . out . println ( " Key ▁ not ▁ found " ) ; } }
inputs: "Print reverse string after removing vowels | Function for replacing the string ; initialize a string of length n ; Traverse through all characters of string ; assign the value to string r from last index of string s ; if s [ i ] is a consonant then print r [ i ] ; Driver function"
Solution in Javascript:
targets: function replaceOriginal ( s , n ) { var r = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { r [ i ] = s . charAt ( n - 1 - i ) ; if ( s . charAt ( i ) != ' ' && s . charAt ( i ) != ' ' && s . charAt ( i ) != ' ' && s . charAt ( i ) != ' ' && s . charAt ( i ) != ' ' ) { document . write ( r [ i ] ) ; } } document . write ( " " ) ; } var s = " " ; var n = s . length ; replaceOriginal ( s , n ) ;
inputs: "Sum of divisors of factorial of a number | C program to find sum of proper divisor of factorial of a number ; function to calculate factorial ; function to calculate sum of divisor ; Returns sum of divisors of n ! ; driver program"
Solution in C:
targets: #include <stdio.h>
int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } int div ( int x ) { int ans = 0 ; for ( int i = 1 ; i <= x ; i ++ ) if ( x % i == 0 ) ans += i ; return ans ; } int sumFactDiv ( int n ) { return div ( fact ( n ) ) ; } int main ( ) { int n = 4 ; printf ( " % d " , sumFactDiv ( n ) ) ; }
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 <stdio.h>
#include <string.h>
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 ) ; printf ( " % s " , str ) ; return 0 ; }
inputs: "Treasure and Cities | A memoization based program to find maximum treasure that can be collected . ; k is current index and col is previous color . ; base case ; we have two options either visit current city or skip that ; check if color of this city is equal to prev visited city ; return max of both options ; Driver code"
How can the above be solved in Python?
targets: MAX = 1001
dp = [ [ - 1 for i in range ( MAX ) ] for i in range ( MAX ) ]
def MaxProfit ( treasure , color , n , k , col , A , B ) :
	if ( k == n ) :
		dp [ k ] [ col ] = 0
		return dp [ k ] [ col ]
	if ( dp [ k ] [ col ] != - 1 ) :
		return dp [ k ] [ col ]
	summ = 0
	if ( col == color [ k ] ) :
		summ += max ( A * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) )
	else :
		summ += max ( B * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) )
	dp [ k ] [ col ] = summ
	return dp [ k ] [ col ]
A = - 5
B = 7
treasure = [ 4 , 8 , 2 , 9 ]
color = [ 2 , 2 , 6 , 2 ]
n = len ( color )
print ( MaxProfit ( treasure , color , n , 0 , 0 , A , B ) )
inputs: "Program to print non square numbers | Javascript program to print first n non - square numbers . ; Print curr_count numbers . curr_count is current gap between two square numbers . ; Skip a square number . ; Count of next non - square numbers is next even number . ; Driver code"
Solution in Javascript:
targets: function printNonSquare ( n ) { let curr_count = 2 , num = 2 , count = 0 ; while ( count < n ) { for ( let i = 0 ; i < curr_count && count < n ; i ++ ) { document . write ( num + " " ) ; count ++ ; num ++ ; } num ++ ; curr_count += 2 ; } } let n = 10 ; printNonSquare ( n ) ;
inputs: "Check if a string has all characters with same frequency with one variation allowed | Assuming only lower case characters ; To check a string S can be converted to a valid string by removing less than or equal to one character . ; freq [ ] : stores the frequency of each character of a string ; Find first character with non - zero frequency ; Find a character with frequency different from freq1 . ; If we find a third non - zero frequency or count of both frequencies become more than 1 , then return false ; If we find a third non - zero freq ; If counts of both frequencies is more than 1 ; Return true if we reach here ; Driver code"
Solution in Python:
targets: CHARS = 26
def isValidString ( str ) :
	freq = [ 0 ] * CHARS
	for i in range ( len ( str ) ) :
		freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1
	freq1 = 0
	count_freq1 = 0
	for i in range ( CHARS ) :
		if ( freq [ i ] != 0 ) :
			freq1 = freq [ i ]
			count_freq1 = 1
			break
	freq2 = 0
	count_freq2 = 0
	for j in range ( i + 1 , CHARS ) :
		if ( freq [ j ] != 0 ) :
			if ( freq [ j ] == freq1 ) :
				count_freq1 += 1
			else :
				count_freq2 = 1
				freq2 = freq [ j ]
				break
	for k in range ( j + 1 , CHARS ) :
		if ( freq [ k ] != 0 ) :
			if ( freq [ k ] == freq1 ) :
				count_freq1 += 1
			if ( freq [ k ] == freq2 ) :
				count_freq2 += 1
			else :
				return False
		if ( count_freq1 > 1 and count_freq2 > 1 ) :
			return False
	return True
if __name__ == " _ _ main _ _ " :
	str = " abcbc "
	if ( isValidString ( str ) ) :
		print ( " YES " )
	else :
		print ( " NO " )
inputs: "Pairs of complete strings in two sets of strings | Returns count of complete pairs from set [ 0. . n - 1 ] and set2 [ 0. . m - 1 ] ; con_s1 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set1 [ i ] . Similarly con_s2 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set2 [ i ] ; Process all strings in set1 [ ] ; initializing all bits to 0 ; Setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; Process all strings in set2 [ ] ; initializing all bits to 0 ; setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; assigning a variable whose all 26 ( 0. . 25 ) bits are set to 1 ; Now consider every pair of integer in con_s1 [ ] and con_s2 [ ] and check if the pair is complete . ; if all bits are set , the strings are complete ! ; Driver code"
Solution in Javascript:
targets: function countCompletePairs ( set1 , set2 , n , m ) { let result = 0 ; let con_s1 = new Array ( n ) ; let con_s2 = new Array ( m ) ; for ( let i = 0 ; i < n ; i ++ ) { con_s1 [ i ] = 0 ; for ( let j = 0 ; j < set1 [ i ] . length ; j ++ ) { con_s1 [ i ] = con_s1 [ i ] | ( 1 << ( set1 [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } } for ( let i = 0 ; i < m ; i ++ ) { con_s2 [ i ] = 0 ; for ( let j = 0 ; j < set2 [ i ] . length ; j ++ ) { con_s2 [ i ] = con_s2 [ i ] | ( 1 << ( set2 [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } } let complete = ( 1 << 26 ) - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( ( con_s1 [ i ] con_s2 [ j ] ) == complete ) { result ++ ; } } } return result ; } let set1 = [ " " , " " , " " , " " ] ; let set2 = [ " " , " " , " " ] let n = set1 . length ; let m = set2 . length ; document . write ( countCompletePairs ( set1 , set2 , n , m ) ) ;
inputs: "Numbers in a Range with given Digital Root | C # implementation of the approach ; Function to return the count of required numbers ; Count of numbers present in given range ; Number of groups of 9 elements starting from L ; Left over elements not covered in factor 9 ; One Number in each group of 9 ; To check if any number in rem satisfy the property ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int countNumbers ( int L , int R , int K ) { if ( K == 9 ) { K = 0 ; } int totalnumbers = R - L + 1 ; int factor9 = totalnumbers / 9 ; int rem = totalnumbers % 9 ; int ans = factor9 ; for ( int i = R ; i > R - rem ; i -- ) { int rem1 = i % 9 ; if ( rem1 == K ) { ans ++ ; } } return ans ; } public static void Main ( ) { int L = 10 ; int R = 22 ; int K = 3 ; Console . WriteLine ( countNumbers ( L , R , K ) ) ; } }
inputs: "Count numbers in range such that digits in it and it 's product with q are unequal | C # program for above approach ; Function to check if all of the digits in a number and it 's product with q  are unequal or not ; convert first number into string ; Insert elements from 1 st number to hash ; Calculate corresponding product ; Convert the product to string ; Using the hash check if any digit of product matches with the digits of input number ; If yes , return false ; Else , return true ; Function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal ; check for every number between l and r ; Driver Code ; Function call"
Solution in C#:
targets: using System ; class GfG { static bool checkIfUnequal ( int n , int q ) { string s1 = n . ToString ( ) ; int [ ] a = new int [ 26 ] ; for ( int i = 0 ; i < s1 . Length ; i ++ ) a [ s1 [ i ] - '0' ] ++ ; int prod = n * q ; string s2 = prod . ToString ( ) ; for ( int i = 0 ; i < s2 . Length ; i ++ ) { if ( a [ s2 [ i ] - '0' ] ) return false ; } return true ; } static int countInRange ( int l , int r , int q ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( checkIfUnequal ( i , q ) ) count ++ ; } return count ; } public static void Main ( ) { int l = 10 , r = 12 , q = 2 ; Console . WriteLine ( countInRange ( l , r , q ) ) ; } }
inputs: Program to find whether a no is power of two | C Program to find whether a no is power of two ; Function to check if x is power of 2 ; Driver program
How can the above be solved in C?
targets: #include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( ceil ( log2 ( n ) ) == floor ( log2 ( n ) ) ) ; } int main ( ) { isPowerOfTwo ( 31 ) ? printf ( " Yes 
 " ) : printf ( " No 
 " ) ; isPowerOfTwo ( 64 ) ? printf ( " Yes 
 " ) : printf ( " No 
 " ) ; return 0 ; }
inputs: Matrix Chain Multiplication | DP | See the Cormen book for details of the following algorithm ; Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in m [ ] [ ] . 0 th row and 0 th column of m [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; L is chain length . ; q = cost / scalar multiplications ; Driver code
How can the above be solved in C?
targets: #include <limits.h>
#include <stdio.h>
int MatrixChainOrder ( int p [ ] , int n ) { int m [ n ] [ n ] ; int i , j , k , L , q ; for ( i = 1 ; i < n ; i ++ ) m [ i ] [ i ] = 0 ; for ( L = 2 ; L < n ; L ++ ) { for ( i = 1 ; i < n - L + 1 ; i ++ ) { j = i + L - 1 ; m [ i ] [ j ] = INT_MAX ; for ( k = i ; k <= j - 1 ; k ++ ) { q = m [ i ] [ k ] + m [ k + 1 ] [ j ] + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( q < m [ i ] [ j ] ) m [ i ] [ j ] = q ; } } } return m [ 1 ] [ n - 1 ] ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ % d ▁ " , MatrixChainOrder ( arr , size ) ) ; getchar ( ) ; return 0 ; }
inputs: "Program to find the Magnitude of a Vector | Function to calculate magnitude of a 3 dimensional vector ; Stores the sum of squares of coordinates of a vector ; Return the magnitude ; Driver Code"
Solution in Javascript:
targets: function vectorMagnitude ( x , y , z ) { var sum = x * x + y * y + z * z ; return Math . sqrt ( sum ) ; } var x = 1 ; var y = 2 ; var z = 3 ; document . write ( vectorMagnitude ( x , y , z ) ) ;
inputs: "Maximum count of common divisors of A and B such that all are co | C # implementation of the above approach ; Function to return the count of common factors of a and b such that all the elements are co - prime to one another ; GCD of a and b ; Include 1 initially ; Find all the prime factors of the gcd ; If gcd is prime ; Return the required answer ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxCommonFactors ( int a , int b ) { int __gcd = gcd ( a , b ) ; int ans = 1 ; for ( int i = 2 ; i * i <= __gcd ; i ++ ) { if ( __gcd % i == 0 ) { ans ++ ; while ( __gcd % i == 0 ) __gcd /= i ; } } if ( __gcd != 1 ) ans ++ ; return ans ; } public static void Main ( String [ ] args ) { int a = 12 , b = 18 ; Console . WriteLine ( maxCommonFactors ( a , b ) ) ; } }
inputs: "Lexicographically smallest K | C # program to find lexicographically smallest K - length substring containing maximum number of vowels ; Function that prints the lexicographically smallest K - length substring containing maximum number of vowels ; Store the length of the string ; Initialize a prefix sum array ; Loop through the string to create the prefix sum array ; Store 1 at the index if it is a vowel ; Otherwise , store 0 ; Process the prefix array ; Initialize the variable to store maximum count of vowels ; Initialize the variable to store substring with maximum count of vowels ; Loop through the prefix array ; Store the current count of vowels ; Update the result if current count is greater than maximum count ; Update lexicographically smallest substring if the current count is equal to the maximum count ; Return the result ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static string maxVowelSubString ( string str , int K ) { int N = str . Length ; int [ ] pref = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' a ' str [ i ] == ' e ' str [ i ] == ' i ' str [ i ] == ' o ' str [ i ] == ' u ' ) pref [ i ] = 1 ; else pref [ i ] = 0 ; if ( i != 0 ) pref [ i ] += pref [ i - 1 ] ; } int maxCount = pref [ K - 1 ] ; string res = str . Substring ( 0 , K ) ; for ( int i = K ; i < N ; i ++ ) { int currCount = pref [ i ] - pref [ i - K ] ; if ( currCount > maxCount ) { maxCount = currCount ; res = str . Substring ( i - K + 1 , K ) ; } else if ( currCount == maxCount ) { string temp = str . Substring ( i - K + 1 , K ) ; if ( string . Compare ( temp , res ) == - 1 ) res = temp ; } } return res ; } public static void Main ( ) { string str = " ceebbaceeffo " ; int K = 3 ; Console . Write ( maxVowelSubString ( str , K ) ) ; } }
inputs: Find maximum ( or minimum ) in Binary Tree | C program to find maximum and minimum in a Binary Tree ; A tree node ; A utility function to create a new node ; Returns maximum value in a given Binary Tree ; Base case ; Return maximum of 3 values : 1 ) Root 's data 2) Max in Left Subtree 3) Max in right subtree ; Driver code ; Function call
How can the above be solved in C?
targets: #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int findMax ( struct Node * root ) { if ( root == NULL ) return INT_MIN ; int res = root -> data ; int lres = findMax ( root -> left ) ; int rres = findMax ( root -> right ) ; if ( lres > res ) res = lres ; if ( rres > res ) res = rres ; return res ; } int main ( void ) { struct Node * NewRoot = NULL ; struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; printf ( " Maximum ▁ element ▁ is ▁ % d ▁ 
 " , findMax ( root ) ) ; return 0 ; }
inputs: "Modify string by removing vowels in between two consonants | C ++ program to remove all Vowels in between two consonants from the string ; Function to check if the character x is a vowel or not ; Returns the updated string formed after removing all the Sandwiched Vowels from the given string ; string to store the Updated String after removing the Sandwiched Vowels ; traverse the string from left to right ; if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched ' ; Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string ; if this character is not a sandwiched Vowel append it to the updated String ; Driver Code ; Remove all the Sandwitched Vowels"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool isVowel ( char x ) { if ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) return true ; else return false ; } string updateSandwichedVowels ( string a ) { int n = a . length ( ) ; string updatedString = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! i i == n - 1 ) { updatedString += a [ i ] ; continue ; } if ( isVowel ( a [ i ] ) && ! isVowel ( a [ i - 1 ] ) && ! isVowel ( a [ i + 1 ] ) ) { continue ; } updatedString += a [ i ] ; } return updatedString ; } int main ( ) { string str = " geeksforgeeks " ; string updatedString = updateSandwichedVowels ( str ) ; cout << updatedString ; return 0 ; }
inputs: "Sum of products of all combination taken ( 1 to n ) at a time | to store sum of every combination ; if we have reached sufficient depth ; find the product of combination ; add the product into sum ; recursion to produce different combination ; function to print sum of products of all combination taken 1 - N at a time ; creating temporary array for storing combination ; call combination with r = i for combination taken i at a time ; displaying sum ; Driver code ; storing numbers from 1 - N in array ; calling allCombination"
How can the above be solved in JS?
targets: let sum = 0 ; function Combination ( a , combi , n , r , depth , index ) { if ( index == r ) { let product = 1 ; for ( let i = 0 ; i < r ; i ++ ) product = product * combi [ i ] ; sum += product ; return ; } for ( let i = depth ; i < n ; i ++ ) { combi [ index ] = a [ i ] ; Combination ( a , combi , n , r , i + 1 , index + 1 ) ; } } function allCombination ( a , n ) { for ( let i = 1 ; i <= n ; i ++ ) { let combi = [ ] ; Combination ( a , combi , n , i , 0 , 0 ) ; document . write ( " " + i + " " + sum + " " ) ; sum = 0 ; } } let n = 5 ; let a = [ ] ; for ( let i = 0 ; i < n ; i ++ ) a [ i ] = i + 1 ; allCombination ( a , n ) ;