Muennighoff/xP3x-sample huggingface dataset:
inputs: "Maximum equlibrium sum in an array | Function to find maximum equilibrium sum . ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function findMaxSum ( $ arr , $ n ) { $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ prefix_sum = $ arr [ $ i ] ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ prefix_sum += $ arr [ $ j ] ; $ suffix_sum = $ arr [ $ i ] ; for ( $ j = $ n - 1 ; $ j > $ i ; $ j -- ) $ suffix_sum += $ arr [ $ j ] ; if ( $ prefix_sum == $ suffix_sum ) $ res = max ( $ res , $ prefix_sum ) ; } return $ res ; } $ arr = array ( -2 , 5 , 3 , 1 , 2 , 6 , -4 , 2 ) ; $ n = count ( $ arr ) ; echo findMaxSum ( $ arr , $ n ) ; ? >
inputs: "Print path from root to all nodes in a Complete Binary Tree | Java program to print path from root to all nodes in a complete binary tree . ; Function to print path of all the nodes nth node represent as given node kth node represents as left and right node ; base condition if kth node value is greater then nth node then its means kth node is not valid so we not store it into the res simply we just return ; Storing node into res ; Print the path from root to node ; store left path of a tree So for left we will go node ( kThNode * 2 ) ; right path of a tree and for right we will go node ( kThNode * 2 + 1 ) ; Function to print path from root to all of the nodes ; res is for store the path from root to particulate node ; Print path from root to all node . third argument 1 because of we have to consider root node is 1 ; Driver Code ; Given Node ; Print path from root to all node ."
Solution in Java:
targets: import java . util . * ; class GFG { static void printPath ( Vector < Integer > res , int nThNode , int kThNode ) { if ( kThNode > nThNode ) return ; res . add ( kThNode ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; System . out . print ( "
"); printPath ( res , nThNode , kThNode * 2 ) ; printPath ( res , nThNode , kThNode * 2 + 1 ) ; res . remove ( res . size ( ) - 1 ) ; } static void printPathToCoverAllNodeUtil ( int nThNode ) { Vector < Integer > res = new Vector < Integer > ( ) ; printPath ( res , nThNode , 1 ) ; } public static void main ( String args [ ] ) { int nThNode = 7 ; printPathToCoverAllNodeUtil ( nThNode ) ; } }
inputs: "Minimum change in lanes required to cross all barriers | C # program for the above approach ; Function to find the minimum number of changes of lane required ; If there is a barrier , then add very large value ; Add the minimum value to move forword with or without crossing barrier ; Return the minimum value of dp [ 0 ] , dp [ 1 ] and dp [ 2 ] ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; public class GFG { static int minChangeInLane ( int [ ] barrier , int n ) { int [ ] dp = { 1 , 0 , 1 } ; for ( int j = 0 ; j < n ; j ++ ) { int val = barrier [ j ] ; if ( val > 0 ) { dp [ val - 1 ] = ( int ) 1e6 ; } for ( int i = 0 ; i < 3 ; i ++ ) { if ( val != i + 1 ) { dp [ i ] = Math . Min ( dp [ i ] , Math . Min ( dp [ ( i + 1 ) % 3 ] , dp [ ( i + 2 ) % 3 ] ) + 1 ) ; } } } return Math . Min ( dp [ 0 ] , Math . Min ( dp [ 1 ] , dp [ 2 ] ) ) ; } static public void Main ( ) { int [ ] barrier = { 0 , 1 , 2 , 3 , 0 } ; int N = barrier . Length ; Console . Write ( minChangeInLane ( barrier , N ) ) ; } }
inputs: Largest gap in an array | A C program to find largest gap between two elements in an array . ; function to solve the given problem ; Driver Code
How can the above be solved in C?
targets: #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int solve ( int a [ ] , int n ) { int max1 = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( abs ( a [ i ] - a [ j ] ) > max1 ) { max1 = abs ( a [ i ] - a [ j ] ) ; } } } return max1 ; } int main ( ) { int arr [ ] = { -1 , 2 , 3 , -4 , -10 , 22 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Largest ▁ gap ▁ is ▁ : ▁ % d " , solve ( arr , size ) ) ; return 0 ; }
inputs: "Undulating numbers | Python3 program to check whether a number is undulating or not ; Considering the definition with restriction that there should be at least 3 digits ; Check if all alternate digits are same or not . ; Driver Code"
How can the above be solved in Python?
targets: def isUndulating ( n ) :
	if ( len ( n ) <= 2 ) :
		return False
	for i in range ( 2 , len ( n ) ) :
		if ( n [ i - 2 ] != n [ i ] ) :
			return False
	return True
n = "1212121"
if ( isUndulating ( n ) ) :
	print ( " Yes " )
else :
	print ( " No " )
inputs: "Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | C # program for the above approach ; Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void canPlace ( string s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s [ i ] == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s [ i + 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s [ i + 1 ] == '0' && s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { string S = "10001" ; int K = 1 ; int N = S . Length ; canPlace ( S , N , K ) ; } }
inputs: "Kronecker Product of two matrices | C code to find the Kronecker Product of two matrices and stores it as matrix C ; rowa and cola are no of rows and columns of matrix A rowb and colb are no of rows and columns of matrix B ; Function to computes the Kronecker Product of two matrices ; i loops till rowa ; k loops till rowb ; j loops till cola ; l loops till colb ; Each element of matrix A is multiplied by whole Matrix B resp and stored as Matrix C ; Driver Code"
Solution in C:
targets: #include <stdio.h>
const int cola = 2 , rowa = 3 , colb = 3 , rowb = 2 ; void Kroneckerproduct ( int A [ ] [ cola ] , int B [ ] [ colb ] ) { int C [ rowa * rowb ] [ cola * colb ] ; for ( int i = 0 ; i < rowa ; i ++ ) { for ( int k = 0 ; k < rowb ; k ++ ) { for ( int j = 0 ; j < cola ; j ++ ) { for ( int l = 0 ; l < colb ; l ++ ) { C [ i + l + 1 ] [ j + k + 1 ] = A [ i ] [ j ] * B [ k ] [ l ] ; printf ( " % d 	 " , C [ i + l + 1 ] [ j + k + 1 ] ) ; } } printf ( " 
 " ) ; } } } int main ( ) { int A [ 3 ] [ 2 ] = { { 1 , 2 } , { 3 , 4 } , { 1 , 0 } } , B [ 2 ] [ 3 ] = { { 0 , 5 , 2 } , { 6 , 7 , 3 } } ; Kroneckerproduct ( A , B ) ; return 0 ; }
inputs: "Ternary Search | Java program to illustrate the iterative approach to ternary search ; Function to perform Ternary Search ; Find the mid1 mid2 ; Check if key is present at any mid ; Since key is not present at mid , check in which region it is present then repeat the Search operation in that region ; The key lies in between l and mid1 ; The key lies in between mid2 and r ; The key lies in between mid1 and mid2 ; Key not found ; Driver code ; Get the array Sort the array if not sorted ; Starting index ; length of array ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result"
How can the above be solved in Java?
targets: class GFG { static int ternarySearch ( int l , int r , int key , int ar [ ] ) { while ( r >= l ) { int mid1 = l + ( r - l ) / 3 ; int mid2 = r - ( r - l ) / 3 ; if ( ar [ mid1 ] == key ) { return mid1 ; } if ( ar [ mid2 ] == key ) { return mid2 ; } if ( key < ar [ mid1 ] ) { r = mid1 - 1 ; } else if ( key > ar [ mid2 ] ) { l = mid2 + 1 ; } else { l = mid1 + 1 ; r = mid2 - 1 ; } } return - 1 ; } public static void main ( String args [ ] ) { int l , r , p , key ; int ar [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; l = 0 ; r = 9 ; key = 5 ; p = ternarySearch ( l , r , key , ar ) ; System . out . println ( " Index ▁ of ▁ " + key + " ▁ is ▁ " + p ) ; key = 50 ; p = ternarySearch ( l , r , key , ar ) ; System . out . println ( " Index ▁ of ▁ " + key + " ▁ is ▁ " + p ) ; } }
inputs: "Insertion Sort | Java program for implementation of Insertion Sort ; Function to sort array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print array of size n ; Driver method"
Solution in Java:
targets: class InsertionSort { void sort ( int arr [ ] ) { int n = arr . length ; for ( int i = 1 ; i < n ; ++ i ) { int key = arr [ i ] ; int j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; InsertionSort ob = new InsertionSort ( ) ; ob . sort ( arr ) ; printArray ( arr ) ; } }
inputs: "Maximum equlibrium sum in an array | Function to find maximum equilibrium sum . ; Driver Code"
Solution in php:
targets: < ? php function findMaxSum ( $ arr , $ n ) { $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ prefix_sum = $ arr [ $ i ] ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ prefix_sum += $ arr [ $ j ] ; $ suffix_sum = $ arr [ $ i ] ; for ( $ j = $ n - 1 ; $ j > $ i ; $ j -- ) $ suffix_sum += $ arr [ $ j ] ; if ( $ prefix_sum == $ suffix_sum ) $ res = max ( $ res , $ prefix_sum ) ; } return $ res ; } $ arr = array ( -2 , 5 , 3 , 1 , 2 , 6 , -4 , 2 ) ; $ n = count ( $ arr ) ; echo findMaxSum ( $ arr , $ n ) ; ? >
inputs: "Find same contacts in a list of contacts | Structure for storing contact details . ; A utility function to fill entries in adjacency matrix representation of graph ; Initialize the adjacency matrix ; Traverse through all contacts ; Add mat from i to j and vice versa , if possible . Since length of each contact field is at max some constant . ( say 30 ) so body execution of this for loop takes constant time . ; A recuesive function to perform DFS with vertex i as source ; Finds similar contacrs in an array of contacts ; vector for storing the solution ; Declare 2D adjaceny matrix for mats ; visited array to keep track of visited nodes ; Fill adjacency matrix ; Since , we made a graph with contacts as nodes with fields as links . Two nodes are linked if they represent the same person . So , total number of connected components and nodes in each component will be our answer . ; Add delimeter to separate nodes of one component from other . ; Print the solution ; Driver Code"
Solution in Python:
targets: class contact :
	def __init__ ( self , field1 , field2 , field3 ) :
		self . field1 = field1
		self . field2 = field2
		self . field3 = field3
def buildGraph ( arr , n , mat ) :
	for i in range ( n ) :
		for j in range ( n ) :
			mat [ i ] [ j ] = 0
	for i in range ( n ) :
		for j in range ( i + 1 , n ) :
			if ( arr [ i ] . field1 == arr [ j ] . field1 or arr [ i ] . field1 == arr [ j ] . field2 or arr [ i ] . field1 == arr [ j ] . field3 or arr [ i ] . field2 == arr [ j ] . field1 or arr [ i ] . field2 == arr [ j ] . field2 or arr [ i ] . field2 == arr [ j ] . field3 or arr [ i ] . field3 == arr [ j ] . field1 or arr [ i ] . field3 == arr [ j ] . field2 or arr [ i ] . field3 == arr [ j ] . field3 ) :
				mat [ i ] [ j ] = 1
				mat [ j ] [ i ] = 1
				break
def DFSvisit ( i , mat , visited , sol , n ) :
	visited [ i ] = True
	sol . append ( i )
	for j in range ( n ) :
		if ( mat [ i ] [ j ] and not visited [ j ] ) :
			DFSvisit ( j , mat , visited , sol , n )
def findSameContacts ( arr , n ) :
	sol = [ ]
	mat = [ [ None ] * n for i in range ( n ) ]
	visited = [ 0 ] * n
	buildGraph ( arr , n , mat )
	for i in range ( n ) :
		if ( not visited [ i ] ) :
			DFSvisit ( i , mat , visited , sol , n )
			sol . append ( - 1 )
	for i in range ( len ( sol ) ) :
		if ( sol [ i ] == - 1 ) :
			print ( )
		else :
			print ( sol [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ contact ( " Gaurav " , " gaurav @ gmail . com " , " gaurav @ gfgQA . com " ) , contact ( " Lucky " , " lucky @ gmail . com " , " + 1234567" ) , contact ( " gaurav123" , " + 5412312" , " gaurav123 @ skype . com " ) , contact ( " gaurav1993" , " + 5412312" , " gaurav @ gfgQA . com " ) , contact ( " raja " , " + 2231210" , " raja @ gfg . com " ) , contact ( " bahubali " , " + 878312" , " raja " ) ]
	n = len ( arr )
	findSameContacts ( arr , n )
inputs: "Gold Mine Problem | JavaScript program to solve Gold Mine problem ; Returns maximum amount of gold that can be collected when journey started from first column and moves allowed are right , right - up and right - down ; Create a table for storing intermediate results and initialize all cells to 0. The first row of goldMineTable gives the maximum gold that the miner can collect when starts that row ; Gold collected on going to the cell on the right ( -> ) ; Gold collected on going to the cell to right up ( / ) ; Gold collected on going to the cell to right down ( \ ) ; Max gold collected from taking either of the above 3 paths ; The max amount of gold collected will be the max value in first column of all rows ; Driver code"
Solution in Javascript:
targets: let MAX = 100 ; function getMaxGold ( gold , m , n ) { let goldTable = new Array ( m ) ; for ( let i = 0 ; i < m ; i ++ ) { goldTable [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { goldTable [ i ] [ j ] = 0 ; } } for ( let col = n - 1 ; col >= 0 ; col -- ) { for ( let row = 0 ; row < m ; row ++ ) { let right = ( col == n - 1 ) ? 0 : goldTable [ row ] [ col + 1 ] ; let right_up = ( row == 0 col == n - 1 ) ? 0 : goldTable [ row - 1 ] [ col + 1 ] ; let right_down = ( row == m - 1 col == n - 1 ) ? 0 : goldTable [ row + 1 ] [ col + 1 ] ; goldTable [ row ] [ col ] = gold [ row ] [ col ] + Math . max ( right , Math . max ( right_up , right_down ) ) ; } } let res = goldTable [ 0 ] [ 0 ] ; for ( let i = 1 ; i < m ; i ++ ) res = Math . max ( res , goldTable [ i ] [ 0 ] ) ; return res ; } let gold = [ [ 1 , 3 , 1 , 5 ] , [ 2 , 2 , 4 , 1 ] , [ 5 , 0 , 2 , 3 ] , [ 0 , 6 , 1 , 2 ] ] ; let m = 4 , n = 4 ; document . write ( getMaxGold ( gold , m , n ) ) ;
inputs: Smallest power of 2 greater than or equal to n |  ; First n in the below condition is for the case where n is 0 ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } int main ( ) { unsigned int n = 0 ; printf ( " % d " , nextPowerOf2 ( n ) ) ; return 0 ; }
inputs: "Count of indices in an array that satisfy the given condition | Function to return the count of indices that satisfy the given condition ; To store the result ; To store the current maximum Initialized to 0 since there are only positive elements in the array ; i is a valid index ; Update the maximum so far ; Increment the counter ; Driver code"
Solution in Python:
targets: def countIndices ( arr , n ) :
	cnt = 0 ;
	max = 0 ;
	for i in range ( n ) :
		if ( max < arr [ i ] ) :
			max = arr [ i ] ;
			cnt += 1 ;
	return cnt ;
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 3 , 4 ] ;
	n = len ( arr ) ;
	print ( countIndices ( arr , n ) ) ;
inputs: "Check whether the given floating point number is a palindrome | C ++ implementation of the approach ; Function that returns true if num is palindrome ; Convert the given floating point number into a string ; Pointers pointing to the first and the last character of the string ; Not a palindrome ; Update the pointers ; Driver code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; bool isPalindrome ( float num ) { stringstream ss ; ss << num ; string s ; ss >> s ; int low = 0 ; int high = s . size ( ) - 1 ; while ( low < high ) { if ( s [ low ] != s [ high ] ) return false ; low ++ ; high -- ; } return true ; } int main ( ) { float n = 123.321f ; if ( isPalindrome ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
inputs: "Number of ways to remove a sub | C # program to count number of ways of removing a substring from a string such that all remaining characters are equal ; Function to return the number of ways of removing a sub - string from s such that all the remaining characters are same ; To store the count of prefix and suffix ; Loop to count prefix ; Loop to count suffix ; First and last characters of the string are same ; Otherwise ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int no_of_ways ( string s ) { int n = s . Length ; int count_left = 0 , count_right = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == s [ 0 ] ) { ++ count_left ; } else break ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == s [ n - 1 ] ) { ++ count_right ; } else break ; } if ( s [ 0 ] == s [ n - 1 ] ) return ( ( count_left + 1 ) * ( count_right + 1 ) ) ; else return ( count_left + count_right + 1 ) ; } public static void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( no_of_ways ( s ) ) ; } }
inputs: "Area of a Circular Sector | PHP program to find Area of a Sector ; Calculating area of the sector ; Driver code"
Solution in php:
targets: < ? php function SectorArea ( $ radius , $ angle ) { if ( $ angle >= 360 ) echo ( " Angle ▁ not ▁ possible " ) ; else { $ sector = ( ( 22 * $ radius * $ radius ) / 7 ) * ( $ angle / 360 ) ; echo ( $ sector ) ; } } $ radius = 9 ; $ angle = 60 ; SectorArea ( $ radius , $ angle ) ; ? >
inputs: "Find the direction from given string | Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code"
Solution in Javascript:
targets: function findDirection ( s ) { let count = 0 ; let d = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ 0 ] == ' ' ) return null ; if ( s [ i ] == ' ' ) count -- ; else { if ( s [ i ] == ' ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == 1 ) d = " " ; else if ( count % 4 == 2 ) d = " " ; else if ( count % 4 == 3 ) d = " " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == - 1 ) d = " " ; else if ( count % 4 == - 2 ) d = " " ; else if ( count % 4 == - 3 ) d = " " ; } return d ; } let s = " " ; document . write ( findDirection ( s ) + " " ) ; s = " " ; document . write ( findDirection ( s ) ) ;
inputs: "Make all array elements equal with minimum cost | Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; Setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver code"
Solution in Python:
targets: def computeCost ( arr , N , X ) :
	cost = 0
	for i in range ( N ) :
		cost += abs ( arr [ i ] - X )
	return cost
def minCostToMakeElementEqual ( arr , N ) :
	low = high = arr [ 0 ]
	for i in range ( N ) :
		if ( low > arr [ i ] ) : low = arr [ i ]
		if ( high < arr [ i ] ) : high = arr [ i ]
	while ( ( high - low ) > 2 ) :
		mid1 = low + ( high - low ) // 3
		mid2 = high - ( high - low ) // 3
		cost1 = computeCost ( arr , N , mid1 )
		cost2 = computeCost ( arr , N , mid2 )
		if ( cost1 < cost2 ) :
			high = mid2
		else :
			low = mid1
	return computeCost ( arr , N , ( low + high ) // 2 )
arr = [ 1 , 100 , 101 ]
N = len ( arr )
print ( minCostToMakeElementEqual ( arr , N ) )
inputs: "Largest subarray with equal number of 0 s and 1 s | A simple C ++ program to find the largest subarray with equal number of 0 s and 1 s ; This function Prints the starting and ending indexes of the largest subarray with equal number of 0 s and 1 s . Also returns the size of such subarray . ; Pick a starting point as i ; Consider all subarrays starting from i ; If this is a 0 sum subarray , then compare it with maximum size subarray calculated so far ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int findSubArray ( int arr [ ] , int n ) { int sum = 0 ; int maxsize = -1 , startindex ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? -1 : 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { ( arr [ j ] == 0 ) ? ( sum += -1 ) : ( sum += 1 ) ; if ( sum == 0 && maxsize < j - i + 1 ) { maxsize = j - i + 1 ; startindex = i ; } } } if ( maxsize == -1 ) cout << " No ▁ such ▁ subarray " ; else cout << startindex << " ▁ to ▁ " << startindex + maxsize - 1 ; return maxsize ; } int main ( ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSubArray ( arr , size ) ; return 0 ; }
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"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static void prCharWithFreq ( string s ) { Dictionary < char , int > d = new Dictionary < char , int > ( ) ; foreach ( char i in s ) { if ( d . ContainsKey ( i ) ) { d [ i ] ++ ; } else { d [ i ] = 1 ; } } foreach ( char i in s ) { if ( d [ i ] != 0 ) { Console . Write ( i + d [ i ] . ToString ( ) + " ▁ " ) ; d [ i ] = 0 ; } } } public static void Main ( string [ ] args ) { string s = " geeksforgeeks " ; prCharWithFreq ( s ) ; } }
inputs: "Find the element in the matrix generated by given rules | Java implementation of the above approach ; Function to return the element in the rth row and cth column from the required matrix ; Condition for lower half of matrix ; Condition if element is in first row ; Starting element of AP in row r ; Common difference of AP in row r ; Position of element to find in AP in row r ; Driver Code"
How can the above be solved in Java?
targets: import java . io . * ; import java . util . * ; class GFG { static int getElement ( int N , int r , int c ) { if ( r > c ) return 0 ; if ( r == 1 ) { return c ; } int a = ( r + 1 ) * ( int ) ( Math . pow ( 2 , ( r - 2 ) ) ) ; int d = ( int ) ( Math . pow ( 2 , ( r - 1 ) ) ) ; c = c - r ; int element = a + d * c ; return element ; } public static void main ( String [ ] args ) { int N = 4 , R = 3 , C = 4 ; System . out . println ( getElement ( N , R , C ) ) ; } }
inputs: "Smallest character in a string having minimum sum of distances between consecutive repetitions | Java program for the above approach ; Function to find the character repeats with minimum distance ; Stores the first and last index ; Initialize with - 1 ; Get the values of last and first occurence ; Update the first index ; Update the last index ; Initialize min ; Get the minimum ; Values must not be same ; Update the minimum distance ; return ans ; Driver Code ; Function Call"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static char minDistChar ( char [ ] s ) { int n = s . length ; int [ ] first = new int [ 26 ] ; int [ ] last = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( first [ s [ i ] - ' a ' ] == - 1 ) { first [ s [ i ] - ' a ' ] = i ; } last [ s [ i ] - ' a ' ] = i ; } int min = Integer . MAX_VALUE ; char ans = '1' ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( last [ i ] == first [ i ] ) continue ; if ( min > last [ i ] - first [ i ] ) { min = last [ i ] - first [ i ] ; ans = ( char ) ( i + ' a ' ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( minDistChar ( str . toCharArray ( ) ) ) ; } }
inputs: "Find the minimum element in a sorted and rotated array | PHP program to find minimum element in a sorted and rotated array ; This condition is needed to handle the case when array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like ( 3 , 4 , 5 , 1 , 2 ) ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function findMin ( $ arr , $ low , $ high ) { if ( $ high < $ low ) return $ arr [ 0 ] ; if ( $ high == $ low ) return $ arr [ $ low ] ; $ mid = $ low + ( $ high - $ low ) / 2 ; if ( $ mid < $ high && $ arr [ $ mid + 1 ] < $ arr [ $ mid ] ) return $ arr [ $ mid + 1 ] ; if ( $ mid > $ low && $ arr [ $ mid ] < $ arr [ $ mid - 1 ] ) return $ arr [ $ mid ] ; if ( $ arr [ $ high ] > $ arr [ $ mid ] ) return findMin ( $ arr , $ low , $ mid - 1 ) ; return findMin ( $ arr , $ mid + 1 , $ high ) ; } $ arr1 = array ( 5 , 6 , 1 , 2 , 3 , 4 ) ; $ n1 = sizeof ( $ arr1 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr1 , 0 , $ n1 - 1 ) . " 
 " ; $ arr2 = array ( 1 , 2 , 3 , 4 ) ; $ n2 = sizeof ( $ arr2 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr2 , 0 , $ n2 - 1 ) . " 
 " ; $ arr3 = array ( 1 ) ; $ n3 = sizeof ( $ arr3 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr3 , 0 , $ n3 - 1 ) . " 
 " ; $ arr4 = array ( 1 , 2 ) ; $ n4 = sizeof ( $ arr4 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr4 , 0 , $ n4 - 1 ) . " 
 " ; $ arr5 = array ( 2 , 1 ) ; $ n5 = sizeof ( $ arr5 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr5 , 0 , $ n5 - 1 ) . " 
 " ; $ arr6 = array ( 5 , 6 , 7 , 1 , 2 , 3 , 4 ) ; $ n6 = sizeof ( $ arr6 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr6 , 0 , $ n6 - 1 ) . " 
 " ; $ arr7 = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n7 = sizeof ( $ arr7 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr7 , 0 , $ n7 - 1 ) . " 
 " ; $ arr8 = array ( 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 ) ; $ n8 = sizeof ( $ arr8 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr8 , 0 , $ n8 - 1 ) . " 
 " ; $ arr9 = array ( 3 , 4 , 5 , 1 , 2 ) ; $ n9 = sizeof ( $ arr9 ) ; echo " The ▁ minimum ▁ element ▁ is ▁ " . findMin ( $ arr9 , 0 , $ n9 - 1 ) . " 
 " ; ? >
inputs: "Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; A recursive function to get x * y ; A recursive function to get a ^ b Works only if a >= 0 and b >= 0 ; driver program to test above functions"
Solution in C:
targets: #include <stdio.h>
int multiply ( int x , int y ) { if ( y ) return ( x + multiply ( x , y - 1 ) ) ; else return 0 ; } int pow ( int a , int b ) { if ( b ) return multiply ( a , pow ( a , b - 1 ) ) ; else return 1 ; } int main ( ) { printf ( " % d " , pow ( 5 , 3 ) ) ; getchar ( ) ; return 0 ; }
inputs: "Find the last two missing digits of the given phone number | C ++ implementation of the approach ; Function to find the last two digits of the number and print the complete number ; Sum of the first eight digits of the number ; if sum < 10 , then the two digits are '0' and the value of sum ; if sum > 10 , then the two digits are the value of sum ; Driver code"
Solution in C++:
targets: #include <iostream>
using namespace std ; void findPhoneNumber ( int n ) { int temp = n ; int sum ; while ( temp != 0 ) { sum += temp % 10 ; temp = temp / 10 ; } if ( sum < 10 ) cout << n << "0" << sum ; else cout << n << sum ; } int main ( ) { long int n = 98765432 ; findPhoneNumber ( n ) ; return 0 ; }
inputs: "Find the sum of first N odd Fibonacci numbers | Python3 program to Find the sum of first N odd Fibonacci numbers ; Function to calculate sum of first N odd Fibonacci numbers ; base values ; Driver code"
How can the above be solved in Python?
targets: mod = 1000000007 ;
def sumOddFibonacci ( n ) :
	Sum = [ 0 ] * ( n + 1 ) ;
	Sum [ 0 ] = 0 ;
	Sum [ 1 ] = 1 ;
	Sum [ 2 ] = 2 ;
	Sum [ 3 ] = 5 ;
	Sum [ 4 ] = 10 ;
	Sum [ 5 ] = 23 ;
	for i in range ( 6 , n + 1 ) :
		Sum [ i ] = ( ( Sum [ i - 1 ] + ( 4 * Sum [ i - 2 ] ) % mod - ( 4 * Sum [ i - 3 ] ) % mod + mod ) % mod + ( Sum [ i - 4 ] - Sum [ i - 5 ] + mod ) % mod ) % mod ;
	return Sum [ n ] ;
n = 6 ;
print ( sumOddFibonacci ( n ) ) ;
inputs: "Find Nth item distributed from infinite items of infinite types based on given conditions | C # code for the above approach ; Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate over the days from 1 ; Iterate over type of item on that day ; Count of items given out should exceed n ; Driver Code"
Solution in C#:
targets: using System ; public class GFG { static int itemType ( int n ) { int count = 0 ; for ( int day = 1 ; ; day ++ ) { for ( int type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) return type ; } } } static public void Main ( ) { int N = 10 ; Console . WriteLine ( itemType ( N ) ) ; } }
inputs: "Maximum count of values of S modulo M lying in a range [ L , R ] after performing given operations on the array | Lookup table ; Function to count the value of S after adding arr [ i ] or arr [ i - 1 ] to the sum S at each time ; Base Case ; Store the mod value ; If the mod value lies in the range then return 1 ; Else return 0 ; Store the current state ; If already computed , return the computed value ; Recursively adding the elements to the sum adding ai value ; Adding arr [ i ] - 1 value ; Return the maximum count to check for root value as well ; Avoid counting idx = 0 as possible solution we are using idx != 0 ; Return the value of current state ; Driver Code"
Solution in Python:
targets: dp = { }
def countMagicNumbers ( idx , sum , a , n , m , l , r ) :
	if ( idx == n ) :
		temp = sum % m
		if ( temp == l or temp == r or ( temp > l and temp < r ) ) :
			dp [ ( idx , sum ) ] = 1
			return dp [ ( idx , sum ) ]
		else :
			dp [ ( idx , sum ) ] = 0
			return dp [ ( idx , sum ) ]
	curr = ( idx , sum )
	if ( curr in dp ) :
		return dp [ curr ]
	ls = countMagicNumbers ( idx + 1 , sum + a [ idx ] , a , n , m , l , r )
	rs = countMagicNumbers ( idx + 1 , sum + ( a [ idx ] - 1 ) , a , n , m , l , r )
	temp1 = max ( ls , rs )
	temp = sum % m
	if ( ( temp == l or temp == r or ( temp > l and temp < r ) ) and idx != 0 ) :
		temp1 += 1
	dp [ ( idx , sum ) ] = temp1
	return dp [ ( idx , sum ) ]
if __name__ == ' _ _ main _ _ ' :
	N = 5
	M = 22
	L = 14
	R = 16
	arr = [ 17 , 11 , 10 , 8 , 15 ]
	print ( countMagicNumbers ( 0 , 0 , arr , N , M , L , R ) )
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 set1 [ i ] . Similarly con_s2 [ i ] is going to store an integer whose set bits represent presence / absence of characters in 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"
How can the above be solved in Python?
targets: def countCompletePairs ( set1 , set2 , n , m ) :
	result = 0
	con_s1 , con_s2 = [ 0 ] * n , [ 0 ] * m
	for i in range ( n ) :
		con_s1 [ i ] = 0
		for j in range ( len ( set1 [ i ] ) ) :
			con_s1 [ i ] = con_s1 [ i ] | ( 1 << ( ord ( set1 [ i ] [ j ] ) - ord ( ' a ' ) ) )
	for i in range ( m ) :
		con_s2 [ i ] = 0
		for j in range ( len ( set2 [ i ] ) ) :
			con_s2 [ i ] = con_s2 [ i ] | ( 1 << ( ord ( set2 [ i ] [ j ] ) - ord ( ' a ' ) ) )
	complete = ( 1 << 26 ) - 1
	for i in range ( n ) :
		for j in range ( m ) :
			if ( ( con_s1 [ i ] con_s2 [ j ] ) == complete ) :
				result += 1
	return result
if __name__ == ' _ _ main _ _ ' :
	set1 = [ " abcdefgh " , " geeksforgeeks " , " lmnopqrst " , " abc " ]
	set2 = [ " ijklmnopqrstuvwxyz " , " abcdefghijklmnopqrstuvwxyz " , " defghijklmnopqrstuvwxyz " ]
	n = len ( set1 )
	m = len ( set2 )
	print ( countCompletePairs ( set1 , set2 , n , m ) )
inputs: "Numbers in range [ L , R ] such that the count of their divisors is both even and prime | C ++ implementation of the approach ; stores whether the number is prime or not ; stores the count of prime numbers less than or equal to the index ; create the sieve ; Create a boolean array " prime [ 0 . . n ] " and initialize all the entries 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 ; stores the prefix sum of number of primes less than or equal to ' i ' ; Driver code ; create the sieve ; ' l ' and ' r ' are the lower and upper bounds of the range ; get the value of count ; display the count"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; #define MAX  1000000
bool prime [ MAX + 1 ] ; int sum [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; memset ( sum , 0 , sizeof ( sum ) ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } for ( int i = 1 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) sum [ i ] = 1 ; sum [ i ] += sum [ i - 1 ] ; } } int main ( ) { SieveOfEratosthenes ( ) ; int l = 3 , r = 9 ; int c = ( sum [ r ] - sum [ l - 1 ] ) ; cout << " Count : ▁ " << c << endl ; return 0 ; }
inputs: Selection Sort | C program for implementation of selection sort ; swap function ; sort function ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver program to test above functions
How can the above be solved in C?
targets: #include <stdio.h>
void swap ( int * xp , int * yp ) { int temp = * xp ; * xp = * yp ; * yp = temp ; } void selectionSort ( int arr [ ] , int n ) { int i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; swap ( & arr [ min_idx ] , & arr [ i ] ) ; } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; printf ( " 
 " ) ; } int main ( ) { int arr [ ] = { 64 , 25 , 12 , 22 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; selectionSort ( arr , n ) ; printf ( " Sorted ▁ array : ▁ 
 " ) ; printArray ( arr , n ) ; return 0 ; }
inputs: "Find all factors of a natural number | Set 1 | A Better ( than Naive ) Solution to find all divisiors ; method to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; Otherwise print both ; Driver method"
How can the above be solved in Python?
targets: import math
def printDivisors ( n ) :
	i = 1
	while i <= math . sqrt ( n ) :
		if ( n % i == 0 ) :
			if ( n / i == i ) :
				print i ,
			else :
				print i , n / i ,
		i = i + 1
print " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ "
printDivisors ( 100 )
inputs: "Minimize steps to reach K from 0 by adding 1 or doubling at each step | Function to find minimum operations ; dp is initialised to store the steps ; For all even numbers ; Driver Code"
How can the above be solved in Python?
targets: def minOperation ( k ) :
	dp = [ 0 ] * ( k + 1 )
	for i in range ( 1 , k + 1 ) :
		dp [ i ] = dp [ i - 1 ] + 1
		if ( i % 2 == 0 ) :
			dp [ i ] = min ( dp [ i ] , dp [ i // 2 ] + 1 )
	return dp [ k ]
if __name__ == ' _ _ main _ _ ' :
	k = 12
	print ( minOperation ( k ) )
inputs: "Position of n among the numbers made of 2 , 3 , 5 & 7 | Javascript Program position of n among the numbers made of 2 , 3 , 5 & 7 ; If number is 2 then it is on the position pos * 2 + 1 ; If number is 3 then it is on the position pos * 2 + 2 ; If number is 5 then it is on the position pos * 2 + 3 ; If number is 7 then it is on the position pos * 2 + 4 ; Driver code"
Solution in Javascript:
targets: function findpos ( n ) { var pos = 0 ; for ( i = 0 ; i < n . length ; i ++ ) { switch ( n . charAt ( i ) ) { case ' ' : pos = pos * 4 + 1 ; break ; case ' ' : pos = pos * 4 + 2 ; break ; case ' ' : pos = pos * 4 + 3 ; break ; case ' ' : pos = pos * 4 + 4 ; break ; } } return pos ; } var n = " " ; document . write ( findpos ( n ) ) ;
inputs: "Number with maximum number of prime factors | Python 3 program to find integer having maximum number of prime factor in first N natural numbers . ; Return smallest number having maximum 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 Python?
targets: from math import sqrt
def maxPrimefactorNum ( N ) :
	arr = [ 0 for i in range ( N + 5 ) ]
	for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) :
		if ( arr [ i ] == 0 ) :
			for j in range ( 2 * i , N + 1 , i ) :
				arr [ j ] += 1
		arr [ i ] = 1
	maxval = 0
	maxint = 1
	for i in range ( 1 , N + 1 , 1 ) :
		if ( arr [ i ] > maxval ) :
			maxval = arr [ i ]
			maxint = i
	return maxint
if __name__ == ' _ _ main _ _ ' :
	N = 40
	print ( maxPrimefactorNum ( N ) )
inputs: "Sort Matrix in alternating ascending and descending order rowwise | C ++ implementation to print row of matrix in ascending or descending order alternatively ; Iterate matrix rowwise ; Sort even rows in ascending order ; compare adjacent elements ; swap adjacent element ; Sort even rows in descending order ; compare adjacent elements ; swap adjacent element ; Printing the final Output ; Driver code"
Solution in C++:
targets: #include <stdio.h>
#define N  4
void func ( int a [ ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] > a [ i ] [ k ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } else { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; ++ k ) { if ( a [ i ] [ j ] < a [ i ] [ k ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = a [ i ] [ k ] ; a [ i ] [ k ] = temp ; } } } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { printf ( " % d ▁ " , a [ i ] [ j ] ) ; } printf ( " 
 " ) ; } } int main ( ) { int a [ N ] [ N ] = { { 5 , 7 , 3 , 4 } , { 9 , 5 , 8 , 2 } , { 6 , 3 , 8 , 1 } , { 5 , 8 , 9 , 3 } } ; func ( a ) ; return 0 ; }
inputs: "Dynamic Programming | Returns true if there is a subset of set [ ] with sum equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Driver Code"
How can the above be solved in JS?
targets: function isSubsetSum ( set , n , sum ) { if ( sum == 0 ) return true ; if ( n == 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } let set = [ 3 , 34 , 4 , 12 , 5 , 2 ] ; let sum = 9 ; let n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
inputs: "Number formed by flipping all bits to the left of rightmost set bit | JavaScript program to find the integer formed after flipping all bits to the left of the rightmost set bit ; Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code"
Solution in Javascript:
targets: let totCount ; let firstCount ; function getTotCount ( num ) { totCount = 1 ; firstCount = 1 ; let temp = 1 ; while ( ( num & temp ) == 0 ) { temp = temp << 1 ; totCount += 1 ; } firstCount = totCount ; temp = num >> totCount ; while ( temp != 0 ) { totCount += 1 ; temp = temp >> 1 ; } } function flipBitsFromRightMostSetBit ( num ) { getTotCount ( num ) ; let num1 = num ^ ( ( 1 << totCount ) - 1 ) ; num1 = num1 ^ ( ( 1 << firstCount ) - 1 ) ; return num1 ; } let n = 120 ; document . write ( flipBitsFromRightMostSetBit ( n ) ) ;
inputs: "Dynamic Programming | Java program for the above approach ; Check if possible subset with given sum is possible or not ; Storing the value - 1 to the matrix ; If the sum is zero it means we got our expected sum ; If the value is not - 1 it means it already call the function with the same value . it will save our from the repetition . ; if the value of a [ n - 1 ] is greater than the sum . we call for the next value ; Here we do two calls because we don ' t ▁ know ▁ which ▁ value ▁ is ▁ ▁ full - fill ▁ our ▁ criteria ▁ ▁ that ' s why we doing two calls ; Driver Code"
Solution in Java:
targets: class GFG { static int subsetSum ( int a [ ] , int n , int sum ) { int tab [ ] [ ] = new int [ n + 1 ] [ sum + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { tab [ i ] [ j ] = - 1 ; } } if ( sum == 0 ) return 1 ; if ( n <= 0 ) return 0 ; if ( tab [ n - 1 ] [ sum ] != - 1 ) return tab [ n - 1 ] [ sum ] ; if ( a [ n - 1 ] > sum ) return tab [ n - 1 ] [ sum ] = subsetSum ( a , n - 1 , sum ) ; else { if ( subsetSum ( a , n - 1 , sum ) != 0 || subsetSum ( a , n - 1 , sum - a [ n - 1 ] ) != 0 ) { return tab [ n - 1 ] [ sum ] = 1 ; } else return tab [ n - 1 ] [ sum ] = 0 ; } } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , 5 , 3 , 7 , 4 } ; int sum = 12 ; if ( subsetSum ( a , n , sum ) != 0 ) { System . out . println ( "YES
"); } else System . out . println ( "NO
"); } }
inputs: "Maximum consecutive repeating character in string | Returns the maximum repeating character in a given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code"
Solution in php:
targets: < ? php function maxRepeating ( $ str ) { $ n = strlen ( $ str ) ; $ count = 0 ; $ res = $ str [ 0 ] ; $ cur_count = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i < $ n - 1 && $ str [ $ i ] == $ str [ $ i + 1 ] ) $ cur_count ++ ; else { if ( $ cur_count > $ count ) { $ count = $ cur_count ; $ res = $ str [ $ i ] ; } $ cur_count = 1 ; } } return $ res ; } $ str = " aaaabbaaccde " ; echo maxRepeating ( $ str ) ; ? >
inputs: "Count trailing zero bits using lookup table | Simple Java code for counting trailing zeros in binary representation of a number ; Driver Code"
Solution in Java:
targets: import java . io . * ; class GFG { public static int countTrailingZero ( int x ) { int count = 0 ; while ( ( x & 1 ) == 0 ) { x = x >> 1 ; count ++ ; } return count ; } public static void main ( String [ ] args ) { System . out . println ( countTrailingZero ( 11 ) ) ; } }
inputs: "Program to find whether a given number is power of 2 | Function to check if x is power of 2 ; Driver code"
How can the above be solved in Python?
targets: def isPowerofTwo ( n ) :
	if ( n == 0 ) :
		return 0
	if ( ( n & ( ~ ( n - 1 ) ) ) == n ) :
		return 1
	return 0
if ( isPowerofTwo ( 30 ) ) :
	print ( ' Yes ' )
else :
	print ( ' No ' )
if ( isPowerofTwo ( 128 ) ) :
	print ( ' Yes ' )
else :
	print ( ' No ' )
inputs: "String obtained by reversing and complementing a Binary string K times | Function to perform K operations upon the string and find modified string ; Number of reverse operations ; Number of complement operations ; If rev is odd parity ; If complement is odd parity ; Complementing each position ; Return the modified string ; Driver Code ; Function call"
Solution in Python:
targets: def ReverseComplement ( s , n , k ) :
	rev = ( k + 1 ) // 2
	complement = k - rev
	if ( rev % 2 ) :
		s = s [ : : - 1 ]
	if ( complement % 2 ) :
		for i in range ( n ) :
			if ( s [ i ] == '0' ) :
				s [ i ] = '1'
			else :
				s [ i ] = '0'
	return s
if __name__ == ' _ _ main _ _ ' :
	str1 = "10011"
	k = 5
	n = len ( str1 )
	print ( ReverseComplement ( str1 , n , k ) )
inputs: "Count possible decoding of a given digit sequence with hidden characters | C # program for the above approach ; If s [ i ] = = ' * ' there can be 9 possible values of * ; If previous character is 1 then words that can be formed are K ( 11 ) , L ( 12 ) , M ( 13 ) , N ( 14 ) O ( 15 ) , P ( 16 ) , Q ( 17 ) , R ( 18 ) , S ( 19 ) ; If previous character is 2 then the words that can be formed are U ( 21 ) , V ( 22 ) , W ( 23 ) , X ( 24 ) Y ( 25 ) , Z ( 26 ) ; If the previous digit is * then all 15 2 - digit characters can be formed ; If s [ i ] != ' * ' ; Adding first in second if s [ i - 1 ] = 1 ; Adding first in second if s [ i - 1 ] = = 2 and s [ i ] <= '6' ; if s [ i - 1 ] == ' * ' the union of above 2 cases has to be done ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int M = 1000000007 ; static int waysOfDecoding ( string s ) { long first = 1 , second = s [ 0 ] == ' * ' ? 9 : s [ 0 ] == '0' ? 0 : 1 ; for ( int i = 1 ; i < s . Length ; i ++ ) { long temp = second ; if ( s [ i ] == ' * ' ) { second = 9 * second ; if ( s [ i - 1 ] == '1' ) second = ( second + 9 * first ) % M ; else if ( s [ i - 1 ] == '2' ) second = ( second + 6 * first ) % M ; else if ( s [ i - 1 ] == ' * ' ) second = ( second + 15 * first ) % M ; } else { second = s [ i ] != '0' ? second : 0 ; if ( s [ i - 1 ] == '1' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == '2' && s [ i ] <= '6' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == ' * ' ) second = ( second + ( s [ i ] <= '6' ? 2 : 1 ) * first ) % M ; } first = temp ; } return ( int ) second ; } static public void Main ( ) { string s = " * " ; Console . WriteLine ( waysOfDecoding ( s ) ) ; } }
inputs: "Check if a number is divisible by 8 using bitwise operators | C ++ program to check whether the number is divisible by 8 or not using bitwise operator ; function to check number is div by 8 or not using bitwise operator ; Driver program"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int Div_by_8 ( int n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } int main ( ) { int n = 16 ; if ( Div_by_8 ( n ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
inputs: "Sophie Germain Prime | CPP program to print all sophie german prime number till n . ; function to detect prime number here we have used sieve method https : www . geeksforgeeks . org / sieve - of - eratosthenes / to detect prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; We have made array till 2 * n + 1 so that we can check prime number till that and conclude about sophie german prime . ; checking every i whether it is sophie german prime or not . ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool sieve ( int n , bool prime [ ] ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } void printSophieGermanNumber ( int n ) { bool prime [ 2 * n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; sieve ( 2 * n + 1 , prime ) ; for ( int i = 2 ; i <= n ; ++ i ) { if ( prime [ i ] && prime [ 2 * i + 1 ] ) cout << i << " ▁ " ; } } int main ( ) { int n = 25 ; printSophieGermanNumber ( n ) ; return 0 ; }