codeparrot/xlcost-text-to-code huggingface dataset:
Find any one of the multiple repeating elements in read only array | C # program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver code ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5
using System ; using System . Collections . Generic ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . Sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . Add ( arr [ i ] , 1 ) ; if ( m [ arr [ i ] ] == 1 ) return arr [ i ] ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; Console . WriteLine ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }
Group multiple occurrence of array elements ordered by first occurrence | A simple C # program to group multiple occurrences of individual array elements ; A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , print it and all subsequent occurrences ; Driver code
using System ; class GFG { static void groupElements ( int [ ] arr , int n ) { bool [ ] visited = new bool [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; visited [ j ] = true ; } } } } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 } ; int n = arr . Length ; groupElements ( arr , n ) ; } }
Group multiple occurrence of array elements ordered by first occurrence | C # program to group multiple occurrences of individual array elements ; A hashing based method to group all occurrences of individual elements ; Creates an empty hashmap ; Traverse the array elements , and store count for every element in HashMap ; Check if element is already in HashMap ; Increment count of element element in HashMap ; Traverse array again ; Check if this is first occurrence ; If yes , then print the element ' count ' times ; And remove the element from HashMap . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void orderedGroup ( int [ ] arr ) { Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int prevCount = 0 ; if ( hM . ContainsKey ( arr [ i ] ) ) prevCount = hM [ arr [ i ] ] ; if ( hM . ContainsKey ( arr [ i ] ) ) hM [ arr [ i ] ] = prevCount + 1 ; else hM . Add ( arr [ i ] , prevCount + 1 ) ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { int count = 0 ; if ( hM . ContainsKey ( arr [ i ] ) ) count = hM [ arr [ i ] ] ; if ( count != 0 ) { for ( int j = 0 ; j < count ; j ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; hM . Remove ( arr [ i ] ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 3 , 10 , 10 , 4 , 1 , 3 } ; orderedGroup ( arr ) ; } }
How to check if two given sets are disjoint ? | C # program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver Code
using System ; class GFG { public virtual bool aredisjoint ( int [ ] set1 , int [ ] set2 ) { for ( int i = 0 ; i < set1 . Length ; i ++ ) { for ( int j = 0 ; j < set2 . Length ; j ++ ) { if ( set1 [ i ] == set2 [ j ] ) { return false ; } } } return true ; } public static void Main ( string [ ] args ) { GFG dis = new GFG ( ) ; int [ ] set1 = new int [ ] { 12 , 34 , 11 , 9 , 3 } ; int [ ] set2 = new int [ ] { 7 , 2 , 1 , 5 } ; bool result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
How to check if two given sets are disjoint ? | C # program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver code
using System ; public class disjoint1 { Boolean aredisjoint ( int [ ] set1 , int [ ] set2 ) { int i = 0 , j = 0 ; Array . Sort ( set1 ) ; Array . Sort ( set2 ) ; while ( i < set1 . Length && j < set2 . Length ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set1 [ i ] > set2 [ j ] ) j ++ ; else return false ; } return true ; } public static void Main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int [ ] set1 = { 12 , 34 , 11 , 9 , 3 } ; int [ ] set2 = { 7 , 2 , 1 , 5 } ; Boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
How to check if two given sets are disjoint ? | C # program to check if two sets are distinct or not ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the first set and store its elements in hash ; Traverse the second set and check if any element of it is already in hash or not . ; Driver method to test above method
using System ; using System . Collections . Generic ; public class GFG { public static bool areDisjoint ( int [ ] set1 , int [ ] set2 ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < set1 . Length ; i ++ ) { set . Add ( set1 [ i ] ) ; } for ( int i = 0 ; i < set2 . Length ; i ++ ) { if ( set . Contains ( set2 [ i ] ) ) { return false ; } } return true ; } public static void Main ( string [ ] args ) { int [ ] set1 = new int [ ] { 10 , 5 , 3 , 4 , 6 } ; int [ ] set2 = new int [ ] { 8 , 7 , 9 , 3 } ; if ( areDisjoint ( set1 , set2 ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Non | C # program to find Non - overlapping sum ; function for calculating Non - overlapping sum of two array ; Insert elements of both arrays ; calculate non - overlapped sum ; Driver code ; size of array ; function call
using System ; using System . Collections . Generic ; class GFG { static int findSum ( int [ ] A , int [ ] B , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . ContainsKey ( A [ i ] ) ) { var v = hash [ A [ i ] ] ; hash . Remove ( A [ i ] ) ; hash . Add ( A [ i ] , 1 + v ) ; } else hash . Add ( A [ i ] , 1 ) ; if ( hash . ContainsKey ( B [ i ] ) ) { var v = hash [ B [ i ] ] ; hash . Remove ( B [ i ] ) ; hash . Add ( B [ i ] , 1 + v ) ; } else hash . Add ( B [ i ] , 1 ) ; } int sum = 0 ; foreach ( KeyValuePair < int , int > entry in hash ) { if ( ( entry . Value ) == 1 ) sum += entry . Key ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] A = { 5 , 4 , 9 , 2 , 3 } ; int [ ] B = { 2 , 8 , 7 , 6 , 3 } ; int n = A . Length ; Console . WriteLine ( findSum ( A , B , n ) ) ; } }
Find elements which are present in first array and not in second | C # simple program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver code
using System ; class GFG { static void findMissing ( int [ ] a , int [ ] b , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 1 , 2 , 6 , 3 , 4 , 5 } ; int [ ] b = { 2 , 4 , 3 , 1 , 0 } ; int n = a . Length ; int m = b . Length ; findMissing ( a , b , n , m ) ; } }
Find elements which are present in first array and not in second | C # efficient program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Store all elements of second array in a hash table ; Print all elements of first array that are not present in hash table ; Driver code
using System ; using System . Collections . Generic ; class GfG { static void findMissing ( int [ ] a , int [ ] b , int n , int m ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < m ; i ++ ) s . Add ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! s . Contains ( a [ i ] ) ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 6 , 3 , 4 , 5 } ; int [ ] b = { 2 , 4 , 3 , 1 , 0 } ; int n = a . Length ; int m = b . Length ; findMissing ( a , b , n , m ) ; } }
Check if two arrays are equal or not | C # program to find given two array are equal or not ; Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver code
using System ; class GFG { public static bool areEqual ( int [ ] arr1 , int [ ] arr2 ) { int n = arr1 . Length ; int m = arr2 . Length ; if ( n != m ) return false ; Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } public static void Main ( ) { int [ ] arr1 = { 3 , 5 , 2 , 5 , 2 } ; int [ ] arr2 = { 2 , 3 , 5 , 5 , 2 } ; if ( areEqual ( arr1 , arr2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Pair with given sum and maximum shortest distance from end | C # code to find maximum shortest distance from endpoints ; Store counts of all elements . ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void makePermutation ( int [ ] a , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . ContainsKey ( a [ i ] ) ) { count [ a [ i ] ] = count [ a [ i ] ] + 1 ; } else { count . Add ( a [ i ] , 1 ) ; } } } static int find_maximum ( int [ ] a , int n , int k ) { Dictionary < int , int > b = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = Math . Min ( 1 + i , n - i ) ; if ( ! b . ContainsKey ( x ) ) b . Add ( x , d ) ; else { b [ x ] = Math . Min ( d , b [ x ] ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . ContainsKey ( k - x ) ) ans = Math . Min ( Math . Max ( b [ x ] , b [ k - x ] ) , ans ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = a . Length ; Console . WriteLine ( find_maximum ( a , n , K ) ) ; } }
Pair with given product | Set 1 ( Find if any pair exists ) | C # program to find if there is a pair with given product . ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver Code
using System ; class GFG { static bool isProduct ( int [ ] arr , int n , int x ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } static void Main ( ) { int [ ] arr = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . Length ; if ( isProduct ( arr , n , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; x = 190 ; if ( isProduct ( arr , n , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Pair with given product | Set 1 ( Find if any pair exists ) | C # program if there exists a pair for given product ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element into it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static bool isProduct ( int [ ] arr , int n , int x ) { HashSet < int > hset = new HashSet < int > ( ) ; if ( n < 2 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) { return true ; } else { continue ; } } if ( x % arr [ i ] == 0 ) { if ( hset . Contains ( x / arr [ i ] ) ) { return true ; } hset . Add ( arr [ i ] ) ; } } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . Length ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } x = 190 ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Check whether a given binary tree is perfect or not | C # program to check whether a given Binary Tree is Perfect or not ; Tree node structure ; Returns depth of leftmost leaf . ; This function tests if a binary tree is perfect or not . It basically checks for two things : 1 ) All leaves are at same level 2 ) All internal nodes have two children ; An empty tree is perfect ; If leaf node , then its depth must be same as depth of all other leaves . ; If internal node and one child is empty ; Left and right subtrees must be perfect . ; Wrapper over isPerfectRec ( ) ; Helper function that allocates a new node with the given key and NULL left and right pointer . ; Driver code
using System ; class GfG { class Node { public int key ; public Node left , right ; } static int findADepth ( Node node ) { int d = 0 ; while ( node != null ) { d ++ ; node = node . left ; } return d ; } static bool isPerfectRec ( Node root , int d , int level ) { if ( root == null ) return true ; if ( root . left == null && root . right == null ) return ( d == level + 1 ) ; if ( root . left == null root . right == null ) return false ; return isPerfectRec ( root . left , d , level + 1 ) && isPerfectRec ( root . right , d , level + 1 ) ; } static bool isPerfect ( Node root ) { int d = findADepth ( root ) ; return isPerfectRec ( root , d , 0 ) ; } static Node newNode ( int k ) { Node node = new Node ( ) ; node . key = k ; node . right = null ; node . left = null ; return node ; } public static void Main ( ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . left = newNode ( 60 ) ; root . right . right = newNode ( 70 ) ; if ( isPerfect ( root ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find pair with greatest product in array | C # program to find a pair with product in given array . ; Function to find greatest number that us ; Driver code
using System ; class GFG { static int findGreatest ( int [ ] arr , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . Max ( result , arr [ i ] ) ; return result ; } static public void Main ( ) { int [ ] arr = { 30 , 10 , 9 , 3 , 35 } ; int n = arr . Length ; Console . WriteLine ( findGreatest ( arr , n ) ) ; } }
Find pair with greatest product in array | C # program to find the largest product number ; Function to find greatest number ; Store occurrences of all elements in hash array ; m [ arr [ i ] ] ++ ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findGreatest ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var a = m [ arr [ i ] ] + 1 ; m . Add ( arr [ i ] , a ) ; } else { m . Add ( arr [ i ] , arr [ i ] ) ; } } Array . Sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m [ result ] == null m [ result ] > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m [ result ] > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . Length ; Console . WriteLine ( findGreatest ( arr , n ) ) ; } }
Remove minimum number of elements such that no common element exist in both array | C # Code to Remove minimum number of elements such that no common element exist in both array ; To find no elements to remove so no common element exist ; To store count of array element ; Count elements of a ; Count elements of b ; Traverse through all common element , and pick minimum occurrence from two arrays ; To return count of minimum elements ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int minRemove ( int [ ] a , int [ ] b , int n , int m ) { Dictionary < int , int > countA = new Dictionary < int , int > ( ) ; Dictionary < int , int > countB = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countA . ContainsKey ( a [ i ] ) ) { var v = countA [ a [ i ] ] ; countA . Remove ( countA [ a [ i ] ] ) ; countA . Add ( a [ i ] , v + 1 ) ; } else countA . Add ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( countB . ContainsKey ( b [ i ] ) ) { var v = countB [ b [ i ] ] ; countB . Remove ( countB [ b [ i ] ] ) ; countB . Add ( b [ i ] , v + 1 ) ; } else countB . Add ( b [ i ] , 1 ) ; } int res = 0 ; foreach ( int x in countA . Keys ) if ( countB . ContainsKey ( x ) ) res += Math . Min ( countB [ x ] , countA [ x ] ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 4 } ; int [ ] b = { 2 , 3 , 4 , 5 , 8 } ; int n = a . Length ; int m = b . Length ; Console . WriteLine ( minRemove ( a , b , n , m ) ) ; } }
Count items common to both the lists but with different prices | C # implementation to count items common to both the lists but with different prices ; details of an item ; function to count items common to both the lists but with different prices ; for each item of ' list1' check if it is in ' list2' but with a different price ; required count of items ; Driver code
using System ; class GFG { class item { public String name ; public int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item [ ] list1 , int m , item [ ] list2 , int n ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( list1 [ i ] . name . CompareTo ( list2 [ j ] . name ) == 0 ) && ( list1 [ i ] . price != list2 [ j ] . price ) ) count ++ ; return count ; } public static void Main ( String [ ] args ) { item [ ] list1 = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item [ ] list2 = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . Length ; int n = list2 . Length ; Console . Write ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }
Count items common to both the lists but with different prices | C # implementation to count items common to both the lists but with different prices ; Details of an item ; Function to count items common to both the lists but with different prices ; ' um ' implemented as hash table that contains item name as the key and price as the value associated with the key ; Insert elements of ' list1' in ' um ' ; For each element of ' list2' check if it is present in ' um ' with a different price value ; Required count of items ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class item { public String name ; public int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item [ ] list1 , int m , item [ ] list2 , int n ) { Dictionary < String , int > um = new Dictionary < String , int > ( ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) um . Add ( list1 [ i ] . name , list1 [ i ] . price ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( um . ContainsKey ( list2 [ i ] . name ) ) && ( um [ list2 [ i ] . name ] != list2 [ i ] . price ) ) count ++ ; return count ; } public static void Main ( String [ ] args ) { item [ ] list1 = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item [ ] list2 = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . Length ; int n = list2 . Length ; Console . Write ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }
Minimum Index Sum for Common Elements of Two Lists |  ; Function to print common Strings with minimum index sum ; resultant list ; iterating over sum in ascending order ; iterating over one list and check index ( Corresponding to given sum ) in other list ; put common Strings in resultant list ; if common String found then break as we are considering index sums in increasing order . ; print the resultant list ; Driver code ; Creating list1 ; Creating list2
using System ; using System . Collections . Generic ; class GFG { static void find ( List < String > list1 , List < String > list2 ) { List < String > res = new List < String > ( ) ; int max_possible_sum = list1 . Count + list2 . Count - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . Count && ( sum - i ) < list2 . Count && list1 [ i ] == list2 [ sum - i ] ) res . Add ( list1 [ i ] ) ; if ( res . Count > 0 ) break ; } for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > list1 = new List < String > ( ) ; list1 . Add ( " GeeksforGeeks " ) ; list1 . Add ( " Udemy " ) ; list1 . Add ( " Coursera " ) ; list1 . Add ( " edX " ) ; List < String > list2 = new List < String > ( ) ; list2 . Add ( " Codecademy " ) ; list2 . Add ( " Khan ▁ Academy " ) ; list2 . Add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }