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
Server Side Programming Articles
Page 951 of 2109
Print pair with maximum AND value in an array in C Program.
In this problem, we are given an array of n positive integers and need to find a pair with the maximum AND value. The bitwise AND operation combines bits where both operands have 1 in the same position. Syntax int maxAND(int arr[], int n); int checkBit(int pattern, int arr[], int n); Algorithm Approach The algorithm works by building the maximum AND value bit by bit from the most significant bit (MSB) to the least significant bit (LSB). For each bit position, it checks if at least two numbers in the array have that bit ...
Read MorePrint Leaf Nodes at a given Level in C language
In C programming, printing leaf nodes at a given level in a binary tree involves traversing the tree to a specific level and identifying nodes that have no children. A leaf node is a node whose both left and right pointers are NULL. Syntax void printLeafNodes(struct node* root, int level); Algorithm The approach uses recursive traversal − Base case: If root is NULL, return Target level: If level == 1, check if current node is a leaf Recursive case: If level > 1, recursively call for left and right subtrees with level-1 ...
Read MorePrint the longest prefix of the given string which is also the suffix of the same string in C Program.
Given a string, we need to find the length of the longest prefix which is also a suffix of the same string. For example, in the string "abcab", "ab" is both a prefix and suffix with length 2. Syntax int longest_prefix_suffix(char str[], int n); Algorithm The approach uses the KMP (Knuth-Morris-Pratt) failure function concept − Start comparing from the middle of the string to avoid overlap Use two pointers: one for prefix (length) and one for suffix (i) When characters match, increment both pointers When they don't match, reset according to the ...
Read MorePrint modified array after multiple array range increment operations in C Program.
Given an array arr[m] with m number of integers and n, which is the value to be added in an array and r queries are given with some start and end. For each query we have to add value n from the start till the end of the limit in an array. Syntax struct range { int start, end; }; void add_tomatrix(int arr[], struct range r[], int n, int size, int m); Example Input and Output Input: arr[] = {1, 2, 3, 4, 5} query[] = { { 0, ...
Read MorePrint lower triangular matrix pattern from given array in C Program.
Given a matrix of n x n, the task is to print that matrix in lower triangular pattern. A lower triangular matrix is a matrix which has elements below the principal diagonal including the principal diagonal elements, with all other elements set as zero. Let's understand this with help of a diagram − Original Matrix 1 2 ...
Read MorePrint the arranged positions of characters to make palindrome in C Program.
You are provided with a string str with some length n. Print the position of every element of the string so it can form a palindrome, else print a message "No palindrome" on screen. What is palindrome? Palindrome is a word, sequence of characters which reads same from the reverse or backward as from the forward manner, like MADAM, racecar. To find a sequence or a word is palindrome we generally store the reverse of a word in a separate string and compare both if they are same then the given word or sequence is palindrome. But ...
Read MorePrint steps to make a number in form of 2^X – 1 in C Program.
Given a number n, we have to print the steps to make the number in the form of 2^X-1 by using XOR operation and increment operations. At odd steps, XOR the number with any 2^M-1, where M is the position of the leftmost unset bit. At even steps, increment the number by 1. Keep performing these steps until n becomes 2^X-1 (a number with all bits set), and print all the steps. Syntax int find_leftmost_unsetbit(int n); void perform_steps(int n); Algorithm The algorithm works by finding the leftmost unset bit and ...
Read MorePrint the lexicographically smallest BFS of the graph starting from 1 in C Program.
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 MorePrint string of odd length in 'X' format in C Program.
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 MorePrint 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 More