codeparrot/xlcost-text-to-code huggingface dataset:
Minimize the sum of the array according the given condition | C # implementation of the above approach ; Function to return the minimum sum ; sort the array to find the minimum element ; finding the number to divide ; Checking to what instance the sum has decreased ; getting the max difference ; Driver Code
using System ; class GFG { static void findMin ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; Array . Sort ( arr ) ; int min = arr [ 0 ] ; int max = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int num = arr [ i ] ; int total = num + min ; int j ; for ( j = 2 ; j <= num ; j ++ ) { if ( num % j == 0 ) { int d = j ; int now = ( num / d ) + ( min * d ) ; int reduce = total - now ; if ( reduce > max ) max = reduce ; } } } Console . WriteLine ( sum - max ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; findMin ( arr , n ) ; } }
Print all the permutation of length L using the elements of an array | Iterative | C # implementation for above approach ; Convert the number to Lth base and print the sequence ; Sequence is of length L ; Print the ith element of sequence ; Print all the permuataions ; There can be ( len ) ^ l permutations ; Convert i to len th base ; Driver code ; function call
using System ; class GFG { static void convert_To_Len_th_base ( int n , int [ ] arr , int len , int L ) { for ( int i = 0 ; i < L ; i ++ ) { Console . Write ( arr [ n % len ] ) ; n /= len ; } Console . WriteLine ( ) ; } static void print ( int [ ] arr , int len , int L ) { for ( int i = 0 ; i < ( int ) Math . Pow ( len , L ) ; i ++ ) { convert_To_Len_th_base ( i , arr , len , L ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int len = arr . Length ; int L = 2 ; print ( arr , len , L ) ; } }
Number of possible permutations when absolute difference between number of elements to the right and left are given | C # implementation of the above approach ; Function to find the number of permutations possible of the original array to satisfy the given absolute differences ; To store the count of each a [ i ] in a map ; if n is odd ; check the count of each whether it satisfy the given criteria or not ; there is only 1 way for middle element . ; for others there are 2 ways . ; now find total ways ; When n is even . ; there will be no middle element so for each a [ i ] there will be 2 ways ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int totalways ( int [ ] arr , int n ) { Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( cnt . ContainsKey ( arr [ i ] ) ) { cnt [ arr [ i ] ] = cnt [ arr [ i ] ] + 1 ; } else { cnt . Add ( arr [ i ] , 1 ) ; } } if ( n % 2 == 1 ) { int start = 0 , endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { if ( i == 0 ) { if ( cnt [ i ] != 1 ) { return 0 ; } } else { if ( cnt [ i ] != 2 ) { return 0 ; } } } int ways = 1 ; start = 2 ; endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } else if ( n % 2 == 0 ) { int start = 1 , endd = n - 1 ; for ( int i = 1 ; i <= endd ; i = i + 2 ) { if ( cnt [ i ] != 2 ) return 0 ; } int ways = 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } return int . MinValue ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 2 , 4 , 4 , 0 , 2 } ; Console . WriteLine ( totalways ( arr , N ) ) ; } }
Proizvolov 's Identity | C # program to implement proizvolov 's identity ; Function to implement proizvolov 's identity ; According to proizvolov 's identity ; Driver code ; Function call
using System ; class GFG { static int proizvolov ( int [ ] a , int [ ] b , int n ) { return n * n ; } public static void Main ( ) { int [ ] a = { 1 , 5 , 6 , 8 , 10 } ; int [ ] b = { 9 , 7 , 4 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( proizvolov ( a , b , n ) ) ; } }
Find the ln ( X ) and log10X with the help of expansion | C # code to Find the ln x and log < sub > 10 < / sub > x with the help of expansion ; Function to calculate ln x using expansion ; terminating value of the loop can be increased to improve the precision ; Function to calculate log10 x ; Driver Code ; setprecision ( 3 ) is used to display the output up to 3 decimal places
using System ; class GFG { static double calculateLnx ( double n ) { double num , mul , cal , sum = 0 ; num = ( n - 1 ) / ( n + 1 ) ; for ( int i = 1 ; i <= 1000 ; i ++ ) { mul = ( 2 * i ) - 1 ; cal = Math . Pow ( num , mul ) ; cal = cal / mul ; sum = sum + cal ; } sum = 2 * sum ; return sum ; } static double calculateLogx ( double lnx ) { return ( lnx / 2.303 ) ; } public static void Main ( String [ ] args ) { double lnx , logx , n = 5 ; lnx = calculateLnx ( n ) ; logx = calculateLogx ( lnx ) ; Console . WriteLine ( " ln ▁ " + n + " ▁ = ▁ " + lnx ) ; Console . WriteLine ( " log10 ▁ " + n + " ▁ = ▁ " + logx ) ; } }
Find the sum of elements of the Matrix generated by the given rules | C # implementation of the approach ; Function to return the required sum ; To store the sum ; For every row ; Update the sum as A appears i number of times in the current row ; Update A for the next row ; Return the sum ; Driver code
using System ; class GFG { static int sum ( int A , int B , int R ) { int sum = 0 ; for ( int i = 1 ; i <= R ; i ++ ) { sum = sum + ( i * A ) ; A = A + B ; } return sum ; } public static void Main ( ) { int A = 5 , B = 3 , R = 3 ; Console . Write ( sum ( A , B , R ) ) ; } }
Count total set bits in all numbers from 1 to n | Set 2 | C # implementation of the approach ; Function to return the sum of the count of set bits in the integers from 1 to n ; Ignore 0 as all the bits are unset ; To store the powers of 2 ; To store the result , it is initialized with n / 2 because the count of set least significant bits in the integers from 1 to n is n / 2 ; Loop for every bit required to represent n ; Total count of pairs of 0 s and 1 s ; totalPairs / 2 gives the complete count of the pairs of 1 s Multiplying it with the current power of 2 will give the count of 1 s in the current bit ; If the count of pairs was odd then add the remaining 1 s which could not be groupped together ; Next power of 2 ; Return the result ; Driver code
using System ; class GFG { static int countSetBits ( int n ) { n ++ ; int powerOf2 = 2 ; int cnt = n / 2 ; while ( powerOf2 <= n ) { int totalPairs = n / powerOf2 ; cnt += ( totalPairs / 2 ) * powerOf2 ; cnt += ( totalPairs % 2 == 1 ) ? ( n % powerOf2 ) : 0 ; powerOf2 <<= 1 ; } return cnt ; } public static void Main ( String [ ] args ) { int n = 14 ; Console . WriteLine ( countSetBits ( n ) ) ; } }
Find the height of a right | C # implementation of the approach ; Function to return the height of the right - angled triangle whose area is X times its base ; Driver code
using System ; class Gfg { static int getHeight ( int X ) { return ( 2 * X ) ; } public static void Main ( ) { int X = 35 ; Console . WriteLine ( getHeight ( X ) ) ; } }
Find sum of inverse of the divisors when sum of divisors and the number is given | C # implementation of above approach ; Function to return the sum of inverse of divisors ; Calculating the answer ; Return the answer ; Driver code ; Function call
using System ; class GFG { static double SumofInverseDivisors ( int N , int Sum ) { double ans = ( double ) ( Sum ) * 1.0 / ( double ) ( N ) ; return ans ; } static public void Main ( ) { int N = 9 ; int Sum = 13 ; Console . Write ( SumofInverseDivisors ( N , Sum ) ) ; } }
Number of triplets such that each value is less than N and each pair sum is a multiple of K | C # implementation of the approach ; Function to return the number of triplets ; Initializing the count array ; Storing the frequency of each modulo class ; If K is odd ; If K is even ; Driver Code ; Function Call
using System ; class GFG { static int NoofTriplets ( int N , int K ) { int [ ] cnt = new int [ K ] ; Array . Fill ( cnt , 0 , cnt . Length , 0 ) ; for ( int i = 1 ; i <= N ; i += 1 ) { cnt [ i % K ] += 1 ; } if ( ( K & 1 ) != 0 ) { return cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; } else { return ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K / 2 ] * cnt [ K / 2 ] * cnt [ K / 2 ] ) ; } } static public void Main ( ) { int N = 3 , K = 2 ; Console . Write ( NoofTriplets ( N , K ) ) ; } }
Find a number containing N | C # implementation of the approach ; Function to compute number using our deduced formula ; Initialize num to n - 1 ; Driver code
using System ; class GFG { static int findNumber ( int n ) { int num = n - 1 ; num = 2 * ( int ) Math . Pow ( 4 , num ) ; num = ( int ) Math . Floor ( num / 3.0 ) ; return num ; } static public void Main ( ) { int n = 5 ; Console . Write ( findNumber ( n ) ) ; } }
Find XOR of numbers from the range [ L , R ] | C # implementation of the approach ; Function to return the XOR of elements from the range [ 1 , n ] ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to return the XOR of elements from the range [ l , r ] ; Driver code
using System ; class GFG { static int findXOR ( int n ) { int mod = n % 4 ; if ( mod == 0 ) return n ; else if ( mod == 1 ) return 1 ; else if ( mod == 2 ) return n + 1 ; else if ( mod == 3 ) return 0 ; return 0 ; } static int findXOR ( int l , int r ) { return ( findXOR ( l - 1 ) ^ findXOR ( r ) ) ; } public static void Main ( ) { int l = 4 , r = 8 ; Console . WriteLine ( findXOR ( l , r ) ) ; } }
Number of elements from the array which are reachable after performing given operations on D | C # implementation of the approach ; Function to return the GCD of a and b ; Function to return the count of reachable integers from the given array ; GCD of A and B ; To store the count of reachable integers ; If current element can be reached ; Return the count ; Driver code
using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findReachable ( int [ ] arr , int D , int A , int B , int n ) { int gcd_AB = GCD ( A , B ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; int D = 4 , A = 4 , B = 6 ; Console . WriteLine ( findReachable ( arr , D , A , B , n ) ) ; } }
Number of trees whose sum of degrees of all the vertices is L | C # implementation of the approach ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the count of required trees ; number of nodes ; Return the result ; Driver code
using System ; class GFG { static long power ( int x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } static long solve ( int L ) { int n = L / 2 + 1 ; long ans = power ( n , n - 2 ) ; return ans ; } static public void Main ( ) { int L = 6 ; Console . WriteLine ( solve ( L ) ) ; } }
Change one element in the given array to make it an Arithmetic Progression | C # program to change one element of an array such that the resulting array is in arithmetic progression . ; Finds the initial term and common difference and prints the resulting array . ; Check if the first three elements are in arithmetic progression ; Check if the first element is not in arithmetic progression ; The first and fourth element are in arithmetic progression ; Print the arithmetic progression ; Driver code
using System ; public class AP { static void makeAP ( int [ ] arr , int n ) { int initial_term , common_difference ; if ( n == 3 ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else if ( ( arr [ 1 ] - arr [ 0 ] ) == arr [ 2 ] - arr [ 1 ] ) { initial_term = arr [ 0 ] ; common_difference = arr [ 1 ] - arr [ 0 ] ; } else if ( ( arr [ 2 ] - arr [ 1 ] ) == ( arr [ 3 ] - arr [ 2 ] ) ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else { common_difference = ( arr [ 3 ] - arr [ 0 ] ) / 3 ; initial_term = arr [ 0 ] ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( initial_term + ( i * common_difference ) + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 7 } ; int n = arr . Length ; makeAP ( arr , n ) ; } }
Find if nCr is divisible by the given prime | C # Implementation of above approach ; Function to return the highest power of p that divides n ! implementing Legendre Formula ; Return the highest power of p which divides n ! ; Function to return N digits number which is divisible by D ; Find the highest powers of p that divide n ! , r ! and ( n - r ) ! ; If nCr is divisible by p ; Driver code
using System ; class GFG { static int getfactor ( int n , int p ) { int pw = 0 ; while ( n != 0 ) { n /= p ; pw += n ; } return pw ; } static int isDivisible ( int n , int r , int p ) { int x1 = getfactor ( n , p ) ; int x2 = getfactor ( r , p ) ; int x3 = getfactor ( n - r , p ) ; if ( x1 > x2 + x3 ) return 1 ; return 0 ; } static public void Main ( ) { int n = 7 , r = 2 , p = 7 ; if ( isDivisible ( n , r , p ) == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if the number is even or odd whose digits and base ( radix ) is given | C # implementation of the approach ; Function that returns true if the number represented by arr [ ] is even in base r ; If the base is even , then the last digit is checked ; If base is odd , then the number of odd digits are checked ; To store the count of odd digits ; Number is odd ; Driver code
using System ; class GFG { static bool isEven ( int [ ] arr , int n , int r ) { if ( r % 2 == 0 ) { if ( arr [ n - 1 ] % 2 == 0 ) return true ; } else { int oddCount = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount % 2 == 0 ) return true ; } return false ; } public static void Main ( ) { int [ ] arr = { 1 , 0 } ; int n = arr . Length ; int r = 2 ; if ( isEven ( arr , n , r ) ) Console . WriteLine ( " Even " ) ; else Console . WriteLine ( " Odd " ) ; } }
Bitwise AND of sub | C # implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; No need to perform more AND operations as | k - X | will increase ; Driver code
using System ; class GFG { static int closetAND ( int [ ] arr , int n , int k ) { int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . Min ( ans , Math . Abs ( k - X ) ) ; if ( X <= k ) break ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 7 , 10 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( closetAND ( arr , n , k ) ) ; } }
Count of quadruplets from range [ L , R ] having GCD equal to K | C # implementation of the approach ; Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; Count the frequency of every possible gcd value in the range ; To store the required count ; Calculate the answer using frequency values ; Return the required count ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int countQuadruplets ( int l , int r , int k ) { int [ ] frequency = new int [ r + 1 ] ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = l ; j <= r ; j ++ ) { frequency [ gcd ( i , j ) ] ++ ; } } long answer = 0 ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= r ; j ++ ) { if ( gcd ( i , j ) == k ) { answer += ( frequency [ i ] * frequency [ j ] ) ; } } } return ( int ) answer ; } static public void Main ( ) { int l = 1 , r = 10 , k = 2 ; Console . WriteLine ( countQuadruplets ( l , r , k ) ) ; } }
Rearrange the array to maximize the number of primes in prefix sum of the array | C # implementation of the approach ; Function to print the re - arranged array ; Count the number of ones and twos in a [ ] ; If the array element is 1 ; Array element is 2 ; If it has at least one 2 Fill up first 2 ; Decrease the cnt of ones if even ; Fill up with odd count of ones ; Fill up with remaining twos ; If even ones , then fill last position ; Print the rearranged array ; Driver code
using System ; class GFG { static void solve ( int [ ] a , int n ) { int ones = 0 , twos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ones ++ ; else twos ++ ; } int ind = 0 ; if ( twos > 0 ) a [ ind ++ ] = 2 ; bool evenOnes = ( ones % 2 == 0 ) ? true : false ; if ( evenOnes ) ones -= 1 ; for ( int i = 0 ; i < ones ; i ++ ) a [ ind ++ ] = 1 ; for ( int i = 0 ; i < twos - 1 ; i ++ ) a [ ind ++ ] = 2 ; if ( evenOnes ) a [ ind ++ ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 1 , 2 , 1 } ; int n = a . Length ; solve ( a , n ) ; } }
Generate an Array in which count of even and odd sum sub | C # implementation of the approach ; Function to generate and print the required array ; Find the number of odd prefix sums ; If no odd prefix sum found ; Calculating the number of even prefix sums ; Stores the current prefix sum ; If current prefix sum is even ; Print 0 until e = EvenPreSums - 1 ; Print 1 when e = EvenPreSums ; Print 0 for rest of the values ; Driver code
using System ; class GFG { static void CreateArray ( int N , int even , int odd ) { int EvenPreSums = 1 ; int temp = - 1 ; int OddPreSums = 0 ; for ( int i = 0 ; i <= N + 1 ; i ++ ) { if ( i * ( ( N + 1 ) - i ) == odd ) { temp = 0 ; OddPreSums = i ; break ; } } if ( temp == - 1 ) { Console . WriteLine ( temp ) ; } else { EvenPreSums = ( ( N + 1 ) - OddPreSums ) ; int e = 1 ; int o = 0 ; int CurrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( CurrSum % 2 == 0 ) { if ( e < EvenPreSums ) { e ++ ; Console . Write ( "0 ▁ " ) ; } else { o ++ ; Console . Write ( "1 ▁ " ) ; CurrSum ++ ; } } else { if ( e < EvenPreSums ) { e ++ ; Console . Write ( "1 ▁ " ) ; CurrSum ++ ; } else { o ++ ; Console . Write ( "0 ▁ " ) ; } } } Console . WriteLine ( ) ; } } static public void Main ( ) { int N = 15 ; int even = 60 , odd = 60 ; CreateArray ( N , even , odd ) ; } }
Minimum operations required to change the array such that | arr [ i ] | C # implementation of the approach ; Function to return the minimum number of operations required ; Minimum and maximum elements from the array ; To store the minimum number of operations required ; To store the number of operations required to change every element to either ( num - 1 ) , num or ( num + 1 ) ; If current element is not already num ; Add the count of operations required to change arr [ i ] ; Update the minimum operations so far ; Driver code
using System ; using System . Linq ; class GFG { static int changeTheArray ( int [ ] arr , int n ) { int minEle = arr . Min ( ) ; int maxEle = arr . Max ( ) ; int minOperations = int . MaxValue ; for ( int num = minEle ; num <= maxEle ; num ++ ) { int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != num ) { operations += ( Math . Abs ( num - arr [ i ] ) - 1 ) ; } } minOperations = Math . Min ( minOperations , operations ) ; } return minOperations ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( changeTheArray ( arr , n ) ) ; } }
Choose X such that ( A xor X ) + ( B xor X ) is minimized | C # implementation of the approach ; Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized ; While either A or B is non - zero ; Position at which both A and B have a set bit ; Inserting a set bit in x ; Right shifting both numbers to traverse all the bits ; Driver code
using System ; class GFG { static int findX ( int A , int B ) { int j = 0 , x = 0 ; while ( A != 0 B != 0 ) { if ( ( A % 2 == 1 ) && ( B % 2 == 1 ) ) { x += ( 1 << j ) ; } A >>= 1 ; B >>= 1 ; j += 1 ; } return x ; } public static void Main ( String [ ] args ) { int A = 2 , B = 3 ; int X = findX ( A , B ) ; Console . WriteLine ( " X ▁ = ▁ " + X + " , ▁ Sum ▁ = ▁ " + ( ( A ^ X ) + ( B ^ X ) ) ) ; } }
Choose X such that ( A xor X ) + ( B xor X ) is minimized | C # implementation of above approach ; Finding X ; Finding Sum ; Driver Code
using System ; class GFG { public static int findX ( int A , int B ) { return A & B ; } public static int findSum ( int A , int B ) { return A ^ B ; } public static void Main ( String [ ] args ) { int A = 2 , B = 3 ; Console . Write ( " X ▁ = ▁ " + findX ( A , B ) + " , ▁ Sum ▁ = ▁ " + findSum ( A , B ) ) ; } }
Compare sum of first N | C # implementation of the approach ; Function that returns true if sum of first n - 1 elements of the array is equal to the last element ; Find the sum of first n - 1 elements of the array ; If sum equals to the last element ; Driver code
using System ; class GFG { static bool isSumEqual ( int [ ] ar , int n ) { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) sum += ar [ i ] ; if ( sum == ar [ n - 1 ] ) return true ; return false ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 10 } ; int n = arr . Length ; if ( isSumEqual ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count number of 1 s in the array after N moves | C # implementation of the above approach ; Function to count number of perfect squares ; Counting number of perfect squares between a and b ; Function to count number of 1 s in array after N moves ; Driver Code ; Initialize array size ; Initialize all elements to 0
using System ; class GFG { static double perfectSquares ( int a , int b ) { return ( Math . Floor ( Math . Sqrt ( b ) ) - Math . Ceiling ( Math . Sqrt ( a ) ) + 1 ) ; } static double countOnes ( int [ ] arr , int n ) { return perfectSquares ( 1 , n ) ; } static public void Main ( ) { int N = 10 ; int [ ] arr = { 0 } ; Console . WriteLine ( countOnes ( arr , N ) ) ; } }
Find the position of box which occupies the given ball | C # implementation of the approach ; Function to print the position of each boxes where a ball has to be placed ; Find the cumulative sum of array A [ ] ; Find the position of box for each ball ; Row number ; Column ( position of box in particular row ) ; Row + 1 denotes row if indexing of array start from 1 ; Driver code
using System ; class GFG { static void printPosition ( int [ ] A , int [ ] B , int sizeOfA , int sizeOfB ) { for ( int i = 1 ; i < sizeOfA ; i ++ ) { A [ i ] += A [ i - 1 ] ; } for ( int i = 0 ; i < sizeOfB ; i ++ ) { int row = lower_bound ( A , 0 , A . Length , B [ i ] ) ; int boxNumber = ( row >= 1 ) ? B [ i ] - A [ row - 1 ] : B [ i ] ; Console . WriteLine ( row + 1 + " , ▁ " + boxNumber + " STRNEWLINE " ) ; } } private static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } static public void Main ( ) { int [ ] A = { 2 , 2 , 2 , 2 } ; int [ ] B = { 1 , 2 , 3 , 4 } ; int sizeOfA = A . Length ; int sizeOfB = B . Length ; printPosition ( A , B , sizeOfA , sizeOfB ) ; } }