Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2469 of 3366
154 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
322 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
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
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
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
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
644 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
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
217 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
158 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