In this problem, we are given an integer N. Our task is to create a program that will find the maximum sum after repeatedly dividing N by a divisor in C++.Program description − we will divide the number N recursively until it becomes one and then sum up all the divisors and find the maximum of all divisors.Let’s take an example to understand the problem, Input − N = 12Output − 22Explanation − let’s divide the number recursively and find the sum.Division 1: 12/2 = 6 Division 2: 6/2 = 3 Division 3: 3/3 = 1 Sum = 12+6+3+1 = ... Read More
In this problem, we are given a matrix. Our task is to create a program that finds the maximum sum of the hourglass in a matrix in C++.Program description − Here, we will find the maximum sum of all hourglasses that can be created for the given matrix elements.Hour glass is a 7 element shape made in the matrix in the following form, X X X X X X XLet’s take an example to understand the problem, Input −array ={ {2 4 0 0} {0 1 1 0} {4 2 1 0} {0 3 0 ... Read More
In this problem, we are given an array arr. Our task is to create a program that will find the maximum sum of i*arr[i] among all rotations of the given array in C++.Program description − Here, we will find the maximum sum of out the sums of all the elements of the array multiplied by their index {i * arr[i]} in the rotation.Let’s take an example to understand the problem, Input − array arr = {4, 8, 1, 5}Output − 37Explanation −All rotations with the sum of i*arr[i] : {4, 8, 1, 5} = 4*0 + 8*1 + 1*2 + ... Read More
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
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
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
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
In this problem, we are given an array of n integers. Our task is to create a program that will find the maximum value of |arr[i]-arr[j]| + |i-j|.Let’s take an example to understand the problem, Input − array = {4, 1, 2}Output − 4Explanation −|arr[0] - arr[1]|+|0-1| = |4-1| + |-1| = 3+1 = 4 |arr[0] - arr[2]|+|0-2| = |4-2| + |-2| = 2+2 = 4 |arr[1] - arr[2 ]|+|1-2| = |1-2| + |1-2| = 1+1 = 2To solve this problem, a simple approach will be using the brute force approach which will be using two loops and finding the ... Read More
In this problem, we are given an array of positive integers and a number k. Our task is to create a program that will find the maximum sum of two nonoverlapping subarrays of the given size(k).So, basically we have two print two non-overlapping (distinct) subarrays that have the maximum sum and are of size k.Let’s take an example to understand the problem, Input −array = {7, 1, 6, 9, 2} , k = 2Output −{7, 1} , {6, 9}Explanation −all subarrays of size 2. {7, 1} : sum = 7+1 = 8 {1, 6} : sum = 1+6 = 7 ... Read More
In this problem, we are given an array of integers and a integers K. our task is to create a program that will find the maximum unique element in every subarray of size K with no duplicates.Let’s take an example to understand the problem, Input −array = {4, 1, 1, 3, 3} k = 3Output −4 3 1Explanation −Sub-array of size 3 Subarray {4, 1, 1}, max = 4 Subarray {1, 1, 3}, max = 3 Subarray {1, 3, 3}, max = 1To solve this problem, one simple method is to run two loops and create subarrays and find distinct ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP