Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are 2 lists as List<int> a which has 3 scores such as {5,6,10} and the other list as List<int> b = {3,6,30}. Comparison should be done to check as follows,
0th index of list a > 0th index of list b = return 1 for a
or 0th index of list a = 0th index of list b = retunr 0 value since it is equal
or 0th index of list a < 0th index of list b = return 1 for b

the comparison should be done to each and every index as (a0,b0),(a1,b1) and (a2,b2).
and should return the total of 1s to a separate array.

sample output for the above example: {1,1}(here 0th index is for the total of list a and 1st index is for the total of list b.

What I have tried:

<pre>class Solution {

    // Complete the compareTriplets function below.
    static List<int> compareTriplets(List<int> a, List<int> b) {
           
           
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        List<int> a = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(aTemp => Convert.ToInt32(aTemp)).ToList();

        List<int> b = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(bTemp => Convert.ToInt32(bTemp)).ToList();

        List<int> result = compareTriplets(a, b);

        textWriter.WriteLine(String.Join(" ", result));

        textWriter.Flush();
        textWriter.Close();
    }
}
Posted
Updated 21-Oct-19 1:07am

Solution

class Solution {

    
    static void Main(string[] args) {
    
        int[] a = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray();
        int[] b = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray();
        int A = 0;
        int B = 0;
        for (int i = 0; i < a.Length; i++)
            if (a[i] > b[i]) A++;
            else if (a[i] < b[i]) B++;
        Console.WriteLine(string.Format("{0} {1}", A, B));
        
    }
}
 
Share this answer
 
Here is an example that gets to close to what you want: linq - Generic list comparison in C# - Stack Overflow[^]
 
Share this answer
 
Comments
Member 13958707 21-Oct-19 5:59am    
static List<int> compareTriplets(List<int> a, List<int> b) {


}
The comparison should be done inside this method. In the main method the joined list is returned.
Start with two variables to count the number of 1 values. Iterate the two lists using an index and compare the values. If the a-value is greater add 1 to the a-total, if the b-value is greater add 1 to the b-total. Repeat for the complete list(s).
 
Share this answer
 
Comments
Member 13958707 21-Oct-19 7:07am    
Since the length of the 2 arrays is same, iterated the array A. This works. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900