Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Sunidhi Bansal
Page 12 of 81
Print numbers in sequence using thread synchronization in C Program.
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 MorePrint leftmost and rightmost nodes of a Binary Tree in C Program.
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 MorePrint maximum sum square sub-matrix of given size in C Program.
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 MorePrint left rotation of array in O(n) time and O(1) space in C Program.
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 MorePrint k different sorted permutations of a given array in C Program.
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 MorePrint index of columns sorted by count of zeroes in the Given Matrix in C Program.
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 MorePrint the corner elements and their sum in a 2-D matrix in C Program.
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 MorePrint the balanced bracket expression using given brackets in C Program
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 MorePrint the last occurrence of elements in array in relative order in C Program.
Given an array with elements and the task is to print the last occurrences of the given elements in the list. Here we not only have to remove the duplicate elements but also we have to maintain the order of the occurrences of the elements in an array as per the last time they have occurred. For example, if we have an array of 6 elements containing some duplicate values i.e., {1, 3, 2, 3, 1, 2}, the result should be in the form of 3 1 2. Syntax void printLastOccurrence(int arr[], int n); ...
Read MorePrint reverse of a Linked List without extra space and modification in C Program.
The task is to print the nodes of a linked list in reverse order without using extra space and without modifying the original linked list. This approach maintains O(1) space complexity by counting nodes and accessing them by position rather than using recursion or additional data structures. 10 21 33 42 89 ...
Read More