Server Side Programming Articles - Page 1901 of 2646

Set Matrix Zeroes in Python

Arnab Chakraborty
Updated on 04-May-2020 06:09:38

2K+ Views

Consider we have a matrix, in that matrix if one element is 0, then make the entire row and column of that matrix to 0. The conversion will be in-place. So if the matrix is −101111111Then the output will be −000101101Let us see the steps −n := number of rows, m := number of columns, set flag := falseif mat[0, 0] = 0, then set flag := trueset row := false, and col := falsefor i in range 1 to nif mat[i, 0] = 0, then set col := True and break the loopfor i in range 1 to mif ... Read More

Print 2D matrix in different lines and without curly braces in C/C++

sudhir sharma
Updated on 03-Feb-2020 09:58:11

295 Views

Here, we will see the code that will print a 2D matrix in c/c++ programming language without using curly braces.Curly braces are separators in a programming language that are used to define separate code blocks in the program. Without curly braces defining scopes is difficult in c/c++.Let’s see the basic code and sample output to print 2D matrix.Example Live Demo#include using namespace std; int main() {    int arr[2][2] = {{12, 67},    {99, 5}};    int n = 2, m = 2;    for (int i = 0; i < m; i++){       for (int j = 0; j < n; j++){          cout

Simplify Path in Python

Arnab Chakraborty
Updated on 04-May-2020 06:08:30

1K+ Views

Suppose we have an absolute path for a file (Like Unix File system), we have to simplify it. Or in other words, we have to convert it to the canonical path. In the UNIX-style file system, a period ‘.’ refers to the current directory. And a double period ‘..’ moves the directory up a level (Parent directory). The properties of canonical paths are as follows.Path must always begin with a slash /There must be only a single slash / between two directory names.Last directory name (if it exists) must not end with a trailing /.Canonical path must be the shortest ... Read More

Minimum Path Sum in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:19:34

265 Views

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b - 1while j >= 0matrix[a, j] ... Read More

Unique Paths II in C++

Arnab Chakraborty
Updated on 04-May-2020 06:06:20

270 Views

Suppose there is a robot is located at the top-left corner of a n x m grid (n rows and m columns). The robot can only move either down side or right side at any point in time. The robot wants to reach the bottom-right corner of the grid (marked 'END in the diagram below).Some cell in the grid is marked, that will be considered as obstacles. So we have to find how many possible unique paths are there? For example if the grid is like [[0, 0, 0], [0, 1, 0], [0, 0, 0]], then the grid will be ... Read More

Unique Paths in Python

Arnab Chakraborty
Updated on 04-May-2020 06:02:28

787 Views

Suppose there is a robot is located at the top-left corner of a n x m grid (n rows and m columns). The robot can only move either down side or right side at any point in time. The robot wants to reach the bottom-right corner of the grid (marked 'END in the diagram below). So we have to find how many possible unique paths are there? For example if m = 3 and n = 2, then the grid will be like below −RoboENDThe output will be 3, So there are total 3 different ways to reach from start ... Read More

Rotate List in C++

Arnab Chakraborty
Updated on 04-May-2020 05:59:49

719 Views

Suppose we have a linked list. We have to rotate the list to the right k places. The value of k is not negative. So if the list is like [1, 23, 4, 5, NULL], and k = 2, then the output will be [4, 5, 1, 2, 3, NULL]Let us see the steps −If the list is empty, then return nulllen := 1create one node called tail := headwhile next of tail is not nullincrease len by 1tail := next of tailnext of tail := headk := k mod lennewHead := nullfor i := 0 to len – ktail ... Read More

Permutation Sequence in C++

Arnab Chakraborty
Updated on 04-May-2020 05:58:12

627 Views

Suppose the set is like [1, 2, 3, ..., n], contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get these sequence for n = 3: ["123", "132", "213", "231", "312", "321"] So if n and k are given, then return the kth permutation sequence. The n will be between 1 to 9 (inclusive) and k will be between 1 to n! (inclusive). For example if n = 3.Let us see the steps −ans := empty string, define array called candidates of size nfor i in range 0 to n – ... Read More

Spiral Matrix II in Python

Arnab Chakraborty
Updated on 04-May-2020 05:56:58

2K+ Views

Suppose we have a positive integer n, we have to generate a square matrix with n2 elements in spiral order. So if n = 5, then the matrix will be −12341213145111615610987Let us see the steps −set (row1, col1) := (0, 0) and (row2, col2) := (n, n), and create one matrix called res, then fill it with 0s, and set num := 1while num n2, then breakfor i in range row1 + 1 to row2, res[i, col2-1] = num, incase num by 1if num > n2, then breakfor i in range col2 – 2 down to col1 – 1, ... Read More

Merge Intervals in Python

Arnab Chakraborty
Updated on 04-May-2020 05:55:55

1K+ Views

Suppose we have a collection of intervals, we have to merge all overlapping intervals. So if the intervals are like [[1, 3], [2, 6], [8, 10], [15, 18]], then the intervals after merging will be [[1, 6], [8, 10], [15, 18]]. This is because there were two intervals those are overlapping, the intervals are [1, 3] and [2, 6], these are merged to [1, 6]Let us see the steps −if interval list length is 0, then return a blank listsort the interval list using quicksort mechanismstack := an empty stack, and insert intervals[0] into the stackfor i in range 1 ... Read More

Advertisements