Muennighoff/xP3x-sample huggingface dataset:
inputs: "Check whether given string can be generated after concatenating given strings | C ++ implementation of the approach ; Function that return true if pre is a prefix of str ; While there are characters to match ; If characters differ at any position ; str starts with pre ; Function that return true if suff is a suffix of str ; While there are characters to match ; If characters differ at any position ; str ends with suff ; Function that returns true if str = a + b or str = b + a ; str cannot be generated by concatenating a and b ; If str starts with a i . e . a is a prefix of str ; Check if the rest of the characters are equal to b i . e . b is a suffix of str ; If str starts with b i . e . b is a prefix of str ; Check if the rest of the characters are equal to a i . e . a is a suffix of str ; Driver code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; bool startsWith ( string str , string pre ) { int strLen = str . length ( ) ; int preLen = pre . length ( ) ; int i = 0 , j = 0 ; while ( i < strLen && j < preLen ) { if ( str [ i ] != pre [ j ] ) return false ; i ++ ; j ++ ; } return true ; } bool endsWith ( string str , string suff ) { int i = str . length ( ) - 0 ; int j = suff . length ( ) - 0 ; while ( i >= 0 && j >= 0 ) { if ( str [ i ] != suff [ j ] ) return false ; i -- ; j -- ; } return true ; } bool checkString ( string str , string a , string b ) { if ( str . length ( ) != a . length ( ) + b . length ( ) ) return false ; if ( startsWith ( str , a ) ) { if ( endsWith ( str , b ) ) return true ; } if ( startsWith ( str , b ) ) { if ( endsWith ( str , a ) ) return true ; } return false ; } int main ( ) { string str = " GeeksforGeeks " ; string a = " Geeksfo " ; string b = " rGeeks " ; if ( checkString ( str , a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
inputs: "Maximum equlibrium sum in an array | Function to find maximum equilibrium sum . ; Array to store prefix sum . ; Array to store suffix sum . ; Variable to store maximum sum . ; Calculate prefix sum . ; Calculate suffix sum and compare it with prefix sum . Update ans accordingly . ; Driver Code"
How can the above be solved in Python?
targets: def findMaxSum ( arr , n ) :
	preSum = [ 0 for i in range ( n ) ]
	suffSum = [ 0 for i in range ( n ) ]
	ans = - 10000000
	preSum [ 0 ] = arr [ 0 ]
	for i in range ( 1 , n ) :
		preSum [ i ] = preSum [ i - 1 ] + arr [ i ]
	suffSum [ n - 1 ] = arr [ n - 1 ]
	if ( preSum [ n - 1 ] == suffSum [ n - 1 ] ) :
		ans = max ( ans , preSum [ n - 1 ] )
	for i in range ( n - 2 , - 1 , - 1 ) :
		suffSum [ i ] = suffSum [ i + 1 ] + arr [ i ]
		if ( suffSum [ i ] == preSum [ i ] ) :
			ans = max ( ans , preSum [ i ] )
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ - 2 , 5 , 3 , 1 , 2 , 6 , - 4 , 2 ]
	n = len ( arr )
	print ( findMaxSum ( arr , n ) )
inputs: "Sieve of Atkin | C ++ program for implementation of Sieve of Atkin ; 2 and 3 are known to be prime ; Initialise the sieve array with false values ; Mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11 ; Main part of Sieve of Atkin ; Mark all multiples of squares as non - prime ; Print primes using sieve [ ] ; Driver program"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int SieveOfAtkin ( int limit ) { if ( limit > 2 ) cout << 2 << " ▁ " ; if ( limit > 3 ) cout << 3 << " ▁ " ; bool sieve [ limit ] ; for ( int i = 0 ; i < limit ; i ++ ) sieve [ i ] = false ; for ( int x = 1 ; x * x < limit ; x ++ ) { for ( int y = 1 ; y * y < limit ; y ++ ) { int n = ( 4 * x * x ) + ( y * y ) ; if ( n <= limit && ( n % 12 == 1 n % 12 == 5 ) ) sieve [ n ] ^= true ; n = ( 3 * x * x ) + ( y * y ) ; if ( n <= limit && n % 12 == 7 ) sieve [ n ] ^= true ; n = ( 3 * x * x ) - ( y * y ) ; if ( x > y && n <= limit && n % 12 == 11 ) sieve [ n ] ^= true ; } } for ( int r = 5 ; r * r < limit ; r ++ ) { if ( sieve [ r ] ) { for ( int i = r * r ; i < limit ; i += r * r ) sieve [ i ] = false ; } } for ( int a = 5 ; a < limit ; a ++ ) if ( sieve [ a ] ) cout << a << " ▁ " ; } int main ( void ) { int limit = 20 ; SieveOfAtkin ( limit ) ; return 0 ; }
inputs: "Print all possible rotations of a given Array | Java program to print all possible rotations of the given array ; Global declaration of array ; Function to reverse array between indices s and e ; Function to generate all possible rotations of array ; Driver code"
Solution in Java:
targets: class GFG { static int arr [ ] = new int [ 10000 ] ; public static void reverse ( int arr [ ] , int s , int e ) { while ( s < e ) { int tem = arr [ s ] ; arr [ s ] = arr [ e ] ; arr [ e ] = tem ; s = s + 1 ; e = e - 1 ; } } public static void fun ( int arr [ ] , int k ) { int n = 4 - 1 ; int v = n - k ; if ( v >= 0 ) { reverse ( arr , 0 , v ) ; reverse ( arr , v + 1 , n ) ; reverse ( arr , 0 , n ) ; } } public static void main ( String args [ ] ) { arr [ 0 ] = 1 ; arr [ 1 ] = 2 ; arr [ 2 ] = 3 ; arr [ 3 ] = 4 ; for ( int i = 0 ; i < 4 ; i ++ ) { fun ( arr , i ) ; System . out . print ( " [ " ) ; for ( int j = 0 ; j < 4 ; j ++ ) { System . out . print ( arr [ j ] + " , ▁ " ) ; } System . out . print ( " ] " ) ; } } }
inputs: "Minimize the difference between the maximum and minimum values of the modified array | C # program to find the minimum difference . ; Function to return required minimum difference ; finding minimum and maximum values ; returning minimum possible difference ; Driver program ; function to return the answer"
Solution in C#:
targets: using System ; class GFG { static int minDiff ( int n , int x , int [ ] A ) { int mn = A [ 0 ] , mx = A [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) { mn = Math . Min ( mn , A [ i ] ) ; mx = Math . Max ( mx , A [ i ] ) ; } return Math . Max ( 0 , mx - mn - 2 * x ) ; } public static void Main ( ) { int n = 3 , x = 3 ; int [ ] A = { 1 , 3 , 6 } ; Console . WriteLine ( minDiff ( n , x , A ) ) ; } }
inputs: "Largest number divisible by 50 that can be formed from a given set of N digits consisting of 0 s and 7 s only | C ++ Program for the above approach ; Print the largest number divisible by 50 ; Counting number of 0 s and 7 s ; If count of 7 is divisible by 50 ; If count of 7 is less than 5 ; If count of 7 is not divisible by 50 ; Count of groups of 5 in which count of 7 s can be grouped ; Driver Code ; Given array ; Size of the array"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; void printLargestDivisible ( int arr [ ] , int N ) { int i , count0 = 0 , count7 = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; else count7 ++ ; } if ( count7 % 50 == 0 ) { while ( count7 -- ) cout << 7 ; while ( count0 -- ) cout << 0 ; } else if ( count7 < 5 ) { if ( count0 == 0 ) cout << " No " ; else cout << "0" ; } else { count7 = count7 - count7 % 5 ; while ( count7 -- ) cout << 7 ; while ( count0 -- ) cout << 0 ; } } int main ( ) { int arr [ ] = { 0 , 7 , 0 , 7 , 7 , 7 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printLargestDivisible ( arr , N ) ; return 0 ; }
inputs: "Find Nth item distributed from infinite items of infinite types based on given conditions | Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate to find the Nth day present is given out ; Find the number of presents given on day is day * ( day + 1 ) / 2 ; Iterate over the type ; Return the resultant type ; Driver code"
How can the above be solved in JS?
targets: function itemType ( n ) { let count = 0 ; let day = 1 ; while ( count + day * ( day + 1 ) / 2 < n ) { count += day * ( day + 1 ) / 2 ; day ++ ; } for ( let type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) { return type ; } } } let N = 10 ; document . write ( itemType ( N ) ) ;
inputs: "Minimum number of given operations required to reduce the array to 0 element | C ++ implementation of the approach ; Function to return the minimum operations required ; Count the frequency of each element ; Maximum element from the array ; Find all the multiples of i ; Delete the multiples ; Increment the operations ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int minOperations ( int * arr , int n ) { int maxi , result = 0 ; vector < int > freq ( 1000001 , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; freq [ x ] ++ ; } maxi = * ( max_element ( arr , arr + n ) ) ; for ( int i = 1 ; i <= maxi ; i ++ ) { if ( freq [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxi ; j = j + i ) { freq [ j ] = 0 ; } result ++ ; } } return result ; } int main ( ) { int arr [ ] = { 2 , 4 , 2 , 4 , 4 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , n ) ; return 0 ; }
inputs: "Find a Fixed Point ( Value equal to index ) in a given array | Python program to check fixed point in an array using binary search ;  ; Return - 1 if there is no Fixed Point ; Driver program to check above functions"
Solution in Python:
targets: def binarySearch ( arr , low , high ) :
	if high >= low :
		mid = ( low + high ) // 2
	if mid is arr [ mid ] :
		return mid
	if mid > arr [ mid ] :
		return binarySearch ( arr , ( mid + 1 ) , high )
	else :
		return binarySearch ( arr , low , ( mid - 1 ) )
	return - 1
arr = [ - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ]
n = len ( arr )
print ( " Fixed ▁ Point ▁ is ▁ " + str ( binarySearch ( arr , 0 , n - 1 ) ) )
inputs: "Find two numbers whose sum and GCD are given | Java program to find two numbers whose sum and GCD is given ; function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code"
Solution in Java:
targets: import java . util . * ; class Solution { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void findTwoNumbers ( int sum , int gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) System . out . println ( " a ▁ = ▁ " + Math . min ( gcd , sum - gcd ) + " , ▁ b ▁ = ▁ " + ( int ) ( sum - Math . min ( gcd , sum - gcd ) ) ) ; else System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int sum = 8 ; int gcd = 2 ; findTwoNumbers ( sum , gcd ) ; } }
inputs: "Print N numbers such that their sum is a Perfect Cube | C ++ implementation to find the N numbers such that their sum is a perfect cube ; Function to find the N numbers such that their sum is a perfect cube ; Loop to find the Ith term of the Centered Hexagonal number ; Driver Code ; Function Call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; void findNumbers ( int n ) { int i = 1 ; while ( i <= n ) { cout << ( 3 * i * ( i - 1 ) + 1 ) << " ▁ " ; i ++ ; } } int main ( ) { int n = 4 ; findNumbers ( n ) ; }
inputs: "Maximize a given unsigned number number by swapping bits at it 's extreme positions. | JavaScript program to find maximum number by swapping extreme bits . ; Traverse bits from both extremes ; Obtaining i - th and j - th bits ; Swapping the bits if lesser significant is greater than higher significant bit and accordingly modifying the number ; Driver code"
Solution in Javascript:
targets: function findMax ( num ) { let num_copy = num ; let j = 4 * 8 - 1 ; let i = 0 ; while ( i < j ) { let m = ( num_copy >> i ) & 1 ; let n = ( num_copy >> j ) & 1 ; if ( m > n ) { let x = ( 1 << i 1 << j ) ; num = num ^ x ; } i ++ ; j -- ; } return num ; } let num = 4 ; document . write ( findMax ( num ) ) ;
inputs: "Sum of first N natural numbers which are divisible by X or Y | Function to calculate the sum of numbers divisible by X or Y ; Driver code"
How can the above be solved in PHP?
targets: < ? php function sum ( $ N , $ X , $ Y ) { $ S1 ; $ S2 ; $ S3 ; $ S1 = floor ( ( ( int ) $ N / $ X ) ) * ( 2 * $ X + ( int ) ( ( int ) $ N / $ X - 1 ) * $ X ) / 2 ; $ S2 = floor ( ( ( int ) $ N / $ Y ) ) * ( 2 * $ Y + ( int ) ( ( int ) $ N / $ Y - 1 ) * $ Y ) / 2 ; $ S3 = floor ( ( ( int ) $ N / ( $ X * $ Y ) ) ) * ( 2 * ( $ X * $ Y ) + ( ( int ) $ N / ( $ X * $ Y ) - 1 ) * ( int ) ( $ X * $ Y ) ) / 2 ; return ceil ( $ S1 + ( $ S2 - $ S3 ) ) ; } $ N = 14 ; $ X = 3 ; $ Y = 5 ; echo sum ( $ N , $ X , $ Y ) ; #This  code is contributed by ajit.
? >
inputs: Count words in a given string | C program to count no of words from given input string . ; returns number of words in str ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Move to next character ; Driver program to tes above functions
How can the above be solved in C?
targets: #include <stdio.h>
#define OUT 	0
#define IN 	1
unsigned countWords ( char * str ) { int state = OUT ; unsigned wc = 0 ; while ( * str ) { if ( * str == ' ▁ ' * str == ' ' * str == ' 	 ' ) state = OUT ; else if ( state == OUT ) { state = IN ; ++ wc ; } ++ str ; } return wc ; } int main ( void ) { char str [ ] = " One ▁ two 	 	 three 
 	 four 	 five ▁ " ; printf ( " No ▁ of ▁ words ▁ : ▁ % u " , countWords ( str ) ) ; return 0 ; }
inputs: "Minimum steps to minimize n as per given condition | A tabulation based solution in Java ; Driver Code"
How can the above be solved in Java?
targets: import java . io . * ; class GFG { static int getMinSteps ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { int min = dp [ i - 1 ] ; if ( i % 2 == 0 ) { min = Math . min ( min , dp [ i / 2 ] ) ; } if ( i % 3 == 0 ) { min = Math . min ( min , dp [ i / 3 ] ) ; } dp [ i ] = min + 1 ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( getMinSteps ( n ) ) ; } }
inputs: "Longest subsequence where each character occurs at least k times | 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 PHP?
targets: < ? php function findSubsequence ( $ str , $ k ) { $ a = array ( 1024 ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ a [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ temp = ord ( $ str [ $ i ] ) - ord ( ' a ' ) ; $ a [ $ temp ] += 1 ; } for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ a [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] >= $ k ) echo $ str [ $ i ] ; } $ k = 2 ; findSubsequence ( " geeksforgeeks " , $ k ) ; ? >
inputs: "Angle between a chord and a tangent when angle in the alternate segment is given | C ++ program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void anglechordtang ( int z ) { cout << " The ▁ angle ▁ between ▁ tangent " << " ▁ and ▁ the ▁ chord ▁ is ▁ " << z << " ▁ degrees " << endl ; } int main ( ) { int z = 48 ; anglechordtang ( z ) ; return 0 ; }
inputs: "Subset Sum Problem in O ( sum ) space | Returns true if there exists a subset with given sum in arr [ ] ; The value of subset [ i % 2 ] [ j ] will be true if there exists a subset of sum j in arr [ 0 , 1 , ... . , i - 1 ] ; A subset with sum 0 is always possible ; If there exists no element no sum is possible ; Driver code"
Solution in php:
targets: < ? php function isSubsetSum ( $ arr , $ n , $ sum ) { $ subset [ 2 ] [ $ sum + 1 ] = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ sum ; $ j ++ ) { if ( $ j == 0 ) $ subset [ $ i % 2 ] [ $ j ] = true ; else if ( $ i == 0 ) $ subset [ $ i % 2 ] [ $ j ] = false ; else if ( $ arr [ $ i - 1 ] <= $ j ) $ subset [ $ i % 2 ] [ $ j ] = $ subset [ ( $ i + 1 ) % 2 ] [ $ j - $ arr [ $ i - 1 ] ] || $ subset [ ( $ i + 1 ) % 2 ] [ $ j ] ; else $ subset [ $ i % 2 ] [ $ j ] = $ subset [ ( $ i + 1 ) % 2 ] [ $ j ] ; } } return $ subset [ $ n % 2 ] [ $ sum ] ; } $ arr = array ( 6 , 2 , 5 ) ; $ sum = 7 ; $ n = sizeof ( $ arr ) ; if ( isSubsetSum ( $ arr , $ n , $ sum ) == true ) echo ( " There ▁ exists ▁ a ▁ subset ▁ with ▁ given ▁ sum " ) ; else echo ( " No ▁ subset ▁ exists ▁ with ▁ given ▁ sum " ) ; ? >
inputs: "Check if a given array is sorted in Spiral manner or not | Java program to implement the above approach ; Function to check if the array is spirally sorted or not ; Stores start index of the array ; Stores end index of an array ; If arr [ start ] greater than arr [ end ] ; Update start ; If arr [ end ] greater than arr [ start ] ; Update end ; Driver code ; Function Call"
Solution in Java:
targets: import java . util . * ; class GFG { static boolean isSpiralSorted ( int [ ] arr , int n ) { int start = 0 ; int end = n - 1 ; while ( start < end ) { if ( arr [ start ] > arr [ end ] ) { return false ; } start ++ ; if ( arr [ end ] > arr [ start ] ) { return false ; } end -- ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 14 , 20 , 18 , 12 , 5 } ; int N = arr . length ; if ( isSpiralSorted ( arr , N ) != false ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
inputs: "Bitwise AND of all the odd numbers from 1 to N | C ++ implementation of the approach ; Function to return the bitwise AND of all the odd integers from the range [ 1 , n ] ; Driver code"
Solution in C++:
targets: #include <iostream>
using namespace std ; int bitwiseAndOdd ( int n ) { return 1 ; } int main ( ) { int n = 10 ; cout << bitwiseAndOdd ( n ) ; return 0 ; }
inputs: Find the minimum element in a sorted and rotated array | C program to find minimum element in a sorted and rotated array ; This condition is needed to handle the case when array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like { 3 , 4 , 5 , 1 , 2 } ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver program to test above functions
How can the above be solved in C?
targets: #include <stdio.h>
int findMin ( int arr [ ] , int low , int high ) { if ( high < low ) return arr [ 0 ] ; if ( high == low ) return arr [ low ] ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return arr [ mid + 1 ] ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return arr [ mid ] ; if ( arr [ high ] > arr [ mid ] ) return findMin ( arr , low , mid - 1 ) ; return findMin ( arr , mid + 1 , high ) ; } int main ( ) { int arr1 [ ] = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr1 , 0 , n1 - 1 ) ) ; int arr2 [ ] = { 1 , 2 , 3 , 4 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr2 , 0 , n2 - 1 ) ) ; int arr3 [ ] = { 1 } ; int n3 = sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr3 , 0 , n3 - 1 ) ) ; int arr4 [ ] = { 1 , 2 } ; int n4 = sizeof ( arr4 ) / sizeof ( arr4 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr4 , 0 , n4 - 1 ) ) ; int arr5 [ ] = { 2 , 1 } ; int n5 = sizeof ( arr5 ) / sizeof ( arr5 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr5 , 0 , n5 - 1 ) ) ; int arr6 [ ] = { 5 , 6 , 7 , 1 , 2 , 3 , 4 } ; int n6 = sizeof ( arr6 ) / sizeof ( arr6 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr6 , 0 , n6 - 1 ) ) ; int arr7 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n7 = sizeof ( arr7 ) / sizeof ( arr7 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr7 , 0 , n7 - 1 ) ) ; int arr8 [ ] = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 } ; int n8 = sizeof ( arr8 ) / sizeof ( arr8 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr8 , 0 , n8 - 1 ) ) ; int arr9 [ ] = { 3 , 4 , 5 , 1 , 2 } ; int n9 = sizeof ( arr9 ) / sizeof ( arr9 [ 0 ] ) ; printf ( " The ▁ minimum ▁ element ▁ is ▁ % d 
 " , findMin ( arr9 , 0 , n9 - 1 ) ) ; return 0 ; }
inputs: "Split a number as sum of K numbers which are not divisible by K | Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code"
Solution in Python:
targets: def printKParts ( N , K ) :
	if ( N % K == 0 ) :
		for i in range ( 1 , K ) :
			print ( "1 , ▁ " ) ;
		print ( N - ( K - 1 ) , end = " " ) ;
	else :
		if ( K == 2 ) :
			print ( " Not ▁ Possible " , end = " " ) ;
			return ;
	for i in range ( 1 , K - 1 ) :
		print ( 1 , end = " , ▁ " ) ;
	print ( 2 , " , ▁ " , ( N - K ) , end = " " ) ;
if __name__ == ' _ _ main _ _ ' :
	N = 18 ;
	K = 5 ;
	printKParts ( N , K ) ;
inputs: "Queries to find longest subsequence having no similar adjacent elements with updates | C # program for the above approach ; Traverse the array arr [ ] ; If previous element is not same as current element ; Traverse the queries ; Replace element at index x with y ; Recalculate for index x ; Subtract contribution of element at index x ; Add contribution of y ; Recalculate for index x + 1 ; Subtract contribution of element at index x + 1 ; Adds contribution of y ; Replace the element ; Driver Code ; Function Call"
Solution in C#:
targets: using System ; class GFG { static void longestSubsequence ( int N , int Q , int [ ] arr , int [ , ] Queries ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count += 1 ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = Queries [ i , 0 ] ; int y = Queries [ i , 1 ] ; if ( x > 1 ) { if ( arr [ x - 1 ] != arr [ x - 2 ] ) { count -= 1 ; } if ( arr [ x - 2 ] != y ) { count += 1 ; } } if ( x < N ) { if ( arr [ x ] != arr [ x - 1 ] ) { count -= 1 ; } if ( y != arr [ x ] ) { count += 1 ; } } Console . Write ( count + " ▁ " ) ; arr [ x - 1 ] = y ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 5 , 2 } ; int N = arr . Length ; int Q = 2 ; int [ , ] Queries = { { 1 , 3 } , { 4 , 2 } } ; longestSubsequence ( N , Q , arr , Queries ) ; } }
inputs: "Divide a number into two parts such that sum of digits is maximum | Java implementation of above approach Returns sum of digits of x ; Returns sum of digits of x ; Returns closest number to x in terms of 9 's. ; Driver code"
Solution in Java:
targets: import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int sumOfDigitsSingle ( int x ) { int ans = 0 ; while ( x != 0 ) { ans += x % 10 ; x /= 10 ; } return ans ; } static int closest ( int x ) { int ans = 0 ; while ( ans * 10 + 9 <= x ) ans = ans * 10 + 9 ; return ans ; } static int sumOfDigitsTwoParts ( int N ) { int A = closest ( N ) ; return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) ; } public static void main ( String args [ ] ) { int N = 35 ; System . out . print ( sumOfDigitsTwoParts ( N ) ) ; } }
inputs: "Sum of even values and update queries on an array | Function to return the sum of even elements after updating value at given index ; Add given value to A [ index ] ; To store the sum of even elements ; If current element is even ; Function to print the result for every query ; Resultant vector that stores the result for every query ; Get sum of even elements after updating value at given index ; Store sum for each query ; Print the result for every query ; Driver code"
How can the above be solved in JS?
targets: function EvenSum ( A , index , value ) { A [ index ] = A [ index ] + value ; var sum = 0 ; for ( var i = 0 ; i < A . length ; i ++ ) if ( A [ i ] % 2 == 0 ) sum = sum + A [ i ] ; return sum ; } function BalanceArray ( A , Q ) { var ANS = [ ] ; var i , sum ; for ( i = 0 ; i < Q . length ; i ++ ) { var index = Q [ i ] [ 0 ] ; var value = Q [ i ] [ 1 ] ; sum = EvenSum ( A , index , value ) ; ANS . push ( sum ) ; } for ( i = 0 ; i < ANS . length ; i ++ ) document . write ( ANS [ i ] + " " ) ; } var A = [ 1 , 2 , 3 , 4 ] ; var Q = [ [ 0 , 1 ] , [ 1 , - 3 ] , [ 0 , - 4 ] , [ 3 , 2 ] ] ; BalanceArray ( A , Q ) ;
inputs: "Pandigital number in a given base | Return true if n is pandigit else return false . ; Checking length is less than base ; Traversing each digit of the number . ; If digit is integer ; If digit is alphabet ; Checking hash array , if any index is unmarked . ; Driver Program"
How can the above be solved in PHP?
targets: < ? php function checkPandigital ( $ b , $ n ) { if ( strlen ( $ n ) < $ b ) return 0 ; $ hash = array ( ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) $ hash [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ n ) ; $ i ++ ) { if ( $ n [ $ i ] >= '0' && $ n [ $ i ] <= '9' ) $ hash [ $ n [ $ i ] - '0' ] = 1 ; else if ( ord ( $ n [ $ i ] ) - ord ( ' A ' ) <= $ b - 11 ) $ hash [ ord ( $ n [ $ i ] ) - ord ( ' A ' ) + 10 ] = 1 ; } for ( $ i = 0 ; $ i < $ b ; $ i ++ ) if ( $ hash [ $ i ] == 0 ) return 0 ; return 1 ; } $ b = 13 ; $ n = "1298450376ABC " ; if ( checkPandigital ( $ b , $ n ) ) echo " Yes " ; else echo " No " ; ? >
inputs: Sum of divisors of factorial of a number | C program to find sum of proper divisor of factorial of a number ; function to calculate factorial ; function to calculate sum of divisor ; Returns sum of divisors of n ! ; driver program
How can the above be solved in C?
targets: #include <stdio.h>
int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } int div ( int x ) { int ans = 0 ; for ( int i = 1 ; i <= x ; i ++ ) if ( x % i == 0 ) ans += i ; return ans ; } int sumFactDiv ( int n ) { return div ( fact ( n ) ) ; } int main ( ) { int n = 4 ; printf ( " % d " , sumFactDiv ( n ) ) ; }
inputs: "Sum of all odd length palindromic numbers within the range [ L , R ] | Function that returns true if the given number is a palindrome ; Here we are generating a new number ( reverse_num ) by reversing the digits of original input number ; If the original input number ( num ) is equal to its reverse ( reverse_num ) then its palindrome else it is not . ; Function that returns true if the given number is of odd length ; Function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; Driver code"
Solution in php:
targets: < ? php function isPalindrome ( $ num ) { $ reverse_num = 0 ; $ remainder ; $ temp ; $ temp = $ num ; while ( $ temp != 0 ) { $ remainder = $ temp % 10 ; $ reverse_num = $ reverse_num * 10 + $ remainder ; $ temp = ( int ) ( $ temp / 10 ) ; } if ( $ reverse_num == $ num ) { return true ; } return false ; } function isOddLength ( $ num ) { $ count = 0 ; while ( $ num > 0 ) { $ num = ( int ) ( $ num / 10 ) ; $ count ++ ; } if ( $ count % 2 != 0 ) { return true ; } return false ; } function sumOfAllPalindrome ( $ L , $ R ) { $ sum = 0 ; if ( $ L <= $ R ) for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( isPalindrome ( $ i ) && isOddLength ( $ i ) ) { $ sum += $ i ; } } return $ sum ; } $ L = 110 ; $ R = 1130 ; echo sumOfAllPalindrome ( $ L , $ R ) ; ? >
inputs: "Print all possible rotations of a given Array | C ++ program to print all possible rotations of the given array ; Global declaration of array ; Function to reverse array between indices s and e ; Function to generate all possible rotations of array ; Driver code"
Solution in C++:
targets: #include <iostream>
using namespace std ; int arr [ 10000 ] ; void reverse ( int arr [ ] , int s , int e ) { while ( s < e ) { int tem = arr [ s ] ; arr [ s ] = arr [ e ] ; arr [ e ] = tem ; s = s + 1 ; e = e - 1 ; } } void fun ( int arr [ ] , int k ) { int n = 4 - 1 ; int v = n - k ; if ( v >= 0 ) { reverse ( arr , 0 , v ) ; reverse ( arr , v + 1 , n ) ; reverse ( arr , 0 , n ) ; } } int main ( ) { arr [ 0 ] = 1 ; arr [ 1 ] = 2 ; arr [ 2 ] = 3 ; arr [ 3 ] = 4 ; for ( int i = 0 ; i < 4 ; i ++ ) { fun ( arr , i ) ; cout << ( " [ " ) ; for ( int j = 0 ; j < 4 ; j ++ ) { cout << ( arr [ j ] ) << " , ▁ " ; } cout << ( " ] " ) ; } }
inputs: "Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver program to test above functions"
Solution in php:
targets: < ? php function cutRod ( $ price , $ n ) { $ val = array ( ) ; $ val [ 0 ] = 0 ; $ i ; $ j ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ max_val = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ max_val = max ( $ max_val , $ price [ $ j ] + $ val [ $ i - $ j - 1 ] ) ; $ val [ $ i ] = $ max_val ; } return $ val [ $ n ] ; } $ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " , cutRod ( $ arr , $ size ) ; ? >
inputs: Icosikaioctagon or Icosioctagon Number | C program for above approach ; Finding the nth icosikaioctagonal Number ; Driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
#include <stdlib.h>
int icosikaioctagonalNum ( int n ) { return ( 26 * n * n - 24 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ % d " , icosikaioctagonalNum ( n ) ) ; return 0 ; }
inputs: "Find the number of pairs such that their gcd is equals to 1 | Java program to find the number of pairs such that gcd equals to 1 ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Function to find the number of pairs such that gcd equals to 1 ; To store maximum number ; To store frequency of each number ; Find frequency and maximum number ; To store number of pairs with gcd equals to 1 ; Traverse through the all possible elements ; Return the number of pairs ; Driver code ; Function call"
Solution in Java:
targets: class GFG { static int N = 100050 ; static int [ ] lpf = new int [ N ] ; static int [ ] mobius = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] == 0 ) lpf [ j ] = i ; } static void Mobius ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } } static int gcd_pairs ( int a [ ] , int n ) { int maxi = 0 ; int [ ] fre = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { fre [ a [ i ] ] ++ ; maxi = Math . max ( a [ i ] , maxi ) ; } least_prime_factor ( ) ; Mobius ( ) ; int ans = 0 ; for ( int i = 1 ; i <= maxi ; i ++ ) { if ( mobius [ i ] == 0 ) continue ; int temp = 0 ; for ( int j = i ; j <= maxi ; j += i ) temp += fre [ j ] ; ans += temp * ( temp - 1 ) / 2 * mobius [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; System . out . print ( gcd_pairs ( a , n ) ) ; } }
inputs: "Count pairs from two sorted arrays whose sum is equal to a given value x | C # implementation to count pairs from both sorted arrays whose sum is equal to a given value ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; generating pairs from both the arrays ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int countPairs ( int [ ] arr1 , int [ ] arr2 , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } public static void Main ( ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int [ ] arr2 = { 2 , 3 , 5 , 8 } ; int m = arr1 . Length ; int n = arr2 . Length ; int x = 10 ; Console . WriteLine ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
inputs: "Program to find height of a Trapezoid | C ++ program for the above approach ; Function to calculate height of the trapezoid ; Apply Heron 's formula ; Calculate the area ; Calculate height of trapezoid ; Print the height ; Driver Code ; Given a , b , p1 and p2"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void findHeight ( float p1 , float p2 , float b , float c ) { float a = max ( p1 , p2 ) - min ( p1 , p2 ) ; float s = ( a + b + c ) / 2 ; float area = sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; float height = ( area * 2 ) / a ; cout << " Height ▁ is : ▁ " << height ; } int main ( ) { float p1 = 25 , p2 = 10 ; float a = 14 , b = 13 ; findHeight ( p1 , p2 , a , b ) ; return 0 ; }
inputs: "Check if the number is balanced | C ++ program to check if a number is Balanced or not ; Function to check whether N is Balanced Number or not ; Calculating the Leftsum and rightSum simultaneously ; Typecasting each character to integer and adding the digit to respective sums ; Driver Code ; Function call"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void BalancedNumber ( string s ) { int Leftsum = 0 ; int Rightsum = 0 ; for ( int i = 0 ; i < s . size ( ) / 2 ; i ++ ) { Leftsum += int ( s [ i ] - '0' ) ; Rightsum += int ( s [ s . size ( ) - 1 - i ] - '0' ) ; } if ( Leftsum == Rightsum ) cout << " Balanced " << endl ; else cout << " Not ▁ Balanced " << endl ; } int main ( ) { string s = "12321" ; BalancedNumber ( s ) ; return 0 ; }
inputs: "Puzzle | Program to find number of squares in a chessboard | Function to return count of squares ; ; A better way to write n * ( n + 1 ) * ( 2 n + 1 ) / 6 ; Driver Code"
Solution in C:
targets: function countSquares ( n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } let n = 4 ; document . write ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( n ) ) ;
inputs: "Check whether the given number is Euclid Number or not | C # program to check Euclid Number ; Function to get the prime numbers ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; store all prime numbers to vector ' arr ' ; Function to check the number for Euclid Number ; Multiply next prime number and check if product + 1 = n holds or not ; Driver code ; Get the prime numbers ; Get n ; Check if n is Euclid Number ; Get n ; Check if n is Euclid Number"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 10000 ; static List < int > arr = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] prime = new bool [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( prime [ p ] ) arr . Add ( p ) ; } static bool isEuclid ( long n ) { long product = 1 ; int i = 0 ; while ( product < n ) { product = product * arr [ i ] ; if ( product + 1 == n ) return true ; i ++ ; } return false ; } public static void Main ( String [ ] args ) { SieveOfEratosthenes ( ) ; long n = 31 ; if ( isEuclid ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; n = 42 ; if ( isEuclid ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
inputs: "Check if string is right to left diagonal or not | Java program to check if the given string is right to left diagonal or not ; Function to check if the given string 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 Java?
targets: import java . io . * ; class GFG { public static boolean is_rtol ( String s ) { int tmp = ( int ) ( Math . sqrt ( s . length ( ) ) ) - 1 ; char first = s . charAt ( tmp ) ; for ( int pos = tmp ; pos < s . length ( ) - 1 ; pos += tmp ) { if ( s . charAt ( pos ) != first ) { return false ; } } return true ; } public static void main ( String args [ ] ) { String str = " abcxabxcaxbcxabc " ; if ( is_rtol ( str ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
inputs: Pell Number | Iterative Pell Number Series in C ; calculate nth pell number ; driver function
How can the above be solved in C?
targets: #include <stdio.h>
int pell ( int n ) { if ( n <= 2 ) return n ; int a = 1 ; int b = 2 ; int c , i ; for ( i = 3 ; i <= n ; i ++ ) { c = 2 * b + a ; a = b ; b = c ; } return b ; } int main ( ) { int n = 4 ; printf ( " % d " , pell ( n ) ) ; return 0 ; }
inputs: "How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code"
Solution in Python:
targets: def isHeap ( arr , i , n ) :
	if i >= int ( ( n - 2 ) / 2 ) :
		return True
	if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) :
		return True
	return False
if __name__ == ' _ _ main _ _ ' :
	arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ]
	n = len ( arr ) - 1
	if isHeap ( arr , 0 , n ) :
		print ( " Yes " )
	else :
		print ( " No " )
inputs: "Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time |  ; Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Subtract 1 from every element so that the elements become in range from 0 to n - 1 ; Use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; To print counts , simply print the number of times n was added at index corresponding to every element ; Driver program to test above functions"
Solution in C#:
targets: using System ; internal class CountFrequency { internal virtual void printfrequency ( int [ ] arr , int n ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ j ] = arr [ j ] - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] = arr [ arr [ i ] % n ] + n ; } for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + 1 + " - > " + arr [ i ] / n ) ; } } public static void Main ( string [ ] args ) { CountFrequency count = new CountFrequency ( ) ; int [ ] arr = new int [ ] { 2 , 3 , 3 , 2 , 5 } ; int n = arr . Length ; count . printfrequency ( arr , n ) ; } }
inputs: "Count of integers of length N and value less than K such that they contain digits only from the given set | C # implementation of the approach ; Function to convert a number into vector ; Push all the digits of N from the end one by one to the vector ; If the original number was 0 ; Reverse the vector elements ; Return the required vector ; Function to return the count of B length integers which are less than C and they contain digits from set A [ ] only ; Convert number to digit array ; Case 1 : No such number possible as the generated numbers will always be greater than C ; Case 2 : All integers of length B are valid as they all are less than C ; contain 0 ; Case 3 ; Update the lower [ ] array such that lower [ i ] stores the count of elements in A [ ] which are less than i ; For first index we can 't use 0 ; Whether ( i - 1 ) digit of generated number can be equal to ( i - 1 ) digit of C ; Is digit [ i - 1 ] present in A ? ; Driver code"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { static int MAX = 10 ; static List < int > numToVec ( int N ) { List < int > digit = new List < int > ( ) ; while ( N != 0 ) { digit . Add ( N % 10 ) ; N = N / 10 ; } if ( digit . Count == 0 ) digit . Add ( 0 ) ; digit . Reverse ( ) ; return digit ; } static int solve ( List < int > A , int B , int C ) { List < int > digit = new List < int > ( ) ; int d , d2 ; digit = numToVec ( C ) ; d = A . Count ; if ( B > digit . Count d == 0 ) return 0 ; else if ( B < digit . Count ) { if ( A [ 0 ] == 0 && B != 1 ) return ( int ) ( ( d - 1 ) * Math . Pow ( d , B - 1 ) ) ; else return ( int ) Math . Pow ( d , B ) ; } else { int [ ] dp = new int [ B + 1 ] ; int [ ] lower = new int [ MAX + 1 ] ; for ( int i = 0 ; i < d ; i ++ ) lower [ A [ i ] + 1 ] = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) lower [ i ] = lower [ i - 1 ] + lower [ i ] ; Boolean flag = true ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= B ; i ++ ) { d2 = lower [ digit [ i - 1 ] ] ; dp [ i ] = dp [ i - 1 ] * d ; if ( i == 1 && A [ 0 ] == 0 && B != 1 ) d2 = d2 - 1 ; if ( flag ) dp [ i ] += d2 ; flag = ( flag & ( lower [ digit [ i - 1 ] + 1 ] == lower [ digit [ i - 1 ] ] + 1 ) ) ; } return dp [ B ] ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 5 } ; List < int > A = new List < int > ( arr ) ; int N = 2 ; int k = 21 ; Console . WriteLine ( solve ( A , N , k ) ) ; } }
inputs: "Split first N natural numbers into two sets with minimum absolute difference of their sums | Function to split the first N natural numbers into two sets having minimum absolute difference of their sums ; Stores the sum of elements of set1 ; Stores the sum of elements of set2 ; Traverse first N natural numbers ; Check if sum of elements of set1 is less than or equal to sum of elements of set2 ; Driver code"
Solution in Javascript:
targets: function minAbsDiff ( N ) { var sumSet1 = 0 ; var sumSet2 = 0 ; for ( i = N ; i > 0 ; i -- ) { if ( sumSet1 <= sumSet2 ) { sumSet1 += i ; } else { sumSet2 += i ; } } return Math . abs ( sumSet1 - sumSet2 ) ; } var N = 6 ; document . write ( minAbsDiff ( N ) ) ;
inputs: "Bitwise AND of sub | C ++ implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; No need to perform more AND operations as | k - X | will increase ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int closetAND ( int arr [ ] , int n , int k ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = min ( ans , abs ( k - X ) ) ; if ( X <= k ) break ; } } return ans ; } int main ( ) { int arr [ ] = { 4 , 7 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << closetAND ( arr , n , k ) ; return 0 ; }
inputs: "Distinct adjacent elements in a binary array | Python3 implementation of the above approach ; if array has only one element , return 1 ; For first element compare with only next element ; For remaining elements compare with both prev and next elements ; For last element compare with only prev element ; Driver code"
Solution in Python:
targets: def distinct ( arr ) :
	count = 0
	if len ( arr ) == 1 :
		return 1
	for i in range ( 0 , len ( arr ) - 1 ) :
		if ( i == 0 ) :
			if ( arr [ i ] != arr [ i + 1 ] ) :
				count += 1
		elif ( i > 0 & i < len ( arr ) - 1 ) :
			if ( arr [ i ] != arr [ i + 1 ] or arr [ i ] != arr [ i - 1 ] ) :
				count += 1
	if ( arr [ len ( arr ) - 1 ] != arr [ len ( arr ) - 2 ] ) :
		count += 1
	return count
arr = [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ]
print ( distinct ( arr ) )
inputs: Calculate 7 n / 8 without using division and multiplication operators | C program to evaluate 7 n / 8 without using * and / ; Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n * Step 2 ) Divide result by 8 ; Driver program to test above function
How can the above be solved in C?
targets: #include <stdio.h>
int multiplyBySevenByEight ( unsigned int n ) { return ( ( n << 3 ) - n ) >> 3 ; } int main ( ) { unsigned int n = 15 ; printf ( " % u " , multiplyBySevenByEight ( n ) ) ; return 0 ; }
inputs: "Count of subarrays having sum equal to its length | Set 2 | Java program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Store count of elements upto current element with length i ; Stores the final count of subarray ; Stores the prefix sum ; If size of subarray is 1 ; Iterate the array ; Find the sum ; Update frequency in map ; Print the total count ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call"
Solution in Java:
targets: import java . util . * ; class GFG { static void countOfSubarray ( int arr [ ] , int N ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int answer = 0 ; int sum = 0 ; if ( mp . get ( 1 ) != null ) mp . put ( 1 , mp . get ( 1 ) + 1 ) ; else mp . put ( 1 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( mp . get ( sum - i ) != null ) answer += mp . get ( sum - i ) ; if ( mp . get ( sum - i ) != null ) mp . put ( sum - i , mp . get ( sum - i ) + 1 ) ; else mp . put ( sum - i , 1 ) ; } System . out . print ( answer ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 2 , 1 , 2 , - 2 , 2 , 4 } ; int N = arr . length ; countOfSubarray ( arr , N ) ; } }
inputs: "Seating arrangement of N boys sitting around a round table such that two particular boys sit together | C # implementation of the approach ; Function to return the total count of ways ; Find ( n - 1 ) factorial ; Return ( n - 1 ) ! * 2 ! ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int Total_Ways ( int n ) { int fac = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } static public void Main ( ) { int n = 5 ; Console . Write ( Total_Ways ( n ) ) ; } }
inputs: "Case | Function to return the sorted string ; Vectors to store the lowercase and uppercase characters ; Sort both the vectors ; If current character is lowercase then pick the lowercase character from the sorted list ; Else pick the uppercase character ; Return the sorted string ; Driver code"
How can the above be solved in JS?
targets: function getSortedString ( s , n ) { var v1 = [ ] ; var v2 = [ ] ; var i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) v1 . push ( s [ i ] ) ; if ( s [ i ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) v2 . push ( s [ i ] ) ; } console . log ( v1 ) ; v1 . sort ( ) ; v2 . sort ( ) ; var j = 0 ; i = 0 ; for ( var k = 0 ; k < n ; k ++ ) { if ( s [ k ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ k ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) { s [ k ] = v1 [ i ] ; ++ i ; } else if ( s [ k ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ k ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) { s [ k ] = v2 [ j ] ; ++ j ; } } return s . join ( " " ) ; } var s = " " ; var n = s . length ; document . write ( getSortedString ( s . split ( " " ) , n ) ) ;