We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Implementation
  4. Between Two Sets
  5. Discussions

Between Two Sets

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • heiks 1 year ago+ 0 comments

    Hello!

    I noticed that Your answer goes through way too many iterations and doesn't take large enough steps, there can be no correct answer between a.Max() and 2a.Max(), therefore skipping those is a good idea.

    Looping through by generating an amount of integers also isnt really necessary, just start from lowest possible correct answer ( a.Max() ) and make sure youre not over the largest possible ( b.Min() ) correct answer.

    Works like a charm :).

    Here is the resulting code with comments.

    public static int getTotalX(List<int> a, List<int> b)
    {
    /*Starting from 0 found, storing largest value in A and smallest value in b. Setting starting point to largest value in A.*/
        int foundCount = 0, maxA = a.Max(), minB = b.Min(), current = maxA;
        while (current <= minB)
        {
    /*If the current value is divisible by all members of both arrays, then it's the one we want.*/
            if (a.All(e => current % e == 0 || e % current == 0) && b.All(e => current % e == 0 || e % current == 0))
                foundCount++;
    /*Iterate the value by largest member of divisors, no reason to take smaller steps.*/
            current+= maxA;
            };
        return foundCount;
    }
    

    " C# " for everyone CTRL+F-ing.

    //Stored a.Max() and b.Min() as dsgarg71 suggested below :).

    3|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature