Muennighoff/xP3x-sample huggingface dataset:
inputs: "Find the subsequence with given sum in a superincreasing sequence | C # implementation of the approach ; Function to find the required subsequence ; Current element cannot be a part of the required subsequence ; Include current element in the required subsequence So update the sum ; Print the elements of the required subsequence ; If the current element was included in the subsequence ; Driver code"
Solution in C#:
targets: using System ; class GFG { static void findSubSeq ( int [ ] arr , int n , int sum ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum < arr [ i ] ) arr [ i ] = - 1 ; else sum -= arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != - 1 ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 17 , 25 , 46 , 94 , 201 , 400 } ; int n = arr . Length ; int sum = 272 ; findSubSeq ( arr , n , sum ) ; } }
inputs: "gOOGLE cASE of a given sentence | Java program to convert a sentence to gOOGLE cASE . ; empty strings ; convert input string to upper case ; checki if character is not a space and adding it to string w ; converting first character to lower case and subsequent initial letter of another word to lower case ; Driver code"
How can the above be solved in Java?
targets: class GFG { static String convert ( String str ) { String w = " " , z = " " ; str = str . toUpperCase ( ) + " ▁ " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) w = w + ch ; else { z = z + ( Character . toLowerCase ( w . charAt ( 0 ) ) ) + w . substring ( 1 ) + " ▁ " ; w = " " ; } } return z ; } public static void main ( String [ ] args ) { String str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; System . out . println ( convert ( str ) ) ; } }
inputs: "Minimum cost to reach a point N from 0 with two different operations allowed | Function to return minimum cost to reach destination ; Initialize cost to 0 ; going backwards until we reach initial position ; if 2 * X jump is better than X + 1 ; if X + 1 jump is better ; Driver program"
Solution in Python:
targets: def minCost ( N , P , Q ) :
	cost = 0
	while ( N > 0 ) :
		if ( N & 1 ) :
			cost += P
			N -= 1
		else :
			temp = N // 2 ;
			if ( temp * P > Q ) :
				cost += Q
			else :
				cost += P * temp
			N //= 2
	return cost
N = 9
P = 5
Q = 1
print ( minCost ( N , P , Q ) )
inputs: "Power of a prime number ‘ r ’ in n ! | C ++ program to find power of a prime number r in n ! ; Function to return power of a no . ' r ' in factorial of n ; Keep dividing n by powers of ' r ' and update count ; Driver program to test above function"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int power ( int n , int r ) { int count = 0 ; for ( int i = r ; ( n / i ) >= 1 ; i = i * r ) count += n / i ; return count ; } int main ( ) { int n = 6 , r = 3 ; printf ( " ▁ % d ▁ " , power ( n , r ) ) ; return 0 ; }
inputs: "Program to find Normal and Trace of a matrix | Size of given matrix ; Returns Normal of a matrix of size n x n ; Returns trace of a matrix of size n x n ; Driver code"
How can the above be solved in JS?
targets: var MAX = 100 ; function findNormal ( mat , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] * mat [ i ] [ j ] ; return parseInt ( Math . sqrt ( sum ) ) ; } function findTrace ( mat , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += mat [ i ] [ i ] ; return sum ; } var mat = [ [ 1 , 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 , 4 ] , [ 5 , 5 , 5 , 5 , 5 ] ] ; document . write ( " " + findTrace ( mat , 5 ) + " " ) ; document . write ( " " + findNormal ( mat , 5 ) ) ;
inputs: "Largest number made up of X and Y with count of X divisible by Y and of Y by X | Function to generate and return the largest number ; Store the smaller in Y ; Store the larger in X ; Stores respective counts ; If N is divisible by Y ; Append X , N times to the answer ; Reduce N to zero ; Reduce N by x ; Append Y , X times to the answer ; If number can be formed ; Otherwise ; Driver code"
How can the above be solved in Python?
targets: def largestNumber ( n , X , Y ) :
	maxm = max ( X , Y )
	Y = X + Y - maxm
	X = maxm
	Xs = 0
	Ys = 0
	while ( n > 0 ) :
		if ( n % Y == 0 ) :
			Xs += n
			n = 0
		else :
			n -= X
			Ys += X
	if ( n == 0 ) :
		while ( Xs > 0 ) :
			Xs -= 1
			print ( X , end = ' ' )
		while ( Ys > 0 ) :
			Ys -= 1
			print ( Y , end = ' ' )
	else :
		print ( " - 1" )
n = 19
X = 7
Y = 5
largestNumber ( n , X , Y )
inputs: "Icositetragonal Number | Function to find icositetragonal number ; Formula to calculate nth icositetragonal number ; Driver code"
How can the above be solved in JS?
targets: function Icositetragonal_num ( n ) { return ( 22 * n * n - 20 * n ) / 2 ; } let n = 3 ; document . write ( Icositetragonal_num ( n ) + " " ) ; n = 10 ; document . write ( Icositetragonal_num ( n ) ) ;
inputs: Linear Search | C code to linearly search x in arr [ ] . If x is present then return its location , otherwise return - 1 ; Driver code ; Function call
How can the above be solved in C?
targets: #include <stdio.h>
int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return -1 ; } int main ( void ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int result = search ( arr , n , x ) ; ( result == -1 ) ? printf ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) : printf ( " Element ▁ is ▁ present ▁ at ▁ index ▁ % d " , result ) ; return 0 ; }
inputs: Extract ' k ' bits from a given position in a number . | C program to extract k bits from a given position . ; Function to extract k bits from p position and returns the extracted value as integer ; Driver code
How can the above be solved in C?
targets: #include <stdio.h>
int bitExtracted ( int number , int k , int p ) { return ( ( ( 1 << k ) - 1 ) & ( number >> ( p - 1 ) ) ) ; } int main ( ) { int number = 171 , k = 5 , p = 2 ; printf ( " The ▁ extracted ▁ number ▁ is ▁ % d " , bitExtracted ( number , k , p ) ) ; return 0 ; }
inputs: "Bubble Sort | Optimized C # implementation of Bubble sort ; An optimized version of Bubble Sort ; swap arr [ j ] and arr [ j + 1 ] ; IF no two elements were swapped by inner loop , then break ; Function to print an array ; Driver method"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void bubbleSort ( int [ ] arr , int n ) { int i , j , temp ; bool swapped ; for ( i = 0 ; i < n - 1 ; i ++ ) { swapped = false ; for ( j = 0 ; j < n - i - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; } } static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 64 , 34 , 25 , 12 , 22 , 11 , 90 } ; int n = arr . Length ; bubbleSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array " ) ; printArray ( arr , n ) ; } }
inputs: "Product of nodes at k | Function to find product of digits of elements at k - th level ; Initialize result ; increasing level number ; decreasing level number ; check if current level is the desired level or not ; required product ; Driver program"
How can the above be solved in Python?
targets: def productAtKthLevel ( tree , k ) :
	level = - 1
	product = 1
	n = len ( tree )
	for i in range ( 0 , n ) :
		if ( tree [ i ] == ' ( ' ) :
			level += 1
		elif ( tree [ i ] == ' ) ' ) :
			level -= 1
		else :
			if ( level == k ) :
				product *= ( int ( tree [ i ] ) - int ( '0' ) )
	return product
tree = " ( 0(5(6 ( ) ( ) ) ( 4 ( ) (9 ( ) ( ) ) ) ) ( 7(1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ) "
k = 2
print ( productAtKthLevel ( tree , k ) )
inputs: "Sum of all divisors from 1 to n | C program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Driver code"
Solution in C:
targets: #include <stdio.h>
int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; } int main ( ) { int n = 4 ; printf ( " % d 
 " , divisorSum ( n ) ) ; n = 5 ; printf ( " % d " , divisorSum ( n ) ) ; return 0 ; }
inputs: "Count of permutations such that sum of K numbers from given range is even | Java program for the above approach ; Function to return the number of all permutations such that sum of K numbers in range is even ; Find total count of even and odd number in given range ; Iterate loop k times and update even_sum & odd_sum using previous values ; Update the prev_even and odd_sum ; Even sum ; Odd sum ; Return even_sum ; Driver Code ; Given ranges ; Length of permutation ; Function call"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { public static void countEvenSum ( int low , int high , int k ) { int even_count = high / 2 - ( low - 1 ) / 2 ; int odd_count = ( high + 1 ) / 2 - low / 2 ; long even_sum = 1 ; long odd_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { long prev_even = even_sum ; long prev_odd = odd_sum ; even_sum = ( prev_even * even_count ) + ( prev_odd * odd_count ) ; odd_sum = ( prev_even * odd_count ) + ( prev_odd * even_count ) ; } System . out . println ( even_sum ) ; } public static void main ( String [ ] args ) { int low = 4 ; int high = 5 ; int K = 3 ; countEvenSum ( low , high , K ) ; } }
inputs: Find a Fixed Point ( Value equal to index ) in a given array | C program to check fixed point in an array using linear search ; If no fixed point present then return - 1 ; Driver program to check above functions
How can the above be solved in C?
targets: #include <stdio.h>
int linearSearch ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == i ) return i ; } return -1 ; } int main ( ) { int arr [ ] = { -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Fixed ▁ Point ▁ is ▁ % d " , linearSearch ( arr , n ) ) ; getchar ( ) ; return 0 ; }
inputs: "Divide a string in N equal parts | C program to divide a string in n equal parts ; Function to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; length od string is 28 ; Print 4 equal parts of the string"
Solution in C:
targets: #include <stdio.h>
#include <string.h>
void divideString ( char * str , int n ) { int str_size = strlen ( str ) ; int i ; int part_size ; if ( str_size % n != 0 ) { printf ( " Invalid ▁ Input : ▁ String ▁ size " ) ; printf ( " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ) ; return ; } part_size = str_size / n ; for ( i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) printf ( " 
 " ) ; printf ( " % c " , str [ i ] ) ; } } int main ( ) { char * str = " a _ simple _ divide _ string _ quest " ; divideString ( str , 4 ) ; getchar ( ) ; return 0 ; }
inputs: "Maximum element between two nodes of BST | Java program to find maximum element in the path between two Nodes of Binary Search Tree . ; Create and return a pointer of new Node . ; Insert a new Node in Binary Search Tree . ; Return the maximum element between a Node and its given ancestor . ; Traversing the path between ansector and Node and finding maximum element . ; Return maximum element in the path between two given Node of BST . ; Finding the LCA of Node x and Node y ; Checking if both the Node lie on the left side of the parent p . ; Checking if both the Node lie on the right side of the parent p . ; Return the maximum of maximum elements occur in path from ancestor to both Node . ; Driver Code ; Creating the root of Binary Search Tree ; Inserting Nodes in Binary Search Tree"
How can the above be solved in Java?
targets: class Solution { static class Node { Node left , right ; int data ; } static Node createNode ( int x ) { Node p = new Node ( ) ; p . data = x ; p . left = p . right = null ; return p ; } static void insertNode ( Node root , int x ) { Node p = root , q = null ; while ( p != null ) { q = p ; if ( p . data < x ) p = p . right ; else p = p . left ; } if ( q == null ) p = createNode ( x ) ; else { if ( q . data < x ) q . right = createNode ( x ) ; else q . left = createNode ( x ) ; } } static int maxelpath ( Node q , int x ) { Node p = q ; int mx = - 1 ; while ( p . data != x ) { if ( p . data > x ) { mx = Math . max ( mx , p . data ) ; p = p . left ; } else { mx = Math . max ( mx , p . data ) ; p = p . right ; } } return Math . max ( mx , x ) ; } static int maximumElement ( Node root , int x , int y ) { Node p = root ; while ( ( x < p . data && y < p . data ) || ( x > p . data && y > p . data ) ) { if ( x < p . data && y < p . data ) p = p . left ; else if ( x > p . data && y > p . data ) p = p . right ; } return Math . max ( maxelpath ( p , x ) , maxelpath ( p , y ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 18 , 36 , 9 , 6 , 12 , 10 , 1 , 8 } ; int a = 1 , b = 10 ; int n = arr . length ; Node root = createNode ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) insertNode ( root , arr [ i ] ) ; System . out . println ( maximumElement ( root , a , b ) ) ; } }
inputs: "Number of subarrays have bitwise OR >= K | Java implementation of the above approach ; Function which builds the segment tree ; Function that returns bitwise OR of segment [ L . . R ] ; Function to count requisite number of subarrays ; Check for subarrays starting with index i ; If OR of subarray [ i . . mid ] >= K , then all subsequent subarrays will have OR >= K therefore reduce high to mid - 1 to find the minimal length subarray [ i . . mid ] having OR >= K ; Increase count with number of subarrays having OR >= K and starting with index i ; Driver code ; Build segment tree ."
Solution in Java:
targets: class GFG { static int N = 100002 ; static int tree [ ] = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int low = i , high = n - 1 , index = Integer . MAX_VALUE ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = Math . min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != Integer . MAX_VALUE ) { count += n - index ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; build ( arr , 1 , 0 , n - 1 ) ; int k = 6 ; System . out . println ( countSubArrays ( arr , n , k ) ) ; } }
inputs: "Sum of leaf nodes at minimum level | Java implementation to find the sum of leaf nodes at minimum level ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to find the sum of leaf nodes at minimum level ; if tree is empty ; if there is only one node ; queue used for level order traversal ; push root node in the queue ' q ' ; count number of nodes in the current level ; traverse the current level nodes ; get front element from ' q ' ; if it is a leaf node ; accumulate data to ' sum ' ; set flag ' f ' to 1 , to signify minimum level for leaf nodes has been encountered ; if top ' s ▁ left ▁ and ▁ right ▁ child ▁ ▁ ▁ exists , ▁ then ▁ push ▁ them ▁ to ▁ ' q ' ; required sum ; Driver Code ; binary tree creation"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static int sumOfLeafNodesAtMinLevel ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; Queue < Node > q = new LinkedList < > ( ) ; int sum = 0 ; boolean f = false ; q . add ( root ) ; while ( f == false ) { int nc = q . size ( ) ; while ( nc -- > 0 ) { Node top = q . peek ( ) ; q . remove ( ) ; if ( top . left == null && top . right == null ) { sum += top . data ; f = true ; } else { if ( top . left != null ) q . add ( top . left ) ; if ( top . right != null ) q . add ( top . right ) ; } } } return sum ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; root . right . left = getNode ( 6 ) ; root . right . right = getNode ( 7 ) ; root . left . right . left = getNode ( 8 ) ; root . right . left . right = getNode ( 9 ) ; System . out . println ( " Sum ▁ = ▁ " + sumOfLeafNodesAtMinLevel ( root ) ) ; } }
inputs: Multiply two integers without using multiplication , division and bitwise operators , and no loops |  ; function to multiply two numbers x and y ; 0 multiplied with anything gives 0 ; Add x one by one ; the case where y is negative ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
int multiply ( int x , int y ) { if ( y == 0 ) return 0 ; if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; if ( y < 0 ) return - multiply ( x , - y ) ; } int main ( ) { printf ( " % d " , multiply ( 5 , -11 ) ) ; getchar ( ) ; return 0 ; }
inputs: "Queries to check whether bitwise AND of a subarray is even or odd | Java implementation of the approach ; Function to precompute the count of even and odd numbers ; If the current element is odd then put 1 at odd [ i ] ; If the current element is even then put 1 at even [ i ] ; Taking the prefix sums of these two arrays so we can get the count of even and odd numbers in a range [ L , R ] in O ( 1 ) ; Function that returns true if the bitwise AND of the subarray a [ L ... R ] is odd ; cnt will store the count of odd numbers in the range [ L , R ] ; Check if all the numbers in the range are odd or not ; Function to perform the queries ; Perform queries ; Driver code ; Queries"
How can the above be solved in Java?
targets: class GFG { static final int MAXN = 1000005 ; static int even [ ] = new int [ MAXN ] ; static int odd [ ] = new int [ MAXN ] ; static void precompute ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } static boolean isOdd ( int L , int R ) { int cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } static void performQueries ( int a [ ] , int n , int q [ ] [ ] , int m ) { precompute ( a , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; if ( isOdd ( L , R ) ) System . out . println ( " Odd " ) ; else System . out . println ( " Even " ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 2 , 1 , 5 , 7 , 6 , 8 , 9 } ; int n = a . length ; int q [ ] [ ] = { { 0 , 2 } , { 1 , 2 } , { 2 , 3 } , { 3 , 6 } } ; int m = q . length ; performQueries ( a , n , q , m ) ; } }
inputs: "Subtraction in the Array | Java implementation of the approach ; Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static void operations ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 , sum = 0 ; while ( k -- > 0 ) { while ( i < n && arr [ i ] - sum == 0 ) i ++ ; if ( i < n && arr [ i ] - sum > 0 ) { System . out . print ( arr [ i ] - sum + " ▁ " ) ; sum = arr [ i ] ; } else System . out . println ( "0" ) ; } } public static void main ( String args [ ] ) { int k = 5 ; int arr [ ] = { 3 , 6 , 4 , 2 } ; int n = arr . length ; operations ( arr , n , k ) ; } }
inputs: "Print all permutations in sorted ( lexicographic ) order | Program to print all permutations of a string in sorted order . ; Following function is needed for library function qsort ( ) . Refer http : www . cplusplus . com / reference / clibrary / cstdlib / qsort / ; A utility function two swap two characters a and b ; This function finds the index of the smallest character which is greater than ' first ' and is present in str [ l . . h ] ; initialize index of ceiling element ; Now iterate through rest of the elements and find the smallest character greater than ' first ' ; Print all permutations of str in sorted order ; Get size of string ; Sort the string in increasing order ; Print permutations one by one ; print this permutation ; Find the rightmost character which is smaller than its next character . Let us call it ' first ▁ char ' ; If there is no such character , all are sorted in decreasing order , means we just printed the last permutation and we are done . ; Find the ceil of ' first ▁ char ' in right of first character . Ceil of a character is the smallest character greater than it ; Swap first and second characters ; Sort the string on right of ' first ▁ char ' ; Driver program to test above function"
Solution in C:
targets: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare ( const void * a , const void * b ) { return ( * ( char * ) a - * ( char * ) b ) ; } void swap ( char * a , char * b ) { char t = * a ; * a = * b ; * b = t ; } int findCeil ( char str [ ] , char first , int l , int h ) { int ceilIndex = l ; for ( int i = l + 1 ; i <= h ; i ++ ) if ( str [ i ] > first && str [ i ] < str [ ceilIndex ] ) ceilIndex = i ; return ceilIndex ; } void sortedPermutations ( char str [ ] ) { int size = strlen ( str ) ; qsort ( str , size , sizeof ( str [ 0 ] ) , compare ) ; bool isFinished = false ; while ( ! isFinished ) { printf ( " % s ▁ 
 " , str ) ; int i ; for ( i = size - 2 ; i >= 0 ; -- i ) if ( str [ i ] < str [ i + 1 ] ) break ; if ( i == -1 ) isFinished = true ; else { int ceilIndex = findCeil ( str , str [ i ] , i + 1 , size - 1 ) ; swap ( & str [ i ] , & str [ ceilIndex ] ) ; qsort ( str + i + 1 , size - i - 1 , sizeof ( str [ 0 ] ) , compare ) ; } } } int main ( ) { char str [ ] = " ABCD " ; sortedPermutations ( str ) ; return 0 ; }
inputs: "Check if sum of divisors of two numbers are same | Python3 program to find if two numbers are equivalent or not ; Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Driver code"
Solution in Python:
targets: import math
def divSum ( n ) :
	sum = 1 ;
	i = 2 ;
	while ( i * i <= n ) :
		if ( n % i == 0 ) :
			sum = ( sum + i + math . floor ( n / i ) ) ;
		i += 1 ;
	return sum ;
def areEquivalent ( num1 , num2 ) :
	return divSum ( num1 ) == divSum ( num2 ) ;
num1 = 559 ;
num2 = 703 ;
if ( areEquivalent ( num1 , num2 ) == True ) :
	print ( " Equivalent " ) ;
else :
	print ( " Not ▁ Equivalent " ) ;
inputs: "Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) program that prints all divisors in sorted order ; Function to print the divisors ; Driver Code"
Solution in Python:
targets: from math import *
def printDivisors ( n ) :
	i = 1
	while ( i * i < n ) :
		if ( n % i == 0 ) :
			print ( i , end = " ▁ " )
		i += 1
	for i in range ( int ( sqrt ( n ) ) , 0 , - 1 ) :
		if ( n % i == 0 ) :
			print ( n // i , end = " ▁ " )
print ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " )
printDivisors ( 100 )
inputs: "Range Query on array whose each element is XOR of index value and previous element | function return derived formula value . ; finding xor value of range [ y ... x ] ; function to solve query for l and r . ; if l or r is 0. ; finding x is divisible by 2 or not . ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function fun ( $ x ) { $ y = ( ( int ) ( $ x / 4 ) * 4 ) ; $ ans = 0 ; for ( $ i = $ y ; $ i <= $ x ; $ i ++ ) $ ans ^= $ i ; return $ ans ; } function query ( $ x ) { if ( $ x == 0 ) return 0 ; $ k = ( int ) ( ( $ x + 1 ) / 2 ) ; return ( $ x %= 2 ) ? 2 * fun ( $ k ) : ( ( fun ( $ k - 1 ) * 2 ) ^ ( $ k & 1 ) ) ; } function allQueries ( $ q , $ l , $ r ) { for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ( query ( $ r [ $ i ] ) ^ query ( $ l [ $ i ] - 1 ) ) , " 
 " ; } $ q = 3 ; $ l = array ( 2 , 2 , 5 ) ; $ r = array ( 4 , 8 , 9 ) ; allQueries ( $ q , $ l , $ r ) ; ? >
inputs: "Print modified array after multiple array range increment operations | structure to store the ( start , end ) index pair for each query ; function to increment values in the given range by a value d for multiple queries ; for each ( start , end ) index pair , perform the following operations on ' sum [ ] ' ; increment the value at index ' start ' by the given value ' d ' in ' sum [ ] ' ; if the index ' ( end + 1 ) ' exists then decrement the value at index ' ( end + 1 ) ' by the given value ' d ' in ' sum [ ] ' ; Now , perform the following operations : accumulate values in the ' sum [ ] ' array and then add them to the corresponding indexes in ' arr [ ] ' ; function to print the elements of the given array ; Driver code ; modifying the array for multiple queries"
How can the above be solved in JS?
targets: class query { constructor ( start , end ) { this . start = start ; this . end = end ; } } function incrementByD ( arr , q_arr , n , m , d ) { let sum = new Array ( n ) ; for ( let i = 0 ; i < sum . length ; i ++ ) { sum [ i ] = 0 ; } for ( let i = 0 ; i < m ; i ++ ) { sum [ q_arr [ i ] . start ] += d ; if ( ( q_arr [ i ] . end + 1 ) < n ) sum [ q_arr [ i ] . end + 1 ] -= d ; } arr [ 0 ] += sum [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; arr [ i ] += sum [ i ] ; } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 3 , 5 , 4 , 8 , 6 , 1 ] ; let q_arr = new Array ( 5 ) ; q_arr [ 0 ] = new query ( 0 , 3 ) ; q_arr [ 1 ] = new query ( 4 , 5 ) ; q_arr [ 2 ] = new query ( 1 , 4 ) ; q_arr [ 3 ] = new query ( 0 , 1 ) ; q_arr [ 4 ] = new query ( 2 , 5 ) ; let n = arr . length ; let m = q_arr . length ; let d = 2 ; document . write ( " " ) ; printArray ( arr , n ) ; incrementByD ( arr , q_arr , n , m , d ) ; document . write ( " " ) ; printArray ( arr , n ) ;
inputs: "Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code"
Solution in php:
targets: < ? php function count2sinRangeAtDigit ( $ number , $ d ) { $ powerOf10 = ( int ) pow ( 10 , $ d ) ; $ nextPowerOf10 = $ powerOf10 * 10 ; $ right = $ number % $ powerOf10 ; $ roundDown = $ number - $ number % $ nextPowerOf10 ; $ roundup = $ roundDown + $ nextPowerOf10 ; $ digit = ( $ number / $ powerOf10 ) % 10 ; if ( $ digit < 2 ) return $ roundDown / 10 ; if ( $ digit == 2 ) return $ roundDown / 10 + $ right + 1 ; return $ roundup / 10 ; } function numberOf2sinRange ( $ number ) { $ s = strval ( $ number ) ; $ len = strlen ( $ s ) ; $ count = 0 ; for ( $ digit = 0 ; $ digit < $ len ; $ digit ++ ) $ count += count2sinRangeAtDigit ( $ number , $ digit ) ; return $ count ; } print ( numberOf2sinRange ( 22 ) . " 
 " ) ; print ( numberOf2sinRange ( 100 ) . " 
 " ) ; ? >
inputs: "Program to find amount of water in a given glass | Program to find the amount of water in j - th glass of i - th row ; Returns the amount of water in jth glass of ith row ; A row number i has maximum i columns . So input column number must be less than i ; There will be i * ( i + 1 ) / 2 glasses till ith row ( including ith row ) ; Initialize all glasses as empty ; Put all water in first glass ; Now let the water flow to the downward glasses till the row number is less than or / equal to i ( given row ) correction : X can be zero for side glasses as they have lower rate to fill ; Fill glasses in a given row . Number of columns in a row is equal to row number ; Get the water from current glass ; Keep the amount less than or equal to capacity in current glass ; Get the remaining amount ; Distribute the remaining amount to the down two glasses ; The index of jth glass in ith row will be i * ( i - 1 ) / 2 + j - 1 ; Driver program to test above function ; Total amount of water"
How can the above be solved in C++?
targets: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
float findWater ( int i , int j , float X ) { if ( j > i ) { printf ( " Incorrect ▁ Inputn " ) ; exit ( 0 ) ; } float glass [ i * ( i + 1 ) / 2 ] ; memset ( glass , 0 , sizeof ( glass ) ) ; int index = 0 ; glass [ index ] = X ; for ( int row = 1 ; row <= i ; ++ row ) { for ( int col = 1 ; col <= row ; ++ col , ++ index ) { X = glass [ index ] ; glass [ index ] = ( X >= 1.0f ) ? 1.0f : X ; X = ( X >= 1.0f ) ? ( X - 1 ) : 0.0f ; glass [ index + row ] += X / 2 ; glass [ index + row + 1 ] += X / 2 ; } } return glass [ i * ( i - 1 ) / 2 + j - 1 ] ; } int main ( ) { int i = 2 , j = 2 ; float X = 2.0 ; printf ( " Amount ▁ of ▁ water ▁ in ▁ jth ▁ glass ▁ of ▁ ith ▁ row ▁ is : ▁ % f " , findWater ( i , j , X ) ) ; return 0 ; }
inputs: "Smallest and Largest N | Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Given Number ; Function Call"
Solution in Javascript:
targets: function findNumberL ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } function findNumberS ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } let N = 3 ; document . write ( " " + findNumberS ( N ) + " " ) ; document . write ( " " + findNumberL ( N ) ) ;
inputs: "Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | Function to find number of unordered pairs ; Maximum element from the array ; Array to store the frequency of each element ; Stores the number of unordered pairs ; Store the frequency of each element ; Find the number of unordered pairs ; If the number j divisible by ith element is present in the array ; If the ith element of the array has frequency more than one ; Driver code"
How can the above be solved in Python?
targets: def freqPairs ( arr , n ) :
	max = arr [ 0 ]
	for i in range ( len ( arr ) ) :
		if arr [ i ] > max :
			max = arr [ i ]
	freq = [ 0 for i in range ( max + 1 ) ]
	count = 0
	for i in range ( n ) :
		freq [ arr [ i ] ] += 1
	for i in range ( n ) :
		for j in range ( 2 * arr [ i ] , max + 1 , arr [ i ] ) :
			if ( freq [ j ] >= 1 ) :
				count += freq [ j ]
		if ( freq [ arr [ i ] ] > 1 ) :
			count += freq [ arr [ i ] ] - 1
			freq [ arr [ i ] ] -= 1
	return count
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 4 , 2 , 6 ]
	n = len ( arr )
	print ( freqPairs ( arr , n ) )
inputs: "Maximize minimum distance between repetitions from any permutation of the given Array | C ++ Program to implement the above approach ; Size of the array ; Stores the frequency of array elements ; Find the highest frequency in the array ; Increase count of max frequent element ; If no repetition is present ; Find the maximum distance ; Return the max distance ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int findMaxLen ( vector < int > & a ) { int n = a . size ( ) ; int freq [ n + 1 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < n ; ++ i ) { freq [ a [ i ] ] ++ ; } int maxFreqElement = INT_MIN ; int maxFreqCount = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( freq [ i ] > maxFreqElement ) { maxFreqElement = freq [ i ] ; maxFreqCount = 1 ; } else if ( freq [ i ] == maxFreqElement ) maxFreqCount ++ ; } int ans ; if ( maxFreqElement == 1 ) ans = 0 ; else { ans = ( ( n - maxFreqCount ) / ( maxFreqElement - 1 ) ) ; } return ans ; } int main ( ) { vector < int > a = { 1 , 2 , 1 , 2 } ; cout << findMaxLen ( a ) << endl ; }
inputs: "Smallest perfect Cube divisible by all elements of an array | Function to return the gcd of two numbers ; Function to return the lcm of all the elements of the array ; To calculate lcm of two numbers multiply them and divide the result by gcd of both the numbers ; Return the LCM of the array elements ; Function to return the smallest perfect cube divisible by all the elements of arr [ ] ; LCM of all the elements of arr [ ] ; If 2 divides lcm cnt number of times ; Check all the numbers that divide lcm ; Return the answer ; Driver code"
Solution in php:
targets: < ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; } function lcmOfArray ( & $ arr , $ n ) { if ( $ n < 1 ) return 0 ; $ lcm = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ lcm = ( $ lcm * $ arr [ $ i ] ) / gcd ( $ lcm , $ arr [ $ i ] ) ; return $ lcm ; } function minPerfectCube ( & $ arr , $ n ) { $ lcm = lcmOfArray ( $ arr , $ n ) ; $ minPerfectCube = $ lcm ; $ cnt = 0 ; while ( $ lcm > 1 && $ lcm % 2 == 0 ) { $ cnt ++ ; $ lcm /= 2 ; } if ( $ cnt % 3 == 2 ) $ minPerfectCube *= 2 ; else if ( $ cnt % 3 == 1 ) $ minPerfectCube *= 4 ; $ i = 3 ; while ( $ lcm > 1 ) { $ cnt = 0 ; while ( $ lcm % $ i == 0 ) { $ cnt ++ ; $ lcm /= $ i ; } if ( $ cnt % 3 == 1 ) $ minPerfectCube *= $ i * $ i ; else if ( $ cnt % 3 == 2 ) $ minPerfectCube *= $ i ; $ i += 2 ; } return $ minPerfectCube ; } $ arr = array ( 10 , 125 , 14 , 42 , 100 ) ; $ n = sizeof ( $ arr ) ; echo ( minPerfectCube ( $ arr , $ n ) ) ; ? >
inputs: "Queries to find the last non | Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string $str [ $l ... $r ] ; Function to return the last non - repeating character ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array"
How can the above be solved in PHP?
targets: < ? php $ MAX = 256 ; $ freq = array_fill ( 0 , 256 , array_fill ( 0 , 1000 , 0 ) ) ; function preCalculate ( $ str , $ n ) { global $ freq ; global $ MAX ; $ freq [ ord ( $ str [ 0 ] ) ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ ch = $ str [ $ i ] ; for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) { $ charToUpdate = chr ( $ j ) ; if ( $ charToUpdate == $ ch ) $ freq [ $ j ] [ $ i ] = $ freq [ $ j ] [ $ i - 1 ] + 1 ; else $ freq [ $ j ] [ $ i ] = $ freq [ $ j ] [ $ i - 1 ] ; } } } function getFrequency ( $ ch , $ l , $ r ) { global $ freq ; if ( $ l == 0 ) return $ freq [ ord ( $ ch ) ] [ $ r ] ; else return ( $ freq [ ord ( $ ch ) ] [ $ r ] - $ freq [ ord ( $ ch ) ] [ $ l - 1 ] ) ; } function lastNonRepeating ( $ str , $ n , $ l , $ r ) { for ( $ i = $ r ; $ i >= $ l ; $ i -- ) { $ ch = $ str [ $ i ] ; if ( getFrequency ( $ ch , $ l , $ r ) == 1 ) return $ ch ; } return " - 1" ; } $ str = " GeeksForGeeks " ; $ n = strlen ( $ str ) ; $ queries = array ( array ( 2 , 9 ) , array ( 2 , 3 ) , array ( 0 , 12 ) ) ; $ q = 3 ; preCalculate ( $ str , $ n ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) { echo ( lastNonRepeating ( $ str , $ n , $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] ) ) , " 
 " ; } ? >
inputs: "Program to print binomial expansion series | Function to print the series ; Calculating and printing first term ; Computing and printing remaining terms ; Find current term using previous terms We increment power of X by 1 , decrement power of A by 1 and compute nCi using previous term by multiplying previous term with ( n - i + 1 ) / i ; Driver Code"
How can the above be solved in Python?
targets: def series ( A , X , n ) :
	term = pow ( A , n )
	print ( term , end = " ▁ " )
	for i in range ( 1 , n + 1 ) :
		term = int ( term * X * ( n - i + 1 ) / ( i * A ) )
		print ( term , end = " ▁ " )
A = 3 ; X = 4 ; n = 5
series ( A , X , n )
inputs: "Ways to write N as sum of two or more positive integers | Set | Function to find the number of partitions of N ; Base case ; Driver code"
How can the above be solved in JS?
targets: function partitions ( n ) { var p = Array ( n + 1 ) . fill ( 0 ) ; p [ 0 ] = 1 ; for ( i = 1 ; i <= n ; ++ i ) { var k = 1 ; while ( ( k * ( 3 * k - 1 ) ) / 2 <= i ) { p [ i ] += ( k % 2 != 0 ? 1 : - 1 ) * p [ i - ( k * ( 3 * k - 1 ) ) / 2 ] ; if ( k > 0 ) { k *= - 1 ; } else { k = 1 - k ; } } } return p [ n ] ; } var N = 20 ; document . write ( partitions ( N ) ) ;
inputs: "Count of indices in an array that satisfy the given condition | C # implementation of the approach ; 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"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int countIndices ( int [ ] arr , int n ) { int cnt = 0 ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countIndices ( arr , n ) ) ; } }
inputs: Cutting a Rod | DP | C program for above approach ; Global Array for the purpose of memoization . ; A recursive program , using , memoization , to implement the rod cutting problem ( Top - Down ) . ; The maximum price will be zero , when either the length of the rod is zero or price is zero . ; If the length of the rod is less than the maximum length , Max_lene will consider it . Now depending upon the profit , either Max_lene we will take it or discard it . ; If the length of the rod is greater than the permitted size , Max_len we will not consider it . ; Max_lene Max_lenill return the maximum value obtained , Max_lenhich is present at the nth roMax_len and Max_lenth column . ; Driver program to test above functions ; Function Call
How can the above be solved in C?
targets: #include <stdio.h>
#include <stdlib.h>
int max ( int a , int b ) { return ( a > b ) ? a : b ; } int t [ 9 ] [ 9 ] ; int un_kp ( int price [ ] , int length [ ] , int Max_len , int n ) { if ( n == 0 Max_len == 0 ) { return 0 ; } if ( length [ n - 1 ] <= Max_len ) { t [ n ] [ Max_len ] = max ( price [ n - 1 ] + un_kp ( price , length , Max_len - length [ n - 1 ] , n ) , un_kp ( price , length , Max_len , n - 1 ) ) ; } else { t [ n ] [ Max_len ] = un_kp ( price , length , Max_len , n - 1 ) ; } return t [ n ] [ Max_len ] ; } int main ( ) { int price [ ] = { 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; int length [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { length [ i ] = i + 1 ; } int Max_len = n ; printf ( " Maximum ▁ obtained ▁ value ▁ is ▁ % d ▁ 
 " , un_kp ( price , length , n , Max_len ) ) ; }
inputs: "Number of permutation with K inversions | Set 2 | Function to count permutations with K inversions ; Store number of permutations with K inversions ; If N = 1 only 1 permutation with no inversion ; For K = 0 only 1 permutation with no inversion ; Otherwise Update each dp state as per the reccurrance relation formed ; Print final count ; Given N and K ; Function Call"
Solution in Javascript:
targets: function numberOfPermWithKInversion ( N , K ) { let dp = new Array ( 2 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } let mod = 1000000007 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 0 ; j <= K ; j ++ ) { if ( i == 1 ) { dp [ i % 2 ] [ j ] = ( j == 0 ) ? 1 : 0 ; } else if ( j == 0 ) dp [ i % 2 ] [ j ] = 1 ; else dp [ i % 2 ] [ j ] = ( dp [ i % 2 ] [ j - 1 ] % mod + ( dp [ 1 - i % 2 ] [ j ] - ( ( Math . max ( j - ( i - 1 ) , 0 ) == 0 ) ? 0 : dp [ 1 - i % 2 ] [ Math . max ( j - ( i - 1 ) , 0 ) - 1 ] ) + mod ) % mod ) % mod ; } } document . write ( dp [ N % 2 ] [ K ] ) ; } let N = 3 , K = 2 ; numberOfPermWithKInversion ( N , K ) ;
inputs: "Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum | C ++ implementation of the missing number such that maximum absolute difference between adjacent element is minimum ; Function to find the missing number such that maximum absolute difference is minimum ; Loop to find the maximum and minimum adjacent element to missing number ; Driver Code ; Function Call"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int missingnumber ( int n , int arr [ ] ) { int mn = INT_MAX , mx = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == -1 && arr [ i - 1 ] != -1 ) { mn = min ( mn , arr [ i - 1 ] ) ; mx = max ( mx , arr [ i - 1 ] ) ; } if ( i < ( n - 1 ) && arr [ i ] == -1 && arr [ i + 1 ] != -1 ) { mn = min ( mn , arr [ i + 1 ] ) ; mx = max ( mx , arr [ i + 1 ] ) ; } } long long int res = ( mx + mn ) / 2 ; return res ; } int main ( ) { int n = 5 ; int arr [ 5 ] = { -1 , 10 , -1 , 12 , -1 } ; int ans = 0 ; int res = missingnumber ( n , arr ) ; cout << res ; return 0 ; }
inputs: "Coin Change | DP | Dynamic Programming C # implementation of Coin Change problem ; table [ i ] will be storing the number of solutions for value i . We need n + 1 rows as the table is constructed in bottom up manner using the base case ( n = 0 ) ; Base case ( If given value is 0 ) ; Pick all coins one by one and update the table [ ] values after the index greater than or equal to the value of the picked coin ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int count ( int [ ] S , int m , int n ) { int [ ] table = new int [ n + 1 ] ; table [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = S [ i ] ; j <= n ; j ++ ) table [ j ] += table [ j - S [ i ] ] ; return table [ n ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int m = arr . Length ; int n = 4 ; Console . Write ( count ( arr , m , n ) ) ; } }
inputs: "Find Prime number just less than and just greater each element of given Array | Python3 program for the above approach ; Utility function to check for primality of a number X by checking whether X haACCs any factors other than 1 and itself . ; Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Driver code ; Input ; Function call"
Solution in Python:
targets: from math import sqrt
def isPrime ( X ) :
	for i in range ( 2 , int ( sqrt ( X ) ) + 1 , 1 ) :
		if ( X % i == 0 ) :
			return False
	return True
def printPrimes ( A , N ) :
	for i in range ( N ) :
		j = A [ i ] - 1
		while ( 1 ) :
			if ( isPrime ( j ) ) :
				print ( j , end = " ▁ " )
				break
			j -= 1
		j = A [ i ] + 1
		while ( 1 ) :
			if ( isPrime ( j ) ) :
				print ( j , end = " ▁ " )
				break
			j += 1
		print ( " " , ▁ end ▁ = ▁ " " )
if __name__ == ' _ _ main _ _ ' :
	A = [ 17 , 28 ]
	N = len ( A )
	printPrimes ( A , N )
inputs: "Number of subarrays with GCD equal to 1 | Java implementation of the approach ; Function to return the required count ; To store the final answer ; To store the GCD starting from index ' i ' ; Loop to find the gcd of each subarray from arr [ i ] to arr [ i ... n - 1 ] ; Increment the count if curr_gcd = 1 ; Return the final answer ; Driver code"
Solution in Java:
targets: class GFG { static int cntSubArr ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_gcd = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_gcd = __gcd ( curr_gcd , arr [ j ] ) ; ans += ( curr_gcd == 1 ) ? 1 : 0 ; } } return ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( cntSubArr ( arr , n ) ) ; } }
inputs: "Check if string is right to left diagonal or not | Python3 program to Check if the given is right to left diagonal or not ; Function to check if the given is right to left diagonal or not ; Iterate over string ; If character is not same as the first character then return false ; Driver Code ; Given String str ; Function Call"
How can the above be solved in Python?
targets: from math import sqrt , floor , ceil
def is_rtol ( s ) :
	tmp = floor ( sqrt ( len ( s ) ) ) - 1
	first = s [ tmp ]
	for pos in range ( tmp , len ( s ) - 1 , tmp ) :
		if ( s [ pos ] != first ) :
			return False
	return True
if __name__ == ' _ _ main _ _ ' :
	str = " abcxabxcaxbcxabc "
	if ( is_rtol ( str ) ) :
		print ( " Yes " )
	else :
		print ( " No " )
inputs: "Find the longest sub | Function to find longest prefix suffix ; To store longest prefix suffix ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Loop calculates lps [ i ] for i = 1 to n - 1 ; ( pat [ i ] != pat [ len ] ) ; If len = 0 ; Function to find the longest substring which is prefix as well as a sub - string of s [ 1. . . n - 2 ] ; Find longest prefix suffix ; If lps of n - 1 is zero ; At any position lps [ i ] equals to lps [ n - 1 ] ; If answer is not possible ; Driver code ; function call"
How can the above be solved in JS?
targets: function compute_lps ( s ) { var n = s . length ; var lps = Array ( n ) ; var len = 0 ; lps [ 0 ] = 0 ; var i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) len = lps [ len - 1 ] ; else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } function Longestsubstring ( s ) { var lps = compute_lps ( s ) ; var n = s . length ; if ( lps [ n - 1 ] == 0 ) { document . write ( - 1 ) ; return ; } for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( lps [ i ] == lps [ n - 1 ] ) { document . write ( s . substring ( 0 , lps [ i ] ) ) ; return ; } } if ( lps [ lps [ n - 1 ] - 1 ] == 0 ) document . write ( - 1 ) ; else document . write ( s . substr ( 0 , lps [ lps [ n - 1 ] - 1 ] ) ) ; } var s = " " ; Longestsubstring ( s ) ;
inputs: "Longest subsequence where each character occurs at least k times | C # Program to find the subsequence with each character occurring at least k times in string s ; Function to find the subsequence ; Taking an extra array to keep record for character count in s ; Counting occurrences of all characters in str [ ] ; Printing characters with count >= k in same order as they appear in str . ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int MAX_CHAR = 26 ; static void findSubsequence ( string str , int k ) { int [ ] a = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . Length ; i ++ ) a [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < str . Length ; i ++ ) if ( a [ str [ i ] - ' a ' ] >= k ) Console . Write ( str [ i ] ) ; } public static void Main ( ) { int k = 2 ; findSubsequence ( " geeksforgeeks " , k ) ; } }