Muennighoff/xP3x-sample huggingface dataset:
inputs: "Find a point that lies inside exactly K given squares | PHP implementation of the approach ; Driver Code"
Solution in php:
targets: < ? php function PointInKSquares ( $ n , $ a , $ k ) { sort ( $ a ) ; return $ a [ $ n - $ k ] ; } $ k = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ a ) ; $ x = PointInKSquares ( $ n , $ a , $ k ) ; echo " ( " . $ x . " , " ▁ . ▁ $ x ▁ . ▁ " ) " ; ? >
inputs: "Count numbers from 1 to n that have 4 as a digit | C ++ program to count numbers having 4 as a digit ; Function to count numbers from 1 to n that have 4 as a digit ; Base case ; d = number of digits minus one in n . For 328 , d is 2 ; computing count of numbers from 1 to 10 ^ d - 1 , d = 0 a [ 0 ] = 0 ; d = 1 a [ 1 ] = count of numbers from 0 to 9 = 1 d = 2 a [ 2 ] = count of numbers from 0 to 99 = a [ 1 ] * 9 + 10 = 19 d = 3 a [ 3 ] = count of numbers from 0 to 999 = a [ 2 ] * 19 + 100 = 171 ; Computing 10 ^ d ; Most significant digit ( msd ) of n , For 328 , msd is 3 which can be obtained using 328 / 100 ; If MSD is 4. For example if n = 428 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 2 ) Count of numbers from 400 to 428 which is 29. ; IF MSD > 4. For example if n is 728 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 and count of numbers from 500 to 699 , i . e . , " a [ 2 ] ▁ * ▁ 6" 2 ) Count of numbers from 400 to 499 , i . e . 100 3 ) Count of numbers from 700 to 728 , recur for 28 ; IF MSD < 4. For example if n is 328 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 299 a 2 ) Count of numbers from 300 to 328 , recur for 28 ; Driver Program"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int countNumbersWith4 ( int n ) { if ( n < 4 ) return 0 ; int d = log10 ( n ) ; int * a = new int [ d + 1 ] ; a [ 0 ] = 0 , a [ 1 ] = 1 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 9 + ceil ( pow ( 10 , i - 1 ) ) ; int p = ceil ( pow ( 10 , d ) ) ; int msd = n / p ; if ( msd == 4 ) return ( msd ) * a [ d ] + ( n % p ) + 1 ; if ( msd > 4 ) return ( msd - 1 ) * a [ d ] + p + countNumbersWith4 ( n % p ) ; return ( msd ) * a [ d ] + countNumbersWith4 ( n % p ) ; } int main ( ) { int n = 328 ; cout << " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " << n << " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " << countNumbersWith4 ( n ) << endl ; return 0 ; }
inputs: "Find the diagonal of the Cube | Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call"
How can the above be solved in JS?
targets: function diagonal_length ( a ) { let L ; L = a * Math . sqrt ( 3 ) ; return L ; } let a = 5 ; document . write ( diagonal_length ( a ) . toFixed ( 5 ) ) ;
inputs: "Pell Number | Iterative Pell Number Series in C # ; calculate nth pell number ; Driver function"
How can the above be solved in C-Sharp?
targets: using System ; class PellNumber { public static int pell ( int n ) { if ( n <= 2 ) return n ; int a = 1 ; int b = 2 ; int c ; for ( int i = 3 ; i <= n ; i ++ ) { c = 2 * b + a ; a = b ; b = c ; } return b ; } public static void Main ( ) { int n = 4 ; Console . Write ( pell ( n ) ) ; } }
inputs: "Find the day number in the current year for the given date | Javascript implementation of the approach ; Function to return the day number of the year for the given date ; Extract the year , month and the day from the date string ; If current year is a leap year and the date given is after the 28 th of February then it must include the 29 th February ; Add the days in the previous months ; Driver code"
Solution in Javascript:
targets: var days = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ; function dayOfYear ( date ) { var year = parseInt ( date . substring ( 0 , 4 ) ) ; var month = parseInt ( date . substring ( 5 , 6 ) ) ; var day = parseInt ( date . substring ( 8 ) ) ; if ( month > 2 && year % 4 == 0 && ( year % 100 != 0 year % 400 == 0 ) ) { ++ day ; } while ( month -- > 0 ) { day = day + days [ month - 1 ] ; } return day ; } var date = " " ; document . write ( dayOfYear ( date ) ) ;
inputs: "How to print maximum number of A 's using given four keys | A recursive Java program to print maximum number of A 's using  following four keys  ; A recursive function that returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; Initialize result ; TRY ALL POSSIBLE BREAK - POINTS For any keystroke N , we need to loop from N - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have Ctrl - A , Ctrl - C and then only Ctrl - V all the way . ; If the breakpoint is s at b 'th  keystroke then the optimal string  would have length  (n-b-1)*screen[b-1]; ; Driver program ; for the rest of the array we will rely on the previous entries to compute new ones"
Solution in Java:
targets: import java . io . * ; class GFG { static int findoptimal ( int N ) { if ( N <= 6 ) return N ; int max = 0 ; int b ; for ( b = N - 3 ; b >= 1 ; b -- ) { int curr = ( N - b - 1 ) * findoptimal ( b ) ; if ( curr > max ) max = curr ; } return max ; } public static void main ( String [ ] args ) { int N ; for ( N = 1 ; N <= 20 ; N ++ ) System . out . println ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with ▁ keystrokes ▁ is ▁ " + N + findoptimal ( N ) ) ; } }
inputs: "Area of largest Circle that can be inscribed in a SemiCircle | Function to find the area of the circle ; Radius cannot be negative ; Area of the largest circle ; Driver code"
Solution in Javascript:
targets: function circlearea ( R ) { if ( R < 0 ) return - 1 ; var a = 3.14 * R * R / 4 ; return a ; } var R = 2 ; document . write ( circlearea ( R ) ) ;
inputs: "Sum of bit differences for numbers from 0 to N | Function to implement fast exponentiation ; Function to return the value for powers of 2 ; Function to convert N into binary ; To store binary representation ; Iterate each digit of n ; Return binary representation ; Function to find difference in bits ; Get binary representation ; total number of bit differences from 0 to N ; Iterate over each binary bit ; If current bit is '1' then add the count of current bit ; Given Number ; Function Call"
How can the above be solved in JS?
targets: function binpow ( a , b ) { let res = 1 ; while ( b ) { if ( b & 1 ) res = res * a ; a = a * a ; b = Math . floor ( b / 2 ) ; } return res ; } function find ( x ) { if ( x == 0 ) return 0 ; let p = Math . log2 ( x ) ; return binpow ( 2 , p + 1 ) - 1 ; } function getBinary ( n ) { let ans = " " ; while ( n ) { let dig = n % 2 ; ans += String ( dig ) ; n = Math . floor ( n / 2 ) ; } return ans ; } function totalCountDifference ( n ) { let ans = getBinary ( n ) ; let req = 0 ; for ( let i = 0 ; i < ans . length ; i ++ ) { if ( ans [ i ] == ' ' ) { req += find ( binpow ( 2 , i ) ) ; } } return req ; } let N = 5 ; document . write ( totalCountDifference ( N ) ) ;
inputs: "Find politeness of a number | A function to count all odd prime factors of a given number n ; Eliminate all even prime factor of number of n ; n must be odd at this point , so iterate for only odd numbers till sqrt ( n ) ; if i divides n , then start counting of Odd divisors ; If n odd prime still remains then count it ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function countOddPrimeFactors ( $ n ) { $ result = 1 ; while ( $ n % 2 == 0 ) $ n /= 2 ; for ( $ i = 3 ; $ i * $ i <= $ n ; $ i += 2 ) { $ divCount = 0 ; while ( $ n % $ i == 0 ) { $ n /= $ i ; ++ $ divCount ; } $ result *= $ divCount + 1 ; } if ( $ n > 2 ) $ result *= 2 ; return $ result ; } function politness ( $ n ) { return countOddPrimeFactors ( $ n ) - 1 ; } $ n = 90 ; echo " Politness ▁ of ▁ " , $ n , " ▁ = ▁ " , politness ( $ n ) , " 
 " ; $ n = 15 ; echo " Politness ▁ of ▁ " , $ n , " ▁ = ▁ " , politness ( $ n ) , " 
 " ; ? >
inputs: "Count n digit numbers not having a particular digit | PHP Implementation of above method ; Finding number of possible number with n digits excluding a particular digit ; Checking if number of digits is zero ; Checking if number of digits is one ; Checking if number of digits is odd ; Calling digitNumber function with ( digit - 1 ) / 2 digits ; ; Calling digitNumber function with n / 2 digits ; Calling digitNumber function Checking if excluding digit is zero or non - zero ; Initializing variables"
How can the above be solved in PHP?
targets: < ? php $ mod = 1000000007 ; function digitNumber ( $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; if ( $ n == 1 ) return 9 ; if ( $ n % 2 != 0 ) { $ temp = digitNumber ( ( $ n - 1 ) / 2 ) % $ mod ; return ( 9 * ( $ temp * $ temp ) % $ mod ) % $ mod ; } else { $ temp = digitNumber ( $ n / 2 ) % $ mod ; return ( $ temp * $ temp ) % $ mod ; } } function countExcluding ( $ n , $ d ) { global $ mod ; if ( $ d == 0 ) return ( 9 * digitNumber ( $ n - 1 ) ) % $ mod ; else return ( 8 * digitNumber ( $ n - 1 ) ) % $ mod ; } $ d = 9 ; $ n = 3 ; print ( countExcluding ( $ n , $ d ) ) ; ? >
inputs: "Find original numbers from gcd ( ) every pair | Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; Reverse array ; Driver code"
Solution in Javascript:
targets: function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function findNumbers ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; reverse ( arr ) ; let freq = new Array ( arr [ 0 ] + 1 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } let size = parseInt ( Math . sqrt ( n ) , 10 ) ; let brr = new Array ( size ) ; brr . fill ( 0 ) ; let x , l = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] ] > 0 && l < size ) { brr [ l ] = arr [ i ] ; freq [ brr [ l ] ] -- ; l ++ ; for ( let j = 0 ; j < l ; j ++ ) { if ( i != j ) { x = __gcd ( arr [ i ] , brr [ j ] ) ; freq [ x ] -= 2 ; } } } } printArr ( brr , size ) ; } function reverse ( input ) { let last = input . length - 1 ; let middle = parseInt ( input . length / 2 , 10 ) ; for ( let i = 0 ; i <= middle ; i ++ ) { let temp = input [ i ] ; input [ i ] = input [ last - i ] ; input [ last - i ] = temp ; } } function __gcd ( a , b ) { if ( b == 0 ) { return a ; } return __gcd ( b , a % b ) ; } let arr = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 ] ; let n = arr . length ; findNumbers ( arr , n ) ;
inputs: "Maximum sum of pairs that are at least K distance apart in an array | Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code"
Solution in Javascript:
targets: function getMaxPairSum ( arr , N , K ) { var preMax = Array ( N ) ; preMax [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . max ( preMax [ i - 1 ] , arr [ i ] ) ; } var res = - 1000000000 ; for ( var i = K ; i < N ; i ++ ) { res = Math . max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } var arr = [ 1 , 2 , 4 , 8 , 6 , 3 ] ; var K = 3 ; var N = arr . length ; document . write ( getMaxPairSum ( arr , N , K ) ) ;
inputs: "Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code"
Solution in php:
targets: < ? php function finalPos ( $ command , $ n , $ x , $ y ) { $ cup ; $ cdown ; $ cleft ; $ cright ; $ final_x ; $ final_y ; $ cup = $ cdown = $ cleft = $ cright = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ command [ $ i ] == ' U ' ) $ cup ++ ; else if ( $ command [ $ i ] == ' D ' ) $ cdown ++ ; else if ( $ command [ $ i ] == ' L ' ) $ cleft ++ ; else if ( $ command [ $ i ] == ' R ' ) $ cright ++ ; } $ final_x = $ x + ( $ cright - $ cleft ) ; $ final_y = $ y + ( $ cdown - $ cup ) ; echo " Final ▁ Position : ▁ " . " ( " . $ final_x . " , ▁ " . $ final_y . " ) " ; } $ command = " DDLRULL " ; $ n = strlen ( $ command ) ; $ x = 3 ; $ y = 4 ; finalPos ( $ command , $ n , $ x , $ y ) ;
inputs: "Construct two N | C # program for the above approach ; Satisfying the given conditions ; Declare the two arrays A and B ; Iterate from range [ 1 , 2 * n ] ; Assign consecutive numbers to same indices of the two arrays ; Print the first array ; Print the second array , B ; Driver Code ; Function Call"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { static void printArrays ( int n ) { List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { if ( i % 2 == 0 ) A . Add ( i ) ; else B . Add ( i ) ; } Console . Write ( " { ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( A [ i ] ) ; if ( i != n - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " ▁ } 
 " ) ; Console . Write ( " { ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( B [ i ] ) ; if ( i != n - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " ▁ } " ) ; } public static void Main ( ) { int N = 5 ; printArrays ( N ) ; } }
inputs: "Detect loop in a linked list | Java program to detect loop in a linked list ; head of list ; Linked list Node ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Driver program to test above functions ; Start with the empty list ; Create loop for testing"
Solution in Java:
targets: class LinkedList { Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } void detectLoop ( ) { Node slow_p = head , fast_p = head ; int flag = 0 ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( " Loop ▁ found " ) ; else System . out . println ( " Loop ▁ not ▁ found " ) ; } public static void main ( String args [ ] ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 20 ) ; llist . push ( 4 ) ; llist . push ( 15 ) ; llist . push ( 10 ) ; llist . head . next . next . next . next = llist . head ; llist . detectLoop ( ) ; } }
inputs: "Online Queries for GCD of array after divide operations | PHP implementation of the approach returns the gcd after all updates in the array ; Function to calculate gcd of onine queries ; stores the gcd of the initial array elements ; calculates the gcd ; performing online queries ; index is 1 based ; divide the array element ; calculates the current gcd ; print the gcd after each step ; Driver code"
Solution in php:
targets: < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function print_gcd_online ( $ n , $ m , $ query , $ arr ) { $ max_gcd = 0 ; $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_gcd = gcd ( $ max_gcd , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ query [ $ i ] [ 0 ] -- ; $ arr [ $ query [ $ i ] [ 0 ] ] /= $ query [ $ i ] [ 1 ] ; $ max_gcd = gcd ( $ arr [ $ query [ $ i ] [ 0 ] ] , $ max_gcd ) ; echo ( $ max_gcd ) , " 
 " ; } } $ n = 3 ; $ m = 3 ; $ query ; $ arr = array ( 36 , 24 , 72 ) ; $ query [ 0 ] [ 0 ] = 1 ; $ query [ 0 ] [ 1 ] = 3 ; $ query [ 1 ] [ 0 ] = 3 ; $ query [ 1 ] [ 1 ] = 12 ; $ query [ 2 ] [ 0 ] = 2 ; $ query [ 2 ] [ 1 ] = 4 ; print_gcd_online ( $ n , $ m , $ query , $ arr ) ; ? >
inputs: "Given an n x n square matrix , find sum of all sub | 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 Code"
How can the above be solved in PHP?
targets: < ? php $ n = 5 ; function printSumTricky ( $ mat , $ k ) { global $ n ; if ( $ k > $ n ) return ; $ stripSum = array ( array ( ) ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ sum += $ mat [ $ i ] [ $ j ] ; $ stripSum [ 0 ] [ $ j ] = $ sum ; for ( $ i = 1 ; $ i < $ n - $ k + 1 ; $ i ++ ) { $ sum += ( $ mat [ $ i + $ k - 1 ] [ $ j ] - $ mat [ $ i - 1 ] [ $ j ] ) ; $ stripSum [ $ i ] [ $ j ] = $ sum ; } } for ( $ i = 0 ; $ i < $ n - $ k + 1 ; $ i ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) $ sum += $ stripSum [ $ i ] [ $ j ] ; echo $ sum , " " ; for ( $ j = 1 ; $ j < $ n - $ k + 1 ; $ j ++ ) { $ sum += ( $ stripSum [ $ i ] [ $ j + $ k - 1 ] - $ stripSum [ $ i ] [ $ j - 1 ] ) ; echo $ sum , " " ; } echo " 
 " ; } } $ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 , 4 ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; $ k = 3 ; printSumTricky ( $ mat , $ k ) ; ? >
inputs: "Minimum steps to change N to 1 by changing it to 2 * N or N / 10 at any step | Java program for the above approach ; Function to check if N can be changed to 1 or not . ; Count the number of 2 in the prime factorisation of N ; Count the number of 5 in the prime factorisation of N ; Driver Code"
Solution in Java:
targets: import java . io . * ; class GFG { static void check ( int N ) { int twos = 0 , fives = 0 ; while ( N % 2 == 0 ) { N /= 2 ; twos ++ ; } while ( N % 5 == 0 ) { N /= 5 ; fives ++ ; } if ( N == 1 && twos <= fives ) { System . out . println ( 2 * fives - twos ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { int N = 50 ; check ( N ) ; } }
inputs: "Construct the smallest possible Array with given Sum and XOR | Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Given sum and value of Bitwise XOR ; Function Call"
How can the above be solved in Python?
targets: def findArray ( _sum , xorr ) :
	if ( xorr > _sum or _sum % 2 != xorr % 2 ) :
		print ( " No ▁ Array ▁ Possible " )
		return
	if ( _sum == xorr ) :
		if ( _sum == 0 ) :
			print ( " Array ▁ is ▁ empty " , " ▁ with ▁ size ▁ 0" )
		else :
			print ( " Array ▁ size ▁ is " , 1 )
			print ( " Array ▁ is " , _sum )
		return
	mid = ( _sum - xorr ) // 2
	if ( xorr & mid == 1 ) :
		print ( " Array ▁ size ▁ is " , 3 )
		print ( " Array ▁ is " , xorr , mid , mid )
	else :
		print ( " Array ▁ size ▁ is " , 2 )
		print ( " Array ▁ is " , ( xorr + mid ) , mid )
_sum = 4
xorr = 2
findArray ( _sum , xorr )
inputs: "Centered Hexadecagonal Number | centered hexadecagonal function ; Formula to calculate nth centered hexadecagonal number ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function center_hexadecagonal_num ( $ n ) { return 8 * $ n * $ n - 8 * $ n + 1 ; } $ n = 2 ; echo $ n , " th ▁ centered ▁ hexadecagonal ▁ number ▁ : ▁ " , center_hexadecagonal_num ( $ n ) ; echo " 
 " ; $ n = 12 ; echo $ n , " th ▁ centered ▁ hexadecagonal ▁ numbe ▁ : ▁ " , center_hexadecagonal_num ( $ n ) ; ? >
inputs: "Maximum area rectangle by picking four sides from array | function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; Driver Code"
Solution in php:
targets: < ? php function findArea ( $ arr , $ n ) { rsort ( $ arr ) ; $ dimension = array ( 0 , 0 ) ; for ( $ i = 0 , $ j = 0 ; $ i < $ n - 1 && $ j < 2 ; $ i ++ ) if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ dimension [ $ j ++ ] = $ arr [ $ i ++ ] ; return ( $ dimension [ 0 ] * $ dimension [ 1 ] ) ; } $ arr = array ( 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ) ; $ n = count ( $ arr ) ; echo findArea ( $ arr , $ n ) ; ? >
inputs: "Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum | C ++ implementation of the missing number such that maximum absolute difference between adjacent element is minimum ; Function to find the missing number such that maximum absolute difference is minimum ; Loop to find the maximum and minimum adjacent element to missing number ; Driver Code ; Function Call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int missingnumber ( int n , int arr [ ] ) { int mn = INT_MAX , mx = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == -1 && arr [ i - 1 ] != -1 ) { mn = min ( mn , arr [ i - 1 ] ) ; mx = max ( mx , arr [ i - 1 ] ) ; } if ( i < ( n - 1 ) && arr [ i ] == -1 && arr [ i + 1 ] != -1 ) { mn = min ( mn , arr [ i + 1 ] ) ; mx = max ( mx , arr [ i + 1 ] ) ; } } long long int res = ( mx + mn ) / 2 ; return res ; } int main ( ) { int n = 5 ; int arr [ 5 ] = { -1 , 10 , -1 , 12 , -1 } ; int ans = 0 ; int res = missingnumber ( n , arr ) ; cout << res ; return 0 ; }
inputs: "Hexanacci Numbers | Function to print the Nth term of the Hexanacci number ; Initialize first five numbers to base cases ; Declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth fifth to second , third , fourth , fifth and curr to sixth respectively ; Driver code"
Solution in Javascript:
targets: function printhexa ( n ) { if ( n < 0 ) return ; let first = 0 ; let second = 0 ; let third = 0 ; let fourth = 0 ; let fifth = 0 ; let sixth = 1 ; let curr = 0 ; if ( n < 6 ) document . write ( first ) ; else if ( n == 6 ) document . write ( sixth ) ; else { for ( let i = 6 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth + sixth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = sixth ; sixth = curr ; } } document . write ( curr ) ; } let n = 11 ; printhexa ( n ) ;
inputs: "Print number in ascending order which contains 1 , 2 and 3 in their digits . | CPP program to print all number containing 1 , 2 and 3 in any order . ; prints all the number containing 1 , 2 , 3 ; check if the number contains 1 , 2 & 3 in any order ; sort all the numbers ; convert the number to string and find if it contains 1 , 2 & 3. ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; string printNumbers ( int numbers [ ] , int n ) { vector < int > oneTwoThree ; for ( int i = 0 ; i < n ; i ++ ) { if ( findContainsOneTwoThree ( numbers [ i ] ) ) oneTwoThree . push_back ( numbers [ i ] ) ; } sort ( oneTwoThree . begin ( ) , oneTwoThree . end ( ) ) ; string result = " " ; for ( auto number : oneTwoThree ) { int value = number ; if ( result . length ( ) > 0 ) result += " , ▁ " ; result += to_string ( value ) ; } return ( result . length ( ) > 0 ) ? result : " - 1" ; } bool findContainsOneTwoThree ( int number ) { string str = to_string ( number ) ; int countOnes = 0 , countTwo = 0 , countThree = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '1' ) countOnes ++ ; else if ( str [ i ] == '2' ) countTwo ++ ; else if ( str [ i ] == '3' ) countThree ++ ; } return ( countOnes && countTwo && countThree ) ; } int main ( ) { int numbers [ ] = { 123 , 1232 , 456 , 234 , 32145 } ; int n = sizeof ( numbers ) / sizeof ( numbers [ 0 ] ) ; string result = printNumbers ( numbers , n ) ; cout << result ; return 0 ; }
inputs: "Find K such that changing all elements of the Array greater than K to K will make array sum N | Function to return K such that changing all elements greater than K to K will make array sum N otherwise return - 1 ; Sorting the array in increasing order ; Loop through all the elements of the array ; Checking if sum of array equals N ; Driver code"
Solution in Python:
targets: def findK ( arr , size , N ) :
	arr = sorted ( arr )
	temp_sum = 0
	for i in range ( size ) :
		temp_sum += arr [ i ]
		if ( N - temp_sum == arr [ i ] * ( size - i - 1 ) ) :
			return arr [ i ]
	return - 1
arr = [ 3 , 1 , 10 , 4 , 8 ]
size = len ( arr )
N = 16
print ( findK ( arr , size , N ) )
inputs: "Perpendicular distance between a point and a Line in 2 D | Function to find distance ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function shortest_distance ( $ x1 , $ y1 , $ a , $ b , $ c ) { $ d = abs ( ( $ a * $ x1 + $ b * $ y1 + $ c ) ) / ( sqrt ( $ a * $ a + $ b * $ b ) ) ; echo " Perpendicular ▁ distance ▁ is ▁ " , $ d ; } $ x1 = 5 ; $ y1 = 6 ; $ a = -2 ; $ b = 3 ; $ c = 4 ; shortest_distance ( $ x1 , $ y1 , $ a , $ b , $ c ) ; ? >
inputs: "Search an element in a sorted and rotated array with duplicates | Java implementation of the approach ; Function to return the index of the key in arr [ l . . h ] if the key is present otherwise return - 1 ; The tricky case , just update left and right ; If arr [ l ... mid ] is sorted ; As this subarray is sorted , we can quickly check if key lies in any of the halves ; If key does not lie in the first half subarray then divide the other half into two subarrays such that we can quickly check if key lies in the other half ; If arr [ l . . mid ] first subarray is not sorted then arr [ mid ... h ] must be sorted subarray ; Driver code"
Solution in Java:
targets: class GFG { static int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( ( arr [ l ] == arr [ mid ] ) && ( arr [ h ] == arr [ mid ] ) ) { l ++ ; h -- ; return search ( arr , l , h , key ) ; } else if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; else return search ( arr , mid + 1 , h , key ) ; } else if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 1 , 2 , 3 , 3 } ; int n = arr . length ; int key = 3 ; System . out . println ( search ( arr , 0 , n - 1 , key ) ) ; } }
inputs: "Number of sequences which has HEAD at alternate positions to the right of the first HEAD | function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code"
How can the above be solved in Python?
targets: def findAllSequence ( N ) :
	if ( N % 2 == 0 ) :
		return ( pow ( 2 , N / 2 + 1 ) + pow ( 2 , N / 2 ) - 2 ) ;
	else :
		return ( pow ( 2 , ( N + 1 ) / 2 ) + pow ( 2 , ( N + 1 ) / 2 ) - 2 ) ;
N = 2 ;
print ( int ( findAllSequence ( N ) ) ) ;
inputs: "Queries on XOR of greatest odd divisor of the range | Precompute the prefix XOR of greatest odd divisor ; Finding the Greatest Odd divisor ; Finding prefix XOR ; Return XOR of the range ; Driver Code"
Solution in Python:
targets: def prefixXOR ( arr , preXOR , n ) :
	for i in range ( 0 , n , 1 ) :
		while ( arr [ i ] % 2 != 1 ) :
			arr [ i ] = int ( arr [ i ] / 2 )
		preXOR [ i ] = arr [ i ]
	for i in range ( 1 , n , 1 ) :
		preXOR [ i ] = preXOR [ i - 1 ] ^ preXOR [ i ]
def query ( preXOR , l , r ) :
	if ( l == 0 ) :
		return preXOR [ r ]
	else :
		return preXOR [ r ] ^ preXOR [ l - 1 ]
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 4 , 5 ]
	n = len ( arr )
	preXOR = [ 0 for i in range ( n ) ]
	prefixXOR ( arr , preXOR , n )
	print ( query ( preXOR , 0 , 2 ) )
	print ( query ( preXOR , 1 , 2 ) )
inputs: "Finding a Non Transitive Co | Java program to find possible non transitive triplets btw L and R ; Function to return gcd of a and b ; function to check for gcd ; a and b are coprime if their gcd is 1. ; Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt ; Generate and check for all possible triplets between L and R ; if we find any such triplets set flag to true ; flag = True indicates that a pair exists between L and R ; Driver code ; finding possible Triplet between 2 and 10 ; finding possible Triplet between 23 and 46"
How can the above be solved in Java?
targets: class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static boolean coprime ( int a , int b ) { return ( gcd ( a , b ) == 1 ) ; } static void possibleTripletInRange ( int L , int R ) { boolean flag = false ; int possibleA = 0 , possibleB = 0 , possibleC = 0 ; for ( int a = L ; a <= R ; a ++ ) { for ( int b = a + 1 ; b <= R ; b ++ ) { for ( int c = b + 1 ; c <= R ; c ++ ) { if ( coprime ( a , b ) && coprime ( b , c ) && ! coprime ( a , c ) ) { flag = true ; possibleA = a ; possibleB = b ; possibleC = c ; break ; } } } } if ( flag == true ) { System . out . println ( " ( " + possibleA + " , ▁ " + possibleB + " , ▁ " + possibleC + " ) " + " ▁ is ▁ one ▁ such ▁ possible ▁ triplet ▁ " + " between ▁ " + L + " ▁ and ▁ " + R ) ; } else { System . out . println ( " No ▁ Such ▁ Triplet ▁ exists " + " between ▁ " + L + " ▁ and ▁ " + R ) ; } } public static void main ( String [ ] args ) { int L , R ; L = 2 ; R = 10 ; possibleTripletInRange ( L , R ) ; L = 23 ; R = 46 ; possibleTripletInRange ( L , R ) ; } }
inputs: Pascal 's Triangle | C program for Pascals Triangle A O ( n ^ 2 ) time and O ( 1 ) extra space function for Pascal 's Triangle ; used to represent C ( line , i ) ; The first value in a line is always 1 ; Driver code
How can the above be solved in C?
targets: void printPascal ( int n ) { for ( int line = 1 ; line <= n ; line ++ ) { int C = 1 ; for ( int i = 1 ; i <= line ; i ++ ) { printf ( " % d ▁ " , C ) ; C = C * ( line - i ) / i ; } printf ( " 
 " ) ; } } int main ( ) { int n = 5 ; printPascal ( n ) ; return 0 ; }
inputs: "Program to find the sum of a Series 1 + 1 / 2 ^ 2 + 1 / 3 ^ 3 + â €¦ . . + 1 / n ^ n | Java program to calculate the following series ; Function to calculate the following series ; Driver Code"
Solution in Java:
targets: import java . io . * ; class Maths { static double Series ( int n ) { int i ; double sums = 0.0 , ser ; for ( i = 1 ; i <= n ; ++ i ) { ser = 1 / Math . pow ( i , i ) ; sums += ser ; } return sums ; } public static void main ( String [ ] args ) { int n = 3 ; double res = Series ( n ) ; res = Math . round ( res * 100000.0 ) / 100000.0 ; System . out . println ( res ) ; } }
inputs: "Minimum flips required to generate continuous substrings of 0 â €™ s and 1 â €™ s | Javascript implementation of the above approach ; Traverse input string and store the count of 0 ; Traverse the input string again to find minimum number of flips ; Driver code"
Solution in Javascript:
targets: function minChanges ( str , N ) { var res ; var count0 = 0 , count1 = 0 ; str . split ( ' ' ) . forEach ( x => { count0 += ( x == ' ' ) ; } ) ; res = count0 ; str . split ( ' ' ) . forEach ( x => { count0 -= ( x == ' ' ) ; count1 += ( x == ' ' ) ; res = Math . min ( res , count1 + count0 ) ; } ) ; return res ; } var N = 9 ; var str = " " ; document . write ( minChanges ( str , N ) ) ;
inputs: "Number of ways to split a binary number such that every part is divisible by 2 | Function to return the required count ; If the splitting is not possible ; To store the count of zeroes ; Counting the number of zeroes ; Return the final answer ; Driver code"
Solution in Python:
targets: def cntSplits ( s ) :
	if ( s [ len ( s ) - 1 ] == '1' ) :
		return 0 ;
	c_zero = 0 ;
	for i in range ( len ( s ) ) :
		c_zero += ( s [ i ] == '0' ) ;
	return int ( pow ( 2 , c_zero - 1 ) ) ;
if __name__ == " _ _ main _ _ " :
	s = "10010" ;
	print ( cntSplits ( s ) ) ;
inputs: "Cutting a Rod | DP | A Naive recursive solution for Rod cutting problem ; A utility function to get the maximum of two integers ; Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Recursively cut the rod in different pieces and compare different configurations ; Driver program to test above functions"
Solution in C++:
targets: #include <stdio.h>
#include <limits.h>
int max ( int a , int b ) { return ( a > b ) ? a : b ; } int cutRod ( int price [ ] , int n ) { if ( n <= 0 ) return 0 ; int max_val = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) max_val = max ( max_val , price [ i ] + cutRod ( price , n - i - 1 ) ) ; return max_val ; } int main ( ) { int arr [ ] = { 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Maximum ▁ Obtainable ▁ Value ▁ is ▁ % dn " , cutRod ( arr , size ) ) ; getchar ( ) ; return 0 ; }
inputs: "Find sum of divisors of all the divisors of a natural number | C ++ 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 . ; Driven Program"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int sumDivisorsOfDivisors ( int n ) { map < int , int > mp ; for ( int j = 2 ; j <= sqrt ( n ) ; j ++ ) { int count = 0 ; while ( n % j == 0 ) { n /= j ; count ++ ; } if ( count ) mp [ j ] = count ; } if ( n != 1 ) mp [ n ] = 1 ; int ans = 1 ; for ( auto it : mp ) { int pw = 1 ; int sum = 0 ; for ( int i = it . second + 1 ; i >= 1 ; i -- ) { sum += ( i * pw ) ; pw *= it . first ; } ans *= sum ; } return ans ; } int main ( ) { int n = 10 ; cout << sumDivisorsOfDivisors ( n ) ; return 0 ; }
inputs: "Remove an element to minimize the LCM of the given array | C ++ implementation of the above approach ; Function to return the LCM of two numbers ; Function to return the minimum LCM after removing a single element from the given array ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing LCM of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing LCM of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be removed ; If any other element is replaced ; Return the minimum LCM ; Driver code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int lcm ( int a , int b ) { int GCD = __gcd ( a , b ) ; return ( a * b ) / GCD ; } int MinLCM ( int a [ ] , int n ) { int Prefix [ n + 2 ] ; int Suffix [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } int main ( ) { int a [ ] = { 5 , 15 , 9 , 36 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << MinLCM ( a , n ) ; return 0 ; }
inputs: "Minimum distance between two occurrences of maximum | C program to find Min distance of maximum element ; function to return min distance ; case a ; case b ; case c ; driver program"
Solution in C:
targets: #include <bits/stdc++.h>
using namespace std ; int minDistance ( int arr [ ] , int n ) { int maximum_element = arr [ 0 ] ; int min_dis = n ; int index = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( maximum_element == arr [ i ] ) { min_dis = min ( min_dis , ( i - index ) ) ; index = i ; } else if ( maximum_element < arr [ i ] ) { maximum_element = arr [ i ] ; min_dis = n ; index = i ; } else continue ; } return min_dis ; } int main ( ) { int arr [ ] = { 6 , 3 , 1 , 3 , 6 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ distance ▁ = ▁ " << minDistance ( arr , n ) ; return 0 ; }
inputs: "Optimal Binary Search Tree | DP | Dynamic Programming code for Optimal Binary Search Tree Problem ; A utility function to get sum of array elements freq [ i ] to freq [ j ] ; A Dynamic Programming based function that calculates minimum cost of a Binary Search Tree . ; Create an auxiliary 2D matrix to store results of subproblems ; For a single key , cost is equal to frequency of the key ; Now we need to consider chains of length 2 , 3 , ... . L is chain length . ; i is row number in cost ; Get column number j from row number i and chain length L ; Try making all keys in interval keys [ i . . j ] as root ; c = cost when keys [ r ] becomes root of this subtree ; Driver Code"
How can the above be solved in Python?
targets: INT_MAX = 2147483647
def sum ( freq , i , j ) :
	s = 0
	for k in range ( i , j + 1 ) :
		s += freq [ k ]
	return s
def optimalSearchTree ( keys , freq , n ) :
	cost = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
	for i in range ( n ) :
		cost [ i ] [ i ] = freq [ i ]
	for L in range ( 2 , n + 1 ) :
		for i in range ( n - L + 2 ) :
			j = i + L - 1
			if i >= n or j >= n :
				break
			cost [ i ] [ j ] = INT_MAX
			for r in range ( i , j + 1 ) :
				c = 0
				if ( r > i ) :
					c += cost [ i ] [ r - 1 ]
				if ( r < j ) :
					c += cost [ r + 1 ] [ j ]
				c += sum ( freq , i , j )
				if ( c < cost [ i ] [ j ] ) :
					cost [ i ] [ j ] = c
	return cost [ 0 ] [ n - 1 ]
if __name__ == ' _ _ main _ _ ' :
	keys = [ 10 , 12 , 20 ]
	freq = [ 34 , 8 , 50 ]
	n = len ( keys )
	print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) )
inputs: "Number of positions with Same address in row major and column major order | C # Program to count the number of positions with same address in row major and column major order ; Returns count of required positions ; horizontal 1D array ; vertical 1D array ; iterating for all possible i ; checking if j is integer ; checking if j lies b / w 1 to N ; iterating for all possible j ; checking if i is integer ; checking if i lies b / w 1 to M ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static int getCount ( int M , int N ) { int count = 0 ; if ( M == 1 ) return N ; if ( N == 1 ) return M ; if ( N > M ) { for ( int i = 1 ; i <= M ; i ++ ) { int numerator = N * i - N + M - i ; int denominator = M - 1 ; if ( numerator % denominator == 0 ) { int j = numerator / denominator ; if ( j >= 1 && j <= N ) count ++ ; } } } else { for ( int j = 1 ; j <= N ; j ++ ) { int numerator = M * j - M + N - j ; int denominator = N - 1 ; if ( numerator % denominator == 0 ) { int i = numerator / denominator ; if ( i >= 1 && i <= M ) count ++ ; } } } return count ; } public static void Main ( ) { int M = 3 , N = 5 ; Console . WriteLine ( getCount ( M , N ) ) ; } }
inputs: "Find the smallest positive number missing from an unsorted array | Set 3 | CPP program to find the smallest positive missing number ; Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int findMissingPositive ( int arr [ ] , int n ) { int m = 1 ; set < int > x ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . insert ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . count ( m ) ) { x . erase ( m ) ; m = m + 1 ; } } } return m ; } int main ( ) { int arr [ ] = { 2 , 3 , -7 , 6 , 8 , 1 , -10 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMissingPositive ( arr , n ) ; return 0 ; }
inputs: "Minimum number of cuts required to pay salary from N length Gold Bar | Python Implementation to find the minimum number of cuts to pay the worker . ; Function to find the minimum number of cuts to pay the worker . ; Nearest Integer to the Log value of the number N ; Driver Code ; Cuts Required in the Length of 15"
How can the above be solved in Python?
targets: import math
def pay ( n ) :
	cuts = int ( math . log ( n , 2 ) )
	return cuts
if __name__ == " _ _ main _ _ " :
	n = 5
	cuts = pay ( n )
	print ( cuts )
	n = 15
	cuts = pay ( n )
	print ( cuts )
inputs: "Find the first repeating element in an array of integers | C ++ program to find first repeating element in arr [ ] ; This function prints the first repeating element in arr [ ] ; Initialize index of first repeating element ; Creates an empty hashset ; Traverse the input array from right to left ; If element is already in hash set , update min ; Else add element to hash set ; Print the result ; Driver method to test above method"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; void printFirstRepeating ( int arr [ ] , int n ) { int min = -1 ; set < int > myset ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) min = i ; else myset . insert ( arr [ i ] ) ; } if ( min != -1 ) cout << " The ▁ first ▁ repeating ▁ element ▁ is ▁ " << arr [ min ] ; else cout << " There ▁ are ▁ no ▁ repeating ▁ elements " ; } int main ( ) { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printFirstRepeating ( arr , n ) ; }
inputs: "Count numbers in range 1 to N which are divisible by X but not by Y | Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function countNumbers ( $ X , $ Y , $ N ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( ( $ i % $ X == 0 ) && ( $ i % $ Y != 0 ) ) $ count ++ ; } return $ count ; } $ X = 2 ; $ Y = 3 ; $ N = 10 ; echo ( countNumbers ( $ X , $ Y , $ N ) ) ; ? >
inputs: "Rank of all elements in an array | Java Code to find rank of elements ; Function to print m Maximum elements ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s ; Use formula to obtain rank ; Driver code"
Solution in Java:
targets: public class GfG { public static void rankify ( int A [ ] , int n ) { float R [ ] = new float [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int r = 1 , s = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != i && A [ j ] < A [ i ] ) r += 1 ; if ( j != i && A [ j ] == A [ i ] ) s += 1 ; } R [ i ] = r + ( float ) ( s - 1 ) / ( float ) 2 ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( R [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 5 , 2 , 1 , 25 , 2 } ; int n = A . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; rankify ( A , n ) ; } }
inputs: "Compress a Binary Tree from top to bottom with overlapping condition | Structure of a node of th tree ; Function to compress all the nodes on the same vertical line ; Stores node by compressing all nodes on the current vertical line ; Check if i - th bit of current bit set or not ; Iterate over the range [ 0 , 31 ] ; Stores count of set bits at i - th positions ; Stores count of clear bits at i - th positions ; Traverse the array ; If i - th bit of current element is set ; Update S ; Update NS ; If count of set bits at i - th position is greater than count of clear bits ; Update ans ; Update getBit ; Function to compress all the nodes on the same vertical line with a single node that satisfies the condition ; Map all the nodes on the same vertical line ; Function to traverse the tree and map all the nodes of same vertical line to vertical distance ; Storing the values in the map ; Recursive calls on left and right subtree ; Getting the range of horizontal distances ; Driver Code ; Function Call"
How can the above be solved in Python?
targets: class TreeNode :
	def __init__ ( self , val = ' ' , left = None , right = None ) :
		self . val = val
		self . left = left
		self . right = right
def evalComp ( arr ) :
	ans = 0
	getBit = 1
	for i in range ( 32 ) :
		S = 0
		NS = 0
		for j in arr :
			if getBit & j :
				S += 1
			else :
				NS += 1
		if S > NS :
			ans += 2 ** i
		getBit <<= 1
	print ( ans , end = " ▁ " )
def compressTree ( root ) :
	mp = { }
	def Trav ( root , hd ) :
		if not root :
			return
		if hd not in mp :
			mp [ hd ] = [ root . val ]
		else :
			mp [ hd ] . append ( root . val )
		Trav ( root . left , hd - 1 )
		Trav ( root . right , hd + 1 )
	Trav ( root , 0 )
	lower = min ( mp . keys ( ) )
	upper = max ( mp . keys ( ) )
	for i in range ( lower , upper + 1 ) :
		evalComp ( mp [ i ] )
if __name__ == ' _ _ main _ _ ' :
	root = TreeNode ( 5 )
	root . left = TreeNode ( 3 )
	root . right = TreeNode ( 2 )
	root . left . left = TreeNode ( 1 )
	root . left . right = TreeNode ( 4 )
	root . right . left = TreeNode ( 1 )
	root . right . right = TreeNode ( 2 )
	compressTree ( root )
inputs: "How to print maximum number of A 's using given four keys | A recursive C program to print maximum number of A 's using  following four keys  ; A recursive function that returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; Initialize result ; TRY ALL POSSIBLE BREAK - POINTS For any keystroke N , we need to loop from N - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have Ctrl - A , Ctrl - C and then only Ctrl - V all the way . ; If the breakpoint is s at b 'th keystroke then  the optimal string would have length  (n-b-1)*screen[b-1]; ; Driver program ; for the rest of the array we will rely on the previous entries to compute new ones"
Solution in C:
targets: #include <stdio.h>
int findoptimal ( int N ) { if ( N <= 6 ) return N ; int max = 0 ; int b ; for ( b = N - 3 ; b >= 1 ; b -- ) { int curr = ( N - b - 1 ) * findoptimal ( b ) ; if ( curr > max ) max = curr ; } return max ; } int main ( ) { int N ; for ( N = 1 ; N <= 20 ; N ++ ) printf ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with ▁ % d ▁ keystrokes ▁ is ▁ % d 
 " , N , findoptimal ( N ) ) ; }
inputs: "Minimum number of deletions so that no two consecutive are same | Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver code ; Function call to prvar answer"
How can the above be solved in JS?
targets: function countDeletions ( s ) { var ans = 0 ; var str = s ; for ( var i = 0 ; i < str . length - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) ans ++ ; return ans ; } var str = " " ; document . write ( countDeletions ( str ) ) ;
inputs: "Sum of all leaf nodes of binary tree | Java program to find sum of all leaf nodes of binary tree ; user define class node ; constructor ; utility function which calculates sum of all leaf nodes ; add root data to sum if root is a leaf node ; propagate recursively in left and right subtree ; driver program ; construct binary tree ; variable to store sum of leaf nodes"
Solution in Java:
targets: public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static int sum ; static void leafSum ( Node root ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) sum += root . data ; leafSum ( root . left ) ; leafSum ( root . right ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right = new Node ( 3 ) ; root . right . right = new Node ( 7 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 8 ) ; sum = 0 ; leafSum ( root ) ; System . out . println ( sum ) ; } }