Programming Articles - Page 2113 of 3363

Spiral Matrix in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:09:34

5K+ Views

Suppose we have a matrix and we have to print the matrix elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion. So if the matrix is like −123456789101112131415161718Then the output will be like [1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 15 16]To solve this, we will follow these steps −currRow := 0 and currCol := 0while currRow and ... Read More

Pow(x, n) in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:51:18

2K+ Views

Suppose we have two inputs x and n. x is a number in range -100.0 to 100.0, and n is a 32-bit signed integer. We have to find x to the power n without using library functions.So if the given inputs are x = 12.1, n = -2, then output will be 0.00683To solve this, we will follow these steps −power := |n| and res := 1.0while power is not 0if last bit of power is 1, then res := res * xx := x * xif n < 0return 1 / resreturn resExample(Python)Let us see the following implementation to ... Read More

Rotate Image in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:45:13

479 Views

Suppose we have one 2D matrix, that is representing one image. We have to rotate this image to 90 degrees clockwise. So if the image is like157963213Then the output will be291165337To solve this, we will follow these steps −Consider temp_mat = [], col := length of matrix – 1for col in range 0 to length of matrixtemp := []for row in range length of matrix – 1 down to -1add matrix[row, col] in tempadd temp into temp_matfor i in range 0 to length of matrixfor j in range 0 to length of matrixmatrix[i, j] := temp_mat[i, j]Example(Python)Let us see the ... Read More

Permutations II in C++

Arnab Chakraborty
Updated on 27-Apr-2020 13:26:05

417 Views

Suppose we have a collection of distinct integers; we have to find all possible permutations. Now if the array stores the duplicate elements, then ignore that state which is looking similar. So if the array is like [1, 1, 3], then the result will be [[1, 1, 3], [1, 3, 1], [3, 1, 1]]To solve this, we will follow these steps −We will use the recursive approach, this will make the list, index. Index is initially 0if index = size of the list then insert list into res array, and returnfor i in range index to length of given list ... Read More

Maximum sum subarray removing at most one element in C++

Narendra Kumar
Updated on 03-Jun-2020 07:49:56

222 Views

In this problem, we are given an array. Our task is to create a program that will find maximum sum subarray removing at most one element in c++.Basically, we need to find one element which when removed provides the maximum sum for the elements remaining in the array.Let’s take an example to understand the problem, Input − array = {5, 1, 9, 2, -1, 7}Output − 24Explanation − we have removed -1 from the array and the sum became the maximum of all possible outcomes.One solution to this problem will be finding the minimum element of the array and then ... Read More

Maximum sum subarray having sum less than or equal to given sums in C++

Narendra Kumar
Updated on 03-Jun-2020 07:52:32

760 Views

In this problem, we are given an array and a sum. Our task is to create a program that will find the maximum sum subarray having a sum less than or equal to given sums in c++.We have to find the subarray of any length less than or equal to n whose sum is less than or equal to the given sum.Let’s take an example to understand the problem, Input − array = {3, 5, 1, 8, 2, 9}, sum = 25Output − 25Explanation − The subarrays with sum less than or equal to 25 are {5, 1, 8, 2, ... Read More

Maximum Sum Path in Two Arrays in C++

Narendra Kumar
Updated on 30-Jan-2020 12:47:36

674 Views

Problem statementGiven two sorted arrays such the arrays may have some common elements. Find the sum of the maximum sum path to reach from beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements. Note that the common elements do not have to be at same indexes.Expected time complexity is O(m+n) where m is the number of elements in arr1[] and n is the number of elements in arrs2[]ExampleIf given input is then output is 35 arr1[] = {2, 3, 7, 10, 12} ar2[] = ... Read More

Maximum sum path in a matrix from top to bottom in C++

Narendra Kumar
Updated on 30-Jan-2020 12:43:02

312 Views

Problem statementConsider a n*n matrix. Suppose each cell in the matrix has a value assigned. We can go from each cell in row i to a diagonally higher cell in row i+1 only [i.e from cell(i, j) to cell(i+1, j-1) and cell(i+1, j+1) only]. Find the path from the top row to the bottom row following the aforementioned condition such that the maximum sum is obtainedExampleIf given input is: {    {5, 6, 1, 17},    {-2, 10, 8, -1},    { 3, -7, -9, 4},    {12, -4, 2, 2} }the maximum sum is (17 + 8 + 4 ... Read More

Maximum sum of non-leaf nodes among all levels of the given binary tree in C++

Narendra Kumar
Updated on 03-Jun-2020 07:55:36

239 Views

In this problem, we are given a binary tree. Our task is to create a program that will find the maximum sum of non-leaf nodes among all levels of the given binary tree in c++.Problem description − we will calculate the sum of all non-leaf nodes of the tree and every level and then print the maximum sum.Let’s take an example to understand the problem, Input −Output − 9Explanation − the sum of non-leaf nodes at each level −Level 1: 4 Level 2: 1+2 = 3 Level 3: 9 (4, 7 are leaf nodes) Level 4: 0To solve this problem, ... Read More

Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++

Narendra Kumar
Updated on 03-Jun-2020 07:57:17

263 Views

In this problem, we are given an array and an integer k. Our task is to create a program that will find the maximum sum of lengths of non-overlapping subarrays with k as the max element in c++.Problem description − here, we have an array and an integer k. We have to find all possible non-overlapping sub-arrays that can be created from this array. And sum the lengths of all the subarrays created.Let’s take an example to understand the problem, Input − array = {3, 7, 1, 2, 3, 1, 6, 3, 2, 5} k = 3Output − 7Explanation − ... Read More

Advertisements