
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
Found 7197 Articles for C++

241 Views
Suppose we have a number n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 312, then the output will be 3, as 312 is 100111000 in binary and there are 3 consecutive 1s.To solve this, we will follow these steps −ret := 0, len := 0for initialize i := 0, when i < 32, update (increase i by 1), do:if n/2 is odd, then(increase len by 1)Otherwiselen := 0ret := maximum of ret and lenreturn retLet us see the following implementation to get better ... Read More

613 Views
Suppose we have a binary tree; we have to check whether its height is balanced or not. We know that for a height balanced tree, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or 1.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs(), this will take node, if node is null, then −return 0l := 1 + dfs(left of node)r := 1 + dfs(right of node)if |l - r| > ... Read More

356 Views
Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.We have to find the total number of mutation groups we can generate.So, if the input is like genes = ... Read More

122 Views
Suppose there is an array, and that is sorted, consider that array is rotated at some pivot, that is unknown to us. So we have to find the maximum from that rotated array. So if the array is like[3, 4, 5, 1, 2], then the output will be 5.To solve this, we will follow these steps −low := 0 and high := last index of array, n := size of array, ans := 0while low arr[mid], then ans := maximum of ans and arr[mid], high := mid – 1else if low = mid, then ans := maximum of ans ... Read More

509 Views
Suppose we have two words S and T, we have to find the minimum number of operations needed to convert from S to T. The operations can be of three types, these areinsert a character, delete a characterreplace a character.So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.To solve this, we will follow these steps −n := size of s, m := size of t, create an array dp of size n + 1for i in range 0 to ndp[i] := new array of size m + 1for j in range 0 to m:dp[i, ... Read More

167 Views
In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.Example Live Demo#include using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) { int MS[N]; MS[N - 1] = arr[N - 1]; for (int i = N - 2; i >= 0; i--) { if (i ... Read More

193 Views
In this tutorial, we will be discussing a program to find maximum sum subarray such that start and end values are same.For this we will be provided with an array containing integers. Our task is to find the subarray with the maximum sum such that the elements are both its ends are equal.Example Live Demo#include using namespace std; //finding the maximum sum int maxValue(int a[], int n) { unordered_map first, last; int pr[n]; pr[0] = a[0]; for (int i = 1; i < n; i++) { pr[i] = pr[i - 1] + a[i]; ... Read More

211 Views
In this tutorial, we will be discussing a program to find maximum sum rectangle in a 2D matrix.For this we will be provided with a matrix. Our task is to find out the submatrix with the maximum sum of its elements.Example Live Demo#include using namespace std; #define ROW 4 #define COL 5 //returning maximum sum recursively int kadane(int* arr, int* start, int* finish, int n) { int sum = 0, maxSum = INT_MIN, i; *finish = -1; int local_start = 0; for (i = 0; i < n; ++i) { sum += arr[i]; ... Read More

118 Views
In this tutorial, we will be discussing a program to find maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array.For this we will be provided with an array containing N intergers and a value K. Our task is to find the maximum sum of the subsequence including elements not nearer than K.Example Live Demo#include using namespace std; //returning maximum sum int maxSum(int* arr, int k, int n) { if (n == 0) return 0; if (n == 1) return arr[0]; ... Read More

287 Views
In this tutorial, we will be discussing a program to find maximum sum path in a matrix from top to bottom.For this we will be provided with a matrix of N*N size. Our task is to find the maximum sum route from top row to bottom row while moving to diagonally higher cell.Example Live Demo#include using namespace std; #define SIZE 10 //finding maximum sum path int maxSum(int mat[SIZE][SIZE], int n) { if (n == 1) return mat[0][0]; int dp[n][n]; int maxSum = INT_MIN, max; for (int j = 0; j < n; j++) ... Read More