codeparrot/xlcost-text-to-code huggingface dataset:
Check if given string can be formed by two other strings or their permutations | C # implementation of the above approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . Join ( " " , s ) ; } static Boolean isPossible ( List < String > v , String str ) { str = countingsort ( str . ToCharArray ( ) ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . Count ; j ++ ) { String temp = v [ i ] + v [ j ] ; temp = countingsort ( temp . ToCharArray ( ) ) ; if ( temp . Equals ( str ) ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; List < String > v = new List < String > ( arr ) ; if ( isPossible ( v , str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Queries to find the first non | C # implementation of the approach ; Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the first non - repeating character in range [ l . . r ] ; Starting from the first character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
using System ; class GFG { static int MAX = 256 ; static int [ , ] freq ; static void preCalculate ( string str , int n ) { freq [ ( int ) str [ 0 ] , 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str [ i ] ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j , i ] = freq [ j , i - 1 ] + 1 ; else freq [ j , i ] = freq [ j , i - 1 ] ; } } } static int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch , r ] ; else return ( freq [ ( int ) ch , r ] - freq [ ( int ) ch , l - 1 ] ) ; } static string firstNonRepeating ( string str , int n , int l , int r ) { for ( int i = l ; i < r ; i ++ ) { char ch = str [ i ] ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void Main ( ) { string str = " GeeksForGeeks " ; int n = str . Length ; int [ , ] queries = { { 0 , 3 } , { 2 , 3 } , { 5 , 12 } } ; int q = queries . Length ; freq = new int [ MAX , n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( firstNonRepeating ( str , n , queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } } }
Length of the largest substring which have character with frequency greater than or equal to half of the substring | C # implementation of the above approach ; Function to return the length of the longest sub string having frequency of a character greater than half of the length of the sub string ; for each of the character ' a ' to ' z ' ; finding frequency prefix array of the character ; Finding the r [ ] and l [ ] arrays . ; for each j from 0 to n ; Finding the lower bound of i . ; storing the maximum value of i - j + 1 ; clearing all the vector so that it can be used for other characters . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxLength ( String s , int n ) { int ans = int . MinValue ; List < int > A = new List < int > ( ) ; List < int > L = new List < int > ( ) ; List < int > R = new List < int > ( ) ; int [ ] freq = new int [ n + 5 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] - ' a ' == i ) count ++ ; freq [ j ] = count ; } for ( int j = 1 ; j < n ; j ++ ) { L . Add ( ( 2 * freq [ j - 1 ] ) - j ) ; R . Add ( ( 2 * freq [ j ] ) - j ) ; } int max_len = int . MinValue ; int min_val = int . MaxValue ; for ( int j = 0 ; j < L . Count ; j ++ ) { min_val = Math . Min ( min_val , L [ j ] ) ; A . Add ( min_val ) ; int l = 0 , r = j ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( A [ mid ] <= R [ j ] ) { max_len = Math . Max ( max_len , j - mid + 1 ) ; r = mid - 1 ; } else { l = mid + 1 ; } } } ans = Math . Max ( ans , max_len ) ; A . Clear ( ) ; R . Clear ( ) ; L . Clear ( ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = " ababbbacbcbcca " ; int n = s . Length ; Console . WriteLine ( maxLength ( s , n ) ) ; } }
Repeated Character Whose First Appearance is Leftmost | C # program to find first repeating character ; The function returns index of the first repeating character in a string . If all characters are repeating then returns - 1 ; Mark all characters as not visited ; Traverse from right and update res as soon as we see a visited character . ; Driver Code
using System ; class GFG { static int NO_OF_CHARS = 256 ; static int firstRepeating ( String str ) { Boolean [ ] visited = new Boolean [ NO_OF_CHARS ] ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) visited [ i ] = false ; int res = - 1 ; for ( int i = str . Length - 1 ; i >= 0 ; i -- ) { if ( visited [ str [ i ] ] == false ) visited [ str [ i ] ] = true ; else res = i ; } return res ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int index = firstRepeating ( str ) ; if ( index == - 1 ) Console . Write ( " Either ▁ all ▁ characters ▁ are ▁ " + " distinct ▁ or ▁ string ▁ is ▁ empty " ) ; else Console . Write ( " First ▁ Repeating ▁ character " + " ▁ is ▁ { 0 } " , str [ index ] ) ; } }
Find maximum sum taking every Kth element in the array | C # implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n , int K ) { int maximum = int . MinValue ; int [ ] sum = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = Math . Max ( maximum , sum [ i ] ) ; } return maximum ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . Length ; int K = 2 ; Console . Write ( maxSum ( arr , n , K ) ) ; } }
Count common characters in two strings | C # implementation of the approach ; Function to return the count of valid indices pairs ; To store the frequencies of characters of string s1 and s2 ; To store the count of valid pairs ; Update the frequencies of the characters of string s1 ; Update the frequencies of the characters of string s2 ; Find the count of valid pairs ; Driver code
using System ; class GFG { static int countPairs ( string s1 , int n1 , string s2 , int n2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; Array . Fill ( freq1 , 0 ) ; Array . Fill ( freq2 , 0 ) ; int i , count = 0 ; for ( i = 0 ; i < n1 ; i ++ ) freq1 [ s1 [ i ] - ' a ' ] ++ ; for ( i = 0 ; i < n2 ; i ++ ) freq2 [ s2 [ i ] - ' a ' ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) count += ( Math . Min ( freq1 [ i ] , freq2 [ i ] ) ) ; return count ; } public static void Main ( ) { string s1 = " geeksforgeeks " , s2 = " platformforgeeks " ; int n1 = s1 . Length , n2 = s2 . Length ; Console . WriteLine ( countPairs ( s1 , n1 , s2 , n2 ) ) ; } }
Find a distinct pair ( x , y ) in given range such that x divides y | C # implementation of the approach ; Driver code
using System ; class GFG { static void findpair ( int l , int r ) { int c = 0 ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = i + 1 ; j <= r ; j ++ ) { if ( j % i == 0 && j != i ) { Console . Write ( i + " , ▁ " + j ) ; c = 1 ; break ; } } if ( c == 1 ) break ; } } static void Main ( ) { int l = 1 , r = 10 ; findpair ( l , r ) ; } }
Check if the given array can be reduced to zeros with the given operation performed given number of times | C # implementation of the approach ; Function that returns true if the array can be reduced to 0 s with the given operation performed given number of times ; Set to store unique elements ; Add every element of the array to the set ; Count of all the unique elements in the array ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static bool check ( int [ ] arr , int N , int K ) { HashSet < int > unique = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { unique . Add ( arr [ i ] ) ; } if ( unique . Count == K ) { return true ; } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 1 , 2 , 3 } ; int N = arr . Length ; int K = 3 ; if ( check ( arr , N , K ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Print all the sum pairs which occur maximum number of times | C # implementation of above approach ; Function to find the sum pairs that occur the most ; Hash - table ; Keep a count of sum pairs ; Variables to store maximum occurrence ; Iterate in the hash table ; Print all sum pair which occur maximum number of times ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findSumPairs ( int [ ] a , int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( mpp . ContainsKey ( a [ i ] + a [ j ] ) ) { var val = mpp [ a [ i ] + a [ j ] ] ; mpp . Remove ( a [ i ] + a [ j ] ) ; mpp . Add ( a [ i ] + a [ j ] , val + 1 ) ; } else { mpp . Add ( a [ i ] + a [ j ] , 1 ) ; } } } int occur = 0 ; foreach ( KeyValuePair < int , int > entry in mpp ) { if ( entry . Value > occur ) { occur = entry . Value ; } } foreach ( KeyValuePair < int , int > entry in mpp ) { if ( entry . Value == occur ) Console . WriteLine ( entry . Key ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 } ; int n = a . Length ; findSumPairs ( a , n ) ; } }
Minimum index i such that all the elements from index i to given index are equal | C # implementation of the approach ; Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call
using System ; class GFG { static int minIndex ( int [ ] arr , int n , int pos ) { int num = arr [ pos ] ; int i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 1 , 1 , 5 , 2 } ; int n = arr . Length ; int pos = 4 ; Console . WriteLine ( minIndex ( arr , n , pos ) ) ; } }
Minimum index i such that all the elements from index i to given index are equal | C # implementation of the approach ; Function to return the minimum required index ; Short - circuit more comparisions as found the border point ; For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not point to the correct index . ; Driver code ; Console . WriteLine ( minIndex ( arr , 2 ) ) ; Should be 1 Console . WriteLine ( minIndex ( arr , 3 ) ) ; Should be 1 Console . WriteLine ( minIndex ( arr , 4 ) ) ; Should be 4
using System ; class GFG { static int minIndex ( int [ ] arr , int pos ) { int low = 0 ; int high = pos ; int i = pos ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] != arr [ pos ] ) { low = mid + 1 ; } else { high = mid - 1 ; i = mid ; if ( mid > 0 && arr [ mid - 1 ] != arr [ pos ] ) { break ; } } } return arr [ low ] == arr [ pos ] ? low : i ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 1 , 1 , 5 , 2 } ; } }
Count of strings that become equal to one of the two strings after one removal | C # implementation of the approach ; Function to return the count of the required strings ; Searching index after longest common prefix ends ; Searching index before longest common suffix ends ; If str1 = str2 ; If only 1 character is different in both the strings ; Checking remaining part of string for equality ; Searching in right of string h ( g to h ) ; Driver code
using System ; class GFG { static int findAnswer ( string str1 , string str2 , int n ) { int l = 0 , r = 0 ; int ans = 2 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str1 [ i ] != str2 [ i ] ) { l = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str1 [ i ] != str2 [ i ] ) { r = i ; break ; } } if ( r < l ) return 26 * ( n + 1 ) ; else if ( l == r ) return ans ; else { for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 [ i ] != str2 [ i - 1 ] ) { ans -- ; break ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 [ i - 1 ] != str2 [ i ] ) { ans -- ; break ; } } return ans ; } } public static void Main ( ) { String str1 = " toy " , str2 = " try " ; int n = str1 . Length ; Console . WriteLine ( findAnswer ( str1 , str2 , n ) ) ; } }
Minimize the maximum minimum difference after one removal from array | C # implementation of the approach ; Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code
using System ; public class GFG { static int findMinDifference ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int diff1 = arr [ n - 1 ] - arr [ 1 ] ; int diff2 = arr [ n - 2 ] - arr [ 0 ] ; return Math . Min ( diff1 , diff2 ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . Length ; Console . Write ( findMinDifference ( arr , n ) ) ; } }
Minimize the maximum minimum difference after one removal from array | C # implementation of the approach ; Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code
using System ; public class GFG { static int findMinDifference ( int [ ] arr , int n ) { int min , secondMin , max , secondMax ; min = secondMax = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 0 ] : arr [ 1 ] ; max = secondMin = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 1 ] : arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } else if ( arr [ i ] < min ) { secondMin = min ; min = arr [ i ] ; } else if ( arr [ i ] < secondMin ) { secondMin = arr [ i ] ; } } int diff = Math . Min ( max - secondMin , secondMax - min ) ; return diff ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( findMinDifference ( arr , n ) ) ; } }
Integers from the range that are composed of a single distinct digit | C # implementation of the approach ; Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code
using System ; class GFG { static Boolean checkDistinct ( int x ) { int last = x % 10 ; while ( x > 0 ) { if ( x % 10 != last ) return false ; x = x / 10 ; } return true ; } static int findCount ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( checkDistinct ( i ) ) count += 1 ; } return count ; } static public void Main ( String [ ] args ) { int L = 10 , R = 50 ; Console . WriteLine ( findCount ( L , R ) ) ; } }
Smallest Pair Sum in an array | C # program to print the sum of the minimum pair ; Function to return the sum of the minimum pair from the array ; If found new minimum ; Minimum now becomes second minimum ; Update minimum ; If current element is > min and < secondMin ; Update secondMin ; Return the sum of the minimum pair ; Driver code
using System ; class GFG { static int smallest_pair ( int [ ] a , int n ) { int min = int . MaxValue , secondMin = int . MaxValue ; for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] < min ) { secondMin = min ; min = a [ j ] ; } else if ( ( a [ j ] < secondMin ) && a [ j ] != min ) secondMin = a [ j ] ; } return ( secondMin + min ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; Console . Write ( smallest_pair ( arr , n ) ) ; } }
Longest subarray with elements divisible by k | C # program of above approach ; function to find longest subarray ; this will contain length of longest subarray found ; Driver code
using System ; class GFG { static int longestsubarray ( int [ ] arr , int n , int k ) { int current_count = 0 ; int max_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) current_count ++ ; else current_count = 0 ; max_count = Math . Max ( current_count , max_count ) ; } return max_count ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 11 , 32 , 64 , 88 } ; int n = arr . Length ; int k = 8 ; Console . Write ( longestsubarray ( arr , n , k ) ) ; } }
Remove elements that appear strictly less than k times | C # program to remove the elements which appear strictly less than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear more than or equal to k times . ; Driver code
using System ; using System . Collections . Generic ; class geeks { public static void removeElements ( int [ ] arr , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp . Add ( arr [ i ] , 1 ) ; else { int x = mp [ arr [ i ] ] ; mp [ arr [ i ] ] = mp [ arr [ i ] ] + ++ x ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp [ arr [ i ] ] >= k ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; removeElements ( arr , n , k ) ; } }
Check if a string contains a palindromic sub | C # program to check if there is a substring palindrome of even length . ; function to check if two consecutive same characters are present ; Driver Code
using System ; public class GFG { static bool check ( String s ) { for ( int i = 0 ; i < s . Length - 1 ; i ++ ) if ( s [ i ] == s [ i + 1 ] ) return true ; return false ; } public static void Main ( ) { String s = " xzyyz " ; if ( check ( s ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Remove elements from the array which appear more than k times | C # program to remove the elements which appear more than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear less than or equal to k times . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void RemoveElements ( int [ ] arr , int n , int k ) { 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 ; } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) && mp [ arr [ i ] ] <= k ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; RemoveElements ( arr , n , k ) ; } }
Find the smallest after deleting given elements | C # program to find the smallest number from the array after n deletions ; Returns minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the smallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it smallestElement ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findSmallestAfterDel ( int [ ] arr , int m , int [ ] del , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] = mp [ del [ i ] ] + 1 ; } else { mp . Add ( del [ i ] , 1 ) ; } } int smallestElement = int . MaxValue ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] - 1 ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else smallestElement = Math . Min ( smallestElement , arr [ i ] ) ; } return smallestElement ; } public static void Main ( String [ ] args ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 4 , 56 , 5 } ; int n = del . Length ; Console . WriteLine ( findSmallestAfterDel ( array , m , del , n ) ) ; } }
Find the largest after deleting the given elements | C # program to find the largest number from the array after n deletions ; Returns maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findlargestAfterDel ( int [ ] arr , int m , int [ ] del , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( del [ i ] , 1 ) ; } } int largestElement = int . MinValue ; for ( int i = 0 ; i < m ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] - 1 ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else largestElement = Math . Max ( largestElement , arr [ i ] ) ; } return largestElement ; } public static void Main ( String [ ] args ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 33 , 56 , 5 } ; int n = del . Length ; Console . WriteLine ( findlargestAfterDel ( array , m , del , n ) ) ; } }
Number of anomalies in an array | A simple C # solution to count anomalies in an array . ; Driver code
using System ; class GFG { static int countAnomalies ( int [ ] arr , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && Math . Abs ( arr [ i ] - arr [ j ] ) <= k ) break ; if ( j == n ) res ++ ; } return res ; } public static void Main ( ) { int [ ] arr = { 7 , 1 , 8 } ; int k = 5 ; int n = arr . Length ; Console . WriteLine ( countAnomalies ( arr , n , k ) ) ; } }