Server Side Programming Articles - Page 2192 of 2650

Print the matrix diagonally downwards in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 08:08:58

475 Views

Given with an array of size n x n and the task is to print the matrix elements of integer type diagonally downwards.Diagonally downwards means printing the array of any size of n x n in diagonally moving downward like in the figure given below −Firstly it will print 1 and then move to 2 print it and moves down to 4 diagonally and print it and so on.ExampleInput: Matrix [3][3] = {    { 1, 2, 3 },    { 4, 5, 6 },    { 7, 8, 9 }} Output: 1 2 4 3 5 7 6 8 ... Read More

Print the arranged positions of characters to make palindrome in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:59:41

212 Views

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 in this question we have to ... Read More

Print steps to make a number in form of 2^X – 1 in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 07:49:02

153 Views

Given a number n, we have to print the steps to make the number as in form of 2^X-1 by using Xor operation.We should XOR the number with any 2^M-1, where M is chosen by you, at odd step. At even step increment the number by 1Keep performing the step until n becomes 2^X-1, and print all the stepsExampleInput: 22 Output:    Step 1 : Xor with 15    Step 2: Increase by 1    Step 3 : Xor with 7    Step 4: Increase by 1    Step 5 : Xor with 1 Input:7 Output: No Steps to be performedAlgorithmint ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 07:36:26

321 Views

We will be given a connected graph with N vertices and M edges. So we have to print the lexicographically smallest DFS of the graph starting from 1.Vertices should be numbered from 1 to NExampleInput: N = 5 M =5    edge(1, 4, arr)    edge(3, 4, arr)    edge(5, 4, arr)    edge(3, 2, arr)    edge(1, 5, arr)    edge(1, 2, arr)    edge(3, 5, arr)    edge(1, 3, arr) output: 1 2 3 4 5Instead of doing a normal DFS, first we will sort the edges associated with each vertex, so that in each turn only the ... Read More

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

Sunidhi Bansal
Updated on 22-Aug-2019 07:28:17

564 Views

We will be given a connected graph with N vertices M edges. So we have to print the lexicographically smallest BFS of the graph starting from 1.Lexicographically means in order starting from the given point till the end point is found.Vertices should be numbered from 1 to NExampleInput: N = 5 M = 5    edges(1, 4, arr)    edges(3, 4, arr)    edges(5, 4, arr)    edges(3, 2, arr)    edges(1, 5, arr)    Output: 1 4 3 2 5Instead of doing a normal BFS traversal with a simple queue on the graph, we can use a priority queue(min ... Read More

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

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

1K+ 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

7K+ 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

1K+ 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

643 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

188 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

Advertisements