Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Clique problem solution

YASH PAL, 31 July 202424 January 2026

In this HackerRank Clique problem solution, A clique in a graph is a 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 n nodes and m edges.

HackerRank Clique problem solution

HackerRank Clique problem solution in Python.

#!/bin/python3

import os
import sys
import math

#
# Complete the clique function below.
#
def clique(n, m):
    low = 1
    high = n
    while low + 1 < high:
        mid = (low + high) // 2
        tedges = int(turan(n, mid))
        if tedges < m:
            low = mid
        else:
            high = mid
    return high

def turan(n, k):
    return (n ** 2 - (n % k) * ((math.ceil(n / k)) ** 2) - (k - (n % k)) * ((n // k) ** 2)) // 2

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        nm = input().split()

        n = int(nm[0])

        m = int(nm[1])

        result = clique(n, m)
        fptr.write(str(result) + 'n')
        print(result)

    fptr.close()

Clique problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();

for (int i=0; i<t; i++) {
int n = scan.nextInt();
int m = scan.nextInt();

int k = 1;
while (k<n && turan(n,k+1)<m)
k++;
System.out.println(k);
}
}

// returns the maximum number of edges on a graph of size n without producing k-clique
static int turan(int n, int k) {
int b = n%(k-1); // # of larger independent sets
int a = k-1-b; // # of smaller independent sets
int j = n/(k-1); // size of smaller sets
return j*j*a*(a-1)/2+j*(j+1)*a*b+(j+1)*(j+1)*b*(b-1)/2;
}
}

Problem solution in C++.

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cstring>
#include <stack>
#include <bitset>

using namespace std;

#define NMAX 2147483647
#define LMAX 9223372036854775807LL
#define pb push_back
#define pob pop_back
#define mp make_pair
#define st first
#define nd second
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i,a,b) for(int i=(a);i>(b);--i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)

int solve1(int n,int k)
{
  int g1 = n%k ;
  int g2 = k - g1 ;
  int sz1 = n/k + 1 ;
  int sz2 = n/k ;
  int ret = g1*sz1*g2*sz2 + g1*(g1-1)*sz1*sz1/2 + g2*(g2-1)*sz2*sz2/2 ;
  return ret ;
}

int solve(int n,int e)
{
  int k,low = 1,high = n + 1 ;
  while(low + 1 < high)
  {
    int mid = low + (high - low)/2 ;
    k = solve1(n,mid) ;
    if(k < e) low = mid ;
    else high = mid ;
  }
  return high ;
}

int main()
{
  //freopen("input.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  int TS;
  scanf("%d",&TS);
  FORE(ts,1,TS)
  {
    int N, M;
    scanf("%d%d",&N,&M);
    printf("%dn",solve(N,M));
  }
  return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int formula(unsigned int vertices, unsigned int k) {
  int squared = vertices * vertices;
  int clique_mod = vertices % k;
  int upper = ceil((double)vertices/(double)k);
  int lower = floor((double)vertices/(double)k);
  
  return (squared - (clique_mod * upper * upper) - (k - clique_mod)* lower * lower) / 2;
}

int main() {
  unsigned int cases, vertices, edges;
  int k;

  scanf("%u", &cases);
  
  while(cases--) {
    scanf("%u %u", &vertices, &edges);

    int min = 1;
    int max = vertices;
    k = (min + max)/2;
    
    while(k <= vertices) {
      int f = formula(vertices, k);
      int f_plus_1 = formula(vertices, k + 1);

      if (edges > f) {
        if (edges <= f_plus_1) {
         printf("%dn", k+1);
         break;
        }
        
        min = k + 1;        
      } else {
        max = k - 1;
      }
      
      k = (min + max)/2;
    }
  }
  
  return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes