Muennighoff/xP3x-sample huggingface dataset:
inputs: "Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | Python3 program for the above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call"
How can the above be solved in Python?
targets: INF = 10000
def minCost ( arr , n ) :
	arr = sorted ( arr )
	m = 2 * n
	cost = [ [ INF for i in range ( m + 1 ) ] for i in range ( n + 1 ) ]
	cost [ 0 ] [ 0 ] = 0
	prev = 0
	for i in range ( 1 , n + 1 ) :
		prev = cost [ i - 1 ] [ 0 ]
		for j in range ( 1 , m + 1 ) :
			cost [ i ] [ j ] = min ( cost [ i ] [ j ] , prev + abs ( j - arr [ i - 1 ] ) )
			prev = min ( prev , cost [ i - 1 ] [ j ] )
	minCost = INF
	for j in range ( 1 , m + 1 ) :
		minCost = min ( minCost , cost [ n ] [ j ] )
	print ( minCost )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 2 , 4 , 4 , 5 , 2 ]
	N = len ( arr )
	minCost ( arr , N )
inputs: "Minimum flips required to generate continuous substrings of 0 â €™ s and 1 â €™ s | Javascript implementation of the above approach ; Traverse input string and store the count of 0 ; Traverse the input string again to find minimum number of flips ; Driver code"
How can the above be solved in JS?
targets: function minChanges ( str , N ) { var res ; var count0 = 0 , count1 = 0 ; str . split ( ' ' ) . forEach ( x => { count0 += ( x == ' ' ) ; } ) ; res = count0 ; str . split ( ' ' ) . forEach ( x => { count0 -= ( x == ' ' ) ; count1 += ( x == ' ' ) ; res = Math . min ( res , count1 + count0 ) ; } ) ; return res ; } var N = 9 ; var str = " " ; document . write ( minChanges ( str , N ) ) ;
inputs: Find a triplet that sum to a given value |  ; returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; If we reach here , then no triplet was found ; Driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
bool find3Numbers ( int A [ ] , int arr_size , int sum ) { int l , r ; for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { for ( int j = i + 1 ; j < arr_size - 1 ; j ++ ) { for ( int k = j + 1 ; k < arr_size ; k ++ ) { if ( A [ i ] + A [ j ] + A [ k ] == sum ) { printf ( " Triplet ▁ is ▁ % d , ▁ % d , ▁ % d " , A [ i ] , A [ j ] , A [ k ] ) ; return true ; } } } } return false ; } int main ( ) { int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; find3Numbers ( A , arr_size , sum ) ; return 0 ; }
inputs: "Count of distinct pair sum between two 1 to N value Arrays | Java implementation to count of distinct pair sum between two Array with values 1 to N ; Function to find the distinct sums ; Set to store distinct sums ; Inserting every sum ; Returning distinct sums ; Driver code"
Solution in Java:
targets: import java . util . * ; class GFG { static int findDistinctSums ( int n ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { s . add ( i + j ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( findDistinctSums ( N ) ) ; } }
inputs: "Print all longest common sub | Javascript program to find all LCS of two strings in sorted order . ; length of lcs ; dp matrix to store result of sub calls for lcs ; A memoization based function that returns LCS of str1 [ i . . len1 - 1 ] and str2 [ j . . len2 - 1 ] ; base condition ; if lcs has been computed ; if characters are same return previous + 1 else max of two sequences after removing i ' th ▁ and ▁ j ' th char one by one ; Function to print all routes common sub - sequences of length lcslen ; if currlcs is equal to lcslen then print it ; if we are done with all the characters of both string ; here we have to print all sub - sequences lexicographically , that ' s ▁ why ▁ we ▁ start ▁ from ▁ ' a ' to ' z ' if this character is  present in both of them then append it in data[] and same  remaining part ; done is a flag to tell that we have printed all the subsequences corresponding to current character ; if character ch is present in str1 then check if it is present in str2 ; if ch is present in both of them and remaining length is equal to remaining lcs length then add ch in sub - sequenece ; If we found LCS beginning with current character . ; This function prints all LCS of str1 and str2 in lexicographic order . ; Find lengths of both strings ; Find length of LCS ; Print all LCS using recursive backtracking data [ ] is used to store individual LCS . ; Driver code"
How can the above be solved in JS?
targets: let MAX = 100 ; let lcslen = 0 ; let dp = new Array ( MAX ) ; function lcs ( str1 , str2 , len1 , len2 , i , j ) { let ret = dp [ i ] [ j ] ; if ( i == len1 j == len2 ) return ret = 0 ; if ( ret != - 1 ) return ret ; ret = 0 ; if ( str1 [ i ] == str2 [ j ] ) ret = 1 + lcs ( str1 , str2 , len1 , len2 , i + 1 , j + 1 ) ; else ret = Math . max ( lcs ( str1 , str2 , len1 , len2 , i + 1 , j ) , lcs ( str1 , str2 , len1 , len2 , i , j + 1 ) ) ; return ret ; } function printAll ( str1 , str2 , len1 , len2 , data , indx1 , indx2 , currlcs ) { if ( currlcs == lcslen ) { data [ currlcs ] = null ; document . write ( data . join ( " " ) + " " ) ; return ; } if ( indx1 == len1 indx2 == len2 ) return ; for ( let ch = ' ' . charCodeAt ( 0 ) ; ch <= ' ' . charCodeAt ( 0 ) ; ch ++ ) { let done = false ; for ( let i = indx1 ; i < len1 ; i ++ ) { if ( ch == str1 [ i ] . charCodeAt ( 0 ) ) { for ( let j = indx2 ; j < len2 ; j ++ ) { if ( ch == str2 [ j ] . charCodeAt ( 0 ) && lcs ( str1 , str2 , len1 , len2 , i , j ) == lcslen - currlcs ) { data [ currlcs ] = String . fromCharCode ( ch ) ; printAll ( str1 , str2 , len1 , len2 , data , i + 1 , j + 1 , currlcs + 1 ) ; done = true ; break ; } } } if ( done ) break ; } } } function prinlAllLCSSorted ( str1 , str2 ) { let len1 = str1 . length , len2 = str2 . length ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } lcslen = lcs ( str1 , str2 , len1 , len2 , 0 , 0 ) ; let data = new Array ( MAX ) ; printAll ( str1 , str2 , len1 , len2 , data , 0 , 0 , 0 ) ; } let str1 = " " , str2 = " " ; prinlAllLCSSorted ( str1 , str2 ) ;
inputs: "Count ways to split array into two subsets having difference between their sum equal to K | Java program for the above approach ; Function to count the number of ways to divide the array into two subsets and such that the difference between their sums is equal to diff ; Store the sum of the set S1 ; Initializing the matrix ; Number of ways to get sum using 0 elements is 0 ; Number of ways to get sum 0 using i elements is 1 ; Traverse the 2D array ; If the value is greater than the sum store the value of previous state ; Return the result ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?
targets: import java . io . * ; public class GFG { static int countSubset ( int [ ] arr , int n , int diff ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum += diff ; sum = sum / 2 ; int t [ ] [ ] = new int [ n + 1 ] [ sum + 1 ] ; for ( int j = 0 ; j <= sum ; j ++ ) t [ 0 ] [ j ] = 0 ; for ( int i = 0 ; i <= n ; i ++ ) t [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( arr [ i - 1 ] > j ) t [ i ] [ j ] = t [ i - 1 ] [ j ] ; else { t [ i ] [ j ] = t [ i - 1 ] [ j ] + t [ i - 1 ] [ j - arr [ i - 1 ] ] ; } } } return t [ n ] [ sum ] ; } public static void main ( String [ ] args ) { int diff = 1 , n = 4 ; int arr [ ] = { 1 , 1 , 2 , 3 } ; System . out . print ( countSubset ( arr , n , diff ) ) ; } }
inputs: Program to compute log a to any base b ( logb a ) | C program to find log ( a ) on any base b using Recursion ; Recursive function to compute log a to the base b ; Driver code
How can the above be solved in C?
targets: #include <stdio.h>
int log_a_to_base_b ( int a , int b ) { return ( a > b - 1 ) ? 1 + log_a_to_base_b ( a / b , b ) : 0 ; } int main ( ) { int a = 3 ; int b = 2 ; printf ( " % d 
 " , log_a_to_base_b ( a , b ) ) ; a = 256 ; b = 4 ; printf ( " % d 
 " , log_a_to_base_b ( a , b ) ) ; return 0 ; }
inputs: "In | C ++ program in - place Merge Sort ; Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] Inplace Implementation ; If the direct merge is already sorted ; Two pointers to maintain start of both arrays to merge ; If element 1 is in right place ; Shift all the elements between element 1 element 2 , right by 1. ; Update all the pointers ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and r ; Sort first and second halves ; Function to print an array ; Driver program to test above functions"
Solution in C:
targets: #include <stdio.h>
void merge ( int arr [ ] , int start , int mid , int end ) { int start2 = mid + 1 ; if ( arr [ mid ] <= arr [ start2 ] ) { return ; } while ( start <= mid && start2 <= end ) { if ( arr [ start ] <= arr [ start2 ] ) { start ++ ; } else { int value = arr [ start2 ] ; int index = start2 ; while ( index != start ) { arr [ index ] = arr [ index - 1 ] ; index -- ; } arr [ start ] = value ; start ++ ; mid ++ ; start2 ++ ; } } } void mergeSort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } void printArray ( int A [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) printf ( " % d ▁ " , A [ i ] ) ; printf ( " 
 " ) ; } int main ( ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; mergeSort ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ; return 0 ; }
inputs: "Pascal 's Triangle | binomialCoeff ; Function to print first n lines of Pascal 's Triangle ; Iterate through every line and print entries in it ; Every line has number of integers equal to line number ; Driver Code"
Solution in Javascript:
targets: function binomialCoeff ( n , k ) { let res = 1 ; if ( k > n - k ) k = n - k ; for ( let i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function printPascal ( n ) { for ( let line = 0 ; line < n ; line ++ ) { for ( let i = 0 ; i <= line ; i ++ ) document . write ( binomialCoeff ( line , i ) + " " ) ; document . write ( " " ) ; } } let n = 7 ; printPascal ( n ) ;
inputs: "Find count of Almost Prime numbers from 1 to N | Java program to count almost prime numbers from 1 to n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to count almost prime numbers from 1 to n ; to store required answer ; 6 is first almost prime number ; to count prime factors ; if it is perfect square ; if I is almost prime number ; Driver code"
Solution in Java:
targets: import java . io . * ; class GFG { static int N = 100005 ; static boolean prime [ ] = new boolean [ N ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < N ; i += p ) prime [ i ] = false ; } } } static int almostPrimes ( int n ) { int ans = 0 ; for ( int i = 6 ; i <= n ; i ++ ) { int c = 0 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) { if ( prime [ j ] ) c ++ ; } else { if ( prime [ j ] ) c ++ ; if ( prime [ i / j ] ) c ++ ; } } } if ( c == 2 ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 21 ; System . out . println ( almostPrimes ( n ) ) ; } }
inputs: "Maximize the largest number K such that bitwise and of K till N is 0 | Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code"
How can the above be solved in JS?
targets: function findMaxK ( N ) { var p = Math . log ( N ) / Math . log ( 2 ) ; return parseInt ( Math . pow ( 2 , p ) ) ; } var N = 5 ; document . write ( findMaxK ( N ) - 1 ) ;
inputs: "Minimum number of consecutive sequences that can be formed in an array | PHP program find the minimum number of consecutive sequences in an array ; Driver Code ; function call to print required answer"
Solution in php:
targets: < ? php function countSequences ( $ arr , $ n ) { $ count = 1 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] + 1 != $ arr [ $ i + 1 ] ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 7 , 3 , 5 , 10 ) ; $ n = count ( $ arr ) ; echo countSequences ( $ arr , $ n ) ; ? >
inputs: "Check if a point lies inside a rectangle | Set | Java program to Check if a point lies on or inside a rectangle | Set - 2 ; function to find if given point lies inside a given rectangle or not . ; Driver code ; bottom - left and top - right corners of rectangle ; given point ; function call"
How can the above be solved in Java?
targets: class GFG { static boolean FindPoint ( int x1 , int y1 , int x2 , int y2 , int x , int y ) { if ( x > x1 && x < x2 && y > y1 && y < y2 ) return true ; return false ; } public static void main ( String [ ] args ) { int x1 = 0 , y1 = 0 , x2 = 10 , y2 = 8 ; int x = 1 , y = 5 ; if ( FindPoint ( x1 , y1 , x2 , y2 , x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
inputs: "Print characters and their frequencies in order of occurrence | C ++ implementation to print the characters and frequencies in order of its occurrence ; Store all characters and their frequencies in dictionary ; Print characters and their frequencies in same order of their appearance ; Print only if this character is not printed before ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void prCharWithFreq ( string s ) { unordered_map < char , int > d ; for ( char i : s ) { d [ i ] ++ ; } for ( char i : s ) { if ( d [ i ] != 0 ) { cout << i << d [ i ] << " ▁ " ; d [ i ] = 0 ; } } } int main ( ) { string s = " geeksforgeeks " ; prCharWithFreq ( s ) ; }
inputs: "Number of solutions of n = x + n âŠ • x | C ++ implementation of above approach ; Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int numberOfSolutions ( int n ) { int c = 0 ; for ( int x = 0 ; x <= n ; ++ x ) if ( n == x + n ^ x ) ++ c ; return c ; } int main ( ) { int n = 3 ; cout << numberOfSolutions ( n ) ; return 0 ; }
inputs: "Counts paths from a point to reach Origin | DP based function to count number of paths ; $dp [ $n + 1 ] [ $m + 1 ] ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code"
Solution in php:
targets: < ? php function countPaths ( $ n , $ m ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ 0 ] [ $ i ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ m ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i ] [ $ j - 1 ] ; return $ dp [ $ n ] [ $ m ] ; } $ n = 3 ; $ m = 2 ; echo " ▁ Number ▁ of ▁ Paths ▁ " , countPaths ( $ n , $ m ) ; ? >
inputs: "Minimum steps to reduce N to 0 by given operations | Function to find the minimum number to steps to reduce N to 0 ; Dictionary for storing the precomputed sum ; Bases Cases ; Check if n is not in dp then only call the function so as to reduce no of recursive calls ; Return the answer ; Given Number N ; Function Call"
How can the above be solved in Python?
targets: def count ( n ) :
	dp = dict ( )
	dp [ 0 ] = 0
	dp [ 1 ] = 1
	if n not in dp :
		dp [ n ] = 1 + min ( n % 2 + count ( n // 2 ) , n % 3 + count ( n // 3 ) )
	return dp [ n ]
N = 6
print ( str ( count ( N ) ) )
inputs: "Area of the largest Rectangle without a given point | C # implementation to find area of largest Rectangle without hole within a given Rectangle ; Function to find the maximum area such that it does not contains any hole ; Area for all the possible positions of the cut ; Find the maximum area among the above rectangles ; Driver Code ; Function call"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void maximumArea ( int l , int b , int x , int y ) { int left , right , above , below ; left = x * b ; right = ( l - x - 1 ) * b ; above = l * y ; below = ( b - y - 1 ) * l ; Console . Write ( Math . Max ( Math . Max ( left , right ) , Math . Max ( above , below ) ) ) ; } public static void Main ( String [ ] args ) { int L = 8 , B = 8 ; int X = 0 , Y = 0 ; maximumArea ( L , B , X , Y ) ; } }
inputs: "Find a Fixed Point ( Value equal to index ) in a given array | javascript 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"
Solution in Javascript:
targets: function binarySearch ( arr , low , high ) { if ( high >= low ) { let mid = Math . floor ( ( 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 ; } let arr = [ - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ] ; let n = arr . length ; document . write ( " " + binarySearch ( arr , 0 , n - 1 ) ) ;
inputs: "Count set bits in an integer | C program to count set bits by pre - storing count set bits in nibbles . ; Recursively get nibble of a given number and map them in the array ; Find last nibble ; Use pre - stored values to find count in last nibble plus recursively add remaining nibbles . ; Driver code"
Solution in C:
targets: #include <stdio.h>
int num_to_bits [ 16 ] = { 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 } ; unsigned int countSetBitsRec ( unsigned int num ) { int nibble = 0 ; if ( 0 == num ) return num_to_bits [ 0 ] ; nibble = num & 0xf ; return num_to_bits [ nibble ] + countSetBitsRec ( num >> 4 ) ; } int main ( ) { int num = 31 ; printf ( " % d 
 " , countSetBitsRec ( num ) ) ; }
inputs: "Insert minimum number in array so that sum of array becomes prime | C # program to find minimum number to insert in array so their sum is prime ; Array to store primes ; function to calculate primes using sieve of eratosthenes ; Find prime number greater than a number ; To return prime number greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; call sieveOfEratostheneses to calculate primes ; To find sum of array elements ; To find prime number greater then sum ; Return difference of sum and num ; Driver Code"
How can the above be solved in C-Sharp?
targets: class GFG { static int MAX = 100005 ; static bool [ ] isPrime = new bool [ MAX ] ; static void sieveOfEratostheneses ( ) { isPrime [ 1 ] = true ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( ! isPrime [ i ] ) { for ( int j = 2 * i ; j < MAX ; j += i ) isPrime [ j ] = true ; } } } static int findPrime ( int n ) { int num = n + 1 ; while ( num > 0 ) { if ( ! isPrime [ num ] ) return num ; num = num + 1 ; } return 0 ; } static int minNumber ( int [ ] arr , int n ) { sieveOfEratostheneses ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( ! isPrime [ sum ] ) return 0 ; int num = findPrime ( sum ) ; return num - sum ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 8 , 12 } ; int n = arr . Length ; System . Console . WriteLine ( minNumber ( arr , n ) ) ; } }
inputs: "Count of subarrays with largest element at least twice the largest of remaining elements | C ++ program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the count of subarrays ; Generate all possible subarrays ; Stores the maximum element of the subarray ; Stores the maximum of all other elements ; Find the maximum element in the subarray [ i , j ] ; Find the maximum of all other elements ; If the maximum of subarray is greater than twice the maximum of other elements ; Print the maximum value obtained ; Driver Code"
Solution in C++:
targets: #include <iostream>
using namespace std ; void countSubarray ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int mxSubarray = 0 ; int mxOther = 0 ; for ( int k = i ; k <= j ; k ++ ) { mxSubarray = max ( mxSubarray , arr [ k ] ) ; } for ( int k = 0 ; k < i ; k ++ ) { mxOther = max ( mxOther , arr [ k ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { mxOther = max ( mxOther , arr [ k ] ) ; } if ( mxSubarray > ( 2 * mxOther ) ) count ++ ; } } cout << count ; } int main ( ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSubarray ( arr , N ) ; return 0 ; }
inputs: "Maximum path sum from top left to bottom right of a matrix passing through one of the given cells | Stores the maximum path sum from the cell ( 1 , 1 ) to ( N , M ) ; Stores the maximum path sum from the cell ( j , j ) to ( N , M ) ; Function to find the maximum path sum from the cell ( 1 , 1 ) to ( N , M ) ; Traverse the first row ; Traverse the first column ; Traverse the matrix ; Update the value of start [ i ] [ j ] ; Function to find the maximum path sum from the cell ( j , j ) to ( N , M ) ; Traverse the last row ; Traverse the last column ; Traverse the matrix ; Update the value of ending [ i ] [ j ] ; Function to find the maximum path sum from the top - left to the bottom right cell such that path contains one of the cells in the array coordinates [ ] [ ] ; Initialize the start and the end matrices ; Calculate the start matrix ; Calculate the end matrix ; Stores the maximum path sum ; Traverse the coordinates ; Update the value of ans ; Print the resultant maximum sum path value ; Driver Code"
Solution in Python:
targets: start = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ]
ending = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ]
def calculateStart ( n , m ) :
	for i in range ( 1 , m , 1 ) :
		start [ 0 ] [ i ] += start [ 0 ] [ i - 1 ]
	for i in range ( 1 , n , 1 ) :
		start [ i ] [ 0 ] += start [ i - 1 ] [ 0 ]
	for i in range ( 1 , n , 1 ) :
		for j in range ( 1 , m , 1 ) :
			start [ i ] [ j ] += max ( start [ i - 1 ] [ j ] , start [ i ] [ j - 1 ] )
def calculateEnd ( n , m ) :
	i = n - 2
	while ( i >= 0 ) :
		ending [ i ] [ m - 1 ] += ending [ i + 1 ] [ m - 1 ]
		i -= 1
	i = m - 2
	while ( i >= 0 ) :
		ending [ n - 1 ] [ i ] += ending [ n - 1 ] [ i + 1 ]
		i -= 1
	i = n - 2
	while ( i >= 0 ) :
		j = m - 2
		while ( j >= 0 ) :
			ending [ i ] [ j ] += max ( ending [ i + 1 ] [ j ] , ending [ i ] [ j + 1 ] )
			j -= 1
		i -= 1
def maximumPathSum ( mat , n , m , q , coordinates ) :
	for i in range ( n ) :
		for j in range ( m ) :
			start [ i ] [ j ] = mat [ i ] [ j ]
			ending [ i ] [ j ] = mat [ i ] [ j ]
	calculateStart ( n , m )
	calculateEnd ( n , m )
	ans = 0
	for i in range ( q ) :
		X = coordinates [ i ] [ 0 ] - 1
		Y = coordinates [ i ] [ 1 ] - 1
		ans = max ( ans , start [ X ] [ Y ] + ending [ X ] [ Y ] - mat [ X ] [ Y ] )
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
	N = 3
	M = 3
	Q = 2
	coordinates = [ [ 1 , 2 ] , [ 2 , 2 ] ]
	maximumPathSum ( mat , N , M , Q , coordinates )
inputs: "Program to find the largest and smallest ASCII valued characters in a string | function that return the largest alphabet . ; initializing max alphabet to ' a ' ; find largest alphabet ; returning largest element ; function that return the smallest alphabet ; initializing smallest alphabet to ' z ' ; find smallest alphabet ; returning smallest alphabet ; Input String ; Calculating size of the string ; calling functions and print returned value"
How can the above be solved in JS?
targets: function largest_alphabet ( a , n ) { let max = ' ' ; for ( let i = 0 ; i < n ; i ++ ) if ( a [ i ] . charCodeAt ( ) > max . charCodeAt ( ) ) max = a [ i ] ; return max ; } function smallest_alphabet ( a , n ) { let min = ' ' ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] . charCodeAt ( ) < min . charCodeAt ( ) ) min = a [ i ] ; return min ; } let a = " " ; let size = a . length ; document . write ( " " ) ; document . write ( largest_alphabet ( a , size ) + " " ) ; document . write ( smallest_alphabet ( a , size ) ) ;
inputs: "Count subarrays having an equal count of 0 s and 1 s segregated | Java program for the above approach ; Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count of subarrays ; If current element is different from the next array element ; Increment count ; Count the frequency of 1 s and 0 s ; Increment count ; Print the final count ; Driver Code ; Function Call"
Solution in Java:
targets: import java . util . * ; class GFG { static void countSubarrays ( int A [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) { ans ++ ; for ( int j = i - 1 , k = i + 2 ; j >= 0 && k < N && A [ j ] == A [ i ] && A [ k ] == A [ i + 1 ] ; j -- , k ++ ) { ans ++ ; } } } System . out . print ( ans + "
"); } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 0 , 0 , 1 , 0 } ; int N = A . length ; countSubarrays ( A , N ) ; } }
inputs: "Number of ways to reach Nth floor by taking at | PHP program to reach N - th stair by taking a maximum of K leap ; elements of combo [ ] stores the no of possible ways to reach it by all combinations of k leaps or less ; assuming leap 0 exist and assigning its value to 1 for calculation ; loop to iterate over all possible leaps upto k ; ; in this loop we count all possible leaps to reach the jth stair with the help of ith leap or less ; if the leap is not more than the i - j ; calculate the value and store in combo [ j ] to reuse it for next leap calculation for the jth stair ; returns the no of possible number of leaps to reach the top of building of n stairs ; N i the no of total stairs K is the value of the greatest leap"
Solution in php:
targets: < ? php function solve ( $ N , $ K ) { $ combo [ $ N + 1 ] = array ( ) ; $ combo [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ K ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ N ; $ j ++ ) { if ( $ j >= $ i ) { $ combo [ $ j ] += $ combo [ $ j - $ i ] ; } } } return $ combo [ $ N ] ; } $ N = 29 ; $ K = 5 ; echo solve ( $ N , $ K ) ; solve ( $ N , $ K ) ; ? >
inputs: "Median of difference of all pairs from an Array | Java program to implement the above approach ; Function check if mid can be median index of the difference array ; Size of the array ; Total possible no of pair possible ; The index of the element in the difference of all pairs from the array ; Count the number of pairs having difference <= mid ; If the difference between end and first element is less then or equal to mid ; Checking for the no of element less than or equal to mid is greater than median or not ; Function to calculate the median of differences of all pairs from the array ; Size of the array ; Initialising the low and high ; Binary search ; Calculate mid ; If mid can be the median of the array ; Returning the median of the differences of pairs from the array ; Driver code"
Solution in Java:
targets: import java . util . * ; class GFG { static boolean possible ( long mid , int [ ] a ) { long n = a . length ; long total = ( n * ( n - 1 ) ) / 2 ; long need = ( total + 1 ) / 2 ; long count = 0 ; long start = 0 , end = 1 ; while ( end < n ) { if ( a [ ( int ) end ] - a [ ( int ) start ] <= mid ) { end ++ ; } else { count += ( end - start - 1 ) ; start ++ ; } } if ( end == n && start < end && a [ ( int ) end - 1 ] - a [ ( int ) start ] <= mid ) { long t = end - start - 1 ; count += ( t * ( t + 1 ) / 2 ) ; } if ( count >= need ) return true ; else return false ; } static long findMedian ( int [ ] a ) { long n = a . length ; long low = 0 , high = a [ ( int ) n - 1 ] - a [ 0 ] ; while ( low <= high ) { long mid = ( low + high ) / 2 ; if ( possible ( mid , a ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 7 , 5 , 2 } ; Arrays . sort ( a ) ; System . out . println ( findMedian ( a ) ) ; } }
inputs: "Pair having all other given pairs lying between its minimum and maximum | C # program for the above approach ; Function to find the position of the pair that covers every pair in the array arr [ ] [ ] ; Stores the index of the resultant pair ; To count the occurences ; Iterate to check every pair ; Set count to 0 ; Condition to checked for overlapping of pairs ; If that pair can cover all other pairs then store its position ; If position not found ; Otherwise ; Driver Code ; Give array of pairs ; Function Call"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void position ( int [ , ] arr , int N ) { int pos = - 1 ; int count ; for ( int i = 0 ; i < N ; i ++ ) { count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i , 0 ] <= arr [ j , 0 ] && arr [ i , 1 ] >= arr [ j , 1 ] ) { count ++ ; } } if ( count == N ) { pos = i ; } } if ( pos == - 1 ) { Console . Write ( pos ) ; } else { Console . Write ( pos + 1 ) ; } } public static void Main ( ) { int [ , ] arr = { { 3 , 3 } , { 1 , 3 } , { 2 , 2 } , { 2 , 3 } , { 1 , 2 } } ; int N = arr . GetLength ( 0 ) ; position ( arr , N ) ; } }
inputs: "Minimum number of operations to move all uppercase characters before all lower case characters | Function to return the minimum number of operations required ; To store the indices of the last uppercase and the first lowercase character ; Find the last uppercase character ; Find the first lowercase character ; If all the characters are either uppercase or lowercase ; Count of uppercase characters that appear after the first lowercase character ; Count of lowercase characters that appear before the last uppercase character ; Return the minimum operations required ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function minOperations ( $ str , $ n ) { $ i ; $ lastUpper = -1 ; $ firstLower = -1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ lastUpper = $ i ; break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ firstLower = $ i ; break ; } } if ( $ lastUpper == -1 $ firstLower == -1 ) return 0 ; $ countUpper = 0 ; for ( $ i = $ firstLower ; $ i < $ n ; $ i ++ ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ countUpper ++ ; } } $ countLower = 0 ; for ( $ i = 0 ; $ i < $ lastUpper ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ countLower ++ ; } } return min ( $ countLower , $ countUpper ) ; } { $ str = " geEksFOrGEekS " ; $ n = strlen ( $ str ) ; echo ( minOperations ( $ str , $ n ) ) ; } ? >
inputs: "Find the repeating and the missing | Added 3 new methods | C program to Find the repeating and missing elements ; Driver code"
Solution in C:
targets: #include <stdio.h>
#include <stdlib.h>
void printTwoElements ( int arr [ ] , int size ) { int i ; printf ( " The repeating element is " for ( i = 0 ; i < size ; i ++ ) { if ( arr [ abs ( arr [ i ] ) - 1 ] > 0 ) arr [ abs ( arr [ i ] ) - 1 ] = - arr [ abs ( arr [ i ] ) - 1 ] ; else printf ( " ▁ % d ▁ " , abs ( arr [ i ] ) ) ; } printf ( " and the missing element is " for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > 0 ) printf ( " % d " , i + 1 ) ; } } int main ( ) { int arr [ ] = { 7 , 3 , 4 , 5 , 5 , 6 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printTwoElements ( arr , n ) ; return 0 ; }
inputs: Add two numbers without using arithmetic operators | C Program to add two numbers without using arithmetic operator ; Iterate till there is no carry ; carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
int Add ( int x , int y ) { while ( y != 0 ) { int carry = x & y ; x = x ^ y ; y = carry << 1 ; } return x ; } int main ( ) { printf ( " % d " , Add ( 15 , 32 ) ) ; return 0 ; }
inputs: "Find depth of the deepest odd level leaf node | C # program to find depth of the deepest odd level leaf node of binary tree ; tree node ; returns a new tree Node ; return max odd number depth of leaf node ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if the node is leaf node and level is odd if level is odd , then update result ; check for left child ; check for right child ; Driver Code ; construct a tree"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static int maxOddLevelDepth ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int result = int . MaxValue ; int level = 0 ; while ( q . Count != 0 ) { int size = q . Count ; level += 1 ; while ( size > 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left == null && temp . right == null && ( level % 2 != 0 ) ) { result = level ; } if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; } size -= 1 ; } } return result ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; root . right . right . right . right . left = newNode ( 11 ) ; int result = maxOddLevelDepth ( root ) ; if ( result == int . MaxValue ) Console . WriteLine ( " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level " ) ; else { Console . Write ( result ) ; Console . WriteLine ( " ▁ is ▁ the ▁ required ▁ depth ▁ " ) ; } } }
inputs: "Modify array to another given array by replacing array elements with the sum of the array | C # program for the above approach ; Function to check if the arr [ ] can be converted to target [ ] by replacing any element in arr [ ] by the sum of arr [ ] ; Store the maximum element ; Store the index of the maximum element ; Traverse the array target [ ] ; If current element is greater than max ; If max element is 1 ; Traverse the array , target [ ] ; If current index is not equal to maximum element index ; Update max ; If max is less than or equal to 0 , ; Update the maximum element ; Recursively call the function ; Driver Code"
Solution in C#:
targets: using System ; class GFG { public static bool isPossible ( int [ ] target ) { int max = 0 ; int index = 0 ; for ( int i = 0 ; i < target . Length ; i ++ ) { if ( max < target [ i ] ) { max = target [ i ] ; index = i ; } } if ( max == 1 ) return true ; for ( int i = 0 ; i < target . Length ; i ++ ) { if ( i != index ) { max -= target [ i ] ; if ( max <= 0 ) return false ; } } target [ index ] = max ; return isPossible ( target ) ; } static public void Main ( ) { int [ ] target = { 9 , 3 , 5 } ; bool res = isPossible ( target ) ; if ( res ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
inputs: "The Stock Span Problem | Java program for a linear time solution for stock span problem without using stack ; An efficient method to calculate stock span values implementing the same idea without using stack ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; A utility function to print elements of array ; Driver code ; Fill the span values in array S [ ] ; print the calculated span values"
How can the above be solved in Java?
targets: class GFG { static void calculateSpan ( int A [ ] , int n , int ans [ ] ) { ans [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int counter = 1 ; while ( ( i - counter ) >= 0 && A [ i ] >= A [ i - counter ] ) { counter += ans [ i - counter ] ; } ans [ i ] = counter ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . length ; int S [ ] = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; } }
inputs: "Smallest divisor of N closest to X | Python3 program for the above approach ; Stores divisors for all numbers in the vector divisors ; i is the divisor and j is the multiple ; Function to compare the closeness of the given target ; Function to find the element closest to target in divisors vector ; Corner cases ; Perform binary search ; Check if target is less than the array element then search in left half ; Check if target is greater than previous to mid , return closest of two ; Repeat for left half ; Check if target is greater than mid ; Update i ; Only single element left after search ; Function to print the divisor of N closest to X ; Function call to calculate and stores divisors of all numbers in a vector ; Stores the closest value to target ; Print the answer ; Driver Code ; Given N & X ; Function Call"
How can the above be solved in Python?
targets: MAX = 10000
divisors = [ [ ] for i in range ( MAX + 1 ) ]
def computeDivisors ( ) :
	global divisors
	global MAX
	for i in range ( 1 , MAX + 1 , 1 ) :
		for j in range ( i , MAX + 1 , i ) :
			divisors [ j ] . append ( i )
def getClosest ( val1 , val2 , target ) :
	if ( target - val1 >= val2 - target ) :
		return val2
	else :
		return val1
def findClosest ( arr , n , target ) :
	if ( target <= arr [ 0 ] ) :
		return arr [ 0 ]
	if ( target >= arr [ n - 1 ] ) :
		return arr [ n - 1 ]
	i = 0
	j = n
	mid = 0
	while ( i < j ) :
		mid = ( i + j ) // 2
		if ( arr [ mid ] == target ) :
			return arr [ mid ]
		if ( target < arr [ mid ] ) :
			if ( mid > 0 and target > arr [ mid - 1 ] ) :
				return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target )
			j = mid
		else :
			if ( mid < n - 1 and target < arr [ mid + 1 ] ) :
				return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target )
			i = mid + 1
	return arr [ mid ]
def printClosest ( N , X ) :
	global divisors
	computeDivisors ( )
	ans = findClosest ( divisors [ N ] , len ( divisors [ N ] ) , X )
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	N = 16
	X = 5
	printClosest ( N , X )
inputs: "Double the first element and move zero to end | Java implementation to rearrange the array elements after modification ; function which pushes all zeros to end of an array . ; Count of non - zero elements ; Traverse the array . If element encountered is non - zero , then replace the element at index ' count ' with this element ; here count is incremented ; Now all non - zero elements have been shifted to front and ' count ' is set as index of first 0. Make all elements 0 from count to end . ; function to rearrange the array elements after modification ; if ' arr [ ] ' contains a single element only ; traverse the array ; if true , perform the required modification ; double current index value ; put 0 in the next index ; increment by 1 so as to move two indexes ahead during loop iteration ; push all the zeros at the end of ' arr [ ] ' ; function to print the array elements ; Driver program to test above"
How can the above be solved in Java?
targets: class GFG { static void pushZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } static void modifyAndRearrangeArr ( int arr [ ] , int n ) { if ( n == 1 ) return ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( arr [ i ] != 0 ) && ( arr [ i ] == arr [ i + 1 ] ) ) { arr [ i ] = 2 * arr [ i ] ; arr [ i + 1 ] = 0 ; i ++ ; } } pushZerosToEnd ( arr , n ) ; } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 2 , 2 , 0 , 6 , 6 , 0 , 0 , 8 } ; int n = arr . length ; System . out . print ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; modifyAndRearrangeArr ( arr , n ) ; System . out . print ( " Modified ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
inputs: "Count of unordered pairs ( x , y ) of Array which satisfy given equation | Return the number of unordered pairs satisfying the conditions ; ans stores the number of unordered pairs ; Making each value of array to positive ; Sort the array ; For each index calculating the right boundary for the unordered pairs ; Return the final result ; Driver code"
Solution in Python:
targets: def numPairs ( a , n ) :
	ans = 0
	for i in range ( n ) :
		a [ i ] = abs ( a [ i ] )
	a . sort ( )
	for i in range ( n ) :
		index = 0
		for j in range ( i + 1 , n ) :
			if ( 2 * a [ i ] >= a [ j - 1 ] and 2 * a [ i ] < a [ j ] ) :
				index = j
		if index == 0 :
			index = n
		ans += index - i - 1
	return ans
a = [ 3 , 6 ]
n = len ( a )
print ( numPairs ( a , n ) )
inputs: "Number with maximum number of prime factors | C # program to find integer having maximum number of prime factor in first N natural numbers . ; Return smallest number having prime factors . ; Sieve of eratosthenes method to count number of prime factors . ; Finding number having maximum number of prime factor . ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 5 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) { for ( int j = 2 * i ; j <= N ; j += i ) { arr [ j ] ++ ; } } arr [ i ] = 1 ; } int maxval = 0 , maxint = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > maxval ) { maxval = arr [ i ] ; maxint = i ; } } return maxint ; } public static void Main ( ) { int N = 40 ; Console . WriteLine ( maxPrimefactorNum ( N ) ) ; } }
inputs: "Longest Bitonic Subsequence | DP | lbs ( ) returns the length of the Longest Bitonic Subsequence in arr [ ] of size n . The function mainly creates two temporary arrays lis [ ] and lds [ ] and returns the maximum lis [ i ] + lds [ i ] - 1. lis [ i ] == > Longest Increasing subsequence ending with arr [ i ] lds [ i ] == > Longest decreasing subsequence starting with arr [ i ] ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right ; Allocate memory for lds and initialize LDS values for all indexes ; Compute LDS values from right to left ; Return the maximum value of lis [ i ] + lds [ i ] - 1 ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function lbs ( & $ arr , $ n ) { $ lis = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lis [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ lis [ $ i ] < $ lis [ $ j ] + 1 ) $ lis [ $ i ] = $ lis [ $ j ] + 1 ; $ lds = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lds [ $ i ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) for ( $ j = $ n - 1 ; $ j > $ i ; $ j -- ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ lds [ $ i ] < $ lds [ $ j ] + 1 ) $ lds [ $ i ] = $ lds [ $ j ] + 1 ; $ max = $ lis [ 0 ] + $ lds [ 0 ] - 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ lis [ $ i ] + $ lds [ $ i ] - 1 > $ max ) $ max = $ lis [ $ i ] + $ lds [ $ i ] - 1 ; return $ max ; } $ arr = array ( 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ) ; $ n = sizeof ( $ arr ) ; echo " Length ▁ of ▁ LBS ▁ is ▁ " . lbs ( $ arr , $ n ) ; ? >
inputs: "Dynamic Programming | A Dynamic Programming solution for subset sum problem ; Returns true if there is a subset of set [ ] with sum equal to given sum ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; uncomment this code to print table ; Driver code"
How can the above be solved in Java?
targets: class GFG { static boolean isSubsetSum ( int set [ ] , int n , int sum ) { boolean subset [ ] [ ] = new boolean [ sum + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ i ] [ 0 ] = false ; for ( int i = 1 ; i <= sum ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { subset [ i ] [ j ] = subset [ i ] [ j - 1 ] ; if ( i >= set [ j - 1 ] ) subset [ i ] [ j ] = subset [ i ] [ j ] || subset [ i - set [ j - 1 ] ] [ j - 1 ] ; } } for ( int i = 0 ; i <= sum ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) System . out . println ( subset [ i ] [ j ] ) ; } return subset [ sum ] [ n ] ; } public static void main ( String args [ ] ) { int set [ ] = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) System . out . println ( " Found ▁ a ▁ subset " + " ▁ with ▁ given ▁ sum " ) ; else System . out . println ( " No ▁ subset ▁ with " + " ▁ given ▁ sum " ) ; } }
inputs: Program to find area of a Trapezoid | CPP program to calculate area of a trapezoid ; Function for the area ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
double Area ( int b1 , int b2 , int h ) { return ( ( b1 + b2 ) / 2 ) * h ; } int main ( ) { int base1 = 8 , base2 = 10 , height = 6 ; double area = Area ( base1 , base2 , height ) ; printf ( " Area ▁ is : ▁ % .1lf " , area ) ; return 0 ; }