Muennighoff/xP3x-sample huggingface dataset:
inputs: "Find a Fixed Point ( Value equal to index ) in a given array | Java program to check fixed point in an array using linear search ; If no fixed point present then return - 1 ; main function"
Solution in Java:
targets: class Main { static int linearSearch ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == i ) return i ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 } ; int n = arr . length ; System . out . println ( " Fixed ▁ Point ▁ is ▁ " + linearSearch ( arr , n ) ) ; } }
inputs: "Count of Octal numbers upto N digits | C # program to find the count of natural octal numbers upto N digits ; Function to return the count of natural octal numbers upto N digits ; Loop to iterate from 1 to N and calculating number of octal numbers for every ' i ' th digit . ; Driver code"
Solution in C#:
targets: using System ; class GFG { static int count ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( int ) ( 7 * Math . Pow ( 8 , i - 1 ) ) ; } return sum ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( count ( N ) ) ; } }
inputs: "Iterative function to check if two trees are identical | Iterative Java program to check if two ; A Binary Tree Node ; Iterative method to find height of Binary Tree ; Return true if both trees are empty ; Return false if one is empty and other is not ; Create an empty queues for simultaneous traversals ; Enqueue Roots of trees in respective queues ; Get front nodes and compare them ; Remove front nodes from queues ; Enqueue left children of both nodes ; If one left child is empty and other is not ; Right child code ( Similar to left child code ) ; Utility function to create a new tree node ; Driver program to test above functions"
Solution in Java:
targets: import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static boolean areIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new LinkedList < Node > ( ) ; Queue < Node > q2 = new LinkedList < Node > ( ) ; q1 . add ( root1 ) ; q2 . add ( root2 ) ; while ( ! q1 . isEmpty ( ) && ! q2 . isEmpty ( ) ) { Node n1 = q1 . peek ( ) ; Node n2 = q2 . peek ( ) ; if ( n1 . data != n2 . data ) return false ; q1 . remove ( ) ; q2 . remove ( ) ; if ( n1 . left != null && n2 . left != null ) { q1 . add ( n1 . left ) ; q2 . add ( n2 . left ) ; } else if ( n1 . left != null n2 . left != null ) return false ; if ( n1 . right != null && n2 . right != null ) { q1 . add ( n1 . right ) ; q2 . add ( n2 . right ) ; } else if ( n1 . right != null n2 . right != null ) return false ; } return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; if ( areIdentical ( root1 , root2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
inputs: "Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code"
Solution in php:
targets: < ? php function findSum ( $ n ) { $ n -- ; $ sum = 0 ; $ sum += ( $ n * ( $ n + 1 ) ) / 2 ; $ sum += ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) ) / 6 ; return $ sum ; } $ n = 3 ; echo findSum ( $ n ) ; ? >
inputs: "Program to find Nth term divisible by a or b | Java program to find nth term divisible by a or b ; Function to return gcd of a and b ; Function to calculate how many numbers from 1 to num are divisible by a or b ; calculate number of terms divisible by a and by b then , remove the terms which is are divisible by both a and b ; Binary search to find the nth term divisible by a or b ; set low to 1 and high to max ( a , b ) * n , here we have taken high as 10 ^ 18 ; if the current term is less than n then we need to increase low to mid + 1 ; if current term is greater than equal to n then high = mid ; Driver code"
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 int divTermCount ( int a , int b , int lcm , int num ) { return num / a + num / b - num / lcm ; } static int findNthTerm ( int a , int b , int n ) { int low = 1 , high = Integer . MAX_VALUE , mid ; int lcm = ( a * b ) / gcd ( a , b ) ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , lcm , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , n = 10 ; System . out . println ( findNthTerm ( a , b , n ) ) ; } }
inputs: "Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | Java program for the above approach ; Function to find area of rectangle inscribed another rectangle of length L and width W ; Area of rectangle ; Return the area ; Driver Code ; Given dimensions ; Function call"
Solution in Java:
targets: import java . io . * ; import java . util . * ; class GFG { static double AreaofRectangle ( int L , int W ) { double area = ( W + L ) * ( W + L ) / 2 ; return area ; } public static void main ( String args [ ] ) { int L = 18 ; int W = 12 ; System . out . println ( AreaofRectangle ( L , W ) ) ; } }
inputs: Print unique rows in a given boolean matrix | Given a binary matrix of M X N of integers , you need to return only unique rows of binary array ; A Trie node ; Only two children needed for 0 and 1 ; A utility function to allocate memory for a new Trie node ; Inserts a new matrix row to Trie . If row is already present , then returns 0 , otherwise insets the row and return 1 ; base case ; Recur if there are more entries in this row ; If all entries of this row are processed ; unique row found , return 1 ; duplicate row found , return 0 ; A utility function to print a row ; The main function that prints all unique rows in a given matrix . ; create an empty Trie ; Iterate through all rows ; insert row to TRIE ; unique row found , print it ; Driver program to test above functions
How can the above be solved in C?
targets: #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW  4
#define COL  5
typedef struct Node { bool isEndOfCol ; struct Node * child [ 2 ] ; } Node ; Node * newNode ( ) { Node * temp = ( Node * ) malloc ( sizeof ( Node ) ) ; temp -> isEndOfCol = 0 ; temp -> child [ 0 ] = temp -> child [ 1 ] = NULL ; return temp ; } bool insert ( Node * * root , int ( * M ) [ COL ] , int row , int col ) { if ( * root == NULL ) * root = newNode ( ) ; if ( col < COL ) return insert ( & ( ( * root ) -> child [ M [ row ] [ col ] ] ) , M , row , col + 1 ) ; else { if ( ! ( ( * root ) -> isEndOfCol ) ) return ( * root ) -> isEndOfCol = 1 ; return 0 ; } } void printRow ( int ( * M ) [ COL ] , int row ) { int i ; for ( i = 0 ; i < COL ; ++ i ) printf ( " % d ▁ " , M [ row ] [ i ] ) ; printf ( " 
 " ) ; } void findUniqueRows ( int ( * M ) [ COL ] ) { Node * root = NULL ; int i ; for ( i = 0 ; i < ROW ; ++ i ) if ( insert ( & root , M , i , 0 ) ) printRow ( M , i ) ; } int main ( ) { int M [ ROW ] [ COL ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 0 , 0 } } ; findUniqueRows ( M ) ; return 0 ; }
inputs: "Check if given string is a substring of string formed by repeated concatenation of z to a | C # program for the above approach ; Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Driver code ; Given string ; Function call"
Solution in C#:
targets: using System ; class GFG { public static void checkInfinite ( String s ) { bool flag = true ; int N = s . Length ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ( char ) ( ( int ) ( s [ i + 1 ] ) + 1 ) ) { continue ; } else if ( s [ i ] == ' a ' && s [ i + 1 ] == ' z ' ) { continue ; } else { flag = false ; break ; } } if ( ! flag ) Console . Write ( " NO " ) ; else Console . Write ( " YES " ) ; } public static void Main ( String [ ] args ) { String s = " ecbaz " ; checkInfinite ( s ) ; } }
inputs: "Largest gap in an array | function to solve the given problem ; Driver Code"
Solution in php:
targets: < ? php function solve ( $ a , $ n ) { $ max1 = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( abs ( $ a [ $ i ] - $ a [ $ j ] ) > $ max1 ) { $ max1 = abs ( $ a [ $ i ] - $ a [ $ j ] ) ; } } } return $ max1 ; } $ arr = array ( -1 , 2 , 3 , -4 , -10 , 22 ) ; $ size = count ( $ arr ) ; echo " Largest ▁ gap ▁ is ▁ : ▁ " , solve ( $ arr , $ size ) ; ? >
inputs: "Add two numbers without using arithmetic operators |"
Solution in C:
targets: int Add ( int x , int y ) { if ( y == 0 ) return x ; else return Add ( x ^ y , ( x & y ) << 1 ) ; }
inputs: "Number of unique triplets whose XOR is zero | C # program to count the number of unique triplets whose XOR is 0 ; function to count the number of unique triplets whose xor is 0 ; To store values that are present ; stores the count of unique triplets ; traverse for all i , j pairs such that j > i ; xor of a [ i ] and a [ j ] ; if xr of two numbers is present , then increase the count ; returns answer ; Driver code"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] a , int n ) { List < int > s = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( a [ i ] ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xr = a [ i ] ^ a [ j ] ; if ( s . Exists ( item => item == xr ) && xr != a [ i ] && xr != a [ j ] ) count ++ ; } } return count / 3 ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 3 , 5 , 10 , 14 , 15 } ; int n = a . Length ; Console . Write ( countTriplets ( a , n ) ) ; } }
inputs: "Maximum absolute difference of value and index sums | C # program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver program"
How can the above be solved in C-Sharp?
targets: using System ; public class MaximumAbsoluteDifference { private static int calculateDiff ( int i , int j , int [ ] array ) { return Math . Abs ( array [ i ] - array [ j ] ) + Math . Abs ( i - j ) ; } private static int maxDistance ( int [ ] array ) { int result = 0 ; for ( int i = 0 ; i < array . Length ; i ++ ) { for ( int j = i ; j < array . Length ; j ++ ) { result = Math . Max ( result , calculateDiff ( i , j , array ) ) ; } } return result ; } public static void Main ( ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; Console . WriteLine ( maxDistance ( array ) ) ; } }
inputs: "Generate all numbers up to N in Lexicographical Order | Python program for the above approach ; Driver code"
Solution in Python:
targets: def lexNumbers ( n ) :
	sol = [ ]
	dfs ( 1 , n , sol )
	print ( " [ " , sol [ 0 ] , end = " " , sep = " " )
	for i in range ( 1 , n ) :
		print ( " , ▁ " , sol [ i ] , end = " " , sep = " " ) print ( " ] " )
def dfs ( temp , n , sol ) :
	if ( temp > n ) :
		return
	sol . append ( temp )
	dfs ( temp * 10 , n , sol )
	if ( temp % 10 != 9 ) :
		dfs ( temp + 1 , n , sol )
n = 15
lexNumbers ( n )
inputs: "Minimum value to be assigned to the elements so that sum becomes greater than initial sum | Function to return the minimum required value ; Find the sum of the array elements ; Return the required value ; Driver code"
Solution in Python:
targets: def findMinValue ( arr , n ) :
	sum = 0
	for i in range ( n ) :
		sum += arr [ i ]
	return ( sum // n ) + 1
arr = [ 4 , 2 , 1 , 10 , 6 ]
n = len ( arr )
print ( findMinValue ( arr , n ) )
inputs: "Count of integers from the range [ 0 , N ] whose digit sum is a multiple of K | C ++ implementation of the approach ; To store the states of the dp ; Function to return the count of numbers from the range [ 0 , n ] whose digit sum is a multiple of k using bottom - up dp ; The digit in this index can only be from [ 0 , num [ idx ] ] ; The digit in this index can be anything from [ 0 , 9 ] ; new_tight is the flag value for the next position ; res can 't be negative ; Function to process the string to a vector of digits from MSD to LSD ; Driver code ; For large input number n ; Total number of digits in n ; Clean dp table ; Process the string to a vector of digits from MSD to LSD"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; #define MAX  100005
#define MOD  1000000007
int dp [ MAX ] [ 101 ] [ 2 ] ; int countNum ( int idx , int sum , int tight , vector < int > num , int len , int k ) { if ( len == idx ) { if ( sum == 0 ) return 1 ; else return 0 ; } if ( dp [ idx ] [ sum ] [ tight ] != -1 ) return dp [ idx ] [ sum ] [ tight ] ; int res = 0 , limit ; if ( tight == 0 ) { limit = num [ idx ] ; } else { limit = 9 ; } for ( int i = 0 ; i <= limit ; i ++ ) { int new_tight = tight ; if ( tight == 0 && i < limit ) new_tight = 1 ; res += countNum ( idx + 1 , ( sum + i ) % k , new_tight , num , len , k ) ; res %= MOD ; } if ( res < 0 ) res += MOD ; return dp [ idx ] [ sum ] [ tight ] = res ; } vector < int > process ( string s ) { vector < int > num ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { num . push_back ( s [ i ] - '0' ) ; } return num ; } int main ( ) { string n = "98765432109876543210" ; int len = n . length ( ) ; int k = 58 ; memset ( dp , -1 , sizeof ( dp ) ) ; vector < int > num = process ( n ) ; cout << countNum ( 0 , 0 , 0 , num , len , k ) ; return 0 ; }
inputs: "Babylonian method for square root |  ; Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Driver program to test above function"
How can the above be solved in C++?
targets: #include <iostream>
using namespace std ; class gfg { public : float squareRoot ( float n ) { float x = n ; float y = 1 ; float e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } } ; int main ( ) { gfg g ; int n = 50 ; cout << " Square ▁ root ▁ of ▁ " << n << " ▁ is ▁ " << g . squareRoot ( n ) ; getchar ( ) ; }
inputs: "Bitwise OR of Bitwise AND of all subarrays of an array | Function to find the Bitwise OR of Bitwise AND of all consecutive subsets of the array ; Stores the required result ; Traverse the given array ; Print the result ; Driver Code"
How can the above be solved in JS?
targets: function findbitwiseOR ( a , n ) { var res = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; document . write ( res ) ; } var A = [ 1 , 2 , 3 ] ; var N = A . length ; findbitwiseOR ( A , N ) ;
inputs: "Encrypt string with product of number of vowels and consonants in substring of size k | C # Program to Encrypt String with product of number of vowels and consonants in subString of size k ; isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the String ; cv to count vowel cc to count consonants ; Counting prefix count of vowel and prefix count of consonants ; generating the encrypted String . ; Driver Code"
Solution in C#:
targets: using System ; class GFG { static bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static String encryptString ( char [ ] s , int n , int k ) { int [ ] cv = new int [ n ] ; int [ ] cc = new int [ n ] ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 0 : 1 ) ; } String ans = " " ; int prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += String . Join ( " " , prod ) ; for ( int i = k ; i < s . Length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += String . Join ( " " , prod ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = " hello " ; int n = s . Length ; int k = 2 ; Console . Write ( encryptString ( s . ToCharArray ( ) , n , k ) + " 
 " ) ; } }
inputs: "Sum of squares of binomial coefficients | C # Program to find the sum of square of binomial coefficient . ; Return the sum of square of binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Finding the sum of square of binomial coefficient . ; Driver function"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int sumofsquare ( int n ) { int [ , ] C = new int [ n + 1 , n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , n ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } int sum = 0 ; for ( i = 0 ; i <= n ; i ++ ) sum += ( C [ n , i ] * C [ n , i ] ) ; return sum ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( sumofsquare ( n ) ) ; } }
inputs: "Dynamic Programming | High | Returns maximum amount of task that can be done till day n ; If n is less than equal to 0 , then no solution exists ; Determines which task to choose on day n , then returns the maximum till that day ; Driver Code"
How can the above be solved in PHP?
targets: < ? php function maxTasks ( $ high , $ low , $ n ) { if ( $ n <= 0 ) return 0 ; return max ( $ high [ $ n - 1 ] + maxTasks ( $ high , $ low , ( $ n - 2 ) ) , $ low [ $ n - 1 ] + maxTasks ( $ high , $ low , ( $ n - 1 ) ) ) ; } $ n = 5 ; $ high = array ( 3 , 6 , 8 , 7 , 6 ) ; $ low = array ( 1 , 5 , 4 , 5 , 3 ) ; print ( maxTasks ( $ high , $ low , $ n ) ) ; ? >
inputs: "Reverse vowels in a given string | utility function to check for vowel ; Function to reverse order of vowels ; Storing the vowels separately ; Placing the vowels in the reverse order in the string ; Driver code"
How can the above be solved in JS?
targets: function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function reverseVowel ( str1 ) { let j = 0 ; let str = str1 . split ( ' ' ) ; let vowel = " " ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { j ++ ; vowel += str [ i ] ; } } for ( let i = 0 ; i < str . length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { str [ i ] = vowel [ -- j ] ; } } return str . join ( " " ) ; } let str = " " ; document . write ( reverseVowel ( str ) ) ;
inputs: "Minimum changes required to make a Catalan Sequence | PHP implementation of the approach ; To store first N Catalan numbers ; Function to find first n Catalan numbers ; Initialize first two values in table ; Filong entries in catalan [ ] using recursive formula ; Function to return the minimum changes required ; Find first n Catalan Numbers ; a and b are first two Catalan Sequence numbers ; Insert first n catalan elements to set ; If catalan element is present in the array then remove it from set ; Return the remaining number of elements in the set ; Driver code"
Solution in php:
targets: < ? php $ MAX = 1000 ; $ catalan = array_fill ( 0 , $ MAX , 0 ) ; function catalanDP ( $ n ) { global $ catalan ; $ catalan [ 0 ] = $ catalan [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ catalan [ $ i ] = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { $ catalan [ $ i ] += $ catalan [ $ j ] * $ catalan [ $ i - $ j - 1 ] ; } } } function CatalanSequence ( $ arr , $ n ) { global $ catalan ; catalanDP ( $ n ) ; $ s = array ( ) ; $ a = $ b = 1 ; array_push ( $ s , $ a ) ; if ( $ n >= 2 ) { array_push ( $ s , $ b ) ; } for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { array_push ( $ s , $ catalan [ $ i ] ) ; } $ s = array_unique ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( in_array ( $ arr [ $ i ] , $ s ) ) { unset ( $ s [ array_search ( $ arr [ $ i ] , $ s ) ] ) ; } } return count ( $ s ) ; } $ arr = array ( 1 , 1 , 2 , 5 , 41 ) ; $ n = count ( $ arr ) ; print ( CatalanSequence ( $ arr , $ n ) ) ; ? >
inputs: "Count of right shifts for each array element to be in its sorted position | Function to find the right shifts required for each element to reach its sorted array position in A [ ] ; Stores required number of shifts for each element ; If the element is at sorted position ; Otherwise ; Calculate right shift ; Print the respective shifts ; Driver Code"
Solution in Python:
targets: def findShifts ( A , N ) :
	shift = [ 0 for i in range ( N ) ]
	for i in range ( N ) :
		if ( i == A [ i ] - 1 ) :
			shift [ i ] = 0
		else :
			shift [ i ] = ( A [ i ] - 1 - i + N ) % N
	for i in range ( N ) :
		print ( shift [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 4 , 3 , 2 , 5 ]
	N = len ( arr )
	findShifts ( arr , N )
inputs: "Turn off the rightmost set bit | Set 2 | Unsets the rightmost set bit of n and returns the result ; Checking whether bit position is set or not ; If bit position is found set , we flip this bit by xoring given number and number with bit position set ; Driver code"
Solution in Python:
targets: def FlipBits ( n ) :
	for bit in range ( 32 ) :
		if ( ( n >> bit ) & 1 ) :
			n = n ^ ( 1 << bit )
			break
	print ( " The ▁ number ▁ after ▁ unsetting ▁ the " , end = " ▁ " )
	print ( " rightmost ▁ set ▁ bit " , n )
if __name__ == ' _ _ main _ _ ' :
	N = 12 ;
	FlipBits ( N )
inputs: "Count set bits in an integer |  ; Check each bit in a number is set or not and return the total count of the set bits . ; ( 1 << i ) = pow ( 2 , i ) ; Driver code"
Solution in Java:
targets: public class GFG { static int countSetBits ( int N ) { int count = 0 ; for ( int i = 0 ; i < 4 * 8 ; i ++ ) { if ( ( N & ( 1 << i ) ) != 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 15 ; System . out . println ( countSetBits ( N ) ) ; } }
inputs: "Pascal 's Triangle | binomialCoeff ; Function to print first n lines of Pascal 's Triangle ; Iterate through every line and print entries in it ; Every line has number of integers equal to line number ; Driver Code"
Solution 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 printPascal ( $ n ) { for ( $ line = 0 ; $ line < $ n ; $ line ++ ) { for ( $ i = 0 ; $ i <= $ line ; $ i ++ ) echo " " . binomialCoeff ( $ line , $ i ) . " ▁ " ; echo " 
 " ; } } $ n = 7 ; printPascal ( $ n ) ; ? >
inputs: "Check if any permutation of string is a K times repeated string | Function to check that permutation of the given string is a K times repeating String ; If length of string is not divisible by K ; Frequency Array ; Initially frequency of each character is 0 ; Computing the frequency of each character in the string ; Loop to check that frequency of every character of the string is divisible by K ; Driver Code"
Solution in Python:
targets: def repeatingString ( s , n , k ) :
	if ( n % k != 0 ) :
		return False
	frequency = [ 0 for i in range ( 123 ) ]
	for i in range ( 123 ) :
		frequency [ i ] = 0
	for i in range ( n ) :
		frequency [ s [ i ] ] += 1
	repeat = n // k
	for i in range ( 123 ) :
		if ( frequency [ i ] % repeat != 0 ) :
			return False
	return True
if __name__ == ' _ _ main _ _ ' :
	s = " abcdcba "
	n = len ( s )
	k = 3
	if ( repeatingString ( s , n , k ) ) :
		print ( " Yes " )
	else :
		print ( " No " )
inputs: "Case | C # implementation of the approach ; Function to return the sorted string ; Vectors to store the lowercase and uppercase characters ; Sort both the vectors ; If current character is lowercase then pick the lowercase character from the sorted list ; Else pick the uppercase character ; Return the sorted string ; Driver code"
Solution in C#:
targets: using System ; using System . Collections . Generic ; class GFG { public static String getSortedString ( char [ ] s , int n ) { List < char > v1 = new List < char > ( ) ; List < char > v2 = new List < char > ( ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] > ' a ' && s [ i ] <= ' z ' ) v1 . Add ( s [ i ] ) ; if ( s [ i ] > ' A ' && s [ i ] <= ' z ' ) v2 . Add ( s [ i ] ) ; } v1 . Sort ( ) ; v2 . Sort ( ) ; int j = 0 ; i = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( s [ k ] > ' a ' && s [ k ] <= ' z ' ) { s [ k ] = v1 [ i ] ; ++ i ; } else if ( s [ k ] > ' A ' && s [ k ] <= ' Z ' ) { s [ k ] = v2 [ j ] ; ++ j ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " gEeksfOrgEEkS " ; int n = s . Length ; Console . WriteLine ( getSortedString ( s . ToCharArray ( ) , n ) ) ; } }
inputs: "Curzon Numbers | Function to check if a number is a Curzon number or not ; Find 2 ^ N + 1 ; Find 2 * N + 1 ; Check for divisibility ; Driver code"
Solution in Javascript:
targets: function checkIfCurzonNumber ( N ) { var powerTerm , productTerm ; powerTerm = Math . pow ( 2 , N ) + 1 ; productTerm = 2 * N + 1 ; if ( powerTerm % productTerm == 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " ) ; } } var N = 5 ; checkIfCurzonNumber ( N ) ; N = 10 ; checkIfCurzonNumber ( N ) ;
inputs: "Maximize difference between sum of even and odd | Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] o 1 - based indexing ; Reverse the array ; Convert arr [ ] o 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call"
How can the above be solved in Python?
targets: def maxPossibleDiff ( arr , N ) :
	arr . append ( - 1 )
	arr = arr [ : : - 1 ]
	arr . append ( - 1 )
	arr = arr [ : : - 1 ]
	maxDiff = 0
	for i in range ( 1 , N + 1 ) :
		if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) :
			maxDiff += arr [ i ]
		if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) :
			maxDiff -= arr [ i ]
	print ( maxDiff )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ]
	N = len ( arr )
	maxPossibleDiff ( arr , N )
inputs: Majority Element | Program for finding out majority element in an array ; Function to find the candidate for Majority ; Function to check if the candidate occurs more than n / 2 * times ; Function to print Majority Element ; Find the candidate for Majority ; Print the candidate if it is Majority ; Driver code ; Function call
How can the above be solved in C?
targets: #include <stdio.h>
#define bool  int
int findCandidate ( int * , int ) ; bool isMajority ( int * , int , int ) ; int findCandidate ( int a [ ] , int size ) { int maj_index = 0 , count = 1 ; int i ; for ( i = 1 ; i < size ; i ++ ) { if ( a [ maj_index ] == a [ i ] ) count ++ ; else count -- ; if ( count == 0 ) { maj_index = i ; count = 1 ; } } return a [ maj_index ] ; } bool isMajority ( int a [ ] , int size , int cand ) { int i , count = 0 ; for ( i = 0 ; i < size ; i ++ ) if ( a [ i ] == cand ) count ++ ; if ( count > size / 2 ) return 1 ; else return 0 ; } void printMajority ( int a [ ] , int size ) { int cand = findCandidate ( a , size ) ; if ( isMajority ( a , size , cand ) ) printf ( " ▁ % d ▁ " , cand ) ; else printf ( " No ▁ Majority ▁ Element " ) ; } int main ( ) { int a [ ] = { 1 , 3 , 3 , 1 , 2 } ; int size = ( sizeof ( a ) ) / sizeof ( a [ 0 ] ) ; printMajority ( a , size ) ; getchar ( ) ; return 0 ; }
inputs: "Product of all the pairs from the given array | Java implementation to Find the product of all the pairs from the given array ; Function to calculate ( x ^ y ) % 1000000007 ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; Return the final result ; Function to return the product of the elements of all possible pairs from the array ; To store the required product ; Iterate for every element of the array ; Each element appears ( 2 * n ) times ; Driver code"
Solution in Java:
targets: import java . util . * ; class GFG { static final int mod = 1000000007 ; static int power ( int x , int y ) { int p = 1000000007 ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int productPairs ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = ( product % mod * ( int ) power ( arr [ i ] , ( 2 * n ) ) % mod ) % mod ; } return product % mod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( productPairs ( arr , n ) ) ; } }
inputs: "Check whether a number can be represented by sum of two squares | C # program to Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; If any prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { public static bool judgeSquareSum ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n /= i ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } static public void Main ( ) { int n = 17 ; if ( judgeSquareSum ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
inputs: "Split squares of first N natural numbers into two sets with minimum absolute difference of their sums | Function to partition squares of N natural number in two subset ; Store the count of blocks of size 8 ; Partition of block of 8 element ; Store the minimum subset difference ; Partition of N elements to minimize their subset sum difference ; Store elements of subset A and B ; If element is of type A ; If the element is of type B ; Print the minimum subset difference ; Print the first subset ; Print the second subset ; Driver Code ; Function call"
Solution in Python:
targets: def minimumSubsetDifference ( N ) :
	blockOfSize8 = N // 8
	str = " ABBABAAB "
	subsetDifference = 0
	partition = " "
	while blockOfSize8 != 0 :
		partition = partition + str
		blockOfSize8 = blockOfSize8 - 1
	A = [ ]
	B = [ ]
	for i in range ( N ) :
		if partition [ i ] == ' A ' :
			A . append ( ( i + 1 ) * ( i + 1 ) )
		else :
			B . append ( ( i + 1 ) * ( i + 1 ) )
	print ( subsetDifference )
	for i in A :
		print ( i , end = " ▁ " )
	print ( )
	for i in B :
		print ( i , end = " ▁ " )
N = 8
minimumSubsetDifference ( N )
inputs: "Find Prime number just less than and just greater each element of given Array | C ++ program for the above approach ; Utility function to check for primality of a number X by checking whether X haACCs any factors other than 1 and itself . ; if ( X % i == 0 ) Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Driver code ; Input ; Function call"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; bool isPrime ( int X ) { for ( int i = 2 ; i * i <= X ; i ++ ) return false ; return true ; } void printPrimes ( int A [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { cout << j << " ▁ " ; break ; } } for ( int j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { cout << j << " ▁ " ; break ; } } cout << endl ; } } int main ( ) { int A [ ] = { 17 , 28 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; printPrimes ( A , N ) ; return 0 ; }
inputs: "Modify string by removing vowels in between two consonants | Java program to remove all Vowels in between two consonants from the string ; Function to check if the character x is a vowel or not ; Returns the updated string formed after removing all the Sandwiched Vowels from the given string ; string to store the Updated String after removing the Sandwiched Vowels ; traverse the string from left to right ; if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched ' ; Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string ; if this character is not a sandwiched Vowel append it to the updated String ; Driver Code ; Remove all the Sandwitched Vowels"
Solution in Java:
targets: import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean isVowel ( char x ) { if ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) return true ; else return false ; } static String updateSandwichedVowels ( String a ) { int n = a . length ( ) ; String updatedString = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 i == n - 1 ) { updatedString += a . charAt ( i ) ; continue ; } if ( isVowel ( a . charAt ( i ) ) == true && isVowel ( a . charAt ( i - 1 ) ) == false && isVowel ( a . charAt ( i + 1 ) ) == false ) { continue ; } updatedString += a . charAt ( i ) ; } return updatedString ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String updatedString = updateSandwichedVowels ( str ) ; System . out . print ( updatedString ) ; } }
inputs: "Majority Element | Function to find Majority element in an array it returns - 1 if there is no majority element ; sort the array in O ( nlogn ) ; increases the count if the same element occurs otherwise starts counting new element ; sets maximum count and stores maximum occured element so far if maximum count becomes greater than n / 2 it breaks out setting the flag ; returns maximum occured element if there is no such element , returns - 1 ; Driver code ; Function calling"
Solution in Python:
targets: def majorityElement ( arr , n ) :
	arr . sort ( )
	count , max_ele , temp , f = 1 , - 1 , arr [ 0 ] , 0
	for i in range ( 1 , n ) :
		if ( temp == arr [ i ] ) :
			count += 1
		else :
			count = 1
			temp = arr [ i ]
		if ( max_ele < count ) :
			max_ele = count
			ele = arr [ i ]
			if ( max_ele > ( n // 2 ) ) :
				f = 1
				break
	if f == 1 :
		return ele
	else :
		return - 1
arr = [ 1 , 1 , 2 , 1 , 3 , 5 , 1 ]
n = len ( arr )
print ( majorityElement ( arr , n ) )
inputs: "Sum of all differences between Maximum and Minimum of increasing Subarrays | Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver Code"
Solution in Python:
targets: def sum_of_differences ( arr , N ) :
	sum = 0
	i = 0
	while ( i < N - 1 ) :
		if arr [ i ] < arr [ i + 1 ] :
			flag = 0
			for j in range ( i + 1 , N - 1 ) :
				if arr [ j ] >= arr [ j + 1 ] :
					sum += ( arr [ j ] - arr [ i ] )
					i = j
					flag = 1
					break
			if flag == 0 and arr [ i ] < arr [ N - 1 ] :
				sum += ( arr [ N - 1 ] - arr [ i ] )
				break
		i += 1
	return sum
arr = [ 6 , 1 , 2 , 5 , 3 , 4 ]
N = len ( arr )
print ( sum_of_differences ( arr , N ) )
inputs: "Program to print ' N ' alphabet using the number pattern from 1 to n | C ++ implementation of the approach ; Function to print the desired Alphabet N Pattern ; Declaring the values of Right , Left and Diagonal values ; Main Loop for the rows ; For the left Values ; Spaces for the diagonals ; Condition for the diagonals ; Spaces for the Right Values ; For the right values ; Driver Code ; Size of the Pattern ; Calling the function to print the desired Pattern"
Solution in C++:
targets: #include <iostream>
using namespace std ; void Alphabet_N_Pattern ( int N ) { int index , side_index , size ; int Right = 1 , Left = 1 , Diagonal = 2 ; for ( index = 0 ; index < N ; index ++ ) { cout << Left ++ ; for ( side_index = 0 ; side_index < 2 * ( index ) ; side_index ++ ) cout << " ▁ " ; if ( index != 0 && index != N - 1 ) cout << Diagonal ++ ; else cout << " ▁ " ; for ( side_index = 0 ; side_index < 2 * ( N - index - 1 ) ; side_index ++ ) cout << " ▁ " ; cout << Right ++ ; cout << endl ; } } int main ( int argc , char * * argv ) { int Size = 6 ; Alphabet_N_Pattern ( Size ) ; }
inputs: "Minimize coins required to obtain all possible values up to N | Function to find minimum count of { 1 , 2 , 5 } valued coins required to make a change of all values in the range [ 1 , N ] ; Number of 5 valueds coins required ; Number of 1 valued coins required ; Number of 2 valued coins required ; Driver Code"
Solution in Python:
targets: def find ( N ) :
	F = int ( ( N - 4 ) / 5 )
	if ( ( N - 5 * F ) % 2 ) == 0 :
		O = 2
	else :
		O = 1
	T = ( N - 5 * F - O ) // 2
	print ( " Count ▁ of ▁ 5 ▁ valueds ▁ coins : ▁ " , F )
	print ( " Count ▁ of ▁ 2 ▁ valueds ▁ coins : ▁ " , T )
	print ( " Count ▁ of ▁ 1 ▁ valueds ▁ coins : ▁ " , O )
if __name__ == ' _ _ main _ _ ' :
	N = 8
	find ( N )
inputs: "Rearrange an array to minimize sum of product of consecutive pair elements | Java program to sort an array such that sum of product of alternate element is minimum . ; create evenArr [ ] and oddArr [ ] ; sort main array in ascending order ; Put elements in oddArr [ ] and evenArr [ ] as per desired value . ; sort evenArr [ ] in descending order ; merge both sub - array and calculate minimum sum of product of alternate elements ; Driver program"
Solution in Java:
targets: import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; import java . util . Vector ; class GFG { static int minSum ( int arr [ ] , int n ) { Vector < Integer > evenArr = new Vector < > ( ) ; Vector < Integer > oddArr = new Vector < > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) { oddArr . add ( arr [ i ] ) ; } else { evenArr . add ( arr [ i ] ) ; } } Comparator comparator = Collections . reverseOrder ( ) ; Collections . sort ( evenArr , comparator ) ; int i = 0 , sum = 0 ; for ( int j = 0 ; j < evenArr . size ( ) ; j ++ ) { arr [ i ++ ] = evenArr . get ( j ) ; arr [ i ++ ] = oddArr . get ( j ) ; sum += evenArr . get ( j ) * oddArr . get ( j ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; System . out . println ( " Minimum ▁ required ▁ sum ▁ = ▁ " + minSum ( arr , n ) ) ; System . out . println ( " Sorted ▁ array ▁ in ▁ required ▁ format ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
inputs: "Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers | C ++ program for above approach ; Function to calculate the probability for the given sum to be equal to sum in N throws of dice ; Base cases ; Driver Code ; Print the answer"
How can the above be solved in C++?
targets: #include <bits/stdc++.h>
using namespace std ; long double find ( int N , int sum ) { if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return 1.0 / 6 ; else return 0 ; } long double s = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) s = s + find ( N - 1 , sum - i ) / 6 ; return s ; } int main ( ) { int N = 4 , a = 13 , b = 17 ; long double probability = 0.0 ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; cout << fixed << setprecision ( 6 ) << probability ; return 0 ; }
inputs: "Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers | C # program for the above approach ; Function to calculate the probability for the given sum to be equal to sum in N throws of dice ; Base cases ; Driver code ; Print the answer"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static double find ( int N , int sum ) { if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return 1.0 / 6 ; else return 0 ; } double s = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) s = s + find ( N - 1 , sum - i ) / 6 ; return s ; } static void Main ( ) { int N = 4 , a = 13 , b = 17 ; double probability = 0.0 ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; Console . WriteLine ( Math . Round ( probability , 6 ) ) ; } }
inputs: "Maximum number of operations required such that no pairs from a Matrix overlap | C # program to implement the above approach ; Function to find maximum count of operations ; Initialize count by 0 ; Iterate over remaining pairs ; Check if first operation is applicable ; Check if 2 nd operation is applicable ; Otherwise ; Return the count of operations ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int find_max ( List < pair > v , int n ) { int count = 0 ; if ( n >= 2 ) count = 2 ; else count = 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( v [ i - 1 ] . first < ( v [ i ] . first - v [ i ] . second ) ) count ++ ; else if ( v [ i + 1 ] . first > ( v [ i ] . first + v [ i ] . second ) ) { count ++ ; v [ i ] . first = v [ i ] . first + v [ i ] . second ; } else continue ; } return count ; } public static void Main ( String [ ] args ) { int n = 3 ; List < pair > v = new List < pair > ( ) ; v . Add ( new pair ( 10 , 20 ) ) ; v . Add ( new pair ( 15 , 10 ) ) ; v . Add ( new pair ( 20 , 16 ) ) ; Console . Write ( find_max ( v , n ) ) ; } }
inputs: "Smallest element greater than X not present in the array | C # implementation of the approach ; Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code"
How can the above be solved in C-Sharp?
targets: using System ; class GFG { static int Next_greater ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 5 , 10 , 4 , 7 } ; int x = 4 ; int n = a . Length ; Console . WriteLine ( Next_greater ( a , n , x ) ) ; } }
inputs: "Perimeter of the Union of Two Rectangles | C # program for the above approach ; Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code"
How can the above be solved in C-Sharp?
targets: using System ; using System . Linq ; public class GFG { static bool doIntersect ( int [ ] X , int [ ] Y ) { if ( X [ 0 ] > X [ 3 ] X [ 2 ] > X [ 1 ] ) return false ; if ( Y [ 0 ] > Y [ 3 ] Y [ 2 ] > Y [ 1 ] ) return false ; return true ; } static int getUnionPerimeter ( int [ ] X , int [ ] Y ) { int perimeter = 0 ; if ( ! doIntersect ( X , Y ) ) { perimeter += 2 * ( Math . Abs ( X [ 1 ] - X [ 0 ] ) + Math . Abs ( Y [ 1 ] - Y [ 0 ] ) ) ; perimeter += 2 * ( Math . Abs ( X [ 3 ] - X [ 2 ] ) + Math . Abs ( Y [ 3 ] - Y [ 2 ] ) ) ; } else { int w = X . Max ( ) - X . Min ( ) ; int l = X . Max ( ) - Y . Min ( ) ; perimeter = 2 * ( l + w ) ; } return perimeter ; } public static void Main ( String [ ] args ) { int [ ] X = { - 1 , 2 , 4 , 6 } ; int [ ] Y = { 2 , 5 , 3 , 7 } ; Console . Write ( getUnionPerimeter ( X , Y ) ) ; } }
inputs: "Print all distinct even and odd prefix Bitwise XORs of first N natural numbers | Java approach for the above approach ; Print all distinct even & odd prefix Bitwise XORs from 1 to N ; Print the even number ; Print the odd number ; Driver Code"
How can the above be solved in Java?
targets: class GFG { static void evenOddBitwiseXOR ( int N ) { System . out . print ( " Even : ▁ " + 0 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i + " ▁ " ) ; } System . out . print ( "
"); System . out . print ( " Odd : ▁ " + 1 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i - 1 + " ▁ " ) ; } if ( N % 4 == 2 ) System . out . print ( N + 1 ) ; else if ( N % 4 == 3 ) System . out . print ( N ) ; } public static void main ( String [ ] args ) { int N = 6 ; evenOddBitwiseXOR ( N ) ; } }
inputs: "Count numbers having 0 as a digit | Returns 1 if x has 0 , else 0 ; Traverse through all digits of x to check if it has 0. ; If current digit is 0 , return true ; Returns count of numbers from 1 to n with 0 as digit ; Initialize count of numbers having 0 as digit . ; Traverse through all numbers and for every number check if it has 0. ; Driver program"
How can the above be solved in Python?
targets: def has0 ( x ) :
	while ( x != 0 ) :
		if ( x % 10 == 0 ) :
			return 1
		x = x // 10
	return 0
def getCount ( n ) :
	count = 0
	for i in range ( 1 , n + 1 ) :
		count = count + has0 ( i )
	return count
n = 107
print ( " Count ▁ of ▁ numbers ▁ from ▁ 1" , " ▁ to ▁ " , n , " ▁ is ▁ " , getCount ( n ) )
inputs: "Check if given array is almost sorted ( elements are at | function for checking almost sort ; One by one compare adjacents . ; Check whether resultant is sorted or not ; is resultant is sorted return true ; Driver Code"
Solution in Javascript:
targets: function almostSort ( A , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { let temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; i ++ ; } } for ( let i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } let A = [ 1 , 3 , 2 , 4 , 6 , 5 ] ; let n = A . length ; if ( almostSort ( A , n ) ) document . write ( " " ) ; else document . write ( " " ) ;