BeamTrailA Two Circles HackerRank Problem


Circles are respresented by vector of string

input = ["3 0 10 5 0 3", "0 1 4 0 1 5"]        

  • input[0] represents 2 circles. and input[1] represent another 2 circles.
  • How input[i] represents a circle?

"3 0 10 5 0 3".
Circle-A center=(3,0). radius=10
Circle-A center=(5,0). radius=3        
Similarly
"0 1 4 0 1 5".
Circle-A center=(0,1). radius=4
Circle-A center=(0,1). radius=5        

Now we need to find whether circle A and circle B are

"Touching", "Disjoint-Outside", "Concentric", "Intersecting", "Disjoint-Inside"        

And we need to return that as output.

So output of above input string would be ["disjoint-Inside", "Concentric"]        

CPP

vector<string> circles(vector<string> circlePairs) {
    vector<string> results;

    for (const auto& pair : circlePairs) {
        stringstream ss(pair);
        double x1, y1, r1, x2, y2, r2;
        ss >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;

        // Distance between centers
        double sq = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        double dist = sqrt(sq);

        // Sum of 2 radii
        double rSum = r1 + r2;

        if ((x2==x1 && y2==y1) && r1 != r2) {
            // Center of 2 circles should be same and radius should be different
            results.push_back("Concentric");
        } else if (dist == rSum) {
            // sq(Distance bet centers) must be equal to rSum^2
            results.push_back("Touching");
        } else if (dist > rSum) {
            results.push_back("Disjoint-Outside");
        } else if (dist < r1 || dist < r2) {
            results.push_back("Disjoint-Inside");
        } else if (r1 != r2 && (x2!=x1 || y2!=y1)) {
            results.push_back("Intersecting");
        }
    }

    return results;
}
int main(){
    vector<string> in = {"3 0 1 5 0 2", "2 0 1 4 0 1", "2 0 1 2 0 2", "2 0 1 5 0 1"};
    vector<string> out = circles(in);
    for (auto&i:out)
        cout << i << " ";
}        


要查看或添加评论,请登录

Amit K.的更多文章

  • Location Sharing App System Design (Bump)

    Location Sharing App System Design (Bump)

    What is Bump Bump is location sharing Mobile App. Install bump on 2 phones(add as friends).

  • Load Balancers & Caches

    Load Balancers & Caches

    What is Load Balancer? Load Balancer evenly distributes incoming traffic/load among webservers/workers that are defined…

  • REST API / Representation State Transfer

    REST API / Representation State Transfer

    Restful Web Server/Application? Web application that implements HTTP CRUD methods in Restful way. Eg: Twitter, facebook…

  • Inter Thread Communication in Rust using Channels

    Inter Thread Communication in Rust using Channels

    What is Channel? Sender and Receiver are connected via Channel. They can send/recv data via channel.

  • Slices in Rust

    Slices in Rust

    What is Slice Slice is somepart of larger data, it always borrow data(Hence RO) from the sliced type. It gives you a…

  • Traits in Rust

    Traits in Rust

    What is Triat in Rust Interface/class in Rust(declared with keyword trait) having Virtual Functions(not pure Virtual)…

  • Facebook News Feed High Level System Design

    Facebook News Feed High Level System Design

    HLD Json Web Token / oauth_token / auth_token based authentication to Facebook GET Newsfeed What we will do? We will…

  • Minimum Path in Triangle. Dynamic Programing, CPP, Rust

    Minimum Path in Triangle. Dynamic Programing, CPP, Rust

    120. Triangle Given a array, return the minimum path sum from top to bottom.

  • The Mindset of successful People

    The Mindset of successful People

    12 key points How the Top 1% Think 1. They take 100% responsibility for their lives.

  • Modules in Rust

    Modules in Rust

    What are modules Module is a block to manage the code in Rust. Module can have(functions, constants, enums, triats…

社区洞察

其他会员也浏览了