Count pairs from two linked lists whose sum is equal to a given value | C # implementation to count pairs from both linked lists whose sum is equal to a given value Note : here we use using System . Collections . Generic for linked list implementation ; method to count all pairs from both the linked lists whose sum is equal to a given value ; traverse the 1 st linked list ; for each node of 1 st list traverse the 2 nd list ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( List < int > head1 , List < int > head2 , int x ) { int count = 0 ; foreach ( int itr1 in head1 ) { int t = itr1 ; foreach ( int itr2 in head2 ) { if ( ( t + itr2 ) == x ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 3 , 1 , 5 , 7 } ; int [ ] arr2 = { 8 , 2 , 5 , 3 } ; List < int > head1 = new List < int > ( arr1 ) ; List < int > head2 = new List < int > ( arr2 ) ; int x = 10 ; Console . WriteLine ( " Count ▁ = ▁ " + countPairs ( head1 , head2 , x ) ) ; } }