The Wayback Machine - https://web.archive.org/web/20240930183803/https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/
Open In App

Sample Practice Problems on Complexity Analysis of Algorithms

Last Updated : 30 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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, otherwise

Solution:  

Let us solve using substitution.

T(n) = 3T(n-1)
       = 3(3T(n-2)) 
       = 32T(n-2)
       = 33T(n-3)
       … 
       …
       = 3nT(n-n)
       = 3nT(0) 
       = 3n

This clearly shows that the complexity of this function is O(3n).

Problem 2: Find the complexity of the recurrence:  

             { 2T(n-1) – 1, if n>0,
T(n) =   { 1, otherwise

Solution: 

Let us try solving this function with substitution.

T(n) = 2T(n-1) – 1
       = 2(2T(n-2)-1)-1 
       = 22(T(n-2)) – 2 – 1
       = 22(2T(n-3)-1) – 2 – 1 
       = 23T(n-3) – 22 – 21 – 20
          …..
       …..
       = 2nT(n-n) – 2n-1 – 2n-2 – 2n-3
          ….. 22 – 21 – 20

       = 2n – 2n-1 – 2n-2 – 2n-3
          ….. 22 – 21 – 20
          = 2n – (2n-1) 

[Note: 2n-1 + 2n-2 + …… +  20 = 2n – 1]

T(n) = 1

Time Complexity is O(1). Note that while the recurrence relation looks exponential
he solution to the recurrence relation here gives a different result.

Problem 3: Find the complexity of the below program: 

C++
void function(int n)
{
    if (n==1)
       return;
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=n; j++)
        {
            cout << "*";
            break;
        }
      cout << endl;
    }
}
Java
/*package whatever //do not write package name here */

static void function(int n)
{
    if (n==1)
       return;
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=n; j++)
        {
            System.out.print("*");
            break;
        }
          System.out.println();
    }
}
// The code is contributed by Nidhi goel. 
Python
def funct(n):
  
    if (n==1):
       return
    for i in range(1, n+1):
        for j in range(1, n + 1):
            print("*", end = "")
            break
          print()
    
# The code is contributed by Nidhi goel. 
C#
/*package whatever //do not write package name here */

public static void function(int n)
{
    if (n==1)
       return;
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=n; j++)
        {
            Console.Write("*");
            break;
        }
          Console.WriteLine();
    }
}
// The code is contributed by Nidhi goel. 
JavaScript
function funct(n)
{
    if (n==1)
       return;
    for (let i=1; i<=n; i++)
    {
        for (let j=1; j<=n; j++)
        {
            console.log("*");
            break;
        }
          console.log();
    }
}
// The code is contributed by Nidhi goel.

Solution: Consider the comments in the following function. 

C++
function(int n)
{
    if (n==1)
       return;
    for (int i=1; i<=n; i++)
    {
        // Inner loop executes only one
        // time due to break statement.
        for (int j=1; j<=n; j++)
        {
            printf("*");
            break;
        }
    }
}
Java
public class Main {
    public static void main(String[] args) {
        int n = 5; // You can change the value of n as needed
        printPattern(n);
    }

    static void printPattern(int n) {
        for (int i = 1; i <= n; i++) {
            // Print a single '*' on each line
            System.out.println("*");
        }
    }
}
Python
def my_function(n):
    if n == 1:
        return
    
    for i in range(1, n+1):
        # Inner loop executes only one
        # time due to break statement.
        for j in range(1, n+1):
            print("*", end="")
            break

my_function(5)  # Example: calling the function with n=5
#this code is contributed by Monu Yadav.
C#
using System;

class Program
{
    static void PrintPattern(int n)
    {
        if (n == 1)
            return;

        for (int i = 1; i <= n; i++)
        {
            // Inner loop executes only once
            // due to the break statement.
#pragma warning disable CS0162
            for (int j = 1; j <= n; j++)
            {
                Console.Write("*");

                // The following break statement exits the inner loop
                // after printing a single '*' character. 
                // If you want the inner loop to iterate through the entire range,
                // you can remove or comment out the break statement.
                break;
            }
#pragma warning restore CS0162
        }
    }

    static void Main()
    {
        int n = 5; // You can change the value of 'n' as needed
        PrintPattern(n);
    }
}
JavaScript
function printStars(n) {
  if (n === 1) {
    return; // If n is 1, exit the function
  }

  for (let i = 1; i <= n; i++) {
    // Outer loop to iterate from 1 to n
    // Inner loop executes only once due to the break statement
    for (let j = 1; j <= n; j++) {
      console.log("*"); // Print a star
      break; // Break the inner loop after printing one star
    }
  }
}

// Example usage:
printStars(5); // Call the function with n = 5

Time Complexity: O(n), Even though the inner loop is bounded by n, but due to the break statement, it is executing only once.

Problem 4: Find the complexity of the below program: 

C++
void function(int n)
{
    int count = 0;
    for (int i=n/2; i<=n; i++)
        for (int j=1; j<=n; j = 2 * j)
            for (int k=1; k<=n; k = k * 2)
                count++;
}
Java
static void function(int n)
{
    int count = 0;
    for (int i = n / 2; i <= n; i++)
        for (int j = 1; j <= n; j = 2 * j)
            for (int k = 1; k <= n; k = k * 2)
                count++;
}

// This code is contributed by rutvik_56.
Python
def function1(n):
    count = 0
    for i in range(n // 2, n + 1):
        for j in range(1, n + 1, 2 * j):
            for k in range(1, n + 1, k * 2):
                count += 1
C#
static void function(int n)
{
    int count = 0;
    for (int i = n / 2; i <= n; i++)
        for (int j = 1; j <= n; j = 2 * j)
            for (int k = 1; k <= n; k = k * 2)
                count++;
}

// This code is contributed by pratham76.
JavaScript
function function1(n)
{
    var count = 0;
    for (var i = n / 2; i <= n; i++)
        for (var j = 1; j <= n; j = 2 * j)
            for (var k = 1; k <= n; k = k * 2)
                count++;
}

Solution: Consider the comments in the following function. 

C++
#include <iostream>
using namespace std;

void function(int n) {
    int count = 0;
    for (int i = n / 2; i <= n; i++) {
        // Executes O(Log n) times
        for (int j = 1; j <= n; j = 2 * j) {
            // Executes O(Log n) times
            for (int k = 1; k <= n; k = k * 2) {
                count++;
            }
        }
    }
    cout << "Count: " << count << endl;
}

int main() {
    // Example usage
    function(10); // Call function with an example value of n
    return 0;
}
Java
public class Main {

    public static void function(int n) {
        int count = 0;
        for (int i = n / 2; i <= n; i++) {

            // Executes O(Log n) times
            for (int j = 1; j <= n; j = 2 * j) {

                // Executes O(Log n) times
                for (int k = 1; k <= n; k = k * 2) {
                    count++;
                }
            }
        }
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        // Example usage
        function(10); // Call function with an example value of n
    }
}
//This code is contributed by Adarsh 
Python
def function(n):
    count = 0
    # Outer loop executes n/2 times
    for i in range(n // 2, n + 1):
        # Middle loop executes O(Log n) times
        j = 1
        while j <= n:
            # Inner loop also executes O(Log n) times
            k = 1
            while k <= n:
                count += 1
                k *= 2
            j *= 2
    print("Count:", count)

# Example usage
function(10)  
JavaScript
function functionMain(n) {
    let count = 0;
    for (let i = Math.floor(n / 2); i <= n; i++) {
        // Executes O(Log n) times
        for (let j = 1; j <= n; j *= 2) {
            // Executes O(Log n) times
            for (let k = 1; k <= n; k *= 2) {
                count++;
            }
        }
    }
    console.log("Count: " + count);
}

functionMain(10); // Example usage

Time Complexity: O(n log2n).

Problem 5: Find the complexity of the below program: 

C++
void function(int n)
{
    int count = 0;
    for (int i=n/2; i<=n; i++)
        for (int j=1; j+n/2<=n; j = j++)
            for (int k=1; k<=n; k = k * 2)
                count++;
}
Java
static void function(int n)
{
    int count = 0;
    for (int i=n/2; i<=n; i++)
        for (int j=1; j+n/2<=n; j = j++)
            for (int k=1; k<=n; k = k * 2)
                count++;
}

// This code is contributed by Pushpesh Raj.
Python3
def function(n):
    # Initialize count to 0
    count = 0

    # Outer loop starts from n/2 and goes up to n
    for i in range(n//2, n+1):
        # Middle loop starts from 1 and goes up to n/2
        for j in range(1, n//2+1):
            # Inner loop starts from 1 and doubles at each step
            for k in range(1, n+1, 2):
                # Increment count at each iteration
                count += 1
JavaScript
function myFunction(n) {
    let count = 0;

    // Outer loop runs from n/2 to n
    for (let i = n / 2; i <= n; i++) {

        // Middle loop runs from 1 to n - n/2
        for (let j = 1; j + n / 2 <= n; j++) {
            
            // Inner loop runs from 1 to n, doubling k in each iteration
            for (let k = 1; k <= n; k = k * 2) {
                count++;
            }
        }
    }

    return count;
}

Solution: Consider the comments in the following function. 

C++
void function(int n)
{
    int count = 0;

    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)

        // middle loop executes  n/2 times
        for (int j=1; j+n/2<=n; j = j++)

            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}

// The code is contributed by Nidhi goel. 
Java
static void function(int n)
{
    int count = 0;

    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)

        // middle loop executes n/2 times
        for (int j=1; j+n/2<=n; j = j++)

            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}

// This code is contributed by Aman Kumar
Python
def function(n):
    count = 0

    # outer loop executes n/2 times
    for i in range(n//2, n+1):

        # middle loop executes  n/2 times
        for j in range((1, n//2 + 1):

            # inner loop executes logn times
            for k in range(1, n+1, 2):
                count++

# The code is contributed by Nidhi goel. 
C#
using System;

public static void function(int n)
{
    int count = 0;

    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)

        // middle loop executes  n/2 times
        for (int j=1; j+n/2<=n; j = j++)

            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}

// The code is contributed by Nidhi goel.
JavaScript
function function(n)
{
    let count = 0;

    // outer loop executes n/2 times
    for (let i= Math.floor(n/2); i<=n; i++)

        // middle loop executes  n/2 times
        for (let j=1; j+n/2<=n; j = j++)

            // inner loop executes logn times
            for (let k=1; k<=n; k = k * 2)
                count++;
}

// The code is contributed by Nidhi goel. 

Time Complexity: O(n2logn).

Problem 6: Find the complexity of the below program: 

C++
#include <iostream>

// Function to print '*' until the sum of consecutive integers exceeds n
void function(int n) {
    int i = 1; // Initialize i to 1
    int s = 1; // Initialize s to 1

    // Loop until the sum of consecutive integers exceeds n
    while (s <= n) {
        i++; // Increment i
        s += i; // Add i to the sum
        std::cout << "*"; // Print '*'
    }
}

int main() {
    int n = 10; // Example value of n
    function(n); // Call the function
    return 0;
}
Java
public class Main {
    // Function to print '*' until the sum of consecutive integers exceeds n
    public static void function(int n) {
        int i = 1; // Initialize i to 1
        int s = 1; // Initialize s to 1

        // Loop until the sum of consecutive integers exceeds n
        while (s <= n) {
            i++; // Increment i
            s += i; // Add i to the sum
            System.out.print("*"); // Print '*'
        }
    }

    public static void main(String[] args) {
        int n = 10; // Example value of n
        function(n); // Call the function
    }
}
Python3
def function(n):
    i = 1  # Initialize i to 1
    s = 1  # Initialize s to 1

    # Loop until the sum of consecutive integers exceeds n
    while s <= n:
        i += 1  # Increment i
        s += i  # Add i to the sum
        print("*", end="")  # Print '*' without a newline

# Example value of n
n = 10
function(n)  # Call the function
JavaScript
function functionJS(n) {
    let i = 1; // Initialize i to 1
    let s = 1; // Initialize s to 1

    // Loop until the sum of consecutive integers exceeds n
    while (s <= n) {
        i++; // Increment i
        s += i; // Add i to the sum
        process.stdout.write("*"); // Print '*' without a newline
    }
}

// Example value of n
let n = 10;
functionJS(n); // Call the function

Solution: We can define the terms ‘s’ according to relation si = si-1 + i. The value of ‘i’ increases by one for each iteration. The value contained in ‘s’ at the ith iteration is the sum of the first ‘i’ positive integers. If k is total number of iterations taken by the program, then while loop terminates if: 1 + 2 + 3 ….+ k = [k(k+1)/2] > n So k = O(√n).
Time Complexity: O(√n).

Problem 7: Find a tight upper bound on the complexity of the below program: 

C++
void function(int n)
{
    int count = 0;
    for (int i=0; i<n; i++)
        for (int j=i; j< i*i; j++)
            if (j%i == 0)
            {
                for (int k=0; k<j; k++)
                    printf("*");
            }
}
Java
void function(int n)
{
    int count = 0;
    for (int i=0; i<n; i++)
        for (int j=i; j< i*i; j++)
            if (j%i == 0)
            {
                for (int k=0; k<j; k++)
                    printf("*");
            }
}
Python3
def myFunction(n):
    for i in range(n):
        for j in range(i, i * i):
            if j % i == 0:  # Check if j is divisible by i
                for k in range(j):
                    print("*", end="")  # Print '*' to the console without newline
        print()  # Move to the next line after printing '*' for each j

# Example usage
myFunction(5)
JavaScript
function myFunction(n) {
    for (let i = 0; i < n; i++) {
        for (let j = i; j < i * i; j++) {
            if (j % i === 0) { // Check if j is divisible by i
                for (let k = 0; k < j; k++) {
                    process.stdout.write("*"); // Print '*' to the console
                }
            }
        }
    }
}


Solution: Consider the comments in the following function. 
 

C++
void function(int n)
{
    int count = 0;

    // executes n times
    for (int i=0; i<n; i++)

        // executes O(n*n) times.
        for (int j=i; j< i*i; j++)
            if (j%i == 0)
            {
                // executes j times = O(n*n) times
                for (int k=0; k<j; k++)
                    printf("*");
            }
}
Java
public class Main {
    static void function(int n) {
        int count = 0; // Initialize count variable

        // executes n times
        for (int i = 0; i < n; i++) {

            // executes O(n*n) times.
            for (int j = i; j < i * i; j++) {
                if (j % i == 0) {
                    // executes j times = O(n*n) times
                    for (int k = 0; k < j; k++) {
                        System.out.print("*");
                        count++; // Increment count
                    }
                }
            }
        }

        System.out.println("\nCount: " + count); // Print count after loops
    }

    public static void main(String[] args) {
        int n = 5; // example value for n
        function(n);
    }
}
Python3
def function(n):
    count = 0

    # executes n times
    for i in range(n):

        # executes O(n*n) times.
        for j in range(i, i*i):
            if j % i == 0:

                # executes j times = O(n*n) times
                for k in range(j):
                    print("*", end="")
JavaScript
// JavaScript equivalent of the Python code

function someFunction(n) {
    let count = 0;

    // executes n times
    for (let i = 0; i < n; i++) {

        // executes O(n*n) times.
        for (let j = i; j < i * i; j++) {
            if (j % i === 0) {

                // executes j times = O(n*n) times
                for (let k = 0; k < j; k++) {
                    process.stdout.write("*");
                }
            }
        }
    }
}

// Example usage
someFunction(5); // You can replace 5 with any desired value for n

Time Complexity: O(n5)

This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.



Previous Article
Next Article

Similar Reads

Asymptotic Notation and Analysis (Based on input size) in Complexity Analysis of Algorithms
Asymptotic Analysis is defined as the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, how the time (or space) taken by an algorithm increases with the input size. Asymptotic notation is
8 min read
Practice Questions on Time Complexity Analysis
Prerequisite: Analysis of Algorithms1. What is the time, and space complexity of the following code: [GFGTABS] CPP int a = 0, b = 0; for (i = 0; i < N; i++) { a = a + rand(); } for (j = 0; j < M; j++) { b = b + rand(); } Java int a = 0, b = 0; for (i = 0; i < N; i++) { a = a + Math.random(); } for (j = 0; j < M; j++) { b = b + Math.rand
8 min read
Time and Space Complexity Analysis of Tree Traversal Algorithms
Let us discuss the Time and Space complexity of different Tree Traversal techniques, such as Inorder Traversal, Preorder Traversal, Postorder Traversal, etc. Time Complexity of Tree Traversal AlgorithmsLet us see different corner cases: Complexity function T(n) — for all problems where tree traversal is involved — can be defined as: T(n) = T(k) + T
2 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 operations performed by a loop as a function of the input s
15+ 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 to be compared. Asymptotic notations are mathemat
8 min read
Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
Algorithms Sample Questions | Set 3 | Time Order Analysis
Question 1: What is the asymptotic boundary of T(n)? [Tex] T(n) = \sum_{i=2}^{n} log_{i}n = log_{2}n + log_{3}n + \ldots + log_{n}n [/Tex] θ( n*log(n) ) θ( n2 ) θ( n ) θ( n*log2(n) ) θ( n2*log2(n) ) Answer: 3 Explanation: To find appropriate upper and lower boundaries, an approach which first comes to mind is to expand
10 min read
Bitwise Algorithms - Basic Practice Problems
Bitwise algorithms are a category of algorithms that operate on individual bits within a data unit, rather than the entire data unit itself. They leverage bitwise operators, which are special operators designed to perform operations on bits. Common bitwise operators include AND (&), OR (|), XOR (^), and NOT (~). Before diving into the world of
5 min read
Bitwise Algorithms - Intermediate Practice Problems
Bitwise operations are fundamental to computer programming and problem-solving. At the intermediate level, you'll explore more complex bitwise algorithms and techniques to solve a variety of coding challenges. This includes topics like bit manipulation, number theory, and optimization using bitwise operations. The practice problems will cover a ran
7 min read
Miscellaneous Problems of Time Complexity
Prerequisite : Asymptotic Notations Time Complexity :Time complexity is the time needed by an algorithm expressed as a function of the size of a problem. It can also be defined as the amount of computer time it needs to run a program to completion. When we solve a problem of time complexity then this definition help the most - "It is the number of
15+ min read
Time Complexity Analysis | Tower Of Hanoi (Recursion)
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk ca
2 min read
Complexity analysis of various operations of Binary Min Heap
A Min Heap is a Complete Binary Tree in which the children nodes have a higher value (lesser priority) than the parent nodes, i.e., any path from the root to the leaf nodes, has an ascending order of elements. In the case of a binary tree, the root is considered to be at height 0, its children nodes are considered to be at height 1, and so on. Each
3 min read
Step Count Method for Time Complexity Analysis
What is Time Complexity? Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm. Time Complexity can be calculated by using Two types of methods. They are: Step Count MethodAsymptotic Notation. Here, we will discuss the Step Count Method. What is Step Count M
4 min read
Time and Space complexity analysis of Selection Sort
The Selection sort algorithm has a time complexity of O(n^2) and a space complexity of O(1) since it does not require any additional memory space apart from a temporary variable used for swapping. Time Complexity Analysis of Selection Sort:Best-case: O(n2), best case occurs when the array is already sorted. (where n is the number of integers in an
2 min read
Time and Space Complexity analysis of Stack operations
What is Stack?Stack is a linear data structure that follows a particular order in which the elements are inserted and deleted. A stack follows the principle of last in first out(LIFO) which means that the last element inserted into the stack should be removed first. Consider an example of plates stacked over one another in the canteen. The plate wh
15+ min read
Time and Space Complexity Analysis of Queue operations
What is Queue?Queue is a linear data structure that follows FIFO approach (First In First Out). One can imagine a queue as a line of people waiting in sequential order which starts from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as the rear and deletions are done from the other end known
15+ min read
Time and Space Complexity Analysis of Prim's Algorithm
The time complexity of Prim's algorithm is O(V2) using an adjacency matrix and O((V +E) log V) using an adjacency list, where V is the number of vertices and E is the number of edges in the graph. The space complexity is O(V+E) for the priority queue and O(V2) for the adjacency matrix representation. The algorithm's time complexity depends on the d
3 min read
Time and Space Complexity Analysis of Kruskal Algorithm
Kruskal's algorithm is a popular algorithm for finding the Minimum Spanning Tree (MST) of a connected, undirected graph. The time complexity of Kruskal's algorithm is O(E log E), where E is the number of edges in the graph. The space complexity of Kruskal's algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the
2 min read
Time and Space Complexity Analysis of Merge Sort
The Time Complexity of Merge Sort is O(n log n) in both the average and worst cases. The space complexity of Merge sort is O(n). AspectComplexityTime ComplexityO(n log n)Space ComplexityO(n)Time Complexity Analysis of Merge Sort:Consider the following terminologies: T(k) = time taken to sort k elementsM(k) = time taken to merge k elements So, it ca
2 min read
Time and Space Complexity Analysis of Quick Sort
The time complexity of Quick Sort is O(n log n) on average case, but can become O(n^2) in the worst-case. The space complexity of Quick Sort in the best case is O(log n), while in the worst-case scenario, it becomes O(n) due to unbalanced partitioning causing a skewed recursion tree that requires a call stack of size O(n). VariationTime ComplexityS
4 min read
Time and Space Complexity Analysis of Bubble Sort
The time complexity of Bubble Sort is O(n^2) in the worst-case scenario and the space complexity of Bubble sort is O(1). Bubble Sort only needs a constant amount of additional space during the sorting process. Complexity TypeComplexityTime ComplexityBest: O(n)Average: O(n^2)Worst: O(n^2)Space ComplexityWorst: O(1)Time Complexity Analysis of Bubble
3 min read
Time and Space Complexity Analysis of Binary Search Algorithm
Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. AspectComplexityTime ComplexityO(log n)Space ComplexityO(1)The time and space complexities of the binary search algorithm are mentioned below.
3 min read
Prune-and-Search | A Complexity Analysis Overview
The word "prune" means to reduce something by removing things that are not necessary. So, Prune-and-Search is an excellent algorithmic paradigm for solving various optimization problems. This approach was first suggested by Nimrod Megiddo in 1983. This approach always consists of several iterations. At each iteration, it discards a fraction, say f,
8 min read
Constant & Linear Space Complexity in Algorithms
Being a programmer, you might have solved a lot of programming challenges. In programming space and time complexity matters a lot when we need to execute a program. Our algorithm should be efficient, and it should take less time. Whenever you're writing a solution to a program, some memory is required to complete, and it is important for our progra
5 min read
Practice Problems on Hashing
In this article, we will discuss the types of questions based on hashing. Before understanding this, you should have idea about hashing, hash function, open addressing and chaining techniques (see: Introduction, Separate chaining, Open addressing). These are some key points in hashing: The purpose of hashing is to achieve search, insert and delete
8 min read
Practice Problems on Game Theory
Game Theory is a topic in competitive programming that involves a certain type of problem, where there are some players who play a game based on given rules and the task is often to find the winner or the winning moves. Game Theory is often asked in short contests with a mixture of other topics like range querying or greedy or dynamic programming.
4 min read
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers. Fastest way to compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The Euclidean algorithm is an efficient and widely used method for GCD c
4 min read
Recursive Practice Problems with Solutions
Basics - Recursion Recursive Functions Tail Recursion Strings - Given a string, print all possible palindromic partitions Check if a number is Palindrome Print all possible strings of length k that can be formed from a set of n characters Recursive Implementation of atoi() Find all even length binary sequences with same sum of first and second half
4 min read
Array Sorting - Practice Problems
Sorting an array means arranging the elements of the array in a certain order. Generally sorting in an array is done to arrange the elements in increasing or decreasing order. Problem statement: Given an array of integers arr, the task is to sort the array in ascending order and return it, without using any built-in functions. Example: Input: arr =
9 min read
RFM Analysis Analysis Using Python
In this article, we are going to see Recency, Frequency, Monetary value analysis using Python. But first, let us understand the RFM analysis briefly. What is RFM analysis? RFM stands for recency, frequency, monetary value. In business analytics, we often use this concept to divide customers into different segments, like high-value customers, medium
3 min read