codeparrot/xlcost-text-to-code huggingface dataset:
Find index of an extra element present in one sorted array | C # code for above approach ; Function to find Index ; Calculating extra element ; returns index of extra element ; function return sum of array elements ; function return index of given element ; Driver Code
using System ; class GFG { static int find_extra_element_index ( int [ ] arrA , int [ ] arrB ) { int extra_element = sum ( arrA ) - sum ( arrB ) ; return indexOf ( arrA , extra_element ) ; } static int sum ( int [ ] arr ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int indexOf ( int [ ] arr , int element ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arrA = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arrB = { 2 , 4 , 6 , 8 , 10 , 12 } ; Console . WriteLine ( find_extra_element_index ( arrA , arrB ) ) ; } }
Largest subset whose all elements are Fibonacci numbers | C # program to find largest Fibonacci subset ; Prints largest subset of an array whose all elements are fibonacci numbers ; Find maximum element in arr [ ] ; Generate all Fibonacci numbers till max and store them ; Now iterate through all numbers and quickly check for Fibonacci ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { public static void findFibSubset ( int [ ] x ) { List < int > fib = new List < int > ( ) ; List < int > result = new List < int > ( ) ; int max = x . Max ( ) ; int a = 0 ; int b = 1 ; while ( b < max ) { int c = a + b ; a = b ; b = c ; fib . Add ( c ) ; } for ( int i = 0 ; i < x . Length ; i ++ ) { if ( fib . Contains ( x [ i ] ) ) { result . Add ( x [ i ] ) ; } } foreach ( int i in result ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; findFibSubset ( a ) ; } }
Pairs of Amicable Numbers | Efficient C # program to count Amicable pairs in an array . ; Calculate the sum of proper divisors ; 1 is a proper divisor ; To handle perfect squares ; Check if pair is amicable ; This function prints count of amicable pairs present in the input array ; Map to store the numbers ; Iterate through each number , and find the sum of proper divisors and check if it 's  also present in the array ; It 's sum of proper divisors ; As the pairs are counted twice , thus divide by 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int sumOfDiv ( int x ) { int sum = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } static Boolean isAmicable ( int a , int b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } static int countPairs ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( sumOfDiv ( arr [ i ] ) ) ) { int sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return count / 2 ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 220 , 284 , 1184 , 1210 , 2 , 5 } ; int n1 = arr1 . Length ; Console . WriteLine ( countPairs ( arr1 , n1 ) ) ; int [ ] arr2 = { 2620 , 2924 , 5020 , 5564 , 6232 , 6368 } ; int n2 = arr2 . Length ; Console . WriteLine ( countPairs ( arr2 , n2 ) ) ; } }
Minimum number of characters required to be removed such that every character occurs same number of times | C # program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input
using System . Collections . Generic ; using System ; class GFG { static int minimumDeletion ( string s , int n ) { Dictionary < char , int > countMap = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countMap . ContainsKey ( s [ i ] ) == true ) { countMap [ s [ i ] ] += 1 ; } else { countMap [ s [ i ] ] = 1 ; } } List < int > countMultiset = new List < int > ( ) ; foreach ( var values in countMap . Values ) { countMultiset . Add ( values ) ; } countMultiset . Sort ( ) ; int ans = 100000000 ; int index = 0 ; int m = countMultiset . Count ; foreach ( var j in countMultiset ) { ans = Math . Min ( ans , n - ( m - index ) * j ) ; index ++ ; } return ans ; } public static void Main ( String [ ] args ) { string S = " geeksforgeeks " ; int N = S . Length ; Console . WriteLine ( minimumDeletion ( S , N ) ) ; } }
Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | C # program of the above approach ; Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void sortArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int num = arr [ i ] ; string s = num . ToString ( ) ; char [ ] temp = s . ToCharArray ( ) ; Array . Sort ( temp ) ; int st = 0 ; int ed = temp . Length - 1 ; while ( st < ed ) { char ch = temp [ st ] ; temp [ st ] = temp [ ed ] ; temp [ ed ] = ch ; st ++ ; ed -- ; } string charsStr = new string ( temp ) ; sort ( s . begin ( ) , s . end ( ) , greater < char > ( ) ) ; arr [ i ] = Int32 . Parse ( charsStr ) ; int sr = ( int ) Math . Sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) arr [ i ] = a ; else arr [ i ] = b ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 29 , 43 , 28 , 12 } ; int N = arr . Length ; sortArr ( arr , N ) ; } }
Minimize cost required to make all array elements greater than or equal to zero | C # program for the above approach ; Function to find the minimum cost to make all array elements greater than or equal to 0 ; Sort the array in ascending order ; Stores the cost to make current array element >= 0 ; Stores the cost to make all array elements >= 0 ; Traverse the array and insert all the elements which are < 0 ; If current array element is negative ; Cost to make all array elements >= 0 ; Update curr if ans is minimum ; Print the minimum cost ; Driver Code ; Given array ; Size of the array ; Given value of X ; Function call to find minimum cost to make all array elements >= 0
using System ; class GFG { static void minCost ( int [ ] arr , int N , int X ) { Array . Sort ( arr ) ; int sum = 0 ; int cost = 0 ; int INT_MAX = Int32 . MaxValue ; int min_cost = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cost = Math . Abs ( arr [ i ] ) * X + ( sum - Math . Abs ( arr [ i ] ) * i ) ; sum += Math . Abs ( arr [ i ] ) ; min_cost = Math . Min ( min_cost , cost ) ; } } Console . Write ( min_cost ) ; } static public void Main ( ) { int [ ] arr = { - 1 , - 3 , - 2 , 4 , - 1 } ; int N = arr . Length ; int X = 2 ; minCost ( arr , N , X ) ; } }
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | C # program for the above approach ; Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Driver Code ; Given vector ; Given value of X & K
using System ; using System . Collections . Generic ; class GFG { static int ans = 0 ; static void countArraysUtil ( List < int > arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) { if ( arr [ i ] > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr [ 0 ] ; arr . RemoveAt ( 0 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . Insert ( 0 , temp ) ; temp = arr [ arr . Count - 1 ] ; arr . RemoveAt ( arr . Count - 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . Add ( temp ) ; arr [ 0 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ 0 ] -- ; arr [ arr . Count - 1 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ arr . Count - 1 ] -- ; } static void countArrays ( List < int > arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) xorVal = xorVal ^ arr [ i ] ; countArraysUtil ( arr , X , K , xorVal ) ; Console . Write ( ans ) ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 10 , 2 , 10 , 5 } ; int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; } }
Difference between maximum and minimum of a set of anagrams from an array | C # program for the above approach ; Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; If the end of Map is reached , then no anagrams are present ; Driver code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int hashFunction ( int N ) { int [ ] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 } ; int value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = N / 10 ; } return value ; } static void findDiff ( int [ ] arr , int n ) { Dictionary < int , List < int > > m = new Dictionary < int , List < int > > ( ) ; int h , min , max ; for ( int i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; if ( ! m . ContainsKey ( h ) ) { m [ h ] = new List < int > ( ) ; } m [ h ] . Add ( arr [ i ] ) ; } foreach ( KeyValuePair < int , List < int > > i in m ) { if ( i . Value . Count > 1 ) { min = Int32 . MaxValue ; max = Int32 . MinValue ; for ( int j = 0 ; j < i . Value . Count ; j ++ ) { if ( m [ i . Key ] [ j ] < min ) { min = m [ i . Key ] [ j ] ; } if ( m [ i . Key ] [ j ] > max ) { max = m [ i . Key ] [ j ] ; } } Console . Write ( max - min ) ; break ; } else if ( m [ i . Key ] . Equals ( m . Last ( ) . Value ) ) Console . Write ( - 1 ) ; } } static void Main ( ) { int [ ] arr = { 121 , 312 , 234 , 211 , 112 , 102 } ; int N = arr . Length ; findDiff ( arr , N ) ; } }
Maximum number of apples that can be eaten by a person | C # program of the above approach ; Function to find the maximum number of apples a person can eat such that the person eat at most one apple in a day . ; Store count of apples and number of days those apples are edible ; Stores indices of the array ; Stores count of days ; Stores maximum count of edible apples ; Traverse both the arrays ; If top element of the apple is not already expired ; Insert count of apples and their expiration date ; Remove outdated apples ; Insert all the apples produces by tree on current day ; Stores top element of pq ; Remove top element of pq ; If count of apples in curr is greater than 0 ; Insert count of apples and their expiration date ; Update total_apples ; Update index ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int cntMaxApples ( int [ ] apples , int [ ] days ) { List < Tuple < int , int > > pq = new List < Tuple < int , int > > ( ) ; int i = 0 ; int n = apples . Length ; int total_apples = 0 ; while ( i < n . Count > 0 ) { if ( i < n && apples [ i ] != 0 ) { pq . Add ( new Tuple < int , int > ( i + days [ i ] - 1 , apples [ i ] ) ) ; pq . Sort ( ) ; } while ( pq . Count > 0 && pq [ 0 ] . Item1 < i ) { pq . RemoveAt ( 0 ) ; } if ( pq . Count > 0 ) { Tuple < int , int > curr = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( curr . Item2 > 1 ) { pq . Add ( new Tuple < int , int > ( curr . Item1 , curr . Item2 - 1 ) ) ; pq . Sort ( ) ; } ++ total_apples ; } ++ i ; } return total_apples ; } static void Main ( ) { int [ ] apples = { 1 , 2 , 3 , 5 , 2 } ; int [ ] days = { 3 , 2 , 1 , 4 , 2 } ; Console . Write ( cntMaxApples ( apples , days ) ) ; } }
Maximum area rectangle by picking four sides from array | C # program for finding maximum area possible of a rectangle ; function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; Driver Code
using System ; using System . Collections ; class GFG { static int findArea ( int [ ] arr , int n ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int [ ] dimension = { 0 , 0 } ; for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . Length ; Console . Write ( findArea ( arr , n ) ) ; } }
Maximum of sum of length of rectangles and squares formed by given sticks |  ; Function to find the maximum of sum of lengths of rectangles and squares formed using given set of sticks ; Stores the count of frequencies of all the array elements ; Stores frequencies which are at least 2 ; Convert all frequencies to nearest smaller even values . ; Sum of elements up to index of multiples of 4 ; Print the sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findSumLength ( List < int > arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; foreach ( int i in arr ) { if ( freq . ContainsKey ( i ) ) freq [ i ] += 1 ; else freq [ i ] = 1 ; } Dictionary < int , int > freq_2 = new Dictionary < int , int > ( ) ; foreach ( KeyValuePair < int , int > entry in freq ) { if ( freq [ entry . Key ] >= 2 ) freq_2 [ entry . Key ] = freq [ entry . Key ] ; } List < int > arr1 = new List < int > ( ) ; foreach ( KeyValuePair < int , int > entry in freq_2 ) arr1 . Add ( entry . Key * ( freq_2 [ entry . Key ] / 2 ) * 2 ) ; arr1 . Sort ( ) ; int summ = 0 ; foreach ( int i in arr1 ) { summ += i ; } Console . WriteLine ( summ ) ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 } ; int n = arr . Count ; findSumLength ( arr , n ) ; } }
Maximum area rectangle by picking four sides from array | c # program for finding maximum area possible of a rectangle ; function for finding max area ; traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; return the product of dimensions ; driver function
using System ; using System . Collections . Generic ; public class GFG { public static int findArea ( int [ ] arr , int n ) { ISet < int > s = new HashSet < int > ( ) ; int first = 0 , second = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . Contains ( arr [ i ] ) ) { s . Add ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } return ( first * second ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . Length ; Console . WriteLine ( findArea ( arr , n ) ) ; } }
Print root to leaf paths without using recursion | C # program to Print root to leaf path WITHOUT using recursion ; A binary tree node ; Function to print root to leaf path for a leaf using parent nodes stored in map ; start from leaf node and keep on pushing nodes into stack till root node is reached ; Start popping nodes from stack and print them ; An iterative function to do preorder traversal of binary tree and print root to leaf path without using recursion ; Corner Case ; Create an empty stack and push root to it ; Create a map to store parent pointers of binary tree nodes ; parent of root is NULL ; Pop all items one by one . Do following for every popped item a ) push its right child and set its parent pointer b ) push its left child and set its parent pointer Note that right child is pushed first so that left is processed first ; Pop the top item from stack ; If leaf node encountered , print Top To Bottom path ; Push right & left children of the popped node to stack . Also set their parent pointer in the map ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
using System ; using System . Collections . Generic ; public class PrintPath { public class Node { public int data ; public Node left , right ; public Node ( int data ) { left = right = null ; this . data = data ; } } ; public static void printTopToBottomPath ( Node curr , Dictionary < Node , Node > parent ) { Stack < Node > stk = new Stack < Node > ( ) ; while ( curr != null ) { stk . Push ( curr ) ; curr = parent [ curr ] ; } while ( stk . Count != 0 ) { curr = stk . Pop ( ) ; Console . Write ( curr . data + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void printRootToLeaf ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . Push ( root ) ; Dictionary < Node , Node > parent = new Dictionary < Node , Node > ( ) ; parent . Add ( root , null ) ; while ( nodeStack . Count != 0 ) { Node current = nodeStack . Pop ( ) ; if ( current . left == null && current . right == null ) printTopToBottomPath ( current , parent ) ; if ( current . right != null ) { parent . Add ( current . right , current ) ; nodeStack . Push ( current . right ) ; } if ( current . left != null ) { parent . Add ( current . left , current ) ; nodeStack . Push ( current . left ) ; } } } public static void Main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 2 ) ; printRootToLeaf ( root ) ; } }
Game of replacing array elements | C # program for Game of Replacement ; Function return which player win the game ; Create hash that will stores all distinct element ; Traverse an array element ; Driver code
using System ; using System . Collections . Generic ; public class GameOfReplacingArrayElements { public static int playGame ( int [ ] arr ) { HashSet < int > set = new HashSet < int > ( ) ; foreach ( int i in arr ) set . Add ( i ) ; return ( set . Count % 2 == 0 ) ? 1 : 2 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 2 , 2 } ; Console . Write ( " Player ▁ " + playGame ( arr ) + " ▁ wins " ) ; } }
Length of longest strict bitonic subsequence | C # implementation to find length of longest strict bitonic subsequence ; function to find length of longest strict bitonic subsequence ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last / first element of that subsequence ; arrays to store the length of increasing and decreasing subsequences which end at them or start from them ; to store the length of longest strict bitonic subsequence ; traverse the array elements from left to right ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' inc ' ; update arr [ i ] subsequence length in ' inc ' and in len_inc [ ] ; traverse the array elements from right to left ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' dcr ' ; update arr [ i ] subsequence length in ' dcr ' and in len_dcr [ ] ; calculating the length of all the strict bitonic subsequence ; required longest length strict bitonic subsequence ; Driver code
using System ; using System . Collections . Generic ; class GfG { static int longLenStrictBitonicSub ( int [ ] arr , int n ) { Dictionary < int , int > inc = new Dictionary < int , int > ( ) ; Dictionary < int , int > dcr = new Dictionary < int , int > ( ) ; int [ ] len_inc = new int [ n ] ; int [ ] len_dcr = new int [ n ] ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( inc . ContainsKey ( arr [ i ] - 1 ) ) len = inc [ arr [ i ] - 1 ] ; len_inc [ i ] = len + 1 ; if ( inc . ContainsKey ( arr [ i ] ) ) { inc . Remove ( arr [ i ] ) ; inc . Add ( arr [ i ] , len_inc [ i ] ) ; } else inc . Add ( arr [ i ] , len_inc [ i ] ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { int len = 0 ; if ( dcr . ContainsKey ( arr [ i ] - 1 ) ) len = dcr [ arr [ i ] - 1 ] ; len_dcr [ i ] = len + 1 ; if ( dcr . ContainsKey ( arr [ i ] ) ) { dcr . Remove ( arr [ i ] ) ; dcr . Add ( arr [ i ] , len_dcr [ i ] ) ; } else dcr . Add ( arr [ i ] , len_dcr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Longest ▁ length ▁ strict ▁ " + " bitonic ▁ subsequence ▁ = ▁ " + longLenStrictBitonicSub ( arr , n ) ) ; } }
Selection Sort VS Bubble Sort |  ; Driver code
using System ; public class GFG { static void Selection_Sort ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { int min_index = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( arr [ j ] < arr [ min_index ] ) min_index = j ; } int temp = arr [ i ] ; arr [ i ] = arr [ min_index ] ; arr [ min_index ] = temp ; } } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 2 , 0 , 1 , 4 , 3 } ; Selection_Sort ( arr , n ) ; Console . Write ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Selection ▁ Sort ▁ is ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Last seen array element ( last appearance is earliest ) | C # program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int lastSeenElement ( int [ ] a , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . ContainsKey ( a [ i ] ) ) { hash [ a [ i ] ] = i ; } else { hash . Add ( a [ i ] , i ) ; } } int res_ind = int . MaxValue , res = 0 ; foreach ( KeyValuePair < int , int > x in hash ) { if ( x . Value < res_ind ) { res_ind = x . Value ; res = x . Key ; } } return res ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 1 , 2 , 2 , 4 , 1 } ; int n = a . Length ; Console . WriteLine ( lastSeenElement ( a , n ) ) ; } }