codeparrot/xlcost-text-to-code huggingface dataset:
Check if string can be rearranged so that every Odd length Substring is Palindrome | C # implementation of the above approach ; Function to check is it possible to rearrange the string such that every odd length substring is palindrome ; Length of the string ; To count number of distinct character in string ; To count frequency of each character ; Inserting into set ; Incrementing the frequency ; All characters in the string are same ; There are more than 2 different character in string ; Currently there is 2 different character in string ; Get the frequencies of the characters that present in string ; Difference between their count is less than or equal to 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool IsPossible ( String s ) { int n = s . Length ; HashSet < char > count = new HashSet < char > ( ) ; Dictionary < char , int > map = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { count . Add ( s [ i ] ) ; if ( map . ContainsKey ( s [ i ] ) ) map [ s [ i ] ] = map [ s [ i ] ] + 1 ; else map . Add ( s [ i ] , 1 ) ; } if ( count . Count == 1 ) return true ; if ( count . Count > 2 ) return false ; String newString = count . ToString ( ) ; int j = 0 ; char it = newString [ j ] ; int x = 0 , y = 0 ; x = ! map . ContainsKey ( it ) ? 0 : map [ it ] ; j ++ ; it = newString [ j ] ; y = ! map . ContainsKey ( it ) ? 0 : map [ it ] ; if ( Math . Abs ( x - y ) <= 1 ) return true ; return false ; } public static void Main ( String [ ] args ) { String s = " aaaddad " ; if ( IsPossible ( s ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Minimum element left from the array after performing given operations | C # implementation of the above approach ; Function to return the minimum possible value of the last element left after performing the given operations ; Driver code
using System ; using System . Linq ; class GFG { static int getMin ( int [ ] arr , int n ) { int minVal = arr . Min ( ) ; return minVal ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 1 , 6 , 9 } ; int n = arr . Length ; Console . WriteLine ( getMin ( arr , n ) ) ; } }
Largest substring with same Characters | C # program to find largest sub string with same characters ; Function to find largest sub string with same characters ; Traverse the string ; If character is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int Substring ( String s ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { ++ temp ; } else { ans = Math . Max ( ans , temp ) ; temp = 1 ; } } ans = Math . Max ( ans , temp ) ; return ans ; } public static void Main ( String [ ] args ) { String s = " abcdddddeff " ; Console . WriteLine ( Substring ( s ) ) ; } }
Number of balanced parenthesis substrings | C # program to find number of balanced parenthesis sub strings ; Function to find number of balanced parenthesis sub strings ; To store required answer ; Vector to stores the number of balanced brackets at each depth . ; d stores checks the depth of our sequence For example level of ( ) is 1 and that of ( ( ) ) is 2. ; If open bracket increase depth ; If closing bracket ; Return the required answer ; Driver code ; Function call
using System ; class GFG { public static int Balanced_Substring ( String str , int n ) { int ans = 0 ; int [ ] arr = new int [ n / 2 + 1 ] ; int d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) d ++ ; else { if ( d == 1 ) { for ( int j = 2 ; j <= n / 2 + 1 && arr [ j ] != 0 ; j ++ ) arr [ j ] = 0 ; } ++ ans ; ans += arr [ d ] ; arr [ d ] ++ ; d -- ; } } return ans ; } public static void Main ( String [ ] args ) { String str = " ( ) ( ) ( ) " ; int n = str . Length ; Console . WriteLine ( Balanced_Substring ( str , n ) ) ; } }
Count ways to divide circle using N non | Java implementation of the above approach ; Function to calculate x ^ y % mod efficiently ; Initialize the answer ; If power is odd ; Update the answer ; Square the base and half the exponent ; Return the value ; Function to calculate ncr % mod efficiently ; Initialize the answer ; Calculate ncr in O ( r ) ; Multiply with the numerator factor ; Calculate the inverse of factor of denominator ; Multiply with inverse value ; Return answer value ; Function to return the number of non intersecting chords ; define mod value ; Value of C ( 2 n , n ) ; Modulo inverse of ( n + 1 ) ; Multiply with modulo inverse ; Return the answer ; Driver code ; Function call
using System ; class GFG { static int power ( long x , int y , int mod ) { long res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; x = ( x * x ) % mod ; y = ( y >> 1 ) ; } return ( int ) ( res % mod ) ; } static int ncr ( int n , int r , int mod ) { long res = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { res = ( res * ( n - i + 1 ) ) % mod ; int inv = power ( i , mod - 2 , mod ) ; res = ( res * inv ) % mod ; } return ( int ) ( res % mod ) ; } static int NoOfChords ( int A ) { int mod = ( int ) ( 1e9 + 7 ) ; long ans = ncr ( 2 * A , A , mod ) ; int inv = power ( A + 1 , mod - 2 , mod ) ; ans = ( ans * inv ) % mod ; return ( int ) ( ans % mod ) ; } public static void Main ( ) { int N = 2 ; Console . WriteLine ( NoOfChords ( N ) ) ; } }
Longest Subarray having strictly positive XOR | C # implementation of the approach ; Function to return the length of the longest sub - array having positive XOR ; To store the XOR of all the elements ; To check if all the elements of the array are 0 s ; Take XOR of all the elements ; If any positive value is found the make the checkallzero false ; If complete array is the answer ; If all elements are equal to zero ; Initialize l and r ; First positive value of the array ; Last positive value of the array ; Maximum length among these two subarrays ; Driver code
using System ; class GFG { static int StrictlyPositiveXor ( int [ ] A , int N ) { int allxor = 0 ; bool checkallzero = true ; for ( int i = 0 ; i < N ; i += 1 ) { allxor ^= A [ i ] ; if ( A [ i ] > 0 ) checkallzero = false ; } if ( allxor != 0 ) return N ; if ( checkallzero ) return - 1 ; int l = N , r = - 1 ; for ( int i = 0 ; i < N ; i += 1 ) { if ( A [ i ] > 0 ) { l = i + 1 ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -= 1 ) { if ( A [ i ] > 0 ) { r = i + 1 ; break ; } } return Math . Max ( N - l , r - 1 ) ; } public static void Main ( ) { int [ ] A = { 1 , 0 , 0 , 1 } ; int N = A . Length ; Console . WriteLine ( StrictlyPositiveXor ( A , N ) ) ; } }
Find minimum length sub | C # implementation of the approach ; Function to return the minimum length of a sub - array which contains { 0 , 1 , 2 , 3 , 4 } as a sub - sequence ; To store the indices where 0 , 1 , 2 , 3 and 4 are present ; To store if there exist a valid prefix of sequence in array ; Base Case ; If current element is 0 ; Update the count of 0 s till now ; Push the index of the new 0 ; To check if previous element of the given sequence is found till now ; If it is the end of sequence ; Iterate for other elements of the sequence ; Binary Search to find closest occurrence less than equal to starting point ; Update the starting point ; Driver Code
using System ; class GFG { static int MAX_INT = 1000000 ; static int solve ( int [ ] array , int N ) { int [ , ] pos = new int [ 5 , 10000 ] ; int [ ] pref = new int [ 5 ] ; if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 , pos . GetLength ( 0 ) - 1 ] = 0 ; } int ans = MAX_INT ; for ( int i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 , pos . GetLength ( 0 ) - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] , pos . GetLength ( 1 ) - 1 ] = i ; if ( array [ i ] == 4 ) { int end = i ; int start = i ; for ( int j = 3 ; j >= 0 ; j -- ) { int s = 0 ; int e = pos . GetLength ( 1 ) - 1 ; int temp = - 1 ; while ( s <= e ) { int m = ( s + e ) / 2 ; if ( pos [ j , m ] <= start ) { temp = pos [ j , m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . Min ( ans , end - start + 1 ) ; } } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 } ; int N = array . Length ; Console . WriteLine ( solve ( array , N ) ) ; } }
Find optimal weights which can be used to weigh all the weights in the range [ 1 , X ] | C # implementation of the approach ; Function to find the optimal weights ; Number of weights required ; Finding the value of required powers of 3 ; Optimal Weights are powers of 3 ; Driver code
using System ; class GFG { static void findWeights ( int X ) { int sum = 0 ; int power = 0 ; int number = 3 ; while ( sum < X ) { sum = number - 1 ; sum /= 2 ; power ++ ; number *= 3 ; } int ans = 1 ; for ( int i = 1 ; i <= power ; i ++ ) { Console . Write ( ans + " ▁ " ) ; ans = ans * 3 ; } } static public void Main ( ) { int X = 2 ; findWeights ( X ) ; } }
Order of indices which is lexicographically smallest and sum of elements is <= X | C # implementation of the approach ; Function to return the chosen indices ; Maximum indices chosen ; Try to replace the first element in ans by i , making the order lexicographically smaller ; If no further replacement possible return ; If found an index smaller than ind and sum not exceeding X then remove index of smallest value from ans and then add index i to ans ; Driver code ; Print the chosen indices
using System ; using System . Collections . Generic ; class GFG { static List < int > solve ( int X , List < int > A ) { int min = int . MaxValue ; int ind = - 1 ; for ( int i = 0 ; i < A . Count ; i ++ ) { if ( A [ i ] < min ) { min = A [ i ] ; ind = i ; } } int maxIndChosen = X / min ; List < int > ans = new List < int > ( ) ; if ( maxIndChosen == 0 ) { return ans ; } for ( int i = 0 ; i < maxIndChosen ; i ++ ) { ans . Add ( ind ) ; } int temp = maxIndChosen ; int sum = maxIndChosen * A [ ind ] ; for ( int i = 0 ; i < ind ; i ++ ) { if ( sum - X == 0 temp == 0 ) break ; while ( ( sum - A [ ind ] + A [ i ] ) <= X && temp != 0 ) { ans . RemoveAt ( 0 ) ; ans . Add ( i ) ; temp -- ; sum += ( A [ i ] - A [ ind ] ) ; } } ans . Sort ( ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 4 , 8 } ; List < int > A = new List < int > ( arr ) ; int X = 18 ; List < int > ans = solve ( X , A ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } }
Find if a binary matrix exists with given row and column sums | C # implementation of above approach ; Function to check if matrix exists ; Store sum of rowsums , max of row sum number of non zero row sums ; Store sum of column sums , max of column sum number of non zero column sums ; Check condition 1 , 2 , 3 ; Driver Code
using System ; public class GFG { static bool matrix_exist ( int [ ] row , int [ ] column , int r , int c ) { int row_sum = 0 ; int column_sum = 0 ; int row_max = - 1 ; int column_max = - 1 ; int row_non_zero = 0 ; int column_non_zero = 0 ; for ( int i = 0 ; i < r ; i ++ ) { row_sum += row [ i ] ; row_max = Math . Max ( row_max , row [ i ] ) ; if ( row [ i ] > 0 ) { row_non_zero ++ ; } } for ( int i = 0 ; i < c ; i ++ ) { column_sum += column [ i ] ; column_max = Math . Max ( column_max , column [ i ] ) ; if ( column [ i ] > 0 ) { column_non_zero ++ ; } } if ( ( row_sum != column_sum ) || ( row_max > column_non_zero ) || ( column_max > row_non_zero ) ) { return false ; } return true ; } static public void Main ( ) { int [ ] row = { 2 , 2 , 2 , 2 , 2 } ; int [ ] column = { 5 , 5 , 0 , 0 } ; int r = row . Length ; int c = column . Length ; if ( matrix_exist ( row , column , r , c ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Length of longest sub | C # program for the above approach ; Function to find maximum distance between unequal elements ; Calculate maxMean ; Iterate over array and calculate largest subarray with all elements greater or equal to maxMean ; Driver code
using System ; class GFG { static int longestSubarray ( int [ ] arr , int n ) { int maxMean = 0 ; for ( int i = 1 ; i < n ; i ++ ) maxMean = Math . Max ( maxMean , ( arr [ i ] + arr [ i - 1 ] ) / 2 ) ; int ans = 0 ; int subarrayLength = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= maxMean ) ans = Math . Max ( ans , ++ subarrayLength ) ; else subarrayLength = 0 ; return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 3 , 3 , 2 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( longestSubarray ( arr , n ) ) ; } }
Maximum distance between two unequal elements | C # implementation of the approach ; Function to return the maximum distance between two unequal elements ; If first and last elements are unequal they are maximum distance apart ; Fix first element as one of the elements and start traversing from the right ; Break for the first unequal element ; To store the distance from the first element ; Fix last element as one of the elements and start traversing from the left ; Break for the first unequal element ; To store the distance from the last element ; Maximum possible distance ; Driver code
using System ; class GFG { static int maxDistance ( int [ ] arr , int n ) { if ( arr [ 0 ] != arr [ n - 1 ] ) return ( n - 1 ) ; int i = n - 1 ; while ( i > 0 ) { if ( arr [ i ] != arr [ 0 ] ) break ; i -- ; } int distFirst = ( i == 0 ) ? - 1 : i ; i = 0 ; while ( i < n - 1 ) { if ( arr [ i ] != arr [ n - 1 ] ) break ; i ++ ; } int distLast = ( i == n - 1 ) ? - 1 : ( n - 1 - i ) ; int maxDist = Math . Max ( distFirst , distLast ) ; return maxDist ; } static public void Main ( ) { int [ ] arr = { 4 , 4 , 1 , 2 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( maxDistance ( arr , n ) ) ; } }
Generate an array of K elements such that sum of elements is N and the condition a [ i ] < a [ i + 1 ] <= 2 * a [ i ] is met | Set 2 | C # implementation of the approach ; Function that print the desired array which satisfies the given conditions ; If the lowest filling condition is void , then it is not possible to generate the required array ; Increase all the elements by cnt ; Start filling from the back till the number is a [ i + 1 ] <= 2 * a [ i ] ; Get the number to be filled ; If it is less than the remaining numbers to be filled ; less than remaining numbers to be filled ; Get the sum of the array ; If this condition is void at any stage during filling up , then print - 1 ; Else add it to the sum ; If the sum condition is not satisified , then print - 1 ; Print the generated array ; Driver code
using System ; class GFG { static void solve ( int n , int k ) { int mini = 0 ; int x1 = 1 ; int [ ] a = new int [ k ] ; for ( int i = 1 ; i <= k ; i ++ ) { mini += x1 ; a [ i - 1 ] = x1 ; x1 += 1 ; } if ( n < mini ) { Console . Write ( " - 1" ) ; return ; } int rem = n - mini ; int cnt = rem / k ; rem = rem % k ; for ( int i = 0 ; i < k ; i ++ ) a [ i ] += cnt ; for ( int i = k - 1 ; i > 0 && rem > 0 ; i -- ) { int xx = a [ i - 1 ] * 2 ; int left = xx - a [ i ] ; if ( rem >= left ) { a [ i ] = xx ; rem -= left ; } else { a [ i ] += rem ; rem = 0 ; } } int sum = a [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) { if ( a [ i ] > 2 * a [ i - 1 ] ) { Console . Write ( " - 1" ) ; return ; } sum += a [ i ] ; } if ( sum != n ) { Console . Write ( " - 1" ) ; return ; } for ( int i = 0 ; i < k ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 26 , k = 6 ; solve ( n , k ) ; } }
Minimizing array sum by applying XOR operation on all elements of the array | C # implementation of the approach ; Function to return the minimized sum ; To store the frequency of bit in every element ; Finding element X ; Taking XOR of elements and finding sum ; Driver code
using System ; class GFG { static int MAX = 25 ; static int getMinSum ( int [ ] arr , int n ) { int [ ] bits_count = new int [ MAX ] ; int max_bit = 0 , sum = 0 , ans = 0 ; for ( int d = 0 ; d < n ; d ++ ) { int e = arr [ d ] , f = 0 ; while ( e > 0 ) { int rem = e % 2 ; e = e / 2 ; if ( rem == 1 ) { bits_count [ f ] += rem ; } f ++ ; } max_bit = Math . Max ( max_bit , f ) ; } for ( int d = 0 ; d < max_bit ; d ++ ) { int temp = ( int ) Math . Pow ( 2 , d ) ; if ( bits_count [ d ] > n / 2 ) ans = ans + temp ; } for ( int d = 0 ; d < n ; d ++ ) { arr [ d ] = arr [ d ] ^ ans ; sum = sum + arr [ d ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 7 , 11 , 15 } ; int n = arr . Length ; Console . WriteLine ( getMinSum ( arr , n ) ) ; } }
Maximum money that can be withdrawn in two steps | C # implementation of the approach ; Function to return the maximum coins we can get ; Update elements such that X > Y ; Take from the maximum ; Refill ; Again , take the maximum ; Driver code
using System ; class GFG { static int maxCoins ( int X , int Y ) { if ( X < Y ) { swap ( X , Y ) ; } int coins = X ; X -- ; coins += Math . Max ( X , Y ) ; return coins ; } static void swap ( int X , int Y ) { int temp = X ; X = Y ; Y = temp ; } public static void Main ( String [ ] args ) { int X = 7 , Y = 5 ; Console . WriteLine ( maxCoins ( X , Y ) ) ; } }
Minimum value of X to make all array elements equal by either decreasing or increasing by X | C # implementation of the approach ; Function that returns the minimum value of X ; Declare a set ; Iterate in the array element and insert them into the set ; If unique elements is 1 ; Unique elements is 2 ; Get both el2 and el1 ; Check if they are even ; If there are 3 unique elements ; Get three unique elements ; Check if their difference is same ; More than 3 unique elements ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findMinimumX ( int [ ] a , int n ) { List < int > st = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! st . Contains ( a [ i ] ) ) st . Add ( a [ i ] ) ; if ( st . Count == 1 ) return 0 ; if ( st . Count == 2 ) { int el1 = st [ 0 ] ; int el2 = st [ 1 ] ; if ( ( el2 - el1 ) % 2 == 0 ) return ( el2 - el1 ) / 2 ; else return ( el2 - el1 ) ; } if ( st . Count == 3 ) { int el1 = st [ 0 ] ; int el2 = st [ 1 ] ; int el3 = st [ 2 ] ; if ( ( el2 - el1 ) == ( el3 - el2 ) ) return el2 - el1 ; else return - 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 4 , 7 , 4 , 1 } ; int n = a . Length ; Console . WriteLine ( findMinimumX ( a , n ) ) ; } }
Maximum elements which can be crossed using given units of a and b | C # implementation of the above approach ; Function to find the number of elements crossed ; Keep a copy of a ; Iterate in the binary array ; If no a and b left to use ; If there is no a ; use b and increase a by 1 if arr [ i ] is 1 ; simply use b ; Use a if theres no b ; Increase a and use b if arr [ i ] == 1 ; Use a ; Driver code
using System ; class GFG { static int findElementsCrossed ( int [ ] arr , int a , int b , int n ) { int aa = a ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a == 0 && b == 0 ) break ; else if ( a == 0 ) { if ( arr [ i ] == 1 ) { b -= 1 ; a = Math . Min ( aa , a + 1 ) ; } else b -= 1 ; } else if ( b == 0 ) a -- ; else if ( arr [ i ] == 1 && a < aa ) { b -= 1 ; a = Math . Min ( aa , a + 1 ) ; } else a -- ; ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 0 , 1 , 0 , 1 } ; int n = arr . Length ; int a = 1 ; int b = 2 ; Console . WriteLine ( findElementsCrossed ( arr , a , b , n ) ) ; } }
Move all zeros to start and ones to end in an Array of random integers | C # implementation of the approach ; Utility function to print the contents of an array ; Function that pushes all the zeros to the start and ones to the end of an array ; To store the count of elements which are not equal to 1 ; Traverse the array and calculate count of elements which are not 1 ; Now all non - ones elements have been shifted to front and ' count1' is set as index of first 1. Make all elements 1 from count to end . ; Initialize lastNonBinary position to zero ; Traverse the array and pull non - zero elements to the required indices ; Ignore the 1 's ; Mark the position Of last NonBinary integer ; Place non - zero element to their required indices ; Put zeros to start of array ; Driver code
using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void pushBinaryToBorder ( int [ ] arr , int n ) { int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 1 ) arr [ count1 ++ ] = arr [ i ] ; while ( count1 < n ) arr [ count1 ++ ] = 1 ; int lastNonOne = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == 1 ) continue ; if ( lastNonOne == 0 ) { lastNonOne = i ; } if ( arr [ i ] != 0 ) arr [ lastNonOne -- ] = arr [ i ] ; } while ( lastNonOne >= 0 ) arr [ lastNonOne -- ] = 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 0 , 0 , 0 , 3 , 6 } ; int n = arr . Length ; pushBinaryToBorder ( arr , n ) ; printArr ( arr , n ) ; } }
Count the pairs of vowels in the given string | C # implementation of the approach ; Function that return true if character ch is a vowel ; Function to return the count of adjacent vowel pairs in the given string ; If current character and the character after it are both vowels ; Driver code
using System ; class GFG { static bool isVowel ( char ch ) { switch ( ch ) { case ' a ' : case ' e ' : case ' i ' : case ' o ' : case ' u ' : return true ; default : return false ; } } static int vowelPairs ( string s , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isVowel ( s [ i ] ) && isVowel ( s [ i + 1 ] ) ) cnt ++ ; } return cnt ; } public static void Main ( ) { string s = " abaebio " ; int n = s . Length ; Console . WriteLine ( vowelPairs ( s , n ) ) ; } }
Minimum possible final health of the last monster in a game | C # implementation of the approach ; Function to return the gcd of two numbers ; Function to return the minimum possible health for the monster ; gcd of first and second element ; gcd for all subsequent elements ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int solve ( int [ ] health , int n ) { int currentgcd = gcd ( health [ 0 ] , health [ 1 ] ) ; for ( int i = 2 ; i < n ; ++ i ) { currentgcd = gcd ( currentgcd , health [ i ] ) ; } return currentgcd ; } public static void Main ( String [ ] args ) { int [ ] health = { 4 , 6 , 8 , 12 } ; int n = health . Length ; Console . WriteLine ( solve ( health , n ) ) ; } }