P, NP, CoNP, NP hard and NP complete | Complexity Classes
Last Updated :
22 Feb, 2025
In computer science, problems are divided into classes known as Complexity Classes. In complexity theory, a Complexity Class is a set of problems with related complexity. With the help of complexity theory, we try to cover the following.
- Problems that cannot be solved by computers.
- Problems that can be efficiently solved (solved in Polynomial time) by computers.
- Problems for which no efficient solution (only exponential time algorithms) exist.
The common resources required by a solution are are time and space, meaning how much time the algorithm takes to solve a problem and the corresponding memory usage.
- The time complexity of an algorithm is used to describe the number of steps required to solve a problem, but it can also be used to describe how long it takes to verify the answer.
- The space complexity of an algorithm describes how much memory is required for the algorithm to operate.
- An algorithm having time complexity of the form O(nk) for input n and constant k is called polynomial time solution. These solutions scale well. On the other hand, time complexity of the form O(kn) is exponential time.
Complexity classes are useful in organizing similar types of problems.

Types of Complexity Classes
This article discusses the following complexity classes:
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision problems(problems with a "yes" or "no" answer) that can be solved by a deterministic machine (our computers) in polynomial time.
Features:
- The solution to P problems is easy to find.
- P is often a class of computational problems that are solvable and tractable. Tractable means that the problems can be solved in theory as well as in practice. But the problems that can be solved in theory but not in practice are known as intractable.
Most of the coding problems that we solve fall in this category like the below.
- Calculating the greatest common divisor.
- Finding a maximum matching.
- Merge Sort
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is the collection of decision problems that can be solved by a non-deterministic machine (note that our computers are deterministic) in polynomial time.
Features:
- The solutions of the NP class might be hard to find since they are being solved by a non-deterministic machine but the solutions are easy to verify.
- Problems of NP can be verified by a deterministic machine in polynomial time.
Example:
Let us consider an example to better understand the NP class. Suppose there is a company having a total of 1000 employees having unique employee IDs. Assume that there are 200 rooms available for them. A selection of 200 employees must be paired together, but the CEO of the company has the data of some employees who can't work in the same room due to personal reasons.
This is an example of an NP problem. Since it is easy to check if the given choice of 200 employees proposed by a coworker is satisfactory or not i.e. no pair taken from the coworker list appears on the list given by the CEO. But generating such a list from scratch seems to be so hard as to be completely impractical.
It indicates that if someone can provide us with the solution to the problem, we can find the correct and incorrect pair in polynomial time. Thus for the NP class problem, the answer is possible, which can be calculated in polynomial time.
This class contains many problems that one would like to be able to solve effectively:
- Boolean Satisfiability Problem (SAT).
- Hamiltonian Path Problem.
- Graph coloring.
Co-NP Class
Co-NP stands for the complement of NP Class. It means if the answer to a problem in Co-NP is No, then there is proof that can be checked in polynomial time.
Features:
- If a problem X is in NP, then its complement X' is also in CoNP.
- For an NP and CoNP problem, there is no need to verify all the answers at once in polynomial time, there is a need to verify only one particular answer "yes" or "no" in polynomial time for a problem to be in NP or CoNP.
Some example problems for CoNP are:
- To check prime number.
- Integer Factorization.
An NP-hard problem is at least as hard as the hardest problem in NP and it is a class of problems such that every problem in NP reduces to NP-hard.
Features:
- All NP-hard problems are not in NP.
- It takes a long time to check them. This means if a solution for an NP-hard problem is given then it takes a long time to check whether it is right or not.
- A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time reduction from L to A.
Some of the examples of problems in Np-hard are:
- Halting problem.
- Qualified Boolean formulas.
- No Hamiltonian cycle.
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hard problems in NP.
Features:
- NP-complete problems are special as any problem in NP class can be transformed or reduced into NP-complete problems in polynomial time.
- If one could solve an NP-complete problem in polynomial time, then one could also solve any NP problem in polynomial time.
Some example problems include:
- Hamiltonian Cycle.
- Satisfiability.
- Vertex cover.
Complexity Class | Characteristic feature |
P | Easily solvable in polynomial time. |
NP | Yes, answers can be checked in polynomial time. |
Co-NP | No, answers can be checked in polynomial time. |
NP-hard | All NP-hard problems are not in NP and it takes a long time to check them. |
NP-complete | A problem that is NP and NP-hard is NP-complete. |
Similar Reads
Analysis of Algorithms Analysis of Algorithms is a fundamental aspect of computer science that involves evaluating performance of algorithms and programs. Efficiency is measured in terms of time and space.Basics on Analysis of Algorithms:Why is Analysis Important?Order of GrowthAsymptotic Analysis Worst, Average and Best
1 min read
Complete Guide On Complexity Analysis - Data Structure and Algorithms Tutorial Complexity analysis is defined as a technique to characterise the time taken by an algorithm with respect to input size (independent from the machine, language and compiler). It is used for evaluating the variations of execution time on different algorithms. What is the need for Complexity Analysis?
15+ min read
Why is Analysis of Algorithm important? Why is Performance of Algorithms Important ? There are many important things that should be taken care of, like user-friendliness, modularity, security, maintainability, etc. Why worry about performance? The answer to this is simple, we can have all the above things only if we have performance. So p
2 min read
Types of Asymptotic Notations in Complexity Analysis of Algorithms We have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don't depend on machine-specific constants and don't require algorithms to be implemented and time taken by programs
8 min read
Worst, Average and Best Case Analysis of Algorithms In the previous post, we discussed how Asymptotic analysis overcomes the problems of the naive way of analyzing algorithms. Now let us learn about What is Worst, Average, and Best cases of an algorithm:1. Worst Case Analysis (Mostly used) In the worst-case analysis, we calculate the upper bound on t
10 min read
Asymptotic Analysis Given two algorithms for a task, how do we find out which one is better? One naive way of doing this is - to implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of
3 min read
How to Analyse Loops for Complexity Analysis of Algorithms We have discussed Asymptotic Analysis, Worst, Average and Best Cases and Asymptotic Notations in previous posts. In this post, an analysis of iterative programs with simple examples is discussed. The analysis of loops for the complexity analysis of algorithms involves finding the number of operation
15+ min read
Sample Practice Problems on Complexity Analysis of Algorithms Prerequisite: Asymptotic Analysis, Worst, Average and Best Cases, Asymptotic Notations, Analysis of loops.Problem 1: Find the complexity of the below recurrence: { 3T(n-1), if n>0,T(n) = { 1, otherwiseSolution: Let us solve using substitution.T(n) = 3T(n-1) = 3(3T(n-2)) = 32T(n-2) = 33T(n-3) ...
14 min read
Basics on Analysis of Algorithms
Why is Analysis of Algorithm important?Why is Performance of Algorithms Important ? There are many important things that should be taken care of, like user-friendliness, modularity, security, maintainability, etc. Why worry about performance? The answer to this is simple, we can have all the above things only if we have performance. So p
2 min read
Asymptotic AnalysisGiven two algorithms for a task, how do we find out which one is better? One naive way of doing this is - to implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of
3 min read
Worst, Average and Best Case Analysis of AlgorithmsIn the previous post, we discussed how Asymptotic analysis overcomes the problems of the naive way of analyzing algorithms. Now let us learn about What is Worst, Average, and Best cases of an algorithm:1. Worst Case Analysis (Mostly used) In the worst-case analysis, we calculate the upper bound on t
10 min read
Types of Asymptotic Notations in Complexity Analysis of AlgorithmsWe have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don't depend on machine-specific constants and don't require algorithms to be implemented and time taken by programs
8 min read
How to Analyse Loops for Complexity Analysis of AlgorithmsWe have discussed Asymptotic Analysis, Worst, Average and Best Cases and Asymptotic Notations in previous posts. In this post, an analysis of iterative programs with simple examples is discussed. The analysis of loops for the complexity analysis of algorithms involves finding the number of operation
15+ min read
How to analyse Complexity of Recurrence RelationThe analysis of the complexity of a recurrence relation involves finding the asymptotic upper bound on the running time of a recursive algorithm. This is usually done by finding a closed-form expression for the number of operations performed by the algorithm as a function of the input size, and then
7 min read
Introduction to Amortized AnalysisAmortized Analysis is used for algorithms where an occasional operation is very slow, but most other operations are faster. In Amortized Analysis, we analyze a sequence of operations and guarantee a worst-case average time that is lower than the worst-case time of a particularly expensive operation.
10 min read
Asymptotic Notations
Big O Notation Tutorial - A Guide to Big O AnalysisBig O notation is a powerful tool used in computer science to describe the time complexity or space complexity of algorithms. Big-O is a way to express the upper bound of an algorithmâs time or space complexity. Describes the asymptotic behavior (order of growth of time or space in terms of input si
10 min read
Big O vs Theta Πvs Big Omega Ω Notations1. Big O notation (O): It defines an upper bound on order of growth of time taken by an algorithm or code with input size. Mathematically, if f(n) describes the running time of an algorithm; f(n) is O(g(n)) if there exist positive constant C and n0 such that,0 <= f(n) <= Cg(n) for all n >=
3 min read
Examples of Big-O analysisPrerequisite: Analysis of Algorithms | Big-O analysis In the previous article, the analysis of the algorithm using Big O asymptotic notation is discussed. In this article, some examples are discussed to illustrate the Big O time complexity notation and also learn how to compute the time complexity o
13 min read
Difference between Big O Notation and TildeIn asymptotic analysis of algorithms we often come across terms like Big O, Omega, Theta and Tilde, which describe the performance of an algorithm. Here, we will see difference between two notations: Big O and Tilde.Big O Notation (O) This notation is basically used to describe the asymptotic upper
4 min read
Analysis of Algorithms | Big-Omega ⦠NotationIn the analysis of algorithms, asymptotic notations are used to evaluate the performance of an algorithm, in its best cases and worst cases. This article will discuss Big-Omega Notation represented by a Greek letter (â¦). Table of Content What is Big-Omega ⦠Notation?Definition of Big-Omega ⦠Notatio
9 min read
Analysis of Algorithms | Î (Theta) NotationIn the analysis of algorithms, asymptotic notations are used to evaluate the performance of an algorithm by providing an exact order of growth. This article will discuss Big - Theta notations represented by a Greek letter (Î).Definition: Let g and f be the function from the set of natural numbers to
6 min read