Muennighoff/xP3x-sample huggingface dataset:
inputs: "Segregate 0 s and 1 s in an array | Function to segregate 0 s and 1 s ; Counts the no of zeros in arr ; Loop fills the arr with 0 until count ; Loop fills remaining arr space with 1 ; Function to print segregated array ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function segregate0and1 ( & $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) $ count ++ ; } for ( $ i = 0 ; $ i < $ count ; $ i ++ ) $ arr [ $ i ] = 0 ; for ( $ i = $ count ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = 1 ; } function toprint ( & $ arr , $ n ) { echo ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ n ) ; toprint ( $ arr , $ n ) ; ? >
inputs: "Reverse words in a given string | C program for above approach : ; temp is for word boundary ; STEP 1 of the above algorithm ; This condition is to make sure that the string start with valid character ( not space ) only ; STEP 2 of the above algorithm"
Solution in C:
targets: void reverseWords ( char * s ) { char * word_begin = NULL ; char * temp = s ; while ( * temp ) { if ( ( word_begin == NULL ) && ( * temp != ' ▁ ' ) ) { word_begin = temp ; } if ( word_begin && ( ( * ( temp + 1 ) == ' ▁ ' ) || ( * ( temp + 1 ) == ' \0' ) ) ) { reverse ( word_begin , temp ) ; word_begin = NULL ; } temp ++ ; } reverse ( s , temp - 1 ) ; }
inputs: "Minimum steps to reduce N to 0 by given operations | C # program for the above approach ; Function to find the minimum number to steps to reduce N to 0 ; Base case ; Recursive call to count the minimum steps needed ; Return the answer ; Driver Code ; Given number N ; Function call"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int minDays ( int n ) { if ( n < 1 ) return n ; int cnt = 1 + Math . Min ( n % 2 + minDays ( n / 2 ) , n % 3 + minDays ( n / 3 ) ) ; return cnt ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( minDays ( N ) ) ; } }
inputs: "Partition problem | DP | A recursive C ++ program for partition problem ; A utility function that returns true if there is a subset of arr [ ] with sun equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of the elements in array ; If sum is odd , there cannot be two subsets with equal sum ; Find if there is subset with sum equal to half of total sum ; Driver code ; Function call"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool isSubsetSum ( int arr [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( arr [ n - 1 ] > sum ) return isSubsetSum ( arr , n - 1 , sum ) ; return isSubsetSum ( arr , n - 1 , sum ) || isSubsetSum ( arr , n - 1 , sum - arr [ n - 1 ] ) ; } bool findPartiion ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; return isSubsetSum ( arr , n , sum / 2 ) ; } int main ( ) { int arr [ ] = { 3 , 1 , 5 , 9 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( findPartiion ( arr , n ) == true ) cout << " Can ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ " " of ▁ equal ▁ sum " ; else cout << " Can ▁ not ▁ be ▁ divided ▁ into ▁ two ▁ subsets " " ▁ of ▁ equal ▁ sum " ; return 0 ; }
inputs: Write a program to calculate pow ( x , n ) |  ; Function to calculate x raised to the power y ; Program to test function power
How can the above be solved in C?
targets: #include <stdio.h>
int power ( int x , unsigned int y ) { if ( y == 0 ) return 1 ; else if ( y % 2 == 0 ) return power ( x , y / 2 ) * power ( x , y / 2 ) ; else return x * power ( x , y / 2 ) * power ( x , y / 2 ) ; } int main ( ) { int x = 2 ; unsigned int y = 3 ; printf ( " % d " , power ( x , y ) ) ; return 0 ; }
inputs: "Count triplets with sum smaller than a given value | C ++ program to count triplets with sum smaller than a given value ; Sort input array ; Initialize result ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there can be total k - j third elements . ; Driver program"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int countTriplets ( int arr [ ] , int n , int sum ) { sort ( arr , arr + n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { ans += ( k - j ) ; j ++ ; } } } return ans ; } int main ( ) { int arr [ ] = { 5 , 1 , 3 , 4 , 7 } ; int n = sizeof arr / sizeof arr [ 0 ] ; int sum = 12 ; cout << countTriplets ( arr , n , sum ) << endl ; return 0 ; }
inputs: "Count Odd and Even numbers in a range from L to R | C # implementation of the above approach ; 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 C-Sharp?
targets: using System ; class GFG { static int countOdd ( int L , int R ) { int N = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) N ++ ; return N ; } public static void Main ( ) { int L = 3 , R = 7 ; int odds = countOdd ( L , R ) ; int evens = ( R - L + 1 ) - odds ; Console . WriteLine ( " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " + odds ) ; Console . WriteLine ( " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " + evens ) ; } }
inputs: "Minimize K whose XOR with given array elements leaves array unchanged | C # program for the above approach ; Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Driver code ; Given array ; Function call"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { static int min_value ( int [ ] arr , int N ) { int X , K ; HashSet < int > S = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { S . Add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; foreach ( int it in S ) { X = ( ( i it ) - ( i & it ) ) ; if ( S . Contains ( X ) ) { count ++ ; } } if ( count == S . Count ) { K = i ; return K ; } } return - 1 ; } static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 3 , 0 , 2 } ; int N = arr . Length ; Console . Write ( min_value ( arr , N ) ) ; } }
inputs: "Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver Code"
How can the above be solved in JS?
targets: let eps = 1e-6 ; function getArea ( base , hypotenuse ) { let height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } function printRightAngleTriangle ( hypotenuse , area ) { let hsquare = hypotenuse * hypotenuse ; let sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; let maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { document . write ( " " ) ; return ; } let low = 0.0 ; let high = sideForMaxArea ; let base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } let height = Math . sqrt ( hsquare - base * base ) ; document . write ( Math . round ( base ) + " " + Math . round ( height ) ) ; } let hypotenuse = 5 ; let area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ;
inputs: "Count the triplets such that A [ i ] < B [ j ] < C [ k ] | Function to return the count of elements in arr [ ] which are less than the given key ; Modified binary search ; Function to return the count of elements in arr [ ] which are greater than the given key ; Modified binary search ; Function to return the count of the required triplets ; Sort all three arrays ; Iterate for all the elements of array B ; Count of elements in A [ ] which are less than the chosen element from B [ ] ; Count of elements in C [ ] which are greater than the chosen element from B [ ] ; Update the count ; Driver code"
How can the above be solved in PHP?
targets: < ? php function countLessThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] < $ key ) { $ l = $ m + 1 ; $ index = $ m ; } else { $ r = $ m - 1 ; } } return ( $ index + 1 ) ; } function countGreaterThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] <= $ key ) { $ l = $ m + 1 ; } else { $ r = $ m - 1 ; $ index = $ m ; } } if ( $ index == -1 ) return 0 ; return ( $ n - $ index ) ; } function countTriplets ( $ n , & $ a , & $ b , & $ c ) { sort ( $ a ) ; sort ( $ b ) ; sort ( $ c ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ current = $ b [ $ i ] ; $ a_index = -1 ; $ c_index = -1 ; $ low = countLessThan ( $ a , $ n , $ current ) ; $ high = countGreaterThan ( $ c , $ n , $ current ) ; $ count += ( $ low * $ high ) ; } return $ count ; } $ a = array ( 1 , 5 ) ; $ b = array ( 2 , 4 ) ; $ c = array ( 3 , 6 ) ; $ size = sizeof ( $ a ) ; echo countTriplets ( $ size , $ a , $ b , $ c ) ; ? >
inputs: "Maximum sum from three arrays such that picking elements consecutively from same is not allowed | JavaScript 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"
How can the above be solved in JS?
targets: var N = 3 ; function FindMaximumSum ( ind , kon , a , b , c , n , dp ) { if ( ind == n ) return 0 ; if ( dp [ ind ] [ kon ] != - 1 ) return dp [ ind ] [ kon ] ; var ans = - 1000000005 ; if ( kon == 0 ) { ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 1 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 2 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; } return dp [ ind ] [ kon ] = ans ; } var a = [ 6 , 8 , 2 , 7 , 4 , 2 , 7 ] ; var b = [ 7 , 8 , 5 , 8 , 6 , 3 , 5 ] ; var c = [ 8 , 3 , 2 , 6 , 8 , 4 , 1 ] ; var n = a . length ; var dp = Array . from ( Array ( n ) , ( ) => Array ( n ) . fill ( - 1 ) ) ; var x = FindMaximumSum ( 0 , 0 , a , b , c , n , dp ) ; var y = FindMaximumSum ( 0 , 1 , a , b , c , n , dp ) ; var z = FindMaximumSum ( 0 , 2 , a , b , c , n , dp ) ; document . write ( Math . max ( x , Math . max ( y , z ) ) ) ;
inputs: "Find Selling Price from given Profit Percentage and Cost | C # implementation of above approach ; calculate Nth term of series ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Driver Code ; Get the CP Profit % ; Printing the returned value"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static float SellingPrice ( float CP , float PP ) { float P_decimal = 1 + ( PP / 100 ) ; float res = P_decimal * CP ; return res ; } public static void Main ( ) { float C = 720 , P = 13 ; Console . Write ( SellingPrice ( C , P ) ) ; } }
inputs: "Smallest N digit number divisible by all possible prime digits | C # implementation of the above approach ; Function to find the minimum number of n digits divisible by all prime digits ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static void minNum ( int n ) { if ( n < 3 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( 210 * ( ( int ) ( Math . Pow ( 10 , n - 1 ) / 210 ) + 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 5 ; minNum ( n ) ; } }
inputs: "Fibonacci Cube Graph | function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; Driver Code"
Solution in php:
targets: < ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; } function findVertices ( $ n ) { return fib ( $ n + 2 ) ; } $ n = 3 ; echo findVertices ( $ n ) ; ? >
inputs: "Find number of subarrays with even sum | C # program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; result may be large enough not to fit in int ; ; to keep track of subarrays with even sum starting from index i ; if a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomeseven when a [ i ] gets added to it . ; if a [ i ] is even then all subarrays starting from index i + 1 which was even remainseven and one extra a [ i ] even subarray gets added to it . ; Driver Code"
Solution in C#:
targets: using System ; public class GFG { public static long countEvenSum ( int [ ] a , int n ) { long res = 0 ; int s = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] % 2 == 1 ) { s = n - i - 1 - s ; } else { s = s + 1 ; } res = res + s ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }
inputs: How to print maximum number of A 's using given four keys | A Dynamic Programming based C program to find maximum number of A 's  that can be printed using four keys  ; this function returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; An array to store result of subproblems ; To pick a breakpoint ; Initializing the optimal lengths array for uptil 6 input strokes . ; Solve all subproblems in bottom manner ; Initialize length of optimal string for n keystrokes ; 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 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
How can the above be solved in C?
targets: #include <stdio.h>
int findoptimal ( int N ) { if ( N <= 6 ) return N ; int screen [ N ] ; int b ; int n ; for ( n = 1 ; n <= 6 ; n ++ ) screen [ n - 1 ] = n ; for ( n = 7 ; n <= N ; n ++ ) { screen [ n - 1 ] = 0 ; for ( b = n - 3 ; b >= 1 ; b -- ) { int curr = ( n - b - 1 ) * screen [ b - 1 ] ; if ( curr > screen [ n - 1 ] ) screen [ n - 1 ] = curr ; } } return screen [ N - 1 ] ; } 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: "Print squares of first n natural numbers without using * , / and | PHP program to print squares of first ' n ' natural numbers wothout using * , / and - ; Initialize ' square ' and first odd number ; Calculate and print squares ; Print square ; Update ' square ' and ' odd ' ; Driver Code"
Solution in php:
targets: < ? php function printSquares ( $ n ) { $ square = 0 ; $ odd = 1 ; for ( $ x = 0 ; $ x < $ n ; $ x ++ ) { echo $ square , " " ; $ square = $ square + $ odd ; $ odd = $ odd + 2 ; } } $ n = 5 ; printSquares ( $ n ) ; ? >
inputs: "Calculate depth of a full Binary tree from Preorder | function to return max of left subtree height or right subtree height ; calc height of left subtree ( In preorder left subtree is processed before right ) ; calc height of right subtree ; Wrapper over findDepthRec ( ) ; Driver program"
Solution in Javascript:
targets: function findDepthRec ( tree , n , index ) { if ( index >= n tree [ index ] == ' ' ) return 0 ; index ++ ; let left = findDepthRec ( tree , n , index ) ; index ++ ; let right = findDepthRec ( tree , n , index ) ; return Math . max ( left , right ) + 1 ; } function findDepth ( tree , n ) { let index = 0 ; return ( findDepthRec ( tree , n , index ) ) ; } let tree = " " . split ( ' ' ) ; let n = tree . length ; document . write ( findDepth ( tree , n ) ) ;
inputs: "Compare two strings considering only alphanumeric characters | Function to check alphanumeric equality of both strings ; variable declaration ; Length of first string ; Length of second string ; To check each and every characters of both string ; If the current character of the first string is not an alphanumeric character , increase the pointer i ; If the current character of the second string is not an alphanumeric character , increase the pointer j ; if all alphanumeric characters of both strings are same then return true ; if any alphanumeric characters of both strings are not same then return false ; If current character matched , increase both pointers to check the next character ; If not same , then return false ; Function to print Equal or Unequal if strings are same or not ; check alphanumeric equality of both strings ; if both are alphanumeric equal , print Equal ; otherwise print Unequal ; Driver code"
How can the above be solved in C++?
targets: #include <iostream>
using namespace std ; bool CompareAlphanumeric ( string & str1 , string & str2 ) { int i , j ; i = 0 ; j = 0 ; int len1 = str1 . size ( ) ; int len2 = str2 . size ( ) ; while ( i <= len1 && j <= len2 ) { while ( i < len1 && ( ! ( ( str1 [ i ] >= ' a ' && str1 [ i ] <= ' z ' ) || ( str1 [ i ] >= ' A ' && str1 [ i ] <= ' Z ' ) || ( str1 [ i ] >= '0' && str1 [ i ] <= '9' ) ) ) ) { i ++ ; } while ( j < len2 && ( ! ( ( str2 [ j ] >= ' a ' && str2 [ j ] <= ' z ' ) || ( str2 [ j ] >= ' A ' && str2 [ j ] <= ' Z ' ) || ( str2 [ j ] >= '0' && str2 [ j ] <= '9' ) ) ) ) { j ++ ; } if ( i == len1 && j == len2 ) return true ; else if ( str1 [ i ] != str2 [ j ] ) return false ; else { i ++ ; j ++ ; } } return false ; } void CompareAlphanumericUtil ( string str1 , string str2 ) { bool res ; res = CompareAlphanumeric ( str1 , str2 ) ; if ( res == true ) cout << " Equal " << endl ; else cout << " Unequal " << endl ; } int main ( ) { string str1 , str2 ; str1 = " Ram , ▁ Shyam " ; str2 = " ▁ Ram ▁ - ▁ Shyam . " ; CompareAlphanumericUtil ( str1 , str2 ) ; str1 = " abc123" ; str2 = "123abc " ; CompareAlphanumericUtil ( str1 , str2 ) ; return 0 ; }
inputs: Count number of squares in a rectangle | C program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
int countSquares ( int m , int n ) { int temp ; if ( n < m ) { temp = n ; n = m ; m = temp ; } return m * ( m + 1 ) * ( 2 * m + 1 ) / 6 + ( n - m ) * m * ( m + 1 ) / 2 ; } int main ( ) { int m = 4 , n = 3 ; printf ( " Count ▁ of ▁ squares ▁ is ▁ % d " , countSquares ( m , n ) ) ; }
inputs: "Hyperfactorial of a number | function to calculate the value of hyperfactorial ; initialise the val to 1 ; returns the hyperfactorial of a number ; Driver code"
How can the above be solved in PHP?
targets: < ? php function boost_hyperfactorial ( $ num ) { $ val = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { $ val = $ val * pow ( $ i , $ i ) ; } return $ val ; } $ num = 5 ; echo boost_hyperfactorial ( $ num ) ; ? >
inputs: "Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } int main ( ) { cout << numberOfPaths ( 3 , 3 ) ; }
inputs: "Alternating Numbers | Java program for the above approach ; Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Driver Code ; Given number N ; Function call"
Solution in Java:
targets: class GFG { static boolean isEvenOddForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 0 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 1 ) return false ; } return true ; } static boolean isOddEvenForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 1 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 0 ) return false ; } return true ; } static boolean isAlternating ( int n ) { String str = Integer . toString ( n ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } public static void main ( String [ ] args ) { int N = 129 ; if ( isAlternating ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
inputs: "Print prime factors of a given integer in decreasing order using Stack | C # program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; Function Call"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { static void PrimeFactors ( int N ) { Stack < int > st = new Stack < int > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . Push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( st . Count != 0 ) { Console . Write ( st . Peek ( ) ) ; st . Pop ( ) ; } } public static void Main ( ) { int N = 8 ; PrimeFactors ( N ) ; ; } }
inputs: "Number of substrings with count of each character as k |"
Solution in C:
targets: #include <stdbool.h>
#include <stdio.h>
#include <string.h>
int min ( int a , int b ) { return a < b ? a : b ; } bool have_same_frequency ( int freq [ ] , int k ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] != k ) { return false ; } } return true ; } int count_substrings ( char * s , int n , int k ) { int count = 0 ; int distinct = 0 ; bool have [ 26 ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) { have [ s [ i ] - ' a ' ] = true ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( have [ i ] ) { distinct ++ ; } } for ( int length = 1 ; length <= distinct ; length ++ ) { int window_length = length * k ; int freq [ 26 ] = { 0 } ; int window_start = 0 ; int window_end = window_start + window_length - 1 ; for ( int i = window_start ; i <= min ( window_end , n - 1 ) ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } while ( window_end < n ) { if ( have_same_frequency ( freq , k ) ) { count ++ ; } freq [ s [ window_start ] - ' a ' ] -- ; window_start ++ ; window_end ++ ; if ( window_end < n ) { freq [ s [ window_end ] - ' a ' ] ++ ; } } } return count ; } int main ( ) { char * s = " aabbcc " ; int k = 2 ; printf ( " % d 
 " , count_substrings ( s , 6 , k ) ) ; s = " aabbc " ; k = 2 ; printf ( " % d 
 " , count_substrings ( s , 5 , k ) ) ; return 0 ; }
inputs: "Count paths with distance equal to Manhattan distance | Function to return the value of nCk ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the number of paths ; Difference between the ' x ' coordinates of the given points ; Difference between the ' y ' coordinates of the given points ; Driver code"
How can the above be solved in PHP?
targets: < ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function countPaths ( $ x1 , $ y1 , $ x2 , $ y2 ) { $ m = abs ( $ x1 - $ x2 ) ; $ n = abs ( $ y1 - $ y2 ) ; return ( binomialCoeff ( $ m + $ n , $ n ) ) ; } { $ x1 = 2 ; $ y1 = 3 ; $ x2 = 4 ; $ y2 = 5 ; echo ( countPaths ( $ x1 , $ y1 , $ x2 , $ y2 ) ) ; }
inputs: "Minimum product of maximum and minimum element over all possible subarrays | Function to find the minimum product of the minimum and maximum among all the possible subarrays ; Stores resultant minimum product ; Traverse the given array arr [ ] ; Min of product of all two pair of consecutive elements ; Return the resultant value ; Driver Code"
Solution in Python:
targets: def findMinMax ( a ) :
	min_val = 1000000000
	for i in range ( 1 , len ( a ) ) :
		min_val = min ( min_val , a [ i ] * a [ i - 1 ] )
	return min_val
if __name__ == ( " _ _ main _ _ " ) :
	arr = [ 6 , 4 , 5 , 6 , 2 , 4 , 1 ]
	print ( findMinMax ( arr ) )
inputs: "Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver Code"
Solution in php:
targets: < ? php function isPrime ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ c ++ ; } if ( $ c == 1 ) return 1 ; else return 0 ; } function findMinNum ( $ arr , $ n ) { $ first = 0 ; $ last = 0 ; $ num ; $ rev ; $ i ; $ hash = array_fill ( 0 , 20 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ hash [ $ arr [ $ i ] ] ++ ; } echo " Minimum ▁ number : ▁ " ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { if ( $ hash [ $ i ] != 0 ) { $ first = $ i ; break ; } } for ( $ i = 9 ; $ i >= 0 ; $ i -- ) { if ( $ hash [ $ i ] != 0 ) { $ last = $ i ; break ; } } $ num = $ first * 10 + $ last ; $ rev = $ last * 10 + $ first ; echo " Prime combinations : " if ( isPrime ( $ num ) && isPrime ( $ rev ) ) echo $ num . " ▁ " . $ rev ; else if ( isPrime ( $ num ) ) echo $ num ; else if ( isPrime ( $ rev ) ) echo $ rev ; else echo " No ▁ combinations ▁ exist " ; } $ arr = array ( 1 , 2 , 4 , 7 , 8 ) ; findMinNum ( $ arr , 5 ) ; ? >
inputs: "Cutting a Rod | DP | C program for above approach ; Global Array for the purpose of memoization . ; A recursive program , using , memoization , to implement the rod cutting problem ( Top - Down ) . ; The maximum price will be zero , when either the length of the rod is zero or price is zero . ; If the length of the rod is less than the maximum length , Max_lene will consider it . Now depending upon the profit , either Max_lene we will take it or discard it . ; If the length of the rod is greater than the permitted size , Max_len we will not consider it . ; Max_lene Max_lenill return the maximum value obtained , Max_lenhich is present at the nth roMax_len and Max_lenth column . ; Driver program to test above functions ; Function Call"
Solution in C:
targets: #include <stdio.h>
#include <stdlib.h>
int max ( int a , int b ) { return ( a > b ) ? a : b ; } int t [ 9 ] [ 9 ] ; int un_kp ( int price [ ] , int length [ ] , int Max_len , int n ) { if ( n == 0 Max_len == 0 ) { return 0 ; } if ( length [ n - 1 ] <= Max_len ) { t [ n ] [ Max_len ] = max ( price [ n - 1 ] + un_kp ( price , length , Max_len - length [ n - 1 ] , n ) , un_kp ( price , length , Max_len , n - 1 ) ) ; } else { t [ n ] [ Max_len ] = un_kp ( price , length , Max_len , n - 1 ) ; } return t [ n ] [ Max_len ] ; } int main ( ) { int price [ ] = { 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; int length [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { length [ i ] = i + 1 ; } int Max_len = n ; printf ( " Maximum ▁ obtained ▁ value ▁ is ▁ % d ▁ 
 " , un_kp ( price , length , n , Max_len ) ) ; }
inputs: "Program to calculate area of a rhombus whose one side and diagonal are given | Java program to calculate the area of a rhombus whose one side and one diagonal is given ; function to calculate the area of the rhombus ; Second diagonal ; area of rhombus ; return the area ; Driver code"
How can the above be solved in Java?
targets: class GFG { static double area ( double d1 , double a ) { double d2 = Math . sqrt ( 4 * ( a * a ) - d1 * d1 ) ; double area = 0.5 * d1 * d2 ; return area ; } public static void main ( String [ ] args ) { double d = 7.07 ; double a = 5 ; System . out . println ( area ( d , a ) ) ; } }
inputs: "Find minimum possible values of A , B and C when two of the ( A + B ) , ( A + C ) and ( B + C ) are given | C # implementation of the approach ; Function to find A , B and C ; Keep minimum number in x ; Find the numbers ; Driver code ; Function call"
Solution in C#:
targets: using System ; class GFG { static void MinimumValue ( int x , int y ) { if ( x > y ) { int temp = x ; x = y ; y = temp ; } int a = 1 ; int b = x - 1 ; int c = y - b ; Console . WriteLine ( a + " ▁ " + b + " ▁ " + c ) ; } public static void Main ( ) { int x = 123 , y = 13 ; MinimumValue ( x , y ) ; } }
inputs: Count all possible groups of size 2 or 3 that have sum as multiple of 3 | C Program to count all possible groups of size 2 or 3 that have sum as multiple of 3 ; Returns count of all possible groups that can be formed from elements of a [ ] . ; Create an array C [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i ; To store the result ; Count elements with remainder 0 , 1 and 2 ; Case 3. a : Count groups of size 2 from 0 remainder elements ; Case 3. b : Count groups of size 2 with one element with 1 remainder and other with 2 remainder ; Case 4. a : Count groups of size 3 with all 0 remainder elements ; Case 4. b : Count groups of size 3 with all 1 remainder elements ; Case 4. c : Count groups of size 3 with all 2 remainder elements ; Case 4. c : Count groups of size 3 with different remainders ; Return total count stored in res ; Driver program to test above functions
How can the above be solved in C?
targets: #include <stdio.h>
int findgroups ( int arr [ ] , int n ) { int c [ 3 ] = { 0 } , i ; int res = 0 ; for ( i = 0 ; i < n ; i ++ ) c [ arr [ i ] % 3 ] ++ ; res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) ; res += c [ 1 ] * c [ 2 ] ; res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 ; res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c [ 1 ] - 2 ) ) / 6 ; res += ( ( c [ 2 ] * ( c [ 2 ] - 1 ) * ( c [ 2 ] - 2 ) ) / 6 ) ; res += c [ 0 ] * c [ 1 ] * c [ 2 ] ; return res ; } int main ( ) { int arr [ ] = { 3 , 6 , 7 , 2 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Required ▁ number ▁ of ▁ groups ▁ are ▁ % d 
 " , findgroups ( arr , n ) ) ; return 0 ; }
inputs: "Sum of all the prime divisors of a number | function to find prime divisors of all numbers from 1 to n ; if the number is prime ; add this prime to all it 's multiples ; Driver code"
How can the above be solved in JS?
targets: function Sum ( N ) { let SumOfPrimeDivisors = new Array ( N + 1 ) ; for ( let i = 0 ; i < SumOfPrimeDivisors . length ; i ++ ) { SumOfPrimeDivisors [ i ] = 0 ; } for ( let i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 0 ) { for ( let j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } let N = 60 ; document . write ( " " + " " + Sum ( N ) + " " ) ;
inputs: "Minimize cost to modify the Array such that even indices have even elements and vice versa | C ++ program for the above approach ; Function to find the minimum cost to modify the array according to the given criteria ; Count of wrong positioned odd and even elements ; Odd Count ; Even Count ; Swapping Cost ; Decrementing cost after swapping ; Only decrementing cost ; Return the minimum cost of the two cases ; Driver Code"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int minimumCost ( int arr [ ] , int N , int X , int Y ) { int even_count = 0 , odd_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) && ( i % 2 == 0 ) ) { odd_count ++ ; } if ( ( arr [ i ] % 2 ) == 0 && ( i & 1 ) ) { even_count ++ ; } } int cost1 = X * min ( odd_count , even_count ) ; int cost2 = Y * ( max ( odd_count , even_count ) - min ( odd_count , even_count ) ) ; int cost3 = ( odd_count + even_count ) * Y ; return min ( cost1 + cost2 , cost3 ) ; } int main ( ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 } , X = 10 , Y = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumCost ( arr , N , X , Y ) ; return 0 ; }
inputs: "Count of buttons pressed in a keypad mobile | Java implementation of the approach ; Array to store how many times a button has to be pressed for typing a particular character ; Function to return the count of buttons pressed to type the given string ; Count the key presses ; Return the required count ; Driver code"
How can the above be solved in Java?
targets: class GFG { static final int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 } ; public static int countKeyPressed ( String str , int len ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) count = count + arr [ str . charAt ( i ) - ' a ' ] ; return count ; } public static void main ( String [ ] args ) { String str = " abcdef " ; int len = str . length ( ) ; System . out . print ( countKeyPressed ( str , len ) ) ; } }
inputs: "Average of even numbers till a given even number | Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function"
Solution in Python:
targets: def averageEven ( n ) :
	if ( n % 2 != 0 ) :
		print ( " Invalid ▁ Input " )
		return - 1
	sm = 0
	count = 0
	while ( n >= 2 ) :
		count = count + 1
		sm = sm + n
		n = n - 2
	return sm // count
n = 16
print ( averageEven ( n ) )
inputs: "Minimum time required to cover a Binary Array | CPP implementation to find the Minimum time required to cover a Binary Array ; function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; check if count is odd ; check ifcount is even ; return the time ; driver code ; initialise N ; array initialisation"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; int solve ( vector < int > arr , int n ) { int k = arr . size ( ) ; bool mp [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = max ( leftSegment , rightSegment ) ; int tim ; if ( maxSegment & 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } int main ( ) { int N = 5 ; vector < int > arr = { 1 , 4 } ; cout << solve ( arr , N ) ; }
inputs: "Find four factors of N with maximum product and sum equal to N | Set | C ++ program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to print those four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; void findfactors ( int n ) { unordered_map < int , int > mpp ; vector < int > v , v1 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push_back ( i ) ; if ( i != ( n / i ) && i != 1 ) v . push_back ( n / i ) ; } } int s = v . size ( ) ; int maxi = -1 ; pair < int , int > mp1 [ n + 5 ] ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = i ; j < s ; j ++ ) { if ( v [ i ] + v [ j ] < n ) { v1 . push_back ( v [ i ] + v [ j ] ) ; mp1 [ v [ i ] + v [ j ] ] = { v [ i ] , v [ j ] } ; mpp [ v [ i ] + v [ j ] ] = 1 ; } } } s = v1 . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int el = n - ( v1 [ i ] ) ; if ( mpp [ el ] == 1 ) { int a = mp1 [ v1 [ i ] ] . first ; int b = mp1 [ v1 [ i ] ] . second ; int c = mp1 [ n - v1 [ i ] ] . first ; int d = mp1 [ n - v1 [ i ] ] . second ; maxi = max ( a * b * c * d , maxi ) ; } } if ( maxi == -1 ) cout << " Not ▁ Possible 
 " ; else { cout << " The ▁ maximum ▁ product ▁ is ▁ " << maxi << endl ; } } int main ( ) { int n = 50 ; findfactors ( n ) ; return 0 ; }
inputs: "Number of values of b such that a = b + ( a ^ b ) | function to return the number of solutions ; Driver Code"
Solution in Python:
targets: def countSolutions ( a ) :
	count = bin ( a ) . count ( '1' )
	return 2 ** count
if __name__ == " _ _ main _ _ " :
	a = 3
	print ( countSolutions ( a ) )
inputs: "Check if a circle lies inside another circle or not | Java program to check if one circle lies inside another circle or not . ; Driver code"
How can the above be solved in Java?
targets: import java . io . * ; class GFG { static void circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( int ) Math . sqrt ( ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) ) ; if ( distSq + r2 == r1 ) { System . out . println ( " The ▁ smaller ▁ circle ▁ lies ▁ completely " + " ▁ inside ▁ the ▁ bigger ▁ circle ▁ with ▁ " + " touching ▁ each ▁ other ▁ " + " at ▁ a ▁ point ▁ of ▁ circumference . ▁ " ) ; } else if ( distSq + r2 < r1 ) { System . out . println ( " The ▁ smaller ▁ circle ▁ lies ▁ completely " + " ▁ inside ▁ the ▁ bigger ▁ circle ▁ without " + " ▁ touching ▁ each ▁ other ▁ " + " at ▁ a ▁ point ▁ of ▁ circumference . " ) ; } else { System . out . println ( " The ▁ smaller ▁ does ▁ not ▁ lies ▁ inside " + " ▁ the ▁ bigger ▁ circle ▁ completely . " ) ; } } public static void main ( String [ ] args ) { int x1 = 10 , y1 = 8 ; int x2 = 1 , y2 = 2 ; int r1 = 30 , r2 = 10 ; circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; } }
inputs: "Program to calculate area of a parallelogram | Javascript program for the above approach ; Function to return the area of parallelogram using sides and angle at the intersection of diagonal ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using sides and angle at the intersection of sides ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using diagonals and angle at the intersection of diagonals ; Calculate area of parallelogram ; Return the answer ; Given diagonal and angle ; Function call ; Print the area"
Solution in Javascript:
targets: function toRadians ( degree ) { let pi = 3.14159265359 ; return ( degree * ( pi / 180 ) ) ; } function Area_Parallelogram1 ( a , b , theta ) { let area = ( Math . abs ( Math . tan ( toRadians ( theta ) ) ) / 2 ) * Math . abs ( a * a - b * b ) ; return area ; } function Area_Parallelogram2 ( a , b , gamma ) { let area = ( Math . abs ( Math . sin ( toRadians ( gamma ) ) ) ) * Math . abs ( a * b ) ; return area ; } function Area_Parallelogram3 ( d1 , d2 , theta ) { let area = ( Math . abs ( Math . sin ( toRadians ( theta ) ) ) / 2 ) * Math . abs ( d1 * d2 ) ; return area ; } let d1 = 3 ; let d2 = 5 ; let theta = 90 ; let area = Area_Parallelogram3 ( d1 , d2 , theta ) ; document . write ( area ) ;
inputs: "Write a function that returns 2 for input 1 and returns 1 for 2 |"
Solution in C:
targets: int invertSub ( int x ) { return ( 3 - x ) ; }
inputs: "Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | Java program for the above approach ; Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Print the answer ; Driver Code ; Given N , K & M"
Solution in Java:
targets: import java . io . * ; class GFG { static void maxValueAtIndexK ( int N , int K , int M ) { int S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; int X = ( M + S1 + S2 ) / N ; System . out . println ( X ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ; } }
inputs: Kronecker Product of two matrices | C code to find the Kronecker Product of two matrices and stores it as matrix C ; rowa and cola are no of rows and columns of matrix A rowb and colb are no of rows and columns of matrix B ; Function to computes the Kronecker Product of two matrices ; i loops till rowa ; k loops till rowb ; j loops till cola ; l loops till colb ; Each element of matrix A is multiplied by whole Matrix B resp and stored as Matrix C ; Driver Code
How can the above be solved in C?
targets: #include <stdio.h>
const int cola = 2 , rowa = 3 , colb = 3 , rowb = 2 ; void Kroneckerproduct ( int A [ ] [ cola ] , int B [ ] [ colb ] ) { int C [ rowa * rowb ] [ cola * colb ] ; for ( int i = 0 ; i < rowa ; i ++ ) { for ( int k = 0 ; k < rowb ; k ++ ) { for ( int j = 0 ; j < cola ; j ++ ) { for ( int l = 0 ; l < colb ; l ++ ) { C [ i + l + 1 ] [ j + k + 1 ] = A [ i ] [ j ] * B [ k ] [ l ] ; printf ( " % d 	 " , C [ i + l + 1 ] [ j + k + 1 ] ) ; } } printf ( " 
 " ) ; } } } int main ( ) { int A [ 3 ] [ 2 ] = { { 1 , 2 } , { 3 , 4 } , { 1 , 0 } } , B [ 2 ] [ 3 ] = { { 0 , 5 , 2 } , { 6 , 7 , 3 } } ; Kroneckerproduct ( A , B ) ; return 0 ; }
inputs: "Program to Convert Radian to Degree | Function for convertion ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function Convert ( $ radian ) { $ pi = 3.14159 ; return ( $ radian * ( 180 / $ pi ) ) ; } $ radian = 5.0 ; $ degree = Convert ( $ radian ) ; echo ( $ degree ) ; ? >
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 ; Driver Code"
Solution in C:
targets: #include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int solve ( int a [ ] , int n ) { int max1 = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( abs ( a [ i ] - a [ j ] ) > max1 ) { max1 = abs ( a [ i ] - a [ j ] ) ; } } } return max1 ; } int main ( ) { int arr [ ] = { -1 , 2 , 3 , -4 , -10 , 22 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Largest ▁ gap ▁ is ▁ : ▁ % d " , solve ( arr , size ) ) ; return 0 ; }
inputs: "Count of repeating digits in a given Number | C ++ program for the above approach ; Function that returns the count of repeating digits of the given number ; Initialize a variable to store count of Repeating digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is greater than 1 ; Increment the count of Repeating digits ; Return count of repeating digit ; Driver Code ; Given array arr [ ] ; Function Call"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; int countRepeatingDigits ( int N ) { int res = 0 ; int cnt [ 10 ] = { 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] > 1 ) { res ++ ; } } return res ; } int main ( ) { int N = 12 ; cout << countRepeatingDigits ( N ) ; return 0 ; }
inputs: "Print all positions of a given string having count of smaller characters equal on both sides | Function to find indexes of the given string that satisfy the condition ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given string ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ str [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; ; Driver Code"
Solution in Javascript:
targets: function printIndexes ( str ) { var N = str . length ; var cntFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var cntLeftFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var cntLeft = 0 ; var cntRight = 0 ; for ( var j = str [ i ] . charCodeAt ( 0 ) - 1 ; j >= 0 ; j -- ) { cntLeft += cntLeftFreq [ j ] ; cntRight += cntFreq [ j ] - cntLeftFreq [ j ] ; } cntLeftFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; if ( cntLeft == cntRight && cntLeft != 0 ) { document . write ( i + " " ) ; } } } var str = " " ; printIndexes ( str ) ;
inputs: "Calculate the value of 2 raised to the power of twice the binary representation of N | C ++ program to implement the above approach ; Function to find the value of power ( X , Y ) in O ( log Y ) ; Stores power ( X , Y ) ; Update X ; Base Case ; Calculate power ( X , Y ) ; If Y is an odd number ; Update res ; Update Y ; Update X ; Function to calculate ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; dp [ N ] * dp [ N ] : Stores value of ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Base Case ; Iterate over the range [ 3 , N ] ; Stores rightmost bit of i ; Stores the value of ( i - y ) ; If x is power of 2 ; Update dp [ i ] ; Update dp [ i ] ; Driver Code"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; #define M  1000000007
long long power ( long long X , long long Y ) { long long res = 1 ; X = X % M ; if ( X == 0 ) return 0 ; while ( Y > 0 ) { if ( Y & 1 ) { res = ( res * X ) % M ; } Y = Y >> 1 ; X = ( X * X ) % M ; } return res ; } long long findValue ( long long N ) { long long dp [ N + 1 ] ; dp [ 1 ] = 2 ; dp [ 2 ] = 1024 ; for ( int i = 3 ; i <= N ; i ++ ) { int y = ( i & ( - i ) ) ; int x = i - y ; if ( x == 0 ) { dp [ i ] = power ( dp [ i / 2 ] , 10 ) ; } else { dp [ i ] = ( dp [ x ] * dp [ y ] ) % M ; } } return ( dp [ N ] * dp [ N ] ) % M ; } int main ( ) { long long n = 150 ; cout << findValue ( n ) ; return 0 ; }