Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by sudhir sharma
975 articles
Maximum Sum Increasing Subsequence using DP in C++ program
In this problem, we are given an array arr[] of size n. Our task is to create a program to find the maximum Sum Increasing Subsequence using DP in C++.Problem Description − To find the maximum sum increasing subsequence, we will be creating a subsequence in which the next element is greater than the current element.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 3, 6, 5, 9}Output20ExplanationIncreasing subsequence with maximum sum: {2, 3, 6, 9} = 2 + 3 + 6 + 9 = 20Solution ApproachTo solve the problem using a dynamic Program Approach. We will ...
Read MoreFind number of subarrays with even sum in C++
In this problem, we are given an array arr[] consisting of N elements. Our task is to find the subarray with even sum.Let’s take an example to understand the problem,Inputarr[] = {2, 1, 3, 4, 2, 5}Output28ExplanationThe subarrays are −{2}, {4}, {2}, {2, 4}, {2, 2}, {1, 3}, {1, 5}, {3, 5}, {4, 2}, {2, 1, 3}, {2, 1, 5}, {2, 3, 5}, {2, 4, 2}, {1, 3, 4}, {1, 3, 2}, {1, 4, 5}, {1, 2, 5}, {3, 4, 5}, {3, 2, 5}, {2, 1, 3, 4}, {2, 1, 3, 2}, {2, 3, 4, 5}, {2, 3, 2, 5}, {2, 4, 2, 5}, {1, 3, 4, 2}, {1, 4, 2, 5}, {3, 4, 2, 5}, {2, 1, 3, 4, 2}, {2, 1, 3, 4, 2, 5}Solution ApproachA simple solution to the problem is using the direct method which is by calculating all subarray and their sums. And the increase count for the subarray with even sum. And at last return count.Program to illustrate the working of our solution,Example#include using namespace std; int countEvenSumSubArray(int arr[], int n){ int evenSumCount = 0, sum = 0; for (int i=0; i
Read MoreFind maximum possible stolen value from houses in C++
In this problem, we are given n houses with some values in them. Our task is to Find the maximum possible stolen value from houses.Problem Description − We have an array houses[] that consist of the values that are in each house. A thief robs the houses but he cannot steal from two adjacent houses as neighbours know about the theft. We need to find the maximum possible value that can be stolen by the thief from the house without getting caught.Let’s take an example to understand the problem, Inputhouses[] = {5, 2, 1, 6, 7, 9, 4, 3}Output23ExplanationThe max ...
Read MoreFind maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++
In this problem, we are given an array arr[] consisting of n elements. We need to Find maximum value of Sum( i*arr[i]) with only rotations on a given array allowed. For find the maximum sum of (i*arr[i]), we can perform any number of rotations.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 7, 2}Output43ExplanationWe will rotate the array once to get the maximum value, After rotation array will be {2, 4, 1, 3, 7}Sum = 0*2 + 1*4 + 2*1 + 3*3 + 4*7 = 0 + 4 + 2 + 9 + 28 = 43Solution ...
Read MoreFind maximum XOR of given integer in a stream of integers in C++
In this problem, we are given Q queries each of which is one of the following type, Type 1 − insertion (1, i) to add the element with value i, in your data structure.Type 2 − findXOR (2, i), to find the XOR of all elements of the data structure with the element i.The data structure should contain only 1 element initially which will be 0.Let’s take an example to understand the problem, InputQueries: (1, 9), (1, 3), (1, 7), (2, 8), (1, 5), (2, 12)Output15 15ExplanationSolving each query, (1, 9) => data structure => {9} (1, 3) => data ...
Read MoreFind Maximum XOR value of a sub-array of size k in C++
In this problem, we are given an array arr[] consisting of n elements and an integer k. Our task is to find the Maximum XOR value of a sub-array of size k.Let’s take an example to understand the problem, Inputarr[] = {3, 1, 6, 2 ,7, 9} k = 3Output12ExplanationAll subarray and the xor of all element of size k, {3, 1, 6} = 4 {1, 6, 2} = 5 {6, 2, 7} = 3 {2, 7, 9} = 12Solution ApproachA simple solution to the problem is by using two loops. One to iterate over the array and other to ...
Read MoreC program to Replace a word in a text by another given word
In this program, we have given three strings txt, oldword, newword. Our task is to create a C program to replace a word in a text by another given word.The program will search for all the occurrences of the oldword in the text and replace it with newword.Let’s take an example to understand the problem −Inputtext = “I am learning programming” oldword = “learning” newword = “practicing”Output“I am practicing programming”To solve this problem, we will first find the number of occurrence of the oldword in the string and then create a new string which will store the text with replaced ...
Read MoreWrite a Program to Find the Maximum Depth or Height of a Tree in C++
In this problem, we are given a binary tree. Our task is to write a program to find the maximum depth or height of the given tree.Let’s take an example to understand the problem, The height of the tree is 3.To find the maximum height of a tree, we will check the heights of its left and right subtree and add one to the maximum of both. This is a recursive process that will continue this the last node is of the tree is found and one is added progressively to find the height of the sub-trees.Above example solved using ...
Read MoreWrite a program to reverse digits of a number in C++
A program to reverse digits of a number will interchange the position of the digits and reverse there order.Let’s suppose be a number abcde the reverse will be edcba.Let’s take an example to understand the problem, Inputn = 786521Output125687To reverse digits of the number, we will take each digit of the number from MSB(unit digit) and add it to reverse number variable, after this divide the original number by 10 and the reverse_number is multiplied by 10. This will be done until the number becomes 0.This repetitive process can be accomplished by two methods, iteration, and recursion, we will create ...
Read MoreFind Next Sparse Number in C++
In this problem, we are given an integer value N. our task is to create a program to find the next spares Number.Sparse Number is a special type of number whose binary conversion does not contain any adjacent 1’s.Example: 5(101) , 16(10000)Problem Description − For the given number N, we need to find the smallest number greater than N which is a sparse number.Let’s take an example to understand the problem, InputN = 7Output8ExplanationThe binary of 8 is 1000 which makes it the smallest sparse number greater than n.Solution ApproachA simple solution to the problem is checking for all numbers ...
Read More