codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array element is concatenation of two elements from another array | C # program for the above approach ; Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver code
using System ; class GFG { static void findConcatenatedNumbers ( int [ ] a , int [ ] b ) { bool ans = true ; int n1 = a . Length ; int n2 = b . Length ; int [ ] cnt = new int [ 100000 ] ; for ( int i = 0 ; i < n1 ; i ++ ) { cnt [ a [ i ] ] = 1 ; } for ( int i = 0 ; i < n2 ; i ++ ) { int left = b [ i ] ; int right = 0 ; int mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left /= 10 ; mul *= 10 ; if ( cnt [ left ] == 1 && cnt [ right ] == 1 ) { ans = false ; Console . Write ( b [ i ] + " ▁ " ) ; } } } if ( ans ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 34 , 4 , 5 } ; int [ ] b = { 26 , 24 , 345 , 4 , 22 } ; findConcatenatedNumbers ( a , b ) ; } }
Number of subarrays having even product | C # implementation of the above approach ; Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product
using System ; public class GFG { static void evenproduct ( int [ ] arr , int length ) { int count = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int product = 1 ; for ( int j = i ; j < arr . Length ; j ++ ) { product *= arr [ j ] ; if ( product % 2 == 0 ) ++ count ; } } Console . WriteLine ( count ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 5 , 4 , 9 } ; int length = arr . Length - 1 ; evenproduct ( arr , length ) ; } }
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | C # program for the above approach ; Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print the result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to [ ] adj ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int ans = 0 ; static List < int > [ ] adj = new List < int > [ ( int ) ( 1e5 ) + 1 ] ; static void dfsUtil ( int u , int par , int depth ) { foreach ( int it in adj [ u ] ) { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ans += depth ; } static void dfs ( int u , int par , int depth ) { dfsUtil ( u , par , depth ) ; Console . Write ( ans + " STRNEWLINE " ) ; } static void countPairs ( int [ , ] edges ) { for ( int i = 0 ; i < edges . GetLength ( 0 ) ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } dfs ( 1 , 1 , 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] edges = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } } ; for ( int i = 0 ; i < adj . GetLength ( 0 ) ; i ++ ) adj [ i ] = new List < int > ( ) ; countPairs ( edges ) ; } }
Sum of decimal equivalents of binary node values in each level of a Binary Tree | C # program for the above approach ; Structure of a Tree Node ; Function to convert binary number to its equivalent decimal value ; Function to calculate sum of decimal equivalent of binary numbers of node values present at each level ; Push root node into queue ; Connect nodes at the same level to form a binary number ; Append the value of the current node to eachLvl ; Insert the Left child to queue , if its not NULL ; Insert the Right child to queue , if its not NULL ; Decrement length by one ; Stores the front element of the queue ; Add decimal equivalent of the binary number formed on the current level to ans ; Finally print ans ; Driver Code ; Given Tree ; Function Call
using System ; using System . Collections . Generic ; class GFG { class TreeNode { public int val ; public TreeNode left , right ; } ; static TreeNode newNode ( int key ) { TreeNode temp = new TreeNode ( ) ; temp . val = key ; temp . left = temp . right = null ; return temp ; } static int convertBinaryToDecimal ( List < int > arr ) { int ans = 0 ; foreach ( int i in arr ) ans = ( ans << 1 ) | i ; return ans ; } static void decimalEquilvalentAtEachLevel ( TreeNode root ) { int ans = 0 ; Queue < TreeNode > que = new Queue < TreeNode > ( ) ; que . Enqueue ( root ) ; while ( true ) { int length = que . Count ; if ( length == 0 ) break ; List < int > eachLvl = new List < int > ( ) ; while ( length > 0 ) { TreeNode temp = que . Peek ( ) ; que . Dequeue ( ) ; eachLvl . Add ( temp . val ) ; if ( temp . left != null ) que . Enqueue ( temp . left ) ; if ( temp . right != null ) que . Enqueue ( temp . right ) ; length -= 1 ; } ans += convertBinaryToDecimal ( eachLvl ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { TreeNode root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; decimalEquilvalentAtEachLevel ( root ) ; } }
Minimize increments required to make differences between all pairs of array elements even | C # program for the above approach ; Function to find the minimum increments required to difference between all pairs of array elements even ; Store the count of odd and even numbers ; Traverse the array ; Increment evenCnt by 1 ; Increment eveCnt by 1 ; Print the minimum of oddCnt and eveCnt ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void minimumOperations ( int [ ] arr , int N ) { int oddCnt = 0 , evenCnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCnt ++ ; } else { oddCnt ++ ; } } Console . Write ( Math . Min ( oddCnt , evenCnt ) ) ; } static void Main ( ) { int [ ] arr = { 4 , 1 , 2 } ; int N = arr . Length ; minimumOperations ( arr , N ) ; } }
Minimum increments required to make array elements alternately even and odd | C # program for the above approach ; Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array [ ] arr ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
using System ; public class GFG { static int minIncr ( int [ ] arr , int n ) { int forEven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 == 1 ) forEven += 1 ; } } return Math . Min ( forEven , n - forEven ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 8 , 9 , 5 } ; int n = arr . Length ; Console . WriteLine ( minIncr ( arr , n ) ) ; } }
Queries to find the minimum array sum possible by removing elements from either end | C # program for the above approach ; Function to find the minimum sum for each query after removing element from either ends till each value Q [ i ] ; Stores the prefix sum from both the ends of the array ; Traverse the array from front ; Insert it into the map m1 ; Traverse the array in reverse ; Insert it into the map m2 ; Traverse the query array ; Print the minimum of the two values as the answer ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void minOperations ( int [ ] arr , int N , int [ ] Q , int M ) { Dictionary < int , int > m1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > m2 = new Dictionary < int , int > ( ) ; int front = 0 , rear = 0 ; for ( int i = 0 ; i < N ; i ++ ) { front += arr [ i ] ; m1 [ arr [ i ] ] = front ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { rear += arr [ i ] ; m2 [ arr [ i ] ] = rear ; } for ( int i = 0 ; i < M ; i ++ ) { Console . Write ( Math . Min ( m1 [ Q [ i ] ] , m2 [ Q [ i ] ] ) + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = arr . Length ; int [ ] Q = { 7 , 6 } ; int M = Q . Length ; minOperations ( arr , N , Q , M ) ; } }
Check if a string contains an anagram of another string as its substring | C # Program to implement the above approach ; Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool checkAnagram ( string s1 , string s2 ) { List < int > s2hash = new List < int > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) s2hash . Add ( 0 ) ; List < int > s1hash = new List < int > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) s1hash . Add ( 0 ) ; int s1len = s1 . Length ; int s2len = s2 . Length ; if ( s1len > s2len ) return false ; int left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 [ right ] - ' a ' ] += 1 ; s2hash [ s2 [ right ] - ' a ' ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { if ( s1hash == s2hash ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 [ right ] - ' a ' ] += 1 ; s2hash [ s2 [ left ] - ' a ' ] -= 1 ; left ++ ; } return false ; } public static void Main ( ) { string s1 = " ab " ; string s2 = " bbpobac " ; if ( checkAnagram ( s1 , s2 ) == true ) Console . WriteLine ( " NO " ) ; else Console . WriteLine ( " YES " ) ; } }
Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | C # program for the above approach ; Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver Code
using System ; class GFG { static void canPlace ( string s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s [ i ] == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s [ i + 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s [ i + 1 ] == '0' && s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { string S = "10001" ; int K = 1 ; int N = S . Length ; canPlace ( S , N , K ) ; } }
Maximum number of intervals that an interval can intersect | C # program for the above approach ; Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void findMaxIntervals ( List < Tuple < int , int > > v , int n ) { int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = n ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ i ] . Item2 < v [ j ] . Item1 v [ i ] . Item1 > v [ j ] . Item2 ) { c -- ; } } maxi = Math . Max ( c , maxi ) ; } Console . Write ( maxi ) ; } static void Main ( ) { List < Tuple < int , int > > arr = new List < Tuple < int , int > > ( ) ; arr . Add ( new Tuple < int , int > ( 1 , 2 ) ) ; arr . Add ( new Tuple < int , int > ( 3 , 4 ) ) ; arr . Add ( new Tuple < int , int > ( 2 , 5 ) ) ; int N = arr . Count ; findMaxIntervals ( arr , N ) ; } }
Check if all disks can be placed at a single rod based on given conditions | C # program to implement the above approach ; Function to check if it is possible to move all disks to a single rod ; Stores if it is possible to move all disks to a single rod ; Traverse the array ; If i - th element is smaller than both its adjacent elements ; If flag is true ; Otherwise ; Driver Code
using System ; public class GFG { static bool check ( int [ ] a , int n ) { bool flag = false ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] && a [ i ] < a [ i - 1 ] ) flag = true ; } if ( flag ) return false ; else return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 2 } ; int N = arr . Length ; if ( check ( arr , N ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Smallest positive integer that does not divide any elements of the given array | C # program for the above approach ; Function to find the smallest number which doesn 't divides any integer in  the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call
using System ; public class GFG { static void smallestNumber ( int [ ] arr , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = Math . Max ( maxi , arr [ i ] ) ; } int ans = - 1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 2 , 6 , 9 , 2 } ; int N = arr . Length ; smallestNumber ( arr , N ) ; } }
Find the duplicate characters in a string in O ( 1 ) space | C # program for the above approach ; Function to find duplicate characters in string without using any additional data structure ; Check if ( i + ' a ' ) is present in str at least once or not . ; Check if ( i + ' a ' ) is present in str at least twice or not . ; Iterate over the characters of the string str ; If str [ i ] has already occurred in str ; Set ( str [ i ] - ' a ' ) - th bit of second ; Set ( str [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code
using System ; class GFG { static void findDuplicate ( string str , int N ) { int first = 0 ; int second = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( first & ( 1 << ( str [ i ] - ' a ' ) ) ) != 0 ) { second = second | ( 1 << ( str [ i ] - ' a ' ) ) ; } else { first = first | ( 1 << ( str [ i ] - ' a ' ) ) ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( ( ( first & ( 1 << i ) ) & ( second & ( 1 << i ) ) ) != 0 ) { Console . Write ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } static public void Main ( ) { string str = " geeksforgeeks " ; int N = str . Length ; findDuplicate ( str , N ) ; } }
Minimize insertions required to make all characters of a given string equal | C # program to implement the above approach ; Function to calculate the minimum number of operations required to make all characters of the string same ; Stores count of operations ; Traverse the string ; Check if adjacent characters are same or not ; Increment count ; Print the count obtained ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minOperations ( string S ) { int count = 0 ; for ( int i = 1 ; i < S . Length ; i ++ ) { if ( S [ i ] != S [ i - 1 ] ) { count += 1 ; } } Console . Write ( count ) ; } public static void Main ( ) { string S = "0101010101" ; minOperations ( S ) ; } }
Count pairs made up of an element divisible by the other from an array consisting of powers of 2 | C # program for the above approach ; Function to count the number of pairs as per the given conditions ; Initialize array set_bits as 0 ; Store the total number of required pairs ; Traverse the array arr [ ] ; Store arr [ i ] in x ; Store the position of the leftmost set bit in arr [ i ] ; Increase bit position ; Divide by 2 to shift bits in right at each step ; Count of pairs for index i till its set bit position ; Increasing count of set bit position of current elelement ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void numberOfPairs ( int [ ] arr , int N ) { int [ ] set_bits = new int [ 31 ] ; for ( int i = 0 ; i < N ; i ++ ) { set_bits [ i ] = 0 ; } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; int bitpos = - 1 ; while ( x > 0 ) { bitpos ++ ; x /= 2 ; } for ( int j = 0 ; j <= bitpos ; j ++ ) { count += set_bits [ j ] ; } set_bits [ bitpos ] ++ ; } Console . Write ( count ) ; } static public void Main ( ) { int [ ] arr = { 4 , 16 , 8 , 64 } ; int N = arr . Length ; numberOfPairs ( arr , N ) ; } }
Queries to find minimum sum of array elements from either end of an array | C # implementation of the above approach ; Function to calculate the minimum sum from either end of the arrays for the given queries ; Traverse the query [ ] array ; Stores sum from start and end of the array ; Calculate distance from start ; Calculate distance from end ; Driver code
using System ; class GFG { static void calculateQuery ( int [ ] arr , int N , int [ ] query , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; int sum_start = 0 , sum_end = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum_start += arr [ j ] ; if ( arr [ j ] == X ) break ; } for ( int j = N - 1 ; j >= 0 ; j -- ) { sum_end += arr [ j ] ; if ( arr [ j ] == X ) break ; } Console . Write ( Math . Min ( sum_end , sum_start ) + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int [ ] queries = { 6 , 5 } ; int N = arr . Length ; int M = queries . Length ; calculateQuery ( arr , N , queries , M ) ; } }
Queries to find minimum sum of array elements from either end of an array | C # implementation of the above approach ; Function to find the minimum sum for the given queries ; Stores prefix and suffix sums ; Stores pairs of prefix and suffix sums ; Traverse the array ; Add element to prefix ; Store prefix for each element ; Traverse the array in reverse ; Add element to suffix ; Storing suffix for each element ; Traverse the array queries [ ] ; Minimum of suffix and prefix sums ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void calculateQuery ( int [ ] arr , int N , int [ ] query , int M ) { int prefix = 0 , suffix = 0 ; Dictionary < int , Tuple < int , int > > mp = new Dictionary < int , Tuple < int , int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { prefix += arr [ i ] ; mp [ arr [ i ] ] = new Tuple < int , int > ( prefix , 0 ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suffix += arr [ i ] ; mp [ arr [ i ] ] = new Tuple < int , int > ( mp [ arr [ i ] ] . Item1 , suffix ) ; } for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; Console . Write ( Math . Min ( mp [ X ] . Item1 , mp [ X ] . Item2 ) + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int [ ] queries = { 6 , 5 } ; int N = arr . Length ; int M = queries . Length ; calculateQuery ( arr , N , queries , M ) ; } }
Queries to find the maximum array element after removing elements from a given range | C # program for the above approach ; Function to find the maximum element after removing elements in range [ l , r ] ; Store prefix maximum element ; Store suffix maximum element ; Prefix max till first index is first index itself ; Traverse the array to fill prefix max array ; Store maximum till index i ; Suffix max till last index is last index itself ; Traverse the array to fill suffix max array ; Store maximum till index i ; Traverse all queries ; Store the starting and the ending index of the query ; Edge Cases ; Otherwise ; Driver Code
using System ; public class GFG { static void findMaximum ( int [ ] arr , int N , int Q , int [ , ] queries ) { int [ ] prefix_max = new int [ N + 1 ] ; int [ ] suffix_max = new int [ N + 1 ] ; prefix_max [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefix_max [ i ] = Math . Max ( prefix_max [ i - 1 ] , arr [ i ] ) ; } suffix_max [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { suffix_max [ i ] = Math . Max ( suffix_max [ i + 1 ] , arr [ i ] ) ; } for ( int i = 0 ; i < Q ; i ++ ) { int l = queries [ i , 0 ] ; int r = queries [ i , 1 ] ; if ( l == 0 && r == ( N - 1 ) ) Console . Write ( "0 STRNEWLINE " ) ; else if ( l == 0 ) Console . Write ( suffix_max [ r + 1 ] + " STRNEWLINE " ) ; else if ( r == ( N - 1 ) ) Console . Write ( prefix_max [ l - 1 ] + " STRNEWLINE " ) ; else Console . Write ( Math . Max ( prefix_max [ l - 1 ] , suffix_max [ r + 1 ] ) + " STRNEWLINE " ) ; } } static public void Main ( ) { int [ ] arr = { 5 , 6 , 8 , 10 , 15 } ; int N = arr . Length ; int [ , ] queries = { { 0 , 1 } , { 0 , 2 } , { 1 , 4 } } ; int Q = queries . GetLength ( 0 ) ; findMaximum ( arr , N , Q , queries ) ; } }
Find index of the element differing in parity with all other array elements | C # program for the above approach ; Function to print the element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Driver code ; Given array ; Size of the array
using System ; class GFG { static void oddOneOut ( int [ ] arr , int N ) { int odd = 0 , even = 0 ; int lastOdd = 0 , lastEven = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { Console . WriteLine ( lastOdd ) ; } else { Console . WriteLine ( lastEven ) ; } } static void Main ( ) { int [ ] arr = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . Length ; oddOneOut ( arr , N ) ; } }
Reduce a string to a valid email address of minimum length by replacing specified substrings | C # program for the above approach ; Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' # such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver code ; To display the result
using System ; public class GFG { static String minEmail ( String email ) { String ans = " " ; int len = email . Length ; ans += email [ 0 ] ; int i = 1 ; bool notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email [ i ] == ' a ' && email [ i + 1 ] == ' t ' ) { ans += ' @ ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email [ i ] == ' d ' && email [ i + 1 ] == ' o ' && email [ i + 2 ] == ' t ' ) { ans += ' . ' ; i += 2 ; } else { ans += email [ i ] ; } i += 1 ; } return ans ; } public static void Main ( String [ ] args ) { String email = " geeksforgeeksatgmaildotcom " ; Console . WriteLine ( minEmail ( email ) ) ; } }