codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of 1 s present in a submatrix of given dimensions in a Binary Matrix | C # program for the above approach ; Function to count number of 1 s present in a sub matrix from ( start_i , start_j ) to ( end_i , end_j ) ; Stores the number of 1 s present in current submatrix ; Traverse the submatrix ; If mat [ x ] [ y ] is equal to 1 ; Increase count by 1 ; Return the total count of 1 s ; Function to find the minimum number of 1 s present in a sub - matrix of size A * B or B * A ; Stores the minimum count of 1 s ; Iterate i from 0 to N ; Iterate j from 0 to M ; If a valid sub matrix of size A * B from ( i , j ) is possible ; Count the number of 1 s present in the sub matrix of size A * B from ( i , j ) ; Update minimum if count is less than the current minimum ; If a valid sub matrix of size B * A from ( i , j ) is possible ; Count the number of 1 s in the sub matrix of size B * A from ( i , j ) ; Update minimum if count is less than the current minimum ; Return minimum as the final result ; Driver Code ; Given Input ; Function call to find the minimum number of 1 s in a submatrix of size A * B or B * A
using System ; using System . Collections . Generic ; class GFG { static int count1s ( int start_i , int start_j , int end_i , int end_j , List < List < int > > mat ) { int count = 0 ; for ( int x = start_i ; x < end_i ; x ++ ) { for ( int y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } static void findMinimumCount ( int N , int M , int A , int B , List < List < int > > mat ) { int minimum = 1000000 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( ( i + A <= N ) && ( j + B <= M ) ) { int count = count1s ( i , j , i + A , j + B , mat ) ; minimum = Math . Min ( count , minimum ) ; } if ( ( i + B <= N ) && ( j + A <= M ) ) { int count = count1s ( i , j , i + B , j + A , mat ) ; minimum = Math . Min ( count , minimum ) ; } } } Console . WriteLine ( minimum ) ; } public static void Main ( ) { int A = 2 , B = 2 ; int N = 3 , M = 4 ; List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > ( new int [ ] { 1 , 0 , 1 , 0 } ) ) ; mat . Add ( new List < int > ( new int [ ] { 0 , 1 , 0 , 1 } ) ) ; mat . Add ( new List < int > ( new int [ ] { 1 , 0 , 1 , 0 } ) ) ; findMinimumCount ( N , M , A , B , mat ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | C # program for the above approach ; Function to check if N can be expressed as an even power of 2 ; Iterate till x is N ; if x is even then return true ; Increment x ; If N is not a power of 2 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool checkEvenPower ( int n ) { int x = 0 ; while ( x < n ) { int value = ( int ) Math . Pow ( 2 , x ) ; if ( value == n ) { if ( x % 2 == 0 ) return true ; else return false ; } x ++ ; } return false ; } static public void Main ( ) { int N = 4 ; Console . Write ( ( checkEvenPower ( N ) ? " Yes " : " No " ) ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | C # program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; Iterate until low > high ; Calculate mid ; If 2 ^ mid is equal to n ; If mid is odd ; Update the value of low ; Update the value of high ; Otherwise ; Driver code
using System ; public class GFG { static String checkEvenPower ( int n ) { int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int value = ( int ) Math . Pow ( 2 , mid ) ; if ( value == n ) { if ( mid % 2 == 1 ) return " No " ; else return " Yes " ; } else if ( value < n ) low = mid + 1 ; else high = mid - 1 ; } return " No " ; } public static void Main ( String [ ] args ) { int N = 4 ; Console . WriteLine ( checkEvenPower ( N ) ) ; } }
Program to check if a number can be expressed as an even power of 2 or not | C # program for the above approach ; Function to check if N can be expressed as an even power of 2 or not ; If N is not a power of 2 ; Bitwise AND operation ; Driver Code
using System ; public class GFG { static bool checkEvenPower ( long N ) { if ( ( N & ( N - 1 ) ) != 0 ) return false ; N = N & 0x55555555 ; return ( N > 0 ) ; } public static void Main ( String [ ] args ) { long N = 4 ; Console . WriteLine ( checkEvenPower ( N ) ? 1 : 0 ) ; } }
Sum of quotients of division of N by powers of K not exceeding N | C # program for the above approach ; Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Print the result ; Driver code ; Given N and K
using System ; class GFG { static void findSum ( int N , int K ) { int ans = 0 ; int i = 1 ; while ( i <= N ) { ans += N / i ; i = i * K ; } Console . Write ( ans ) ; } static void Main ( ) { int N = 10 , K = 2 ; findSum ( N , K ) ; } }
Count Arithmetic Progressions having sum S and common difference equal to D | C # program for the above approach ; Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver code
using System ; class GFG { static int countAPs ( int S , int D ) { S = S * 2 ; int answer = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } static void Main ( ) { int S = 12 , D = 1 ; Console . Write ( countAPs ( S , D ) ) ; } }
Minimize insertions to make sum of every pair of consecutive array elements at most K | C # program for the above approach ; Function to count minimum number of insertions required to make sum of every pair of adjacent elements at most K ; Stores if it is possible to make sum of each pair of adjacent elements at most K ; Stores the count of insertions ; Stores the previous value to index i ; Traverse the array ; If arr [ i ] is greater than or equal to K ; Mark possible 0 ; If last + arr [ i ] is greater than K ; Increment res by 1 ; Assign arr [ i ] to last ; If possible to make the sum of pairs of adjacent elements at most K ; Otherwise print " - 1" ; Driver code
using System ; class GFG { static void minimumInsertions ( int [ ] arr , int N , int K ) { bool possible = true ; int res = 0 ; int last = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= K ) { possible = false ; break ; } if ( last + arr [ i ] > K ) res ++ ; last = arr [ i ] ; } if ( possible ) { Console . Write ( res ) ; } else { Console . Write ( " - 1" ) ; } } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int K = 6 ; int N = arr . Length ; minimumInsertions ( arr , N , K ) ; } }
Maximum sum of K | C # Program to implement the above approach ; Function to count the number of distinct elements present in the array ; Insert all elements into the Set ; Return the size of set ; Function that finds the maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find a subarray of size K ; Initialize Set ; Calculate sum of the distinct elements ; If the set size is same as the count of distinct elements ; Update the maximum value ; Driver Code ; Stores the count of distinct elements
using System ; using System . Collections . Generic ; class GFG { static int distinct ( int [ ] arr , int n ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { set . Add ( arr [ i ] ) ; } return set . Count ; } static int maxSubSum ( int [ ] arr , int n , int k , int totalDistinct ) { if ( k > n ) return 0 ; int max = 0 , sum = 0 ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { sum = 0 ; HashSet < int > set = new HashSet < int > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { sum += arr [ j ] ; set . Add ( arr [ j ] ) ; } if ( set . Count == totalDistinct ) max = Math . Max ( sum , max ) ; } return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; int N = arr . Length ; int totalDistinct = distinct ( arr , N ) ; Console . WriteLine ( maxSubSum ( arr , N , K , totalDistinct ) ) ; } }
Maximum sum of K | C # program for the above approach ; Function to count the number of distinct elements present in the array ; Insert array elements into set ; Return the st size ; Function to calculate maximum sum of K - length subarray having same unique elements as arr [ ] ; Not possible to find an subarray of length K from an N - sized array , if K > N ; Traverse the array ; Update the mp ; If i >= K , then decrement arr [ i - K ] element 's one  occurence ; If frequency of any element is 0 then remove the element ; If mp size is same as the count of distinct elements of array arr [ ] then update maximum sum ; Function that finds the maximum sum of K - length subarray having same number of distinct elements as the original array ; Size of array ; Stores count of distinct elements ; Print maximum subarray sum ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int distinct ( List < int > arr , int N ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } return st . Count ; } static int maxSubarraySumUtil ( List < int > arr , int N , int K , int totalDistinct ) { if ( K > N ) return 0 ; int mx = 0 ; int sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] += 1 ; else mp [ arr [ i ] ] = 1 ; sum += arr [ i ] ; if ( i >= K ) { if ( mp . ContainsKey ( arr [ i - K ] ) ) mp [ arr [ i - K ] ] -= 1 ; else mp [ arr [ i - K ] ] = 1 ; sum -= arr [ i - K ] ; if ( mp [ arr [ i - K ] ] == 0 ) mp . Remove ( arr [ i - K ] ) ; } if ( mp . Count == totalDistinct ) mx = Math . Max ( mx , sum ) ; } return mx ; } static void maxSubarraySum ( List < int > arr , int K ) { int N = arr . Count ; int totalDistinct = distinct ( arr , N ) ; Console . WriteLine ( maxSubarraySumUtil ( arr , N , K , totalDistinct ) ) ; } public static void Main ( ) { List < int > arr = new List < int > { 7 , 7 , 2 , 4 , 2 , 7 , 4 , 6 , 6 , 6 } ; int K = 6 ; maxSubarraySum ( arr , K ) ; } }
Number of Irreflexive Relations on a Set | C # program for above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver code
using System ; public class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int irreflexiveRelation ( int N ) { return power ( 2 , N * N - N ) ; } public static void Main ( String [ ] args ) { int N = 2 ; Console . WriteLine ( irreflexiveRelation ( N ) ) ; } }
Count Arithmetic Progressions having sum N and common difference equal to 1 | C # program for above approach ; Function to count all possible AP series with common difference 1 and sum of elements equal to N ; Stores the count of AP series ; Traverse through all factors of 2 * N ; Check for the given conditions ; Increment count ; Print count - 1 ; Driver code ; Given value of N ; Function call to count required number of AP series
using System ; public class GFG { static void countAPs ( long N ) { long count = 0 ; for ( long i = 1 ; i * i <= 2 * N ; i ++ ) { long res = 2 * N ; if ( res % i == 0 ) { long op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res && ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } Console . WriteLine ( count - 1 ) ; } public static void Main ( String [ ] args ) { long N = 963761198400L ; countAPs ( N ) ; } }
Check if concatenation of first and last digits forms a prime number or not for each array element | C # program for above approach package whatever do not write package name here ; Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver code ; Computes and stores primes using Sieve ; Function call to perform queries
using System ; public class GFG { static int [ ] sieve = new int [ 105 ] ; static void buildSieve ( ) { for ( int i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( int i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( int j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } static bool isAnyPrime ( int first , int last ) { int num1 = first * 10 + last ; int num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } static void performQueries ( int [ ] q ) { for ( int i = 0 ; i < q . Length ; i ++ ) { int A = q [ i ] ; int last = A % 10 ; int first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) Console . Write ( " True STRNEWLINE " ) ; else Console . Write ( " False STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int [ ] q = { 30 , 66 } ; buildSieve ( ) ; performQueries ( q ) ; } }
Check if a given number N has at least one odd divisor not exceeding N | C # program for the above approach ; Function to check whether N has at least one odd divisor not exceeding N - 1 or not ; Stores the value of N ; Reduce the given number N by dividing it by 2 ; If N is divisible by an odd divisor i ; Check if N is an odd divisor after reducing N by dividing it by 2 ; Otherwise ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static string oddDivisor ( int N ) { int X = N ; while ( N % 2 == 0 ) { N /= 2 ; } for ( int i = 3 ; i * i <= X ; i += 2 ) { if ( N % i == 0 ) { return " Yes " ; } } if ( N != X ) { return " Yes " ; } return " No " ; } static public void Main ( ) { int N = 10 ; Console . Write ( oddDivisor ( N ) ) ; } }
Queries to count numbers from a given range which are divisible the sum of their digits | C # program for the above approach ; Function to check if the number x is divisible by its sum of digits ; Stores sum of digits of x ; Temporarily store x ; Calculate sum of digits of x ; If x is not divisible by sum ; Otherwise ; Function to perform the precomputation ; Iterate from i equals 1 to 1e5 ; Check if i is divisible by sum of its digits ; Convert [ ] arr to prefix sum array ; Driver Code ; Given queries ; Using inclusion - exclusion principle , calculate the result
using System ; class GFG { static int [ ] arr = new int [ 100005 ] ; static bool isDivisible ( int x ) { int sum = 0 ; int temp = x ; while ( x != 0 ) { sum += x % 10 ; x /= 10 ; } if ( temp % sum != 0 ) return false ; else return true ; } static void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { if ( isDivisible ( i ) ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 100000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] Q = { { 5 , 9 } , { 5 , 20 } } ; int [ ] q ; precompute ( ) ; for ( int i = 0 ; i < Q . GetLength ( 0 ) ; i ++ ) { q = GetRow ( Q , i ) ; Console . WriteLine ( arr [ q [ 1 ] ] - arr [ q [ 0 ] - 1 ] ) ; } } }
Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M | C # program for the above approach ; Stores the prime factor of numbers ; Function to find GCD of a and b ; Base Case ; Otherwise , calculate GCD ; Function to precompute the prime numbers till 1000000 ; Initialize all the positions with their respective values ; Iterate over the range [ 2 , sqrt ( 10 ^ 6 ) ] ; If i is prime number ; Mark it as the factor ; Utility function to count the number of steps to make X and Y equal ; Initialise steps ; Iterate x is at most 1 ; Divide with the smallest prime factor ; If X and Y can 't be  made equal ; Return steps ; Function to find the minimum number of steps required to make X and Y equal ; Generate all the prime factors ; Calculate GCD of x and y ; Divide the numbers by their gcd ; If not possible , then return - 1 ; ; Return the resultant number of steps ; Driver code
using System ; class GFG { static int [ ] primes = new int [ 1000006 ] ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void preprocess ( ) { for ( int i = 1 ; i <= 1000000 ; i ++ ) primes [ i ] = i ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( primes [ i ] == i ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { if ( primes [ j ] == j ) primes [ j ] = i ; } } } } static int Steps ( int x , int m ) { int steps = 0 ; bool flag = false ; while ( x > 1 ) { if ( primes [ x ] > m ) { flag = true ; break ; } x /= primes [ x ] ; steps ++ ; } if ( flag ) return - 1 ; return steps ; } static int minimumSteps ( int x , int y , int m ) { preprocess ( ) ; int g = gcd ( x , y ) ; x = x / g ; y = y / g ; int x_steps = Steps ( x , m ) ; int y_steps = Steps ( y , m ) ; if ( x_steps == - 1 y_steps == - 1 ) return - 1 ; return x_steps + y_steps ; } static void Main ( ) { int X = 160 ; int Y = 180 ; int M = 10 ; Console . Write ( minimumSteps ( X , Y , M ) ) ; } }
Length of longest subsequence consisting of Non | C # program for above approach ; Function to check if n is a deficient number or not ; Stores sum of divisors ; Iterate over the range [ 1 , sqrt ( N ) ] ; If n is divisible by i ; If divisors are equal , add only one of them ; Otherwise add both ; Function to print the longest subsequence which does not contain any deficient numbers ; Stores the count of array elements which are non - deficient ; Traverse the array ; If element is non - deficient ; Return the answer ; Driver code
using System ; public class GFG { static bool isNonDeficient ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum >= 2 * n ; } static int LongestNonDeficientSubsequence ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNonDeficient ( arr [ i ] ) ) { res += 1 ; } } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 13 , 55 , 240 , 32 , 24 , 27 , 56 , 80 , 100 , 330 , 89 } ; int N = arr . Length ; Console . WriteLine ( LongestNonDeficientSubsequence ( arr , N ) ) ; } }
Number of subarrays consisting only of Pronic Numbers | C # program for the approach ; Function to check if a number is pronic number or not ; Iterate over the range [ 1 , sqrt ( N ) ] ; Return true if N is pronic ; Otherwise , return false ; Function to count the number of subarrays consisting of pronic numbers ; Stores the count of subarrays ; Stores the number of consecutive array elements which are pronic ; Traverse the array ; If i is pronic ; Return the total count ; Driver code
using System ; class GFG { static bool isPronic ( int n ) { int range = ( int ) Math . Sqrt ( n ) ; for ( int i = 0 ; i < range + 1 ; i ++ ) { if ( i * ( i + 1 ) == n ) return true ; } return false ; } static int countSub ( int [ ] arr , int n ) { int ans = 0 ; int ispro = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPronic ( arr [ i ] ) ) ispro += 1 ; else ispro = 0 ; ans += ispro ; } return ans ; } static void Main ( ) { int [ ] arr = { 5 , 6 , 12 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countSub ( arr , n ) ) ; } }
Efficient method to store a Lower Triangular Matrix using row | C # program for the above approach ; Dimensions of a matrix ; Structure of the efficient matrix ; Function to set the values in the Matrix ; Function to store the values in the Matrix ; Function to display the elements of the matrix ; Traverse the matrix ; Function to generate an efficient matrix ; Declare efficient Matrix ; Initialize the Matrix ; Set the values in matrix ; Return the matrix ; Driver Code ; Stores the efficient matrix ; Print the Matrix
using System ; public class GFG { static int N = 5 ; class Matrix { public int [ ] A ; public int size ; } ; static void Set ( Matrix mat , int i , int j , int x ) { if ( i >= j ) mat . A [ i * ( i - 1 ) / 2 + j - 1 ] = x ; } static int Get ( Matrix mat , int i , int j ) { if ( i >= j ) { return mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ; } else { return 0 ; } } static void Display ( Matrix mat ) { int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { if ( i >= j ) { Console . Write ( " { 0 } ▁ " , mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ) ; } else { Console . Write ( "0 ▁ " ) ; } } Console . Write ( " STRNEWLINE " ) ; } } static Matrix createMat ( int [ , ] Mat ) { Matrix mat = new Matrix ( ) ; mat . size = N ; mat . A = new int [ ( mat . size * ( mat . size + 1 ) ) / 2 ] ; int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { Set ( mat , i , j , Mat [ i - 1 , j - 1 ] ) ; } } return mat ; } public static void Main ( String [ ] args ) { int [ , ] Mat = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 2 , 0 , 0 , 0 } , { 1 , 2 , 3 , 0 , 0 } , { 1 , 2 , 3 , 4 , 0 } , { 1 , 2 , 3 , 4 , 5 } } ; Matrix mat = createMat ( Mat ) ; Display ( mat ) ; } }
Replace all array elements with the nearest power of its previous element | C # program for the above approach ; Function to calculate log x for given base ; Function to replace all array elements with the nearest power of previous adjacent nelement ; For the first element , set the last array element to its previous element ; Traverse the array ; Find K for which x ^ k is nearest to arr [ i ] ; Find the power to x nearest to arr [ i ] ; Update x ; Driver Code ; Function Call ; Display the array
using System ; class GFG { static int LOG ( int a , int b ) { return ( int ) ( Math . Log ( a ) / Math . Log ( b ) ) ; } static void repbyNP ( int [ ] arr , int n ) { int x = arr [ n - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = LOG ( arr [ i ] , x ) ; int temp = arr [ i ] ; if ( Math . Abs ( Math . Pow ( x , k ) - arr [ i ] ) < Math . Abs ( Math . Pow ( x , k + 1 ) - arr [ i ] ) ) arr [ i ] = ( int ) Math . Pow ( x , k ) ; else arr [ i ] = ( int ) Math . Pow ( x , k + 1 ) ; x = temp ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 3 , 11 } ; int n = arr . Length ; repbyNP ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | C # program for the above approach ; Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver code
using System ; class GFG { public static int lastElement ( int [ ] arr , int n ) { int sum = 0 ; int multiplier = n % 2 == 0 ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * multiplier ; multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) ; } return sum ; } static void Main ( ) { int [ ] arr = { 3 , 4 , 2 , 1 } ; int N = 4 ; Console . WriteLine ( lastElement ( arr , N ) ) ; } }