Muennighoff/xP3x-sample huggingface dataset:
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"
Solution 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: "Program to find whether a no is power of two |  ; Function to check if x is power of 2 ; Driver program to test above function"
Solution in C:
targets: #include <stdio.h>
#include <stdbool.h>
bool isPowerOfTwo ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 2 != 0 ) return 0 ; n = n / 2 ; } return 1 ; } int main ( ) { isPowerOfTwo ( 31 ) ? printf ( " Yes 
 " ) : printf ( " No 
 " ) ; isPowerOfTwo ( 64 ) ? printf ( " Yes 
 " ) : printf ( " No 
 " ) ; return 0 ; }
inputs: "Find sum of divisors of all the divisors of a natural number | Python3 program to find sum of divisors of all the divisors of a natural number . ; Returns sum of divisors of all the divisors of n ; Calculating powers of prime factors and storing them in a map mp [ ] . ; If n is a prime number ; For each prime factor , calculating ( p ^ ( a + 1 ) - 1 ) / ( p - 1 ) and adding it to answer . ; Driver Code"
How can the above be solved in Python?
targets: import math as mt
def sumDivisorsOfDivisors ( n ) :
	mp = dict ( )
	for j in range ( 2 , mt . ceil ( mt . sqrt ( n ) ) ) :
		count = 0
		while ( n % j == 0 ) :
			n //= j
			count += 1
		if ( count ) :
			mp [ j ] = count
	if ( n != 1 ) :
		mp [ n ] = 1
	ans = 1
	for it in mp :
		pw = 1
		summ = 0
		for i in range ( mp [ it ] + 1 , 0 , - 1 ) :
			summ += ( i * pw )
			pw *= it
		ans *= summ
	return ans
n = 10
print ( sumDivisorsOfDivisors ( n ) )
inputs: "Search an element in a Doubly Linked List | C # program to implement the above approach ; Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always null ; Link the old list to the new node ; If pointer to head is not null ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < -> 15 < -> 8 < -> 9 < -> 14"
Solution in C#:
targets: using System ; class GFG { public class Node { public int data ; public Node next ; public Node prev ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = head_ref ; if ( head_ref != null ) { head_ref . prev = new_node ; } head_ref = new_node ; return head_ref ; } static int search ( Node head_ref , int x ) { Node temp = head_ref ; int pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } public static void Main ( String [ ] args ) { Node head = null ; int X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; Console . Write ( search ( head , X ) ) ; } }
inputs: "Difference between sums of odd level and even level nodes of a Binary Tree | CPP program to find difference between sums of odd level and even level nodes of binary tree ; tree node ; returns a new tree Node ; return difference of sums of odd level and even level ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if level no . is even or odd and accordingly update the evenSum or oddSum ; check for left child ; check for right child ; driver program ; construct a tree"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int evenOddLevelDifference ( Node * root ) { if ( ! root ) return 0 ; queue < Node * > q ; q . push ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( level % 2 == 0 ) evenSum += temp -> data ; else oddSum += temp -> data ; if ( temp -> left ) { q . push ( temp -> left ) ; } if ( temp -> right ) { q . push ( temp -> right ) ; } size -= 1 ; } } return ( oddSum - evenSum ) ; } int main ( ) { Node * root = newNode ( 5 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 4 ) ; root -> left -> right -> left = newNode ( 3 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 7 ) ; int result = evenOddLevelDifference ( root ) ; cout << " diffence ▁ between ▁ sums ▁ is ▁ : : ▁ " ; cout << result << endl ; return 0 ; }
inputs: "Binomial Coefficient | DP | C # Code for Dynamic Programming | Set 9 ( Binomial Coefficient ) ; Returns value of Binomial Coefficient C ( n , k ) ; Base Cases ; Recur ; Driver program to test above function"
Solution in C#:
targets: using System ; class GFG { static int binomialCoeff ( int n , int k ) { if ( k > n ) return 0 ; if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } public static void Main ( ) { int n = 5 , k = 2 ; Console . Write ( " Value ▁ of ▁ C ( " + n + " , " + k + " ) ▁ is ▁ " + binomialCoeff ( n , k ) ) ; } }
inputs: "Minimum swaps of same | C # program to implement the above approach ; Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , print 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , print - 1 ; Driver code ; Function Call"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void minimumSwaps ( int [ ] arr1 , int [ ] arr2 , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { Console . Write ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } Console . Write ( flag ) ; return ; } Console . Write ( - 1 ) ; } public static void Main ( ) { int [ ] arr1 = { 11 , 14 , 20 , 2 } ; int [ ] arr2 = { 5 , 9 , 6 , 3 } ; int N = arr1 . Length ; minimumSwaps ( arr1 , arr2 , N ) ; } }
inputs: "Program to print ASCII Value of all digits of a given number | C ++ program to convert the digits of a number to its ASCII values ; Function to convert digits of N to respective ASCII values ; Driver Code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int convertToASCII ( int N ) { string num = to_string ( N ) ; for ( char ch : num ) { cout << ch << " ▁ ( " << ( int ) ch << " ) 
 " ; } } int main ( ) { int N = 36 ; convertToASCII ( N ) ; return 0 ; }
inputs: "Check if product of digits of a number at even and odd places is equal | Java implementation of the approach ; To store the respective product ; Converting integer to String ; Traversing the String ; Driver code"
Solution in Java:
targets: import java . util . * ; class GFG { static void getResult ( int n ) { int proOdd = 1 ; int proEven = 1 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) if ( i % 2 == 0 ) proOdd = proOdd * ( num . charAt ( i ) - '0' ) ; else proEven = proEven * ( num . charAt ( i ) - '0' ) ; if ( proOdd == proEven ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int n = 4324 ; getResult ( n ) ; } }
inputs: "Find if a point lies inside a Circle | PHP program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver Code"
Solution in php:
targets: < ? php function isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) { if ( ( $ x - $ circle_x ) * ( $ x - $ circle_x ) + ( $ y - $ circle_y ) * ( $ y - $ circle_y ) <= $ rad * $ rad ) return true ; else return false ; } $ x = 1 ; $ y = 1 ; $ circle_x = 0 ; $ circle_y = 1 ; $ rad = 2 ; if ( isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) ) echo " Inside " ; else echo " Outside " ; ? >
inputs: "Print Longest Palindromic Subsequence | CPP program to print longest palindromic subsequence ; Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a string length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; Driver program to test above function"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; string lcs ( string & X , string & Y ) { int m = X . length ( ) ; int n = Y . length ( ) ; int L [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } int index = L [ m ] [ n ] ; string lcs ( index + 1 , ' \0' ) ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) i -- ; else j -- ; } return lcs ; } string longestPalSubseq ( string & str ) { string rev = str ; reverse ( rev . begin ( ) , rev . end ( ) ) ; return lcs ( str , rev ) ; } int main ( ) { string str = " GEEKSFORGEEKS " ; cout << longestPalSubseq ( str ) ; return 0 ; }
inputs: "Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Given matrix ; Size of matrix ; Function call"
How can the above be solved in JS?
targets: let r , c ; let dir = [ [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , 1 ] , [ 1 , - 1 ] ] ; function valid ( i , j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return true ; return false ; } function find ( i , j , v ) { let s = 0 ; for ( let k = 0 ; k < 8 ; k ++ ) { let ni = i + dir [ k ] [ 0 ] , nj = j + dir [ k ] [ 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni ] [ nj ] ; } return s ; } function findsumofneighbors ( M ) { let v = new Array ( r ) ; for ( var i = 0 ; i < v . length ; i ++ ) { v [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { v [ i ] [ j ] = find ( i , j , M ) ; document . write ( v [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let M = [ [ 1 , 4 , 1 ] , [ 2 , 4 , 5 ] , [ 3 , 1 , 2 ] ] ; r = M . length ; c = M [ 0 ] . length ; findsumofneighbors ( M ) ;
inputs: "Maximum time such that absolute difference between hour and minute lies in given range | C ++ program for the above approach ; Function checks whether given time is correct ; To check minute value of time ; To check hour value of time ; Changes in value is not allowed at position where ' ? ' is not present ; Function checks whether the absolute difference between hour and minute value is within [ L , R ] ; Checks if the difference is outside the give range ; Displays time in proper 24 - hour format ; Function find the desired value of time whose difference lies in the range [ L , R ] ; Decrease hour value from 23 to 0 ; Check if the hour value is valid if not valid then no need to change minute value , since time will still remain in valid , to check hour value flag is set to 1. ; Decrease minute value from 59 to 0 ; Check if the minute value is valid , if not valid then skip the current iteration , to check ' minute ' value flag is set to 0. ; Driver code ; Input time ; Difference range"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool isValid ( char a1 , char a2 , string str , int flag ) { char v1 , v2 ; if ( flag == 0 ) { v1 = str [ 4 ] ; v2 = str [ 3 ] ; } else { v1 = str [ 1 ] ; v2 = str [ 0 ] ; } if ( v1 != a1 && v1 != ' ? ' ) return false ; if ( v2 != a2 && v2 != ' ? ' ) return false ; return true ; } bool inRange ( int hh , int mm , int L , int R ) { int a = abs ( hh - mm ) ; if ( a < L a > R ) return false ; return true ; } void displayTime ( int hh , int mm ) { if ( hh > 10 ) cout << hh << " : " ; else if ( hh < 10 ) cout << "0" << hh << " : " ; if ( mm > 10 ) cout << mm << endl ; else if ( mm < 10 ) cout << "0" << mm << endl ; } void maximumTimeWithDifferenceInRange ( string str , int L , int R ) { int i , j ; int h1 , h2 , m1 , m2 ; for ( i = 23 ; i >= 0 ; i -- ) { h1 = i % 10 ; h2 = i / 10 ; if ( ! isValid ( h1 + '0' , h2 + '0' , str , 1 ) ) { continue ; } for ( j = 59 ; j >= 0 ; j -- ) { m1 = j % 10 ; m2 = j / 10 ; if ( ! isValid ( m1 + '0' , m2 + '0' , str , 0 ) ) { continue ; } if ( inRange ( i , j , L , R ) ) { displayTime ( i , j ) ; return ; } } } if ( inRange ( i , j , L , R ) ) displayTime ( i , j ) ; else cout << " - 1" << endl ; } int main ( ) { string timeValue = " ? ? : ? ? " ; int L = 20 , R = 39 ; maximumTimeWithDifferenceInRange ( timeValue , L , R ) ; return 0 ; }
inputs: "Find a triplet ( A , B , C ) such that 3 * A + 5 * B + 7 * C is equal to N | Function to find a triplet ( A , B , C ) such that 3 * A + 5 * B + 7 * C is N ; Iterate over the range [ 0 , N7 ] ; Iterate over the range [ 0 , N5 ] ; Find the value of A ; If A is greater than or equal to 0 and divisible by 3 ; Otherwise , print - 1 ; Driver Code"
Solution in Python:
targets: def CalculateValues ( N ) :
	for C in range ( 0 , N // 7 + 1 ) :
		for B in range ( 0 , N // 5 + 1 ) :
			A = N - 7 * C - 5 * B
			if ( A >= 0 and A % 3 == 0 ) :
				print ( " A ▁ = " , A / 3 , " , ▁ B ▁ = " , B , " , ▁ \ ▁ C ▁ = " , C , sep = " ▁ " )
				return
	print ( - 1 )
	return
if __name__ == ' _ _ main _ _ ' :
	N = 19
	CalculateValues ( 19 )
inputs: "Minimum count of increment of K size subarrays required to form a given Array | Java implementation to find the minimum number of operations required to change an array of all zeros such that every element is greater than the given array ; Function to find the minimum number of operations required to change all the array of zeros such that every element is greater than the given array ; Declaring the difference array of size N ; Number of operations ; First update the D [ i ] value with the previous value ; The index i has to be incremented ; We have to perform ( b [ i ] - d [ i ] ) operations more ; Increment the range i to i + k by need ; Check if i + k is valid index ; Driver Code ; Function Call"
How can the above be solved in Java?
targets: class GFG { static void find_minimum_operations ( int n , int b [ ] , int k ) { int d [ ] = new int [ n + 1 ] ; int i , operations = 0 , need ; for ( i = 0 ; i < n ; i ++ ) { if ( i > 0 ) { d [ i ] += d [ i - 1 ] ; } if ( b [ i ] > d [ i ] ) { operations += b [ i ] - d [ i ] ; need = b [ i ] - d [ i ] ; d [ i ] += need ; if ( i + k <= n ) { d [ i + k ] -= need ; } } } System . out . println ( operations ) ; } public static void main ( String [ ] args ) { int n = 5 ; int b [ ] = { 1 , 2 , 3 , 4 , 5 } ; int k = 2 ; find_minimum_operations ( n , b , k ) ; } }
inputs: "Minimum LCM and GCD possible among all possible sub | Function to return minimum GCD among all subarrays ; Minimum GCD among all sub - arrays will be the GCD of all the elements of the array ; Function to return minimum LCM among all subarrays ; Minimum LCM among all sub - arrays will be the minimum element from the array ; Driver code"
How can the above be solved in PHP?
targets: < ? php function __gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return __gcd ( $ b % $ a , $ a ) ; } function minGCD ( $ arr , $ n ) { $ minGCD = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ minGCD = __gcd ( $ minGCD , $ arr [ $ i ] ) ; return $ minGCD ; } function minLCM ( $ arr , $ n ) { $ minLCM = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minLCM = min ( $ minLCM , $ arr [ $ i ] ) ; return $ minLCM ; } $ arr = array ( 2 , 66 , 14 , 521 ) ; $ n = sizeof ( $ arr ) ; echo " LCM = " ▁ . ▁ minLCM ( $ arr , ▁ $ n ) ▁ . ▁ " , " echo " GCD ▁ = ▁ " . minGCD ( $ arr , $ n ) ; ? >
inputs: "Search , insert and delete in a sorted array | C program to implement insert operation in an sorted array . ; Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver program to test above function ; Inserting key"
Solution in C:
targets: #include <stdio.h>
int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; } int main ( ) { int arr [ 20 ] = { 12 , 16 , 20 , 40 , 50 , 70 } ; int capacity = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int n = 6 ; int i , key = 26 ; printf ( " Before Insertion : " for ( i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; n = insertSorted ( arr , n , key , capacity ) ; printf ( " After Insertion : " for ( i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; return 0 ; }
inputs: "Average of odd numbers till a given odd number | Program to find average of odd numbers till a given odd number . ; Function to calculate the average of odd numbers ; driver function"
Solution in C:
targets: #include <stdio.h>
int averageOdd ( int n ) { if ( n % 2 == 0 ) { printf ( " Invalid ▁ Input " ) ; return -1 ; } return ( n + 1 ) / 2 ; } int main ( ) { int n = 15 ; printf ( " % d " , averageOdd ( n ) ) ; 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 C-Sharp?
targets: using System ; 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 ) ; } static public void Main ( ) { int n = 5 ; Console . WriteLine ( CountWays ( n ) ) ; } }
inputs: "Remove minimum number of characters so that two strings become anagram | Python 3 program to find minimum number of characters to be removed to make two strings anagram . ; function to calculate minimum numbers of characters to be removed to make two strings anagram ; make hash array for both string and calculate frequency of each character ; count frequency of each character in first string ; count frequency of each character in second string ; traverse count arrays to find number of characters to be removed ; Driver program to run the case"
How can the above be solved in Python?
targets: CHARS = 26
def remAnagram ( str1 , str2 ) :
	count1 = [ 0 ] * CHARS
	count2 = [ 0 ] * CHARS
	i = 0
	while i < len ( str1 ) :
		count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1
		i += 1
	i = 0
	while i < len ( str2 ) :
		count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1
		i += 1
	result = 0
	for i in range ( 26 ) :
		result += abs ( count1 [ i ] - count2 [ i ] )
	return result
if __name__ == " _ _ main _ _ " :
	str1 = " bcadeh "
	str2 = " hea "
	print ( remAnagram ( str1 , str2 ) )
inputs: Check if array contains contiguous integers with duplicates allowed | Sorting based C ++ implementation to check whether the array contains a set of contiguous integers ; function to check whether the array contains a set of contiguous integers ; Sort the array ; After sorting , check if current element is either same as previous or is one more . ; Driver program to test above
How can the above be solved in C?
targets: #include <bits/stdc++.h>
using namespace std ; bool areElementsContiguous ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] > 1 ) return false ; return true ; } int main ( ) { int arr [ ] = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( areElementsContiguous ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
inputs: "Smallest of three integers without comparison operators | JavaScript program to find Smallest of three integers without comparison operators ; Driver Code"
How can the above be solved in JS?
targets: function smallest ( x , y , z ) { let c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } let x = 12 , y = 15 , z = 5 ; document . write ( " " + smallest ( x , y , z ) ) ;
inputs: "Alternate Odd and Even Nodes in a Singly Linked List | Java program to rearrange nodes as alternate odd even nodes in a Singly Linked List ; class node ; A utility function to print linked list ; Function to create newNode in a linkedlist ; Function to insert at beginning ; Function to rearrange the odd and even nodes ; Odd Value in Even Position Add pointer to current node in odd stack ; Even Value in Odd Position Add pointer to current node in even stack ; Swap Data at the top of two stacks ; Driver code"
Solution in Java:
targets: import java . util . * ; class GFG { static class Node { int data ; Node next ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } System . out . println ( ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node insertBeg ( Node head , int val ) { Node temp = newNode ( val ) ; temp . next = head ; head = temp ; return head ; } static void rearrangeOddEven ( Node head ) { Stack < Node > odd = new Stack < Node > ( ) ; Stack < Node > even = new Stack < Node > ( ) ; int i = 1 ; while ( head != null ) { if ( head . data % 2 != 0 && i % 2 == 0 ) { odd . push ( head ) ; } else if ( head . data % 2 == 0 && i % 2 != 0 ) { even . push ( head ) ; } head = head . next ; i ++ ; } while ( odd . size ( ) > 0 && even . size ( ) > 0 ) { int k = odd . peek ( ) . data ; odd . peek ( ) . data = even . peek ( ) . data ; even . peek ( ) . data = k ; odd . pop ( ) ; even . pop ( ) ; } } public static void main ( String args [ ] ) { Node head = newNode ( 8 ) ; head = insertBeg ( head , 7 ) ; head = insertBeg ( head , 6 ) ; head = insertBeg ( head , 5 ) ; head = insertBeg ( head , 3 ) ; head = insertBeg ( head , 2 ) ; head = insertBeg ( head , 1 ) ; System . out . println ( " Linked ▁ List : " ) ; printList ( head ) ; rearrangeOddEven ( head ) ; System . out . println ( " Linked ▁ List ▁ after ▁ " + " Rearranging : " ) ; printList ( head ) ; } }
inputs: "Smallest power of 2 greater than or equal to n | C program to find smallest power of 2 greater than or equal to n ; First n in the below condition is for the case where n is 0 ; Driver Code"
Solution in C:
targets: #include <stdio.h>
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } int main ( ) { unsigned int n = 0 ; printf ( " % d " , nextPowerOf2 ( n ) ) ; return 0 ; }
inputs: "Check if count of divisors is even or odd | Function to count the divisors ; Initialize count of divisors ; Note that this loop runs till square root ; If divisors are equal increment count by one Otherwise increment count by 2 ; Driver Code"
Solution in php:
targets: < ? php function countDivisors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) + 1 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ count += ( $ n / $ i == $ i ) ? 1 : 2 ; } if ( $ count % 2 == 0 ) echo " Even 
 " ; else echo " Odd 
 " ; } echo " The ▁ count ▁ of ▁ divisor : ▁ " ; countDivisors ( 10 ) ; ? >
inputs: "Program to find whether a given number is power of 2 | Java program of the above approach ; Function to check if x is power of 2 ; Driver code"
How can the above be solved in Java?
targets: import java . io . * ; class GFG { static boolean isPowerofTwo ( int n ) { if ( n == 0 ) return false ; if ( ( n & ( ~ ( n - 1 ) ) ) == n ) return true ; return false ; } public static void main ( String [ ] args ) { if ( isPowerofTwo ( 30 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isPowerofTwo ( 128 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
inputs: "Count Odd and Even numbers in a range from L to R | Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code"
How can the above be solved in PHP?
targets: < ? php function countOdd ( $ L , $ R ) { $ N = ( $ R - $ L ) / 2 ; if ( $ R % 2 != 0 $ L % 2 != 0 ) $ N ++ ; return $ N ; } $ L = 3 ; $ R = 7 ; $ odds = countOdd ( $ L , $ R ) ; $ evens = ( $ R - $ L + 1 ) - $ odds ; echo " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " . $ odds . " 
 " ; echo " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " . $ evens ; ? >
inputs: "Count set bits in an integer |  ; Check each bit in a number is set or not and return the total count of the set bits . ; ( 1 << i ) = pow ( 2 , i ) ; Driver Code"
Solution in C:
targets: #include <stdio.h>
int countSetBits ( int N ) { int count = 0 ; for ( int i = 0 ; i < sizeof ( int ) * 8 ; i ++ ) { if ( N & ( 1 << i ) ) count ++ ; } return count ; } int main ( ) { int N = 15 ; printf ( " % d " , countSetBits ( N ) ) ; return 0 ; }
inputs: "Minimum value of " max ▁ + ▁ min " in a subarray | PHP program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code"
Solution in php:
targets: < ? php function maxSum ( $ arr , $ n ) { if ( $ n < 2 ) return -1 ; $ ans = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ i = 1 ; $ i + 1 < $ n ; $ i ++ ) $ ans = min ( $ ans , ( $ arr [ $ i ] + $ arr [ $ i + 1 ] ) ) ; return $ ans ; } $ arr = array ( 1 , 12 , 2 , 2 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >
inputs: "Shortest path with exactly k edges in a directed and weighted graph | C ++ program to find shortest path with exactly k edges ; Define number of vertices in the graph and inifinite value ; A naive recursive function to count walks from u to v with k edges ; Base cases ; Initialize result ; Go to all adjacents of u and recur ; driver program to test above function ; Let us create the graph shown in above diagram"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; #define V  4
#define INF  INT_MAX
int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 0 ; if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ; if ( k <= 0 ) return INF ; int res = INF ; for ( int i = 0 ; i < V ; i ++ ) { if ( graph [ u ] [ i ] != INF && u != i && v != i ) { int rec_res = shortestPath ( graph , i , v , k - 1 ) ; if ( rec_res != INF ) res = min ( res , graph [ u ] [ i ] + rec_res ) ; } } return res ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 10 , 3 , 2 } , { INF , 0 , INF , 7 } , { INF , INF , 0 , 6 } , { INF , INF , INF , 0 } } ; int u = 0 , v = 3 , k = 2 ; cout << " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is ▁ " << shortestPath ( graph , u , v , k ) ; return 0 ; }
inputs: "Count ways to express a number as sum of consecutive numbers | Utility method to compute number of ways in which N can be represented as sum of consecutive number ; constraint on values of L gives us the time Complexity as O ( N ^ 0.5 ) ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function countConsecutive ( $ N ) { $ count = 0 ; for ( $ L = 1 ; $ L * ( $ L + 1 ) < 2 * $ N ; $ L ++ ) { $ a = ( int ) ( 1.0 * $ N - ( $ L * ( int ) ( $ L + 1 ) ) / 2 ) / ( $ L + 1 ) ; if ( $ a - ( int ) $ a == 0.0 ) $ count ++ ; } return $ count ; } $ N = 15 ; echo countConsecutive ( $ N ) , " 
 " ; $ N = 10 ; echo countConsecutive ( $ N ) , " 
 " ; ? >
inputs: "Program to construct DFA for Regular Expression C ( A + B ) + | Function to find whether the given is Accepted by the DFA ; If n <= 1 , then prNo ; To count the matched characters ; Check if the first character is C ; Traverse the rest of string ; If character is A or B , increment count by 1 ; If the first character is not C , pr - 1 ; If all characters matches ; Driver Code"
How can the above be solved in Python?
targets: def DFA ( str , N ) :
	if ( N <= 1 ) :
		print ( " No " )
		return
	count = 0
	if ( str [ 0 ] == ' C ' ) :
		count += 1
		for i in range ( 1 , N ) :
			if ( str [ i ] == ' A ' or str [ i ] == ' B ' ) :
				count += 1
			else :
				break
	else :
		print ( " No " )
		return
	if ( count == N ) :
		print ( " Yes " )
	else :
		print ( " No " )
if __name__ == ' _ _ main _ _ ' :
	str = " CAABBAAB "
	N = len ( str )
	DFA ( str , N )
inputs: "Binomial Coefficient | DP |  ; function to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; base case ; better time complexity for lesser r value ; array of elements from n - r + 1 to n ; for numbers from 1 to r find arr [ j ] such that gcd ( i , arr [ j ] ) > 1 ; if gcd > 1 , divide both by gcd ; if i becomes 1 , no need to search arr ; for ( int i : arr ) single pass to multiply the numerator ; Driver code"
How can the above be solved in Java?
targets: import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int nCr ( int n , int r ) { if ( r > n ) return 0 ; if ( r > n - r ) C ( n , r ) = C ( n , n - r ) r = n - r ; int mod = 1000000007 ; int [ ] arr = new int [ r ] ; for ( int i = n - r + 1 ; i <= n ; i ++ ) { arr [ i + r - n - 1 ] = i ; } long ans = 1 ; for ( int k = 1 ; k < r + 1 ; k ++ ) { int j = 0 , i = k ; while ( j < arr . length ) { int x = gcd ( i , arr [ j ] ) ; if ( x > 1 ) { arr [ j ] /= x ; i /= x ; } if ( i == 1 ) break ; j += 1 ; } } ans = ( ans * i ) % mod ; return ( int ) ans ; } public static void main ( String [ ] args ) { int n = 5 , r = 2 ; System . out . print ( " Value ▁ of ▁ C ( " + n + " , ▁ " + r + " ) ▁ is ▁ " + nCr ( n , r ) + "
"); } }
inputs: "Smallest integer greater than n such that it consists of digit m exactly k times | Java implementation of the approach ; Function that returns true if n contains digit m exactly k times ; Function to return the smallest integer > n with digit m occurring exactly k times ; Driver code"
How can the above be solved in Java?
targets: class GFG { static boolean digitWell ( int n , int m , int k ) { int cnt = 0 ; while ( n > 0 ) { if ( n % 10 == m ) ++ cnt ; n /= 10 ; } return cnt == k ; } static int findInt ( int n , int m , int k ) { int i = n + 1 ; while ( true ) { if ( digitWell ( i , m , k ) ) return i ; i ++ ; } } public static void main ( String [ ] args ) { int n = 111 , m = 2 , k = 2 ; System . out . println ( findInt ( n , m , k ) ) ; } }
inputs: "Flatten a multilevel linked list |"
Solution in C:
targets: struct List { int data ; struct List * next ; struct List * child ; } ;
inputs: "Find maximum height to cut all chocolates horizontally such that at least K amount remains | Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code"
How can the above be solved in JS?
targets: function cal ( arr , mid ) { let chocolate = 0 for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] >= mid ) chocolate += arr [ i ] - mid } return chocolate } function maximumCut ( arr , K ) { let low = 0 let high = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } while ( low <= high ) { mid = Math . floor ( ( low + high ) / 2 ) ; chocolate = cal ( arr , mid ) if ( chocolate == K ) { return mid } else if ( chocolate < K ) { high = mid - 1 } else { low = mid + 1 if ( mid > high ) high = mid } } return high } let N = 4 ; let K = 7 ; let arr = [ 15 , 20 , 8 , 17 ] ; document . write ( maximumCut ( arr , K ) )
inputs: "Area of a circle inscribed in a rectangle which is inscribed in a semicircle | Function to find the area of the circle ; radius cannot be negative ; area of the circle ; Driver code"
Solution in php:
targets: < ? php function area ( $ r ) { if ( $ r < 0 ) return -1 ; $ area = 3.14 * pow ( $ r / ( 2 * sqrt ( 2 ) ) , 2 ) ; return $ area ; } $ a = 5 ; echo area ( $ a ) ;
inputs: "Find the player to reach at least N by multiplying with any value from given range | Java program for the above approach ; Function to find the winner ; Backtrack from N to 1 ; Driver Code"
Solution in Java:
targets: import java . util . * ; class GFG { static char Winner ( int N ) { boolean player = true ; while ( N > 1 ) { int den = ( player ) ? 9 : 2 ; int X = N / den ; int Y = N % den ; N = ( Y > 0 ) ? X + 1 : X ; player = ! player ; } if ( player ) return ' B ' ; else return ' A ' ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( Winner ( N ) ) ; } }
inputs: "Program to find whether a given number is power of 2 | Function to check if x is power of 2 ; Driver code"
How can the above be solved in JS?
targets: function isPowerofTwo ( n ) { if ( n == 0 ) return false ; if ( ( n & ( ~ ( n - 1 ) ) ) == n ) return true ; return false ; } if ( isPowerofTwo ( 30 ) == true ) document . write ( " " ) ; else document . write ( " " ) ; if ( isPowerofTwo ( 128 ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
inputs: Largest Independent Set Problem | DP | A naive recursive implementation of Largest Independent Set problem ; A utility function to find max of two integers ; A binary tree node has data , pointer to left child and a pointer to right child ; The function returns size of the largest independent set in a given binary tree ; Calculate size excluding the current node ; Calculate size including the current node ; Return the maximum of two sizes ; A utility function to create a node ; Driver program to test above functions ; Let us construct the tree given in the above diagram
How can the above be solved in C?
targets: #include <stdio.h>
#include <stdlib.h>
int max ( int x , int y ) { return ( x > y ) ? x : y ; } struct node { int data ; struct node * left , * right ; } ; int LISS ( struct node * root ) { if ( root == NULL ) return 0 ; int size_excl = LISS ( root -> left ) + LISS ( root -> right ) ; int size_incl = 1 ; if ( root -> left ) size_incl += LISS ( root -> left -> left ) + LISS ( root -> left -> right ) ; if ( root -> right ) size_incl += LISS ( root -> right -> left ) + LISS ( root -> right -> right ) ; return max ( size_incl , size_excl ) ; } struct node * newNode ( int data ) { struct node * temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { struct node * root = newNode ( 20 ) ; root -> left = newNode ( 8 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 12 ) ; root -> left -> right -> left = newNode ( 10 ) ; root -> left -> right -> right = newNode ( 14 ) ; root -> right = newNode ( 22 ) ; root -> right -> right = newNode ( 25 ) ; printf ( " Size ▁ of ▁ the ▁ Largest ▁ Independent ▁ Set ▁ is ▁ % d ▁ " , LISS ( root ) ) ; return 0 ; }
inputs: "Reverse words in a given string | C ++ program to reverse a string s = input ( )"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int main ( ) { string s [ ] = { " i " , " like " , " this " , " program " , " very " , " much " } ; string ans = " " ; for ( int i = 5 ; i >= 0 ; i -- ) { ans += s [ i ] + " ▁ " ; } cout << ( " Reversed ▁ String : " ) << endl ; cout << ( ans . substr ( 0 , ans . length ( ) - 1 ) ) << endl ; return 0 ; }
inputs: "Smallest power of 2 greater than or equal to n | PHP program to find smallest power of 2 greater than or equal to n ; First n in the below condition is for the case where n is 0 ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function nextPowerOf2 ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; } $ n = 0 ; echo ( nextPowerOf2 ( $ n ) ) ; ? >
inputs: "Program to find Nth term of series 1 , 6 , 17 , 34 , 56 , 86 , 121 , 162 , ... ... . | calculate Nth term of series ; Driver code"
Solution in Python:
targets: def nthTerm ( n ) :
	return 3 * pow ( n , 2 ) - 4 * n + 2
N = 4
print ( nthTerm ( N ) )
inputs: Average of odd numbers till a given odd number | Program to find average of odd numbers till a given odd number . ; Function to calculate the average of odd numbers ; driver function
How can the above be solved in C?
targets: #include <stdio.h>
int averageOdd ( int n ) { if ( n % 2 == 0 ) { printf ( " Invalid ▁ Input " ) ; return -1 ; } return ( n + 1 ) / 2 ; } int main ( ) { int n = 15 ; printf ( " % d " , averageOdd ( n ) ) ; return 0 ; }
inputs: "Insertion Sort | Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print an array of size n ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function insertionSort ( & $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ; $ j = $ i - 1 ; while ( $ j >= 0 && $ arr [ $ j ] > $ key ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; } $ arr [ $ j + 1 ] = $ key ; } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " 
 " ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSort ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >
inputs: "Program to duplicate Vowels in String | C ++ program for printing string with duplicate vowels ; Function to check for the Vowel ; Function to get the resultant string with vowels duplicated ; Another string to store the resultant string ; Loop to check for each character ; Driver Code ; Print the original string ; Print the resultant string"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool isVowel ( char ch ) { ch = toupper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } string duplicateVowels ( string str ) { int t = str . length ( ) ; string res = " " ; for ( int i = 0 ; i < t ; i ++ ) { if ( isVowel ( str [ i ] ) ) { res += str [ i ] ; } res += str [ i ] ; } return res ; } int main ( ) { string str = " helloworld " ; cout << " Original ▁ String : ▁ " << str << endl ; string res = duplicateVowels ( str ) ; cout << " String ▁ with ▁ Vowels ▁ duplicated : ▁ " << res << endl ; }
inputs: "Search , insert and delete in a sorted array | C program to implement delete operation in a sorted array ; To search a ley to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver code"
Solution in C:
targets: #include <stdio.h>
int binarySearch ( int arr [ ] , int low , int high , int key ) ; int binarySearch ( int arr [ ] , int low , int high , int key ) { if ( high < low ) return -1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } int deleteElement ( int arr [ ] , int n , int key ) { int pos = binarySearch ( arr , 0 , n - 1 , key ) ; if ( pos == -1 ) { printf ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } int main ( ) { int i ; int arr [ ] = { 10 , 20 , 30 , 40 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int key = 30 ; printf ( " Array ▁ before ▁ deletion 
 " ) ; for ( i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; n = deleteElement ( arr , n , key ) ; printf ( " Array after deletion " for ( i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; }
inputs: "Number of n digit stepping numbers | Space optimized solution | Java program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ j ] stores count of i digit stepping numbers ending with digit j . ; To store result of length i - 1 before updating dp [ j ] for length i . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; Driver code"
Solution in Java:
targets: class GFG { static long answer ( int n ) { int [ ] dp = new int [ 10 ] ; int [ ] prev = new int [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { prev [ j ] = dp [ j ] ; } for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ j ] = prev [ j + 1 ] ; else if ( j == 9 ) dp [ j ] = prev [ j - 1 ] ; else dp [ j ] = prev [ j - 1 ] + prev [ j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ j ] ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( answer ( n ) ) ; } }
inputs: "Given an n x n square matrix , find sum of all sub | An efficient Java program to find sum of all subsquares of size k x k ; Size of given matrix ; A O ( n ^ 2 ) function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; 1 : PREPROCESSING To store sums of all strips of size k x 1 ; Go column by column ; Calculate sum of first k x 1 rectangle in this column ; Calculate sum of remaining rectangles ; 2 : CALCULATE SUM of Sub - Squares using stripSum [ ] [ ] ; Calculate and print sum of first subsquare in this row ; Calculate sum of remaining squares in current row by removing the leftmost strip of previous sub - square and adding a new strip ; Driver program to test above function"
How can the above be solved in Java?
targets: import java . io . * ; class GFG { static int n = 5 ; static void printSumTricky ( int mat [ ] [ ] , int k ) { if ( k > n ) return ; int stripSum [ ] [ ] = new int [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum += mat [ i ] [ j ] ; stripSum [ 0 ] [ j ] = sum ; for ( int i = 1 ; i < n - k + 1 ; i ++ ) { sum += ( mat [ i + k - 1 ] [ j ] - mat [ i - 1 ] [ j ] ) ; stripSum [ i ] [ j ] = sum ; } } for ( int i = 0 ; i < n - k + 1 ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) sum += stripSum [ i ] [ j ] ; System . out . print ( sum + " ▁ " ) ; for ( int j = 1 ; j < n - k + 1 ; j ++ ) { sum += ( stripSum [ i ] [ j + k - 1 ] - stripSum [ i ] [ j - 1 ] ) ; System . out . print ( sum + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 , 4 } , { 5 , 5 , 5 , 5 , 5 } , } ; int k = 3 ; printSumTricky ( mat , k ) ; } }
inputs: "Largest gap in an array | A C program to find largest gap between two elements in an array . ; function to solve the given problem ; finding maximum and minimum of an array ; Driver Code"
Solution in C:
targets: #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int solve ( int a [ ] , int n ) { int min1 = a [ 0 ] ; int max1 = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max1 ) max1 = a [ i ] ; if ( a [ i ] < min1 ) min1 = a [ i ] ; } return abs ( min1 - max1 ) ; } int main ( ) { int arr [ ] = { -1 , 2 , 3 , 4 , -10 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Largest ▁ gap ▁ is ▁ : ▁ % d " , solve ( arr , size ) ) ; return 0 ; }
inputs: "Maximum sum from three arrays such that picking elements consecutively from same is not allowed | PHP implementation of the approach ; Function to return the maximum sum ; Base case ; Already visited ; If the element has been taken from first array in previous step ; If the element has been taken from second array in previous step ; If the element has been taken from third array in previous step ; Driver code ; Pick element from first array ; Pick element from second array ; Pick element from third array ; Print the maximum of them"
Solution in php:
targets: < ? php $ N = 3 ; function FindMaximumSum ( $ ind , $ kon , $ a , $ b , $ c , $ n , $ dp ) { global $ N ; if ( $ ind == $ n ) return 0 ; if ( $ dp [ $ ind ] [ $ kon ] != -1 ) return $ dp [ $ ind ] [ $ kon ] ; $ ans = -1e9 + 5 ; if ( $ kon == 0 ) { $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; } else if ( $ kon == 1 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; } else if ( $ kon == 2 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; } return $ dp [ $ ind ] [ $ kon ] = $ ans ; } $ a = array ( 6 , 8 , 2 , 7 , 4 , 2 , 7 ) ; $ b = array ( 7 , 8 , 5 , 8 , 6 , 3 , 5 ) ; $ c = array ( 8 , 3 , 2 , 6 , 8 , 4 , 1 ) ; $ n = count ( $ a ) ; $ dp = array_fill ( 0 , $ n , array_fill ( 0 , $ N , -1 ) ) ; $ x = FindMaximumSum ( 0 , 0 , $ a , $ b , $ c , $ n , $ dp ) ; $ y = FindMaximumSum ( 0 , 1 , $ a , $ b , $ c , $ n , $ dp ) ; $ z = FindMaximumSum ( 0 , 2 , $ a , $ b , $ c , $ n , $ dp ) ; print ( max ( $ x , max ( $ y , $ z ) ) ) ; ? >
inputs: "Pair formation such that maximum pair sum is minimized | Python 3 Program to divide the array into N pairs such that maximum pair is minimized ; After Sorting Maintain two variables i and j pointing to start and end of array Such that smallest element of array pairs with largest element ; Driver Code"
How can the above be solved in Python?
targets: def findOptimalPairs ( arr , N ) :
	arr . sort ( reverse = False )
	i = 0
	j = N - 1
	while ( i <= j ) :
		print ( " ( " , arr [ i ] , " , " , arr [ j ] , " ) " , end = " ▁ " )
		i += 1
		j -= 1
if __name__ == ' _ _ main _ _ ' :
	arr = [ 9 , 6 , 5 , 1 ]
	N = len ( arr )
	findOptimalPairs ( arr , N )