codeparrot/xlcost-text-to-code huggingface dataset:
Minimize maximum array element possible by at most K splits on the given array | C # program for the above approach ; Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code
using System ; class GFG { static bool possible ( int [ ] A , int N , int mid , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += ( A [ i ] - 1 ) / mid ; } return count <= K ; } static int minimumMaximum ( int [ ] A , int N , int K ) { int lo = 1 ; Array . Sort ( A ) ; int hi = A [ N - 1 ] ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 2 } ; int K = 4 ; int N = arr . Length ; Console . WriteLine ( minimumMaximum ( arr , N , K ) ) ; } }
Maximum value of X such that difference between any array element and X does not exceed K | C # program for the above approach ; Function to find maximum value of X such that | A [ i ] - X | a K ; Stores the smallest array element ; Store the possible value of X ; Traverse the array A [ ] ; If required criteria is not satisfied ; Update ans ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maximumNumber ( int [ ] arr , int N , int K ) { int mn = 100000000 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < mn ) mn = arr [ i ] ; } int ans = mn + K ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . Abs ( arr [ i ] - ans ) > K ) { ans = - 1 ; break ; } } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 } ; int K = 2 ; int N = arr . Length ; maximumNumber ( arr , N , K ) ; } }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | C # program for the above approach ; Function to check if a number is prime or not ; Base Cases ; Check if n is divisible by 2 or 3 ; Iterate over every 6 number from the range [ 5 , sqrt ( N ) ] ; If n is found to be non - prime ; Otherwise , return true ; Function to check if a number can be represented as the sum of a prime number and a perfect square or not ; Stores all perfect squares less than N ; Store the perfect square in the array ; Iterate over all perfect squares ; Store the difference of perfect square from n ; If difference is prime ; Update flag ; Break out of the loop ; If N is the sum of a prime number and a perfect square ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static void sumOfPrimeSquare ( int n ) { int i = 0 ; List < int > squares = new List < int > ( ) ; while ( i * i < n ) { squares . Add ( i * i ) ; i ++ ; } bool flag = false ; for ( i = 0 ; i < squares . Count ; i ++ ) { int difference = n - squares [ i ] ; if ( isPrime ( difference ) ) { flag = true ; break ; } } if ( flag ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } public static void Main ( ) { int N = 27 ; sumOfPrimeSquare ( N ) ; } }
Check if a number can be represented as a sum of a Prime Number and a Perfect Square | C # program for the above approach ; Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Code
using System ; class GFG { static void SieveOfEratosthenes ( bool [ ] prime , int n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void sumOfPrimeSquare ( int n ) { bool flag = false ; bool [ ] prime = new bool [ n + 1 ] ; Array . Fill ( prime , true ) ; SieveOfEratosthenes ( prime , n ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) continue ; int dif = n - i ; if ( Math . Ceiling ( ( double ) Math . Sqrt ( dif ) ) == Math . Floor ( ( double ) Math . Sqrt ( dif ) ) ) { flag = true ; break ; } } if ( flag ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int N = 27 ; sumOfPrimeSquare ( N ) ; } }
Check if any subarray of length M repeats at least K times consecutively or not | C # program for the above approach ; Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver code
using System ; class GFG { static bool check ( int [ ] arr , int M , int K , int ind ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < K ; j ++ ) { if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) { return false ; } } } return true ; } static bool SubarrayRepeatsKorMore ( int [ ] arr , int N , int M , int K ) { for ( int ind = 0 ; ind <= N - M * K ; ind ++ ) { if ( check ( arr , M , K , ind ) ) { return true ; } } return false ; } static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = arr . Length ; if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Check if any subarray of length M repeats at least K times consecutively or not | C # program for the above approach ; Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code
using System ; class GFG { public static bool checkExists ( int [ ] arr , int N , int M , int K ) { int count = 0 ; for ( int i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int N = arr . Length ; int M = 2 , K = 2 ; if ( checkExists ( arr , N , M , K ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Number of common digits present in two given numbers | C # program for the above approach ; Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Driver code ; Input
using System ; class GFG { static int CommonDigits ( int N , int M ) { int count = 0 ; int [ ] freq1 = new int [ 10 ] ; int [ ] freq2 = new int [ 10 ] ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = N / 10 ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = M / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } static void Main ( ) { int N = 748294 ; int M = 34298156 ; Console . WriteLine ( CommonDigits ( N , M ) ) ; } }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | C # program for the above approach ; Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
using System ; class GFG { static void minimumNumber ( int K , int X ) { if ( K > X ) { Console . Write ( " - 1" ) ; return ; } int ans = 0 ; int sum = 0 ; for ( int i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } Console . Write ( ans ) ; } public static void Main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }
Smallest value of N such that the sum of all natural numbers from K to N is at least X | C # program for the above approach ; Function to check if the sum of natural numbers from K to N is >= X ; Function to find the minimum value of N such that the sum of natural numbers from K to N is at least X ; If K is greater than X ; Perform the Binary Search ; If the sum of the natural numbers from K to mid is atleast X ; Update res ; Update high ; Otherwise , update low ; Print the value of res as the answer ; Driver Code
using System ; class GFG { static bool isGreaterEqual ( int N , int K , int X ) { return ( ( N * 1L * ( N + 1 ) / 2 ) - ( ( K - 1 ) * 1L * K / 2 ) ) >= X ; } static void minimumNumber ( int K , int X ) { if ( K > X ) { Console . Write ( " - 1" ) ; return ; } int low = K , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , K , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } Console . WriteLine ( res ) ; } public static void Main ( ) { int K = 5 , X = 13 ; minimumNumber ( K , X ) ; } }
Count pairs with product of indices equal to the product of elements present at those indices | C # program for the above approach ; Function to count the number of pairs having product of indices equal to the product of elements at that indices ; Stores the count of valid pairs ; Generate all possible pairs ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver Code
using System ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 2 , 6 } ; int N = arr . Length ; Console . Write ( CountPairs ( arr , N ) ) ; } }
Length of the longest path ending at vertex V in a Graph | C # program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex v from any source node ; Stores the maximum length of the path ending at vertex v ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int distance ; static void dfs ( int src , List < List < int > > Adj , List < bool > visited , int level ) { visited [ src ] = true ; distance = Math . Max ( distance , level ) ; foreach ( int child in Adj [ src ] ) { if ( ( child != src ) && ( visited [ child ] == false ) ) { dfs ( child , Adj , visited , level + 1 ) ; } } visited [ src ] = false ; } static int maximumLength ( int [ , ] mat , int v ) { distance = 0 ; int N = mat . GetLength ( 0 ) ; List < bool > visited = new List < bool > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { visited . Add ( false ) ; } List < List < int > > Adj = new List < List < int > > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { Adj . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 0 ) ; j ++ ) { if ( mat [ i , j ] == 1 ) { Adj [ i ] . Add ( j ) ; } } } dfs ( v , Adj , visited , 0 ) ; return distance ; } static void Main ( ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 } , { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } } ; int v = 2 ; Console . Write ( maximumLength ( mat , v ) ) ; } }
Minimum removals required such that given string consists only of a pair of alternating characters | C # program for the above approach ; Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Driver Code ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
using System ; using System . Collections . Generic ; class GFG { static int findLength ( string s , char i , char j ) { char required = i ; int length = 0 ; for ( int k = 0 ; k < s . Length ; k ++ ) { char curr = s [ k ] ; if ( curr == required ) { length += 1 ; if ( required == i ) required = j ; else required = i ; } } return length ; } static int minimumDeletions ( string S ) { int len = 0 ; int n = S . Length ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = i + 1 ; j < 26 ; j ++ ) { int newLen = findLength ( S , ( char ) ( i + 97 ) , ( char ) ( j + 97 ) ) ; len = Math . Max ( len , newLen ) ; } } return n - len ; } public static void Main ( string [ ] args ) { string S = " adebbeeaebd " ; Console . WriteLine ( minimumDeletions ( S ) ) ; } }
Smallest value of X satisfying the condition X % A [ i ] = B [ i ] for two given arrays | C # program for the above approach ; Function to calculate modulo inverse of a w . r . t m using Extended Euclid Algorithm ; Base Case ; Perform extended euclid algorithm ; q is quotient ; m is remainder now , process same as euclid 's algorithm ; If x1 is negative ; Make x1 positive ; Function to implement Chinese Remainder Theorem to find X ; Stores the product of array elements ; Traverse the array ; Update product ; Initialize the result ; Apply the above formula ; Function to calculate the product of all elements of the array a [ ] ; Stores product of all array elements ; Traverse the array ; Return the product ; Function to find the value of X that satisfies the given condition ; Stores the required smallest value using Chinese Remainder Theorem ; Stores the product of all array elements ; The equation is Y + K * M >= P Therefore , calculate K = ceil ( ( P - Y ) / M ) ; So , X = Y + K * M ; Print the resultant value of X ; Driver Code
using System ; public class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int [ ] A , int [ ] B , int N ) { int prod = 1 ; for ( int i = 0 ; i < N ; i ++ ) prod *= A [ i ] ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pp = prod / A [ i ] ; result += B [ i ] * inv ( pp , A [ i ] ) * pp ; } return result % prod ; } static int product ( int [ ] a ) { int ans = 1 ; for ( int i = 0 ; i < a . Length ; i ++ ) { ans *= a [ i ] ; } return ans ; } public static void findSmallestInteger ( int [ ] A , int [ ] B , int P ) { int Y = findMinX ( A , B , A . Length ) ; int M = product ( A ) ; int K = ( int ) Math . Ceiling ( ( ( double ) P - ( double ) Y ) / ( double ) M ) ; int X = Y + K * M ; Console . WriteLine ( X ) ; } public static void Main ( string [ ] args ) { int [ ] A = { 3 , 4 , 5 } ; int [ ] B = { 2 , 3 , 1 } ; int P = 72 ; findSmallestInteger ( A , B , P ) ; } }
Minimum number of basic logic gates required to realize given Boolean expression | C # implementation of the above approach ; Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Driver Code ; Input ; Function call to count the total number of gates required
using System ; public class GFG { static void numberOfGates ( string s ) { int N = s . Length ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' . ' s [ i ] == ' + ' s [ i ] == '1' ) { ans ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { string S = " ( 1 - A ) . B + C " ; numberOfGates ( S ) ; } }
Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | C # program for the above approach ; Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
using System ; class GFG { static void findPairs ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] = Math . Abs ( arr [ i ] ) ; Array . Sort ( arr ) ; int left = 0 ; int ans = 0 ; for ( int right = 0 ; right < N ; right ++ ) { while ( 2 * arr [ left ] < arr [ right ] ) left ++ ; ans += ( right - left ) ; } Console . Write ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 7 } ; int N = arr . Length ; findPairs ( arr , N ) ; } }
Calculate absolute difference between minimum and maximum sum of pairs in an array | C # Program to implement the above approach ; Function to find the difference between the maximum and minimum sum of a pair ( arr [ i ] , arr [ j ] ) from the array such that i < j and arr [ i ] < arr [ j ] ; Stores the maximum from the suffix of the array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Stores the maximum sum of any pair ; Calculate the maximum sum ; Stores the maximum sum of any pair ; Stores the minimum of suffixes from the given array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Calculate the minimum sum ; Return the resultant difference ; Driver Code ; Function Call
using System ; class GFG { static int GetDiff ( int [ ] A , int N ) { int [ ] SuffMaxArr = new int [ N ] ; SuffMaxArr [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMaxArr [ i ] = Math . Max ( SuffMaxArr [ i + 1 ] , A [ i + 1 ] ) ; } int MaximumSum = Int32 . MinValue ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMaxArr [ i ] ) MaximumSum = Math . Max ( MaximumSum , A [ i ] + SuffMaxArr [ i ] ) ; } int MinimumSum = Int32 . MaxValue ; int [ ] SuffMinArr = new int [ N ] ; SuffMinArr [ N - 1 ] = Int32 . MaxValue ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMinArr [ i ] = Math . Min ( SuffMinArr [ i + 1 ] , A [ i + 1 ] ) ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMinArr [ i ] ) { MinimumSum = Math . Min ( MinimumSum , A [ i ] + SuffMinArr [ i ] ) ; } } return Math . Abs ( MaximumSum - MinimumSum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 3 , 7 , 5 , 6 } ; int N = arr . Length ; Console . WriteLine ( GetDiff ( arr , N ) ) ; } }
Minimum number of swaps required to make parity of array elements same as their indices | C # program to implement the above approach ; Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver Code
using System ; class GFG { static void minimumSwaps ( int [ ] arr , int N ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( even ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 2 , 7 , 6 } ; int N = arr . Length ; minimumSwaps ( arr , N ) ; } }
Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | C # program for the above approach ; Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
using System ; class GFG { static int mininsert ( int [ ] arr , int K , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = Math . Min ( arr [ i ] , arr [ i + 1 ] ) ; int b = Math . Max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 10 , 25 , 21 } ; int K = 2 ; int N = arr . Length ; Console . Write ( mininsert ( arr , K , N ) ) ; } }
Sum of numbers formed by consecutive digits present in a given string | C # Program to implement the above approach ; Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver code
using System ; class GFG { static int sumOfDigits ( string s ) { int curr = 0 ; int ret = 0 ; foreach ( char ch in s ) { if ( ch >= 48 && ch <= 57 ) { curr = curr * 10 + ch - '0' ; } else { ret += curr ; curr = 0 ; } } ret += curr ; return ret ; } static void Main ( ) { string S = "11aa32bbb5" ; Console . WriteLine ( sumOfDigits ( S ) ) ; } }
Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | C # program to implement the above approach ; Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver Code
using System ; public class GFG { static void print_triplet ( int [ ] arr , int n ) { for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { Console . Write ( i - 1 + " ▁ " + i + " ▁ " + ( i + 1 ) ) ; return ; } } Console . Write ( - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 5 , 2 , 1 , 6 } ; int N = arr . Length ; print_triplet ( arr , N ) ; } }