Found 1401 Articles for C

Print string of odd length in ‘X’ format in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:20:47

830 Views

Given with a string program must print the string in ‘X’ format. For reference, see the image given below.Here, one variable can be used to print from left right(“i”) and other variable can used to print from right to left(“j”) and we can take other variable k which is used for space calculation.Below is the C++ implementation of the algorithm given.AlgorithmSTART Step 1 ->Declare Function void print(string str, int len)    Loop For int i = 0 and i < len and i++       Set int j = len-1- i       Loop For int k = ... Read More

Print numbers in sequence using thread synchronization in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:15:18

6K+ Views

Given with the threads the program must print the thread based on their priorities starting from 0 to 10.What is a thread?Thread is lightweight process that runs inside a program. A simple program can contain n number of threads.Unlike java, multithreading is not supported by the language standards, POSIX Threads (Pthreads) is the standard used in multithreading in C/C++. C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature.How it works in our program?To use the thread functions we use header file #include. This header file will include ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 07:11:00

980 Views

Given a binary tree with left and right children and the task is to print the exact right and left child of the given tree.Leftmost nodes are the nodes which are associated on the left side from the parent node of the tree and rightmost nodes are which are associated on the right side from the parent node of the root.ExampleInput: 106 20 320 100 21 61 52 Output: 106 20 320 100 52AlgorithmStart Step 1 -> create structure of a node    Declare int data    Declare struct node *left and *right Step 2 -> create struct node* newNode(int ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 07:04:58

486 Views

Given a matrix of NxN find a sub matrix of MxM where M=1 such that addition of all the elements of matrix MxM is maximum. Input of matrix NxN can contain zero, positive and negative integer values.ExampleInput:    {{1, 1, 1, 1, 1},    {2, 2, 2, 2, 2},    {3, 3, 3, 3, 3},    {4, 4, 4, 4, 4},    {5, 5, 5, 5, 5} } Output:    4 4    5 5The above problem can be solved by a simple solution in which we can take whole matrix NxN, then find out all possible MxM matrix and ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 06:54:19

88 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.We want to rotate an array from a index k like −ExamplesInput: arr[] = {1, 2, 3, 4, 5}    K1 = 1    K2 = 3    K3 = 6 Output:    2 3 4 5 1    4 5 1 2 3    2 3 4 5 1AlgorithmSTART Step 1 -> Declare function void leftRotate(int arr[], int n, int k)    Declare int cal = k% n    Loop For int i=0 and i In ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 06:49:37

143 Views

Given an array a[] 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.ExampleInput: arr[] = {2, 5, 6, 2, 2, 2, 2}, k = 4 Output:    0 3 4 5 6 1 2    3 0 4 5 6 1 2    0 3 4 5 6 1 2    3 0 4 5 6 1 2Sort the given array and keep track of the original indices of each element. That gives one required permutation. Now if ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 06:38:22

96 Views

Given an array of size NxM where N number of rows and M number of columns, and the task is to print the number of zeroes in every column of a corresponding matrix after performing sort operation on the basis of number of zeros present in any column.For example if the 1st column contain 1 zeros and 2nd column doesn’t contain any number of zeros and 3rd column contain 2 zeroes then the result should be − 3 1 2.ExampleInput:    0 0 0    1 1 1    1 0 1 Output: 1 3 2ExplanationNote − the matrix is ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 06:29:35

2K+ Views

Given an array of size 2X2 and the challenge is to print the sum of all the corner elements stored in an array.Assume a matrix mat[r][c], with some row “r” and column “c” starting row and column from 0, then its corner elements will be; mat[0][0], mat[0][c-1], mat[r-1][0], mat[r-1][c-1]. Now the task is to get these corner elements and sum those corner elements i.e., mat[0][0] + mat[0][c-1] + mat[r-1][0] + mat[r-1][c-1], and print the result on the screen.ExampleInput: Enter the matrix elements :    10 2 10    2 3 4    10 4 10 Output: sum of matrix is ... Read More

Print the balanced bracket expression using given brackets in C Program

Sunidhi Bansal
Updated on 22-Aug-2019 06:24:14

229 Views

Given four variables a, b, c, d with predefined values that will print the given bracket depending upon the variable used.Where variable, a for (( b for () c for )( d for ))The task is to use all the given brackets and print the balanced bracket expression, if we cannot form a balanced bracket expression then print -1. In case of multiple answers we can print any of the multiple answers which can be formed using the given brackets.ExampleInput: a = 3, b = 2, c = 4, d = 3 Output : (((((()()()()())))))()()To achieve this result we can, ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 06:07:00

246 Views

Given an array a[] 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.Like we have an array of 6 elements also containing some duplicate values i.e., {1, 3, 2, 3, 1, 2} so the result should be in form of 3 1 2.ExampleInput: a[]={4, 2, 2, 4, 1, 5, 1} Output : 2 4 5 1AlgorithmSTART ... Read More

Advertisements