Programming Articles

Page 971 of 2547

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 238 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 267 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 201 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 446 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

Print the last occurrence of elements in array in relative order in C Program.

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

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 More

Print reverse of a Linked List without extra space and modification in C Program.

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

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

Binary Search using pthread in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 641 Views

Binary search is one of the most efficient searching algorithms for sorted arrays. It works by repeatedly dividing the search space in half. In this article, we'll explore how to implement binary search using multiple threads (pthread) in C to potentially improve performance by parallelizing the search across different segments of the array. Syntax #include int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); int pthread_join(pthread_t thread, void **retval); Example: Multithreaded Binary ...

Read More

Binary representation of next greater number with same number of 1's and 0's in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 360 Views

In C programming, finding the binary representation of the next greater number with the same number of 1's and 0's is a classic problem that can be solved using a next-permutation approach. Given a binary string, we need to find the smallest binary number that is greater than the given number and contains exactly the same count of 0's and 1's. Syntax char* nextBinary(char* bin); Algorithm The algorithm works in two main steps − Find the rightmost '01' pattern and swap it to '10' Rearrange all bits after the swap position to ...

Read More

BFS using vectors & queue as per the algorithm of CLRS in C Program?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 443 Views

In graph theory, BFS (Breadth-First Search) is a fundamental traversal algorithm described in CLRS (Introduction to Algorithms). It explores vertices level by level using a queue data structure and maintains vertex states using colors. Syntax void BFS(int graph[][MAX_VERTICES], int vertices, int source); Algorithm The CLRS BFS algorithm uses the following pseudocode − BFS(G, s) - begin for each vertex u in G.V - {s}, do u.color := white u.d := infinity ...

Read More
Showing 9701–9710 of 25,466 articles
« Prev 1 969 970 971 972 973 2547 Next »
Advertisements