Sunidhi Bansal

Sunidhi Bansal

809 Articles Published

Articles by Sunidhi Bansal

Page 12 of 81

Print the lexicographically smallest BFS of the graph starting from 1 in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 597 Views

We will be given a connected graph with N vertices and M edges. We need to print the lexicographically smallest BFS traversal of the graph starting from vertex 1. Lexicographically smallest means visiting vertices in the smallest numerical order possible at each level. Instead of using a regular queue for BFS, we use a priority queue (min-heap) to always visit the smallest numbered unvisited vertex first. Syntax void lexicographicalBFS(int graph[][MAX], int n, int start); Algorithm The approach uses a priority queue to ensure we always visit the smallest numbered vertex next − ...

Read More

Print string of odd length in 'X' format in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

Given a string of odd length, we need to print it in 'X' format where characters are displayed diagonally from top-left to bottom-right and top-right to bottom-left, creating an X pattern. t t u n t i o r a i l a p l o ...

Read More

Print numbers in sequence using thread synchronization in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 7K+ Views

In C programming, thread synchronization is used to coordinate the execution of multiple threads to achieve a desired sequence. This program demonstrates how to print numbers from 1 to 10 in sequence using two threads − one for even numbers and one for odd numbers. What is a Thread? A thread is a lightweight process that runs inside a program. A single program can contain multiple threads executing concurrently. Unlike Java, C does not have built-in multithreading support. Instead, it relies on POSIX Threads (Pthreads) library for multithreading functionality. Syntax pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...

Read More

Print leftmost and rightmost nodes of a Binary Tree in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 1K+ Views

In C, printing the leftmost and rightmost nodes of a binary tree involves traversing the tree level by level and capturing the first and last nodes at each level. This technique uses level-order traversal (BFS) to identify boundary nodes. Syntax struct node { int data; struct node* left; struct node* right; }; void printBoundaryNodes(struct node* root); Algorithm The approach uses a queue-based level-order traversal − Process each level completely before moving to the next For each level, store the ...

Read More

Print maximum sum square sub-matrix of given size in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 703 Views

Given a matrix of NxN, find a sub-matrix of MxM where M=1 such that the sum of all elements in the MxM matrix is maximum. The input matrix can contain zero, positive and negative integer values. Finding Maximum Sum Sub-matrix Original 5x5 Matrix 1 1 1 1 ...

Read More

Print left rotation of array in O(n) time and O(1) space in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 225 Views

We are given an array of some size n and multiple integer values, we need to rotate an array from a given index k. Left rotation means moving elements to the left by k positions, where elements that go beyond the beginning wrap around to the end. We want to rotate an array from an index k like − Array Left Rotation by k=2 Original Array: 1 2 3 ...

Read More

Print k different sorted permutations of a given array in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 255 Views

Given an array containing N integers, the challenge is to print k different permutations of indices such that the values at those indices form a non-decreasing sequence. Print -1 if it is not possible. Syntax void findSortedPermutations(int arr[], int n, int k); Algorithm The approach works by sorting the array while keeping track of original indices. If any two consecutive elements are equal, they can be swapped to generate different permutations − Create pairs of (value, original_index) for each array element Sort the pairs by value to get the first permutation Count ...

Read More

Print index of columns sorted by count of zeroes in the Given Matrix in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 192 Views

Given a matrix of size NxM where N is the number of rows and M is the number of columns, we need to print the column indices sorted by the count of zeros in each column. This means we first count zeros in each column, then sort the columns based on these counts, and finally print the column indices (1-based indexing). For example, if column 1 contains 1 zero, column 2 contains 0 zeros, and column 3 contains 2 zeros, the result should be − 2 1 3 (sorted by zero count: 0, 1, 2). Syntax ...

Read More

Print the corner elements and their sum in a 2-D matrix in C Program.

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 3K+ Views

Given a 2D matrix, the challenge is to find and print the corner elements along with their sum. In a matrix, corner elements are located at the four corners: top-left, top-right, bottom-left, and bottom-right positions. For a matrix mat[r][c] with rows from 0 to r-1 and columns from 0 to c-1, the corner elements are: mat[0][0], mat[0][c-1], mat[r-1][0], and mat[r-1][c-1]. The task is to extract these corner elements and calculate their sum. Syntax // Corner elements of a matrix mat[r][c] mat[0][0] // Top-left corner mat[0][c-1] ...

Read More

Print the balanced bracket expression using given brackets in C Program

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 423 Views

Given four variables a, b, c, d with predefined values that represent different types of bracket pairs. The task is to use all the given brackets and print a balanced bracket expression using these bracket types. Where variables represent − a for (( b for () c for )( d for )) If we cannot form a balanced bracket expression then print "can't be formed". In case of multiple valid answers, we can print any of them. Syntax void print(int a, int b, int c, int d); Algorithm To ...

Read More
Showing 111–120 of 809 articles
« Prev 1 10 11 12 13 14 81 Next »
Advertisements