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.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Graph Theory
  4. Clique

Clique

Problem
Submissions
Leaderboard
Discussions
Editorial

A clique in a graph is set of nodes such that there is an edge between any two distinct nodes in the set. Finding the largest clique in a graph is a computationally difficult problem. Currently no polynomial time algorithm is known for solving this. However, you wonder what is the minimum size of the largest clique in any graph with nodes and edges.

For example, consider a graph with nodes and edges. The graph below shows nodes with edges and no cliques. It is evident that the addition of any edge must create two cliques with members each.

image

Input Format

The first line contains an integer , the number of test cases.

Each of the next lines contains two space-separated integers and .

Constraints

Output Format

For each test case, print the minimum size of the largest clique that must be formed given and .

Sample Input

3  
3 2  
4 6  
5 7

Sample Output

2  
4  
3

Explanation

For the first case, we have two cliques with two nodes each:

image

For the second test case, the only valid graph having nodes and edges is one where each pair of nodes is connected. So the size of the largest clique cannot be smaller than .

image

For the third test case, it is easy to verify that any graph with nodes and . The solid lines in the graph below indicate the maximum edges that can be added without forming a clique larger than . The dashed lines could connect any two nodes not connected by solid lines producing a clique of size .

image
Hints Turan's theorem gives us an upper bound on the number of edges a graph can have if we wish that it should not have a clique of size . Though the bound is not exact, it is easy to extend the statement of the theorem to get an exact bound in terms of and . Once this is done, we can binary search for the largest such that . See: Turan's Theorem

Author

HackerRank

Difficulty

Medium

Max Score

60

Submitted By

6775

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in