codeparrot/xlcost-text-to-code huggingface dataset:
Binary Indexed Tree : Range Updates and Point Queries | C # program to demonstrate Range Update and Point Queries Without using BIT ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver code ; Find the element at Index 4 ; Find the element at Index 3
using System ; class GfG { static void update ( int [ ] arr , int l , int r , int val ) { arr [ l ] += val ; if ( r + 1 < arr . Length ) arr [ r + 1 ] -= val ; } static int getElement ( int [ ] arr , int i ) { int res = 0 ; for ( int j = 0 ; j <= i ; j ++ ) res += arr [ j ] ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; int l = 2 , r = 4 , val = 2 ; update ( arr , l , r , val ) ; int index = 4 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( arr , l , r , val ) ; index = 3 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; } }
Binary Indexed Tree : Range Updates and Point Queries | C # code to demonstrate Range Update and * Point Queries on a Binary Index Tree . * This method only works when all array * values are initially 0. ; Max tree size ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BITree ; Update index to that of parent in update View ; Constructs Binary Indexed Tree for given array of size n . ; Initialize BITree [ ] as 0 ; Store the actual values in BITree [ ] using update ( ) ; SERVES THE PURPOSE OF getElement ( ) Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Initialize result ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Return the sum ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Increase value at ' l ' by ' val ' ; Decrease value at ' r + 1' by ' val ' ; Driver Code ; Add 2 to all the element from [ 2 , 4 ] ; Find the element at Index 4 ; Add 2 to all the element from [ 0 , 3 ] ; Find the element at Index 3
using System ; public class GFG { public const int MAX = 1000 ; public static int [ ] BITree = new int [ MAX ] ; public static void updateBIT ( int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void constructBITree ( int [ ] arr , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { BITree [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { updateBIT ( n , i , arr [ i ] ) ; } } public static int getSum ( int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void update ( int l , int r , int n , int val ) { updateBIT ( n , l , val ) ; updateBIT ( n , r + 1 , - val ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 0 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; constructBITree ( arr , n ) ; int l = 2 , r = 4 , val = 2 ; update ( l , r , n , val ) ; int index = 4 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( l , r , n , val ) ; index = 3 ; Console . WriteLine ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; } }
Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | C # program for the above approach ; Function to check if the indices ( i , j ) are valid indices in a Matrix or not ; Cases for invalid cells ; Return true if valid ; Function to find indices of cells of a matrix from which traversal leads to out of the matrix ; Starting from cell ( 0 , 0 ) , traverse in right direction ; Stores direction changes ; Iterate until the current cell exceeds beyond the matrix ; Current index ; If the current cell is 1 ; Update arr [ i ] [ j ] = 0 ; Update indices according to the direction ; Otherwise ; Update indices according to the direction ; The exit cooridnates ; Driver Code ; Number of rdows ; Number of columns ; Given matrix arr [ ] [ ]
using System ; using System . Collections . Generic ; class GFG { static bool issafe ( int m , int n , int i , int j ) { if ( i < 0 ) return false ; if ( j < 0 ) return false ; if ( i >= m ) return false ; if ( j >= n ) return false ; return true ; } static int [ ] endpoints ( int [ , ] arr , int m , int n ) { int i = 0 ; int j = 0 ; int current_i = 0 ; int current_j = 0 ; char current_d = ' r ' ; Dictionary < char , char > rcd = new Dictionary < char , char > ( ) ; rcd [ ' l ' ] = ' u ' ; rcd [ ' u ' ] = ' r ' ; rcd [ ' r ' ] = ' d ' ; rcd [ ' d ' ] = ' l ' ; while ( issafe ( m , n , i , j ) ) { current_i = i ; current_j = j ; if ( arr [ i , j ] == 1 ) { char move_in = rcd [ current_d ] ; arr [ i , j ] = 0 ; if ( move_in == ' u ' ) i -= 1 ; else if ( move_in == ' d ' ) i += 1 ; else if ( move_in == ' l ' ) j -= 1 ; else if ( move_in == ' r ' ) j += 1 ; current_d = move_in ; } else { if ( current_d == ' u ' ) i -= 1 ; else if ( current_d == ' d ' ) i += 1 ; else if ( current_d == ' l ' ) j -= 1 ; else if ( current_d == ' r ' ) j += 1 ; } } return new int [ ] { current_i , current_j } ; } public static void Main ( string [ ] args ) { int M = 3 ; int N = 5 ; int [ , ] arr = { { 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 1 , 1 , 1 , 0 , 0 } } ; int [ ] p = endpoints ( arr , M , N ) ; Console . WriteLine ( " ( " + p [ 0 ] + " , ▁ " + p [ 1 ] + " ) " + " STRNEWLINE " ) ; } }
Check if a number can be represented as sum of two positive perfect cubes | C # program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { public static void sumOfTwoPerfectCubes ( int N ) { Dictionary < int , int > cubes = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . Add ( ( i * i * i ) , i ) ; var val = cubes . Keys . ToList ( ) ; foreach ( var key in val ) { int firstNumber = cubes [ 1 ] ; int secondNumber = N - cubes [ 1 ] ; if ( cubes . ContainsKey ( secondNumber ) ) { Console . Write ( " True " ) ; return ; } } Console . Write ( " False " ) ; } static public void Main ( ) { int N = 28 ; sumOfTwoPerfectCubes ( N ) ; } }
Count subsets consisting of each element as a factor of the next element in that subset | C # Program to implement the above approach ; Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Print the result ; Driver Code ; Function Call
using System ; class GFG { static int mod = 1000000007 ; static void countSets ( int [ ] arr , int n ) { int cnt = 0 ; int maxE = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( maxE < arr [ i ] ) maxE = arr [ i ] ; } int [ ] sieve = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) sieve [ arr [ i ] ] = 1 ; for ( int i = 1 ; i <= maxE ; i ++ ) { if ( sieve [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxE ; j += i ) { if ( sieve [ j ] != 0 ) sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % mod ; } } } for ( int i = 0 ; i <= maxE ; i ++ ) cnt = ( cnt % mod + sieve [ i ] % mod ) % mod ; Console . WriteLine ( cnt % mod ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 } ; int N = arr . Length ; countSets ( arr , N ) ; } }
Find the first day of a given year from a base year having first day as Monday | C # program to implement the above approach ; Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver code
using System ; class GFG { static void findDay ( int Y , int B ) { int lyear , rest , totaldays , day ; Y = ( Y - 1 ) - B ; lyear = Y / 4 ; rest = Y - lyear ; totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; day = ( totaldays % 7 ) ; if ( day == 0 ) Console . WriteLine ( " Monday " ) ; else if ( day == 1 ) Console . WriteLine ( " Tuesday " ) ; else if ( day == 2 ) Console . WriteLine ( " Wednesday " ) ; else if ( day == 3 ) Console . WriteLine ( " Thursday " ) ; else if ( day == 4 ) Console . WriteLine ( " Friday " ) ; else if ( day == 5 ) Console . WriteLine ( " Saturday " ) ; else if ( day == 6 ) Console . WriteLine ( " Sunday " ) ; else Console . WriteLine ( " INPUT ▁ YEAR ▁ IS ▁ WRONG ! " ) ; } static void Main ( ) { int Y = 2020 , B = 1900 ; findDay ( Y , B ) ; } }
Queries to update array elements in a range [ L , R ] to satisfy given conditions | C # program to implement the above approach ; Function to print the array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] into arr [ ] ; Driver Code ; Given array ; Size of the array ; Stores count of query ; Function Call
using System ; class GFG { static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void modifyArray ( int [ ] arr , int N , int [ , ] Q , int cntQuery ) { int [ ] arr1 = new int [ N + 2 ] ; int [ ] arr2 = new int [ N + 2 ] ; for ( int i = 0 ; i < cntQuery ; i ++ ) { int L = Q [ i , 0 ] + 1 , R = Q [ i , 1 ] + 1 ; arr1 [ L ] ++ ; arr1 [ R + 1 ] -- ; arr2 [ R + 1 ] -= R - L + 1 ; } for ( int i = 1 ; i <= N ; i ++ ) arr1 [ i ] += arr1 [ i - 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i - 1 ] = arr2 [ i ] ; printArray ( arr , N ) ; } public static void Main ( ) { int [ ] arr = { 0 , 0 , 0 , 0 } ; int N = arr . Length ; int [ , ] Q = { { 1 , 3 } , { 0 , 1 } } ; int cntQuery = 2 ; modifyArray ( arr , N , Q , cntQuery ) ; } }
Maximize product obtained by taking one element from each array of a given list | C # program for the above approach ; Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Driver Code ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
using System ; using System . Collections . Generic ; using System . Linq ; public class GFG { static int findProduct ( int number_1 , int number_2 ) { if ( number_1 == Int32 . MinValue number_2 == Int32 . MinValue ) { return 0 ; } else return number_1 * number_2 ; } static List < int > calculateProduct ( List < List < int > > List , int index ) { int highest = List [ index ] . Max ( ) ; int lowest = List [ index ] . Min ( ) ; if ( index + 1 == List . Count ) { if ( lowest < 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , lowest } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { Int32 . MinValue , lowest } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { highest , Int32 . MinValue } ) ; } } List < int > temp = calculateProduct ( List , index + 1 ) ; int positive = temp [ 0 ] ; int negative = temp [ 1 ] ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( new List < int > ( ) { Math . Max ( highPos , lowNeg ) , Math . Min ( highNeg , lowPos ) } ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new List < int > ( ) { lowNeg , lowPos } ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new List < int > ( ) { Math . Max ( lowPos , highPos ) , Math . Min ( lowNeg , highNeg ) } ) ; } return ( new List < int > ( ) { 0 , 0 } ) ; } static public void Main ( ) { List < List < int > > arr = new List < List < int > > ( ) ; arr . Add ( new List < int > ( ) { - 3 , - 4 } ) ; arr . Add ( new List < int > ( ) { 1 , 2 , - 3 } ) ; List < int > ans = calculateProduct ( arr , 0 ) ; Console . WriteLine ( ans [ 0 ] ) ; } }
Reduce a number N by at most D to maximize count of trailing nines | C # program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
using System ; class GFG { static void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = ( int ) Math . Log10 ( n ) + 1 ; int p10 = 10 ; for ( int i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } Console . WriteLine ( res ) ; } public static void Main ( String [ ] args ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; } }
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | C # program to implement the above approach ; Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; If N is odd ; Driver code
using System ; class GFG { static int MinimumNoOfWays ( int [ ] arr , int n ) { int mini_no_of_ways ; if ( n % 2 == 0 ) { mini_no_of_ways = n / 2 ; } else { mini_no_of_ways = n / 2 + 1 ; } return mini_no_of_ways ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 4 , 2 , 1 , 5 } ; int N = arr . Length ; Console . WriteLine ( MinimumNoOfWays ( arr , N ) ) ; } }
Chocolate Distribution Problem | Set 2 | C # program for above approach ; Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code
using System ; public class GFG { public static int minChocolates ( int [ ] a , int n ) { int i = 0 , j = 0 ; int res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } public static int get_sum ( int peak , int start , int end ) { int count = end - start + 1 ; peak = ( peak > count ) ? peak : count ; int s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } static public void Main ( ) { int [ ] a = { 5 , 5 , 4 , 3 , 2 , 1 } ; int n = a . Length ; Console . WriteLine ( " Minimum ▁ number ▁ of ▁ chocolates ▁ = ▁ " + minChocolates ( a , n ) ) ; } }
Remove array elements to reduce frequency of each array element to at most K | C # program to implement the above approach ; Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver code
using System ; using System . Linq ; class GFG { static int [ ] RemoveElemArr ( int [ ] arr , int n , int k ) { if ( n == 0 n == 1 ) return arr ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . Length > j ) { arr = arr . Take ( arr . Length - 1 ) . ToArray ( ) ; } return arr ; } static void printArray ( int [ ] arr ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void UtilRemov ( int [ ] arr , int n , int k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 } ; int k = 2 ; int n = arr . Length ; UtilRemov ( arr , n , k ) ; } }
Count pairs of equal array elements remaining after every removal | C # program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver code ; Given Array
using System ; using System . Collections . Generic ; class GFG { static void pairs_after_removing ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] ++ ; } else { mp [ arr [ i ] ] = 1 ; } } foreach ( KeyValuePair < int , int > element in mp ) { int i = element . Key ; cntPairs += mp [ i ] * ( mp [ i ] - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] ; Console . Write ( pairs_after_arr_i_removed + " ▁ " ) ; } return ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 3 , 2 } ; int N = arr . Length ; pairs_after_removing ( arr , N ) ; } }
Modify N by adding its smallest positive divisor exactly K times | C # program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver code
using System ; class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } static public void Main ( ) { int N = 6 , K = 4 ; Console . Write ( findValOfNWithOperat ( N , K ) ) ; } }
Partition array into minimum number of equal length subsets consisting of a single distinct value | C # program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Recursive function to return gcd of a and b ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int CntOfSubsetsByPartitioning ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } } int freqGCD = 0 ; foreach ( KeyValuePair < int , int > i in freq ) { freqGCD = __gcd ( freqGCD , i . Value ) ; } return ( N ) / freqGCD ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; Console . Write ( CntOfSubsetsByPartitioning ( arr , N ) ) ; } }
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | C # program for the above approach ; Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Driver code ; Given matrix ; Size of matrix ; Function call
using System ; public class GFG { private static int r , c ; static int [ , ] dir = { { 1 , 0 } , { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { - 1 , - 1 } , { - 1 , 1 } , { 1 , 1 } , { 1 , - 1 } } ; public static bool valid ( int i , int j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return true ; return false ; } static int find ( int i , int j , int [ , ] v ) { int s = 0 ; for ( int k = 0 ; k < 8 ; k ++ ) { int ni = i + dir [ k , 0 ] , nj = j + dir [ k , 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni , nj ] ; } return s ; } static void findsumofneighbors ( int [ , ] M ) { int [ , ] v = new int [ r , c ] ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { v [ i , j ] = find ( i , j , M ) ; Console . Write ( v [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( String [ ] args ) { int [ , ] M = { { 1 , 4 , 1 } , { 2 , 4 , 5 } , { 3 , 1 , 2 } } ; r = M . GetLength ( 0 ) ; c = M . GetLength ( 1 ) ; findsumofneighbors ( M ) ; } }