Muennighoff/xP3x-sample huggingface dataset:
inputs: "Count composite fibonacci numbers from given array | C ++ program to implement the above approach ; Function to find all Fibonacci numbers up to Max ; Store all Fibonacci numbers upto Max ; Stores previous element of Fibonacci sequence ; Stores previous element of Fibonacci sequence ; Insert prev into hashmap ; Insert all the Fibonacci numbers up to Max ; Insert curr into hashmap ; Stores curr into temp ; Update curr ; Update prev ; Function to find all Composite numbers up to Max ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to find the numbers which is both a composite and Fibonacci number ; Stores the largest element of the array ; Traverse the array arr [ ] ; Update Max ; isPrim [ i ] check i is a prime number or not ; Stores all the Fibonacci numbers ; Traverse the array arr [ ] ; current element is not a composite number ; If current element is a Fibonacci and composite number ; Print current element ; Driver Code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; set < int > createhashmap ( int Max ) { set < int > hashmap ; int curr = 1 ; int prev = 0 ; hashmap . insert ( prev ) ; while ( curr <= Max ) { hashmap . insert ( curr ) ; int temp = curr ; curr = curr + prev ; prev = temp ; } return hashmap ; } vector < bool > SieveOfEratosthenes ( int Max ) { vector < bool > isPrime ( Max , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= Max ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= Max ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } int cntFibonacciPrime ( int arr [ ] , int N ) { int Max = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { Max = max ( Max , arr [ i ] ) ; } vector < bool > isPrime = SieveOfEratosthenes ( Max ) ; set < int > hashmap = createhashmap ( Max ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) continue ; if ( ( hashmap . count ( arr [ i ] ) ) && ! isPrime [ arr [ i ] ] ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 13 , 55 , 7 , 3 , 5 , 21 , 233 , 144 , 89 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cntFibonacciPrime ( arr , N ) ; return 0 ; }
inputs: "Maximum difference between groups of size two | PHP program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code"
Solution in php:
targets: < ? php function CalculateMax ( $ arr , $ n ) { sort ( $ arr ) ; $ min_sum = $ arr [ 0 ] + $ arr [ 1 ] ; $ max_sum = $ arr [ $ n - 1 ] + $ arr [ $ n - 2 ] ; return abs ( $ max_sum - $ min_sum ) ; } $ arr = array ( 6 , 7 , 1 , 11 ) ; $ n = sizeof ( $ arr ) ; echo CalculateMax ( $ arr , $ n ) , " 
 " ; ? >
inputs: "Count of Binary Digit numbers smaller than N | method returns count of binary digit numbers smaller than N ; queue to store all intermediate binary digit numbers ; binary digits start with 1 ; loop until we have element in queue ; push next binary digit numbers only if current popped element is N ; uncomment below line to print actual number in sorted order ; Driver code"
Solution in Javascript:
targets: function countOfBinaryNumberLessThanN ( N ) { var q = [ ] ; q . push ( 1 ) ; var cnt = 0 ; var t ; while ( q . length > 0 ) { t = q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; } var N = 200 ; document . write ( countOfBinaryNumberLessThanN ( N ) + " " ) ;
inputs: "Count of alphabets having ASCII value less than and greater than k | Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code"
How can the above be solved in Python?
targets: def CountCharacters ( str , k ) :
	cnt = 0
	l = len ( str )
	for i in range ( l ) :
		if ( ord ( str [ i ] ) < k ) :
			cnt += 1
	return cnt
if __name__ == " _ _ main _ _ " :
	str = " GeeksForGeeks "
	k = 90
	count = CountCharacters ( str , k )
	print ( " Characters ▁ with ▁ ASCII ▁ values " , " less ▁ than ▁ K ▁ are " , count )
	print ( " Characters ▁ with ▁ ASCII ▁ values " , " greater ▁ than ▁ or ▁ equal ▁ to ▁ K ▁ are " , len ( str ) - count )
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"
Solution 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: "Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base cases ; For cases n > 2 ; Driver code"
How can the above be solved in Java?
targets: import java . io . * ; class GFG { static int CountWays ( int n ) { if ( n == 0 ) { return 1 ; } if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 1 + 1 ; } return CountWays ( n - 1 ) + CountWays ( n - 3 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( CountWays ( n ) ) ; } }
inputs: "Largest Square in a Binary Matrix with at most K 1 s for multiple Queries | C # implementation to find the largest square in the matrix such that it contains atmost K 1 's  ; Function to find the largest square in the matrix such that it contains atmost K 1 's  ; Precomputing the countDP prefix sum of the matrix ; Loop to solve Queries ; Calculating the maximum possible distance of the centre from edge ; Calculating the number of 1 s in the submatrix ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void largestSquare ( int [ , ] matrix , int R , int C , int [ ] q_i , int [ ] q_j , int K , int Q ) { int [ , ] countDP = new int [ R , C ] ; countDP [ 0 , 0 ] = matrix [ 0 , 0 ] ; for ( int i = 1 ; i < R ; i ++ ) countDP [ i , 0 ] = countDP [ i - 1 , 0 ] + matrix [ i , 0 ] ; for ( int j = 1 ; j < C ; j ++ ) countDP [ 0 , j ] = countDP [ 0 , j - 1 ] + matrix [ 0 , j ] ; for ( int i = 1 ; i < R ; i ++ ) for ( int j = 1 ; j < C ; j ++ ) countDP [ i , j ] = matrix [ i , j ] + countDP [ i - 1 , j ] + countDP [ i , j - 1 ] - countDP [ i - 1 , j - 1 ] ; for ( int q = 0 ; q < Q ; q ++ ) { int i = q_i [ q ] ; int j = q_j [ q ] ; int min_dist = Math . Min ( Math . Min ( i , j ) , Math . Min ( R - i - 1 , C - j - 1 ) ) ; int ans = - 1 ; for ( int k = 0 ; k <= min_dist ; k ++ ) { int x1 = i - k , x2 = i + k ; int y1 = j - k , y2 = j + k ; int count = countDP [ x2 , y2 ] ; if ( x1 > 0 ) count -= countDP [ x1 - 1 , y2 ] ; if ( y1 > 0 ) count -= countDP [ x2 , y1 - 1 ] ; if ( x1 > 0 && y1 > 0 ) count += countDP [ x1 - 1 , y1 - 1 ] ; if ( count > K ) break ; ans = 2 * k + 1 ; } Console . Write ( ans + " 
 " ) ; } } public static void Main ( String [ ] args ) { int [ , ] matrix = { { 1 , 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 0 , 0 , 1 , 0 } } ; int K = 9 , Q = 1 ; int [ ] q_i = { 1 } ; int [ ] q_j = { 2 } ; largestSquare ( matrix , 4 , 5 , q_i , q_j , K , Q ) ; } }
inputs: "Reverse vowels in a given string | utility function to check for vowel ; Function to reverse order of vowels ; Start two indexes from two corners and move toward each other ; swapping ; Driver function"
How can the above be solved in JS?
targets: function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function reverseVowel ( str ) { let i = 0 ; let j = str . length - 1 ; let str1 = str . split ( " " ) ; while ( i < j ) { if ( ! isVowel ( str1 [ i ] ) ) { i ++ ; continue ; } if ( ! isVowel ( str1 [ j ] ) ) { j -- ; continue ; } let t = str1 [ i ] ; str1 [ i ] = str1 [ j ] ; str1 [ j ] = t ; i ++ ; j -- ; } let str2 = ( str1 ) . join ( " " ) ; return str2 ; } let str = " " ; document . write ( reverseVowel ( str ) ) ;
inputs: "DFA for Strings not ending with " THE " | C program to implement DFS that accepts all string that do not end with " THE " ; dfa tells the number associated with the present state ; This function is for the starting state ( zeroth ) of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; This function is for the first state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; On receiving ' H ' or ' h ' goto second state ( 2 ) ; else goto starting state ( 0 ) ; This function is for the second state of DFA ; On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 ) ; This function is for the third state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 ) ; store length of string ; driver code"
Solution in C:
targets: #include <stdio.h>
#include <string.h>
int dfa = 0 ; void start ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; } void state1 ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; else if ( c == ' h ' c == ' H ' ) dfa = 2 ; else dfa = 0 ; } void state2 ( char c ) { if ( c == ' e ' c == ' E ' ) dfa = 3 ; else if ( c == ' t ' c == ' T ' ) dfa = 1 ; else dfa = 0 ; } void state3 ( char c ) { if ( c == ' t ' c == ' T ' ) dfa = 1 ; else dfa = 0 ; } bool isAccepted ( char str [ ] ) { int len = strlen ( str ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( dfa == 0 ) start ( str [ i ] ) ; else if ( dfa == 1 ) state1 ( str [ i ] ) ; else if ( dfa == 2 ) state2 ( str [ i ] ) ; else state3 ( str [ i ] ) ; } return ( dfa != 3 ) ; } int main ( ) { char str [ ] = " forTHEgeeks " ; if ( isAccepted ( str ) == true ) printf ( " ACCEPTED 
 " ) ; else printf ( " NOT ▁ ACCEPTED 
 " ) ; return 0 ; }
inputs: Write you own Power without using multiplication ( * ) and division ( / ) operators |  ; Works only if a >= 0 and b >= 0 ; driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
int pow ( int a , int b ) { if ( b == 0 ) return 1 ; int answer = a ; int increment = a ; int i , j ; for ( i = 1 ; i < b ; i ++ ) { for ( j = 1 ; j < a ; j ++ ) { answer += increment ; } increment = answer ; } return answer ; } int main ( ) { printf ( " % d " , pow ( 5 , 3 ) ) ; getchar ( ) ; return 0 ; }
inputs: "Distance between a point and a Plane in 3 D | C program to find the Perpendicular ( shortest ) distance between a point and a Plane in 3 D . ; Function to find distance ; Driver Code ; Function call"
Solution in C:
targets: #include <stdio.h>
#include <math.h>
void shortest_distance ( float x1 , float y1 , float z1 , float a , float b , float c , float d ) { d = fabs ( ( a * x1 + b * y1 + c * z1 + d ) ) ; float e = sqrt ( a * a + b * b + c * c ) ; printf ( " Perpendicular ▁ distance ▁ is ▁ % f " , d / e ) ; return ; } int main ( ) { float x1 = 4 ; float y1 = -4 ; float z1 = 3 ; float a = 2 ; float b = -2 ; float c = 5 ; float d = 8 ; shortest_distance ( x1 , y1 , z1 , a , b , c , d ) ; }
inputs: "Program to calculate distance between two points | C ++ code to compute distance ; Function to calculate distance ; Calculating distance ; Drivers Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; float distance ( int x1 , int y1 , int x2 , int y2 ) { return sqrt ( pow ( x2 - x1 , 2 ) + pow ( y2 - y1 , 2 ) * 1.0 ) ; } int main ( ) { cout << distance ( 3 , 4 , 4 , 3 ) ; return 0 ; }
inputs: "Sum of all the prime divisors of a number | PHP program to find sum of prime divisors of N ; Function to check if the number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to find sum of prime divisors of N ; Driver code"
Solution in php:
targets: < ? php $ N = 1000005 ; function isPrime ( $ n ) { global $ N ; if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function SumOfPrimeDivisors ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( isPrime ( $ i ) ) $ sum += $ i ; } } return $ sum ; } $ n = 60 ; echo " Sum ▁ of ▁ prime ▁ divisors ▁ of ▁ 60 ▁ is ▁ " . SumOfPrimeDivisors ( $ n ) ; ? >
inputs: "All about Bit Manipulation | Function to clear the ith bit of the given number N ; Create the mask for the ith bit unset ; Return the update value"
Solution in C#:
targets: static int clearBit ( int num , int i ) { int mask = ~ ( 1 << i ) ; return num & mask ; }
inputs: "Lexicographically smallest K | C ++ program for the above approach ; Function to find lexicographically smallest subsequence of size K ; Length of string ; Stores the minimum subsequence ; Traverse the string S ; If the stack is empty ; Iterate till the current character is less than the the character at the top of stack ; Check if there are enough characters remaining to obtain length K ; If stack size is < K ; Push the current character into it ; Stores the resultant string ; Iterate until stack is empty ; Reverse the string ; Print the string ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void smallestSubsequence ( string & S , int K ) { int N = S . size ( ) ; stack < char > answer ; for ( int i = 0 ; i < N ; ++ i ) { if ( answer . empty ( ) ) { answer . push ( S [ i ] ) ; } else { while ( ( ! answer . empty ( ) ) && ( S [ i ] < answer . top ( ) ) && ( answer . size ( ) - 1 + N - i >= K ) ) { answer . pop ( ) ; } if ( answer . empty ( ) || answer . size ( ) < K ) { answer . push ( S [ i ] ) ; } } } string ret ; while ( ! answer . empty ( ) ) { ret . push_back ( answer . top ( ) ) ; answer . pop ( ) ; } reverse ( ret . begin ( ) , ret . end ( ) ) ; cout << ret ; } int main ( ) { string S = " aabdaabc " ; int K = 3 ; smallestSubsequence ( S , K ) ; return 0 ; }
inputs: "Find the two repeating elements in a given array | printRepeating function ; S is for sum of elements in arr ; P is for product of elements in arr ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code"
Solution in Javascript:
targets: function printRepeating ( arr , size ) { var S = 0 ; var P = 1 ; var x , y ; var D ; var n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * parseInt ( ( n + 1 ) / 2 ) ; P = parseInt ( P / fact ( n ) ) ; D = parseInt ( Math . sqrt ( S * S - 4 * P ) ) ; x = parseInt ( ( D + S ) / 2 ) ; y = parseInt ( ( S - D ) / 2 ) ; document . write ( " " ) ; document . write ( x + " " + y ) ; } function fact ( n ) { var ans = false ; if ( n == 0 ) return 1 ; else return ( n * fact ( n - 1 ) ) ; } var arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] ; var arr_size = arr . length ; printRepeating ( arr , arr_size ) ;
inputs: "Check if N and M can be made equal by increasing N by A and decreasing M by B | C # program to check if two numbers can be made equal by increasing the first by a and decreasing the second by b ; Function to whether the numbers can be made equal or not ; Check whether the numbers can reach an equal point or not ; M and N cannot be made equal by increasing M and decreasing N when M is already greater than N ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static bool checkEqualNo ( int m , int n , int a , int b ) { if ( m <= n ) { if ( ( n - m ) % ( a + b ) == 0 ) { return true ; } else { return false ; } } else { return false ; } } public static void Main ( String [ ] args ) { int M = 2 ; int N = 8 ; int A = 3 ; int B = 3 ; if ( checkEqualNo ( M , N , A , B ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
inputs: "Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | C # implementation of above approach ; Function to return the count of required pairs ; Special case ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; Count of possible pairs ; Driver code"
Solution in C#:
targets: using System ; class GFG { static int countPairs ( int n ) { if ( n == 2 ) return 4 ; int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = n - max ; return count ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( countPairs ( n ) ) ; } }
inputs: "Minimum steps to reduce N to 0 by given operations | Java program for the above approach ; Function to find the minimum number to steps to reduce N to 0 ; Dictionary for storing the precomputed sum ; Bases Cases ; Check if n is not in dp then only call the function so as to reduce no of recursive calls ; Return the answer ; Driver Code ; Given number N ; Function call"
Solution in Java:
targets: import java . util . HashMap ; class GFG { static int count ( int n ) { HashMap < Integer , Integer > dp = new HashMap < Integer , Integer > ( ) ; dp . put ( 0 , 0 ) ; dp . put ( 1 , 1 ) ; if ( ! dp . containsKey ( n ) ) dp . put ( n , 1 + Math . min ( n % 2 + count ( n / 2 ) , n % 3 + count ( n / 3 ) ) ) ; return dp . get ( n ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( String . valueOf ( ( count ( N ) ) ) ) ; } }
inputs: "Sum of all distances between occurrences of same characters in a given string | C ++ program for the above approach ; Function to calculate the sum of distances between occurrences of same characters in a string ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int findSum ( string s ) { int sum = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . size ( ) ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } int main ( ) { string s = " ttt " ; cout << findSum ( s ) << endl ; }
inputs: "Check if a large number is divisible by 2 , 3 and 5 or not | Java program to Check if a large number is divisible by 2 , 3 and 5 or not . ; function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code"
Solution in Java:
targets: class GFG { static int SumOfDigits ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( str . charAt ( i ) - '0' ) ; return sum ; } static boolean Divisible ( String str , int n ) { if ( SumOfDigits ( str , n ) % 3 == 0 && str . charAt ( n - 1 ) == '0' ) return true ; return false ; } public static void main ( String [ ] args ) { String str = "263730746028908374890" ; int n = str . length ( ) ; if ( Divisible ( str , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
inputs: "Intersecting rectangle when bottom | C # program to find intersection rectangle of given two rectangles . ; function to find intersection rectangle of given two rectangles . ; gives bottom - left point of intersection rectangle ; gives top - right point of intersection rectangle ; no intersection ; gives top - left point of intersection rectangle ; gives bottom - right point of intersection rectangle ; Driver code ; bottom - left and top - right corners of first rectangle ; bottom - left and top - right corners of first rectangle ; function call"
Solution in C#:
targets: using System ; class GFG { static void FindPoints ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 ) { int x5 = Math . Max ( x1 , x3 ) ; int y5 = Math . Max ( y1 , y3 ) ; int x6 = Math . Min ( x2 , x4 ) ; int y6 = Math . Min ( y2 , y4 ) ; if ( x5 > x6 y5 > y6 ) { Console . WriteLine ( " No ▁ intersection " ) ; return ; } Console . Write ( " ( " + x5 + " , ▁ " + y5 + " ) ▁ " ) ; Console . Write ( " ( " + x6 + " , ▁ " + y6 + " ) ▁ " ) ; int x7 = x5 ; int y7 = y6 ; Console . Write ( " ( " + x7 + " , ▁ " + y7 + " ) ▁ " ) ; int x8 = x6 ; int y8 = y5 ; Console . Write ( " ( " + x8 + " , ▁ " + y8 + " ) ▁ " ) ; } public static void Main ( ) { int x1 = 0 , y1 = 0 , x2 = 10 , y2 = 8 ; int x3 = 2 , y3 = 3 , x4 = 7 , y4 = 9 ; FindPoints ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) ; } }
inputs: "Construct Special Binary Tree from given Inorder traversal | program to construct tree from inorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Prototypes of a utility function to get the maximum value in inorder [ start . . end ] ; Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver program to test above functions ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Insorder traversal"
Solution in C:
targets: #include <stdio.h>
#include <stdlib.h>
struct node { int data ; struct node * left ; struct node * right ; } ; int max ( int inorder [ ] , int strt , int end ) ; struct node * newNode ( int data ) ; struct node * buildTree ( int inorder [ ] , int start , int end ) { if ( start > end ) return NULL ; int i = max ( inorder , start , end ) ; struct node * root = newNode ( inorder [ i ] ) ; if ( start == end ) return root ; root -> left = buildTree ( inorder , start , i - 1 ) ; root -> right = buildTree ( inorder , i + 1 , end ) ; return root ; } int max ( int arr [ ] , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } struct node * newNode ( int data ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return node ; } void printInorder ( struct node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; printf ( " % d ▁ " , node -> data ) ; printInorder ( node -> right ) ; } int main ( ) { int inorder [ ] = { 5 , 10 , 40 , 30 , 28 } ; int len = sizeof ( inorder ) / sizeof ( inorder [ 0 ] ) ; struct node * root = buildTree ( inorder , 0 , len - 1 ) ; printf ( " Inorder traversal of the constructed tree is " printInorder ( root ) ; return 0 ; }
inputs: "Minimum number of operations required to reduce N to 0 | Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code"
Solution in Javascript:
targets: function downToZero ( n ) { if ( n <= 3 ) return n ; let dp = new Array ( n + 1 ) dp . fill ( - 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; let sqr ; for ( let i = 4 ; i <= n ; i ++ ) { sqr = Math . sqrt ( i ) ; let best = Number . MAX_VALUE ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } let n = 4 ; document . write ( downToZero ( n ) ) ;
inputs: "Randomized Binary Search Algorithm | To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; If we reach here , then element was not present ; Driver code"
Solution in Javascript:
targets: function getRandom ( x , y ) { return Math . floor ( x + Math . floor ( Math . random ( ) * 10 ) % ( y - x + 1 ) ) ; } function randomizedBinarySearch ( arr , l , r , x ) { while ( l <= r ) { let m = getRandom ( l , r ) ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } let arr = [ 2 , 3 , 4 , 10 , 40 ] ; let n = arr . length ; let x = 10 ; let result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) document . write ( " " ) ; else document . write ( " " , result ) ;
inputs: "Nth term of Ruler Function Series | C # program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; Console . WriteLine ( x ) ; } public static void Main ( string [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }
inputs: "Insert minimum number in array so that sum of array becomes prime | function to check if a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Find prime number greater than a number ; find prime greater than n ; check if num is prime ; Increment num ; To find number to be added so sum of array is prime ; To find sum of array elements ; If sum is already prime return 0 ; To find prime number greater than sum ; Return difference of sum and num ; Driver code"
Solution in Python:
targets: def isPrime ( n ) :
	if n <= 1 :
		return False
	for i in range ( 2 , n ) :
		if n % i == 0 :
			return False
	return True
def findPrime ( n ) :
	num = n + 1
	while ( num ) :
		if isPrime ( num ) :
			return num
		num += 1
	return 0
def minNumber ( arr ) :
	s = 0
	for i in range ( 0 , len ( arr ) ) :
		s += arr [ i ]
	if isPrime ( s ) :
		return 0
	num = findPrime ( s )
	return num - s
arr = [ 2 , 4 , 6 , 8 , 12 ]
print ( minNumber ( arr ) )
inputs: "Program to find transpose of a matrix | C # Program to find transpose of a matrix ; Finds transpose of A [ ] [ ] in - place ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int N = 4 ; static void transpose ( int [ , ] A ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { int temp = A [ i , j ] ; A [ i , j ] = A [ j , i ] ; A [ j , i ] = temp ; } } public static void Main ( ) { int [ , ] A = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; transpose ( A ) ; Console . WriteLine ( " Modified ▁ matrix ▁ is ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( A [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } }
inputs: "Count smaller elements on right side |  ; initialize all the counts in countSmaller array as 0 ; Utility function that prints out an array on a line ; Driver program to test above functions"
Solution in C:
targets: void constructLowerArray ( int * arr [ ] , int * countSmaller , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; printf ( " 
 " ) ; } int main ( ) { int arr [ ] = { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int * low = ( int * ) malloc ( sizeof ( int ) * n ) ; constructLowerArray ( arr , low , n ) ; printArray ( low , n ) ; return 0 ; }
inputs: "Check if given four integers ( or sides ) make rectangle | Function to check if the given integers value make a rectangle ; Square is also a rectangle ; Driver code"
Solution in Javascript:
targets: function isRectangle ( a , b , c , d ) { if ( a == b && a == c && a == d && c == d && b == c && b == d ) return true ; else if ( a == b && c == d ) return true ; else if ( a == d && c == b ) return true ; else if ( a == c && d == b ) return true ; else return false ; } let a = 1 , b = 2 , c = 3 , d = 4 ; if ( isRectangle ( a , b , c , d ) ) document . write ( " " ) ; else document . write ( " " ) ;
inputs: "Majority Element | Function to find Majority element in an array ; sentinels ; update maxCount if count of current element is greater ; if maxCount is greater than n / 2 return the corresponding element ; Driver code ; Function calling"
Solution in php:
targets: < ? php function findMajority ( $ arr , $ n ) { $ maxCount = 0 ; $ index = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count ++ ; } if ( $ count > $ maxCount ) { $ maxCount = $ count ; $ index = $ i ; } } if ( $ maxCount > $ n / 2 ) echo $ arr [ $ index ] . " 
 " ; else echo " No ▁ Majority ▁ Element " . " 
 " ; } $ arr = array ( 1 , 1 , 2 , 1 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; findMajority ( $ arr , $ n ) ;
inputs: "Find K consecutive integers such that their sum is N | Function to check if a number can be expressed as the sum of k consecutive ; Finding the first term of AP ; Checking if first term is an integer ; Loop to prvar the K consecutive integers ; Driver Code"
How can the above be solved in JS?
targets: function checksum ( n , k ) { var first_term = ( ( ( 2 * n ) / k + ( 1 - k ) ) / 2.0 ) ; if ( first_term - parseInt ( ( first_term ) ) == 0 ) { for ( i = parseInt ( first_term ) ; i <= first_term + k - 1 ; i ++ ) { document . write ( i + " " ) ; } } else document . write ( " " ) ; } var n = 33 , k = 6 ; checksum ( n , k ) ;
inputs: Program to find GCD or HCF of two numbers | C program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int main ( ) { int a = 98 , b = 56 ; printf ( " GCD ▁ of ▁ % d ▁ and ▁ % d ▁ is ▁ % d ▁ " , a , b , gcd ( a , b ) ) ; return 0 ; }
inputs: "Rabin | d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; hash value for pattern t = 0 hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern if the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative values of t , converting it to positive ; Driver Code ; A prime number ; Function Call"
How can the above be solved in Python?
targets: d = 256
def search ( pat , txt , q ) :
	M = len ( pat )
	N = len ( txt )
	i = 0
	j = 0
	h = 1
	for i in xrange ( M - 1 ) :
		h = ( h * d ) % q
	for i in xrange ( M ) :
		p = ( d * p + ord ( pat [ i ] ) ) % q
		t = ( d * t + ord ( txt [ i ] ) ) % q
	for i in xrange ( N - M + 1 ) :
		if p == t :
			for j in xrange ( M ) :
				if txt [ i + j ] != pat [ j ] :
					break
				else : j += 1
			if j == M :
				print " Pattern ▁ found ▁ at ▁ index ▁ " + str ( i )
		if i < N - M :
			t = ( d * ( t - ord ( txt [ i ] ) * h ) + ord ( txt [ i + M ] ) ) % q
			if t < 0 :
				t = t + q
txt = " GEEKS ▁ FOR ▁ GEEKS "
pat = " GEEK "
q = 101
search ( pat , txt , q )
inputs: "Maximum sum from a tree with adjacent levels not allowed | Tree node class for Binary Tree representation ; Recursive function to find the maximum sum returned for a root node and its grandchildren ; Returns maximum sum with adjacent levels not allowed . This function mainly uses getSumAlternate ( ) ; We compute sum of alternate levels starting first level and from second level . And return maximum of two values . ; Driver code"
How can the above be solved in JS?
targets: class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function getSumAlternate ( root ) { if ( root == null ) return 0 ; let sum = root . data ; if ( root . left != null ) { sum += getSum ( root . left . left ) ; sum += getSum ( root . left . right ) ; } if ( root . right != null ) { sum += getSum ( root . right . left ) ; sum += getSum ( root . right . right ) ; } return sum ; } function getSum ( root ) { if ( root == null ) return 0 ; return Math . max ( getSumAlternate ( root ) , ( getSumAlternate ( root . left ) + getSumAlternate ( root . right ) ) ) ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . left . right = new Node ( 5 ) ; root . right . left . right . left = new Node ( 6 ) ; document . write ( getSum ( root ) ) ;
inputs: "Number of ways to reach at starting node after travelling through exactly K edges in a complete graph | Java program for the above approach ; Function to find number of ways to reach from node 1 to 1 again , after moving exactly K edges ; Initialize a dp [ ] array , where dp [ i ] stores number of ways to reach at a i node ; Initialize the dp array with 0 ; Base Case ; Iterate for the number of edges moved ; Sum will store number of ways to reach all the nodes ; Iterate for every possible state for the current step ; Update the value of the dp array after travelling each edge ; Print dp [ 0 ] as the answer ; Driver Code ; Given Input ; Function Call"
Solution in Java:
targets: class GFG { static void numberOfWays ( int n , int k ) { int [ ] dp = new int [ 1000 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { int numWays = 0 ; for ( int j = 0 ; j < n ; j ++ ) { numWays += dp [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { dp [ j ] = numWays - dp [ j ] ; } } System . out . println ( dp [ 0 ] + "
"); } public static void main ( String args [ ] ) { int N = 5 , K = 3 ; numberOfWays ( N , K ) ; } }
inputs: "Sum of absolute differences of indices of occurrences of each array element | Set 2 | Stores the count of occurrences and previous index of every element ; Constructor ; Function to calculate the sum of absolute differences of indices of occurrences of array element ; Stores the count of elements and their previous indices ; Initialize 2 arrays left [ ] and right [ ] of size N ; Traverse the given array ; If arr [ i ] is present in the Map ; Update left [ i ] to 0 and update the value of arr [ i ] in map ; Otherwise , get the value from the map and update left [ i ] ; Clear the map to calculate right [ ] array ; Traverse the array arr [ ] in reverse ; If arr [ i ] is present in theMap ; Update right [ i ] to 0 and update the value of arr [ i ] in the Map ; Otherwise get the value from the map and update right [ i ] ; Iterate in the range [ 0 , N - 1 ] and print the sum of left [ i ] and right [ i ] as the result ; Driver Code"
Solution in Javascript:
targets: class pair { constructor ( count , prevIndex ) { this . count = count ; this . prevIndex = prevIndex ; } } function findSum ( arr , n ) { let map = new Map ( ) ; let left = new Array ( n ) ; let right = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! map . has ( arr [ i ] ) ) { left [ i ] = 0 ; map . set ( arr [ i ] , new pair ( 1 , i ) ) ; } else { let tmp = map . get ( arr [ i ] ) ; left [ i ] = ( tmp . count ) * ( i - tmp . prevIndex ) + left [ tmp . prevIndex ] ; map . set ( arr [ i ] , new pair ( tmp . count + 1 , i ) ) ; } } map . clear ( ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( ! map . has ( arr [ i ] ) ) { right [ i ] = 0 ; map . set ( arr [ i ] , new pair ( 1 , i ) ) ; } else { let tmp = map . get ( arr [ i ] ) ; right [ i ] = ( tmp . count ) * ( Math . abs ( i - tmp . prevIndex ) ) + right [ tmp . prevIndex ] ; map . set ( arr [ i ] , new pair ( tmp . count + 1 , i ) ) ; } } for ( let i = 0 ; i < n ; i ++ ) document . write ( left [ i ] + right [ i ] + " " ) ; } let arr = [ 1 , 3 , 1 , 1 , 2 ] ; let N = arr . length ; findSum ( arr , N ) ;
inputs: "Remove an element to minimize the LCM of the given array | Java implementation of the above approach ; Function to return the LCM of two numbers ; Function to return the minimum LCM after removing a single element from the given array ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing LCM of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing LCM of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be removed ; If any other element is replaced ; Return the minimum LCM ; Driver code"
Solution in Java:
targets: class GFG { static int lcm ( int a , int b ) { int GCD = __gcd ( a , b ) ; return ( a * b ) / GCD ; } static int MinLCM ( int a [ ] , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 15 , 9 , 36 } ; int n = a . length ; System . out . println ( MinLCM ( a , n ) ) ; } }
inputs: "Partition problem | DP | Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of all elements ; Initialze the part array as 0 ; Fill the partition table in bottom up manner ; The element to be included in the sum cannot be greater than the sum ; Check if sum - arr [ i ] could be formed from a subset using elements before index i ; Driver code ; Function call"
How can the above be solved in JS?
targets: function findPartiion ( arr , n ) { let sum = 0 ; let i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; let part = new Array ( parseInt ( sum / 2 + 1 , 10 ) ) ; for ( i = 0 ; i <= parseInt ( sum / 2 , 10 ) ; i ++ ) { part [ i ] = false ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = parseInt ( sum / 2 , 10 ) ; j >= arr [ i ] ; j -- ) { if ( part [ j - arr [ i ] ] == true j == arr [ i ] ) part [ j ] = true ; } } return part [ parseInt ( sum / 2 , 10 ) ] ; } let arr = [ 1 , 3 , 3 , 2 , 3 , 2 ] ; let n = arr . length ; if ( findPartiion ( arr , n ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
inputs: "K | Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest element  after removing array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code"
Solution in php:
targets: < ? php function ksmallest ( $ arr , $ n , $ k ) { sort ( $ arr ) ; if ( $ k < $ arr [ 0 ] ) return $ k ; if ( $ k == $ arr [ 0 ] ) return $ arr [ 0 ] + 1 ; if ( $ k > $ arr [ $ n - 1 ] ) return $ k + $ n ; if ( $ arr [ 0 ] == 1 ) $ k -- ; else $ k -= ( $ arr [ 0 ] - 1 ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ c = $ arr [ $ i ] - $ arr [ $ i - 1 ] - 1 ; if ( $ k <= $ c ) return $ arr [ $ i - 1 ] + $ k ; else $ k -= $ c ; } return $ arr [ $ n - 1 ] + $ k ; } $ k = 1 ; $ arr = array ( 1 ) ; $ n = sizeof ( $ arr ) ; echo ksmallest ( $ arr , $ n , $ k ) ; ? >
inputs: "Sum of prime numbers without odd prime digits | Java program for above approach ; Find all prime numbers ; Store all prime numbers ; Function to check if a digit is odd prime or not ; Function to find sum ; To store required answer ; Get all prime numbers ; Traverse through all the prime numbers ; Flag stores 1 if a number does not contain any odd primes ; Find all digits of a number ; If number does not contain any odd primes ; Return the required answer ; Driver code ; Function call"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static int MAX = 100005 ; static Vector < Integer > addPrimes ( ) { int n = MAX ; boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) ans . add ( p ) ; return ans ; } static boolean is_prime ( int n ) { return ( n == 3 n == 5 n == 7 ) ; } static int find_Sum ( int n ) { int sum = 0 ; Vector < Integer > v = addPrimes ( ) ; for ( int i = 0 ; i < v . size ( ) && n > 0 ; i ++ ) { int flag = 1 ; int a = v . get ( i ) ; while ( a != 0 ) { int d = a % 10 ; a = a / 10 ; if ( is_prime ( d ) ) { flag = 0 ; break ; } } if ( flag == 1 ) { n -- ; sum = sum + v . get ( i ) ; } } return sum ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( find_Sum ( n ) ) ; } }
inputs: "Minimum removals to make array sum odd | Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code"
Solution in php:
targets: < ? php function findCount ( $ arr , $ n ) { $ countOdd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 == 1 ) $ countOdd ++ ; if ( $ countOdd % 2 == 0 ) return 1 ; else return 0 ; } $ arr = array ( 1 , 2 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; echo ( findCount ( $ arr , $ n ) ) ; ? >
inputs: "Lexicographically smallest string of length N and sum K | Function to return the lexicographically smallest string of length n that satisfies the given condition ; Iteration from the last position in the array ; If k is a positive integer ; ' z ' needs to be inserted ; Add the required character ; Driver code"
Solution in Python:
targets: def lexo_small ( n , k ) :
	arr = " " ;
	for i in range ( n ) :
		arr += ' a ' ;
	for i in range ( n - 1 , - 1 , - 1 ) :
		k -= i ;
		if ( k >= 0 ) :
			if ( k >= 26 ) :
				arr = arr [ : i ] + ' z ' + arr [ i + 1 : ] ;
				k -= 26 ;
			else :
				c = ( k + 97 - 1 ) ;
				arr = arr [ : i ] + chr ( c ) + arr [ i + 1 : ] ;
				k -= ord ( arr [ i ] ) - ord ( ' a ' ) + 1 ;
		else :
			break ;
		k += i ;
	return arr ;
if __name__ == ' _ _ main _ _ ' :
	n = 5 ; k = 42 ;
	arr = lexo_small ( n , k ) ;
	print ( arr ) ;
inputs: "Minimize sum of smallest elements from K subsequences of length L | C # program to find the minimum possible sum of the smallest elements from K subsequences ; Function to find the minimum sum ; Sort the array ; Calculate sum of smallest K elements ; Return the sum ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int findMinSum ( int [ ] arr , int K , int L , int size ) { if ( K * L > size ) return - 1 ; int minsum = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < K ; i ++ ) minsum += arr [ i ] ; return minsum ; } public static void Main ( ) { int [ ] arr = { 2 , 15 , 5 , 1 , 35 , 16 , 67 , 10 } ; int K = 3 ; int L = 2 ; int length = arr . Length ; Console . Write ( findMinSum ( arr , K , L , length ) ) ; } }
inputs: "Chocolate Distribution Problem | Set 2 | C # program for above approach ; Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code"
Solution in C#:
targets: using System ; public class GFG { public static int minChocolates ( int [ ] a , int n ) { int i = 0 , j = 0 ; int res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } public static int get_sum ( int peak , int start , int end ) { int count = end - start + 1 ; peak = ( peak > count ) ? peak : count ; int s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } static public void Main ( ) { int [ ] a = { 5 , 5 , 4 , 3 , 2 , 1 } ; int n = a . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ chocolates ▁ = ▁ " + minChocolates ( a , n ) ) ; } }
inputs: "Nth non | function to find the nth Non - Square Number ; conversion from int to long double is necessary in order to preserve decimal places after square root . ; calculating the result ; initializing the term number ; Print the result"
How can the above be solved in PHP?
targets: < ? php function findNthNonSquare ( $ n ) { $ x = $ n ; $ ans = $ x + floor ( 0.5 + sqrt ( $ x ) ) ; return ( int ) $ ans ; } $ n = 16 ; echo " The ▁ " . $ n . " th ▁ Non - Square ▁ number ▁ is ▁ " ; echo findNthNonSquare ( $ n ) ;
inputs: "Repeated Unit Divisibility | CPP program to find least value of k for which R ( k ) is divisible by n ; To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int repUnitValue ( int n ) { if ( n % 2 == 0 n % 5 == 0 ) return 0 ; int rem = 1 ; int power = 1 ; int k = 1 ; while ( rem % n != 0 ) { k ++ ; power = power * 10 % n ; rem = ( rem + power ) % n ; } return k ; } int main ( ) { int n = 13 ; cout << repUnitValue ( n ) ; return 0 ; }
inputs: Find if possible to visit every nodes in given Graph exactly once based on given conditions | C ++ program for above approach . ; Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Driver Code ; Given Input ; Function Call
How can the above be solved in C?
targets: #include <bits/stdc++.h>
using namespace std ; void findpath ( int N , int a [ ] ) { if ( a [ 0 ] ) { printf ( " % d ▁ " , N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) printf ( " % d ▁ " , i ) ; return ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ! a [ i ] && a [ i + 1 ] ) { for ( int j = 1 ; j <= i ; j ++ ) printf ( " % d ▁ " , j ) ; printf ( " % d ▁ " , N + 1 ) ; for ( int j = i + 1 ; j <= N ; j ++ ) printf ( " % d ▁ " , j ) ; return ; } } for ( int i = 1 ; i <= N ; i ++ ) printf ( " % d ▁ " , i ) ; printf ( " % d ▁ " , N + 1 ) ; } int main ( ) { int N = 3 , arr [ ] = { 0 , 1 , 0 } ; findpath ( N , arr ) ; }