Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Value not sticking in return value for a list (C#)

There is probably a simple answer but while practicing some questions on hacker rank the "aScore" isn’t saying at a certain value, the first value in a[i] is greater than b[i]. I’ve been trying to debug the code but I can’t print what I want to the console on hacker rank, it’s forced to come out to the answer’s output somehow.

public static List<int> compareTriplets(List<int> a, List<int> b)
{
    int aScore=0;
    for(int i=0; i ==(a.Count-1);i++)
    if (a[i]>b[i]){
        aScore++;
    }
    return new List<int>(){aScore,1} ;   
    }

}

Code Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

for(int i=0; i ==(a.Count-1);i++) should be for(int i=0; i < a.Count; i++)

The for loop executes and iterates as long as the condition (second "part") resolves to true.

In your original code, it does not: 0 does not equal a.Count-1, so the loop body is not executed even once.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading