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
-
Economics & Finance
C++ Articles
Page 129 of 597
Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
We are given with an array of elements ranging from values 1 to n. Some elements are repeated, and some are missing. The goal is to find the frequencies of all elements in O(n) time and O(1) extra space.InputArr[]= { 1, 2, 2, 3, 4, 4, 4, 5 }Output1→ 1, 2 → 2, 3→ 1, 4→ 3, 5→ 5Explanation − The highest element is 5, the output shows the number of times each element appears in the array.InputArr[]= { 1, 4, 4, 5, 5, 5, 5 }Output1→ 1, 2 →0, 3→ 0, 4→ 2, 5→ 4Explanation − The highest element ...
Read MoreMaximum value in an array after m range increment operations in C++
In this problem, we are given an array arr[] of N elements initialized with 0. Our task is to create a program to find the Maximum value in an array after m range increment operations in C++.Problem DescriptionOn the array, we will perform m range increment operation of the type, update[L, R, K] = Add value K, to all elements in the range.After performing m operations on the array. We need to find the element with maximum value in the array.Let’s take an example to understand the problem, InputN = 6, m = 4 Update[][] = {{1, 4, 12}, {0, ...
Read MoreMaximize the value of x + y + z such that ax + by + cz = n in C++
We are given with integers a, b, c, n. The goal is to maximize the sum of x, y and z such that ax+by+cz=n.From above formula, cz=n-(ax+by) z= (n- (ax+by))/cBy fixing x and y, calculate z using the above formula, for each x, y and z. Calculate sum and store the maximum such sum obtained.Inputn = 6, a = 3, b = 4, c = 5;Outputmaximum x+y+z is 2.Explanation − for x=2, y=0 and z=0 ax+by+cz=n.3*2+0*4+0*5=6 = nInputn = 4, a = 3, b = 1, c = 2;Outputmaximum x+y+z=4Explanation − for x=0, y=4 and z=4 ax+by+cz=n.0*3+4*1+0*2=4 = nApproach used ...
Read MoreMaximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
We are given with an array of integers. The goal is to maximize the value of expression −arr[j]-arr[i] + arr[l]-arr[k] ; i
Read MoreMaximum and Minimum Product Subsets in C++
We are given with an array of integers of size N. The goal here is to find the Maximum and Minimum Product Subsets. We will do this by taking two product variables, one for minimum product found so far minProd and other for maximum product found so far, maxProd.While traversing the array we will multiply each element with both minProd and maxProd. Also keep a check on previous maximum product, previous minimum product, current maximum product, current minimum product and the current element itself.InputArr[]= { 1, 2, 5, 0, 2 }OutputMaximum Product: 20 Minimum Product: 0Explanation − Starting from the ...
Read MoreMaximum and minimum sums from two numbers with digit replacements in C++
We are given with two positive numbers num1 and num2. The goal is to find the minimum sum and maximum sum possible of these two after a digit replacement in both of them. We are allowed to replace digits from each of numbers in both of numbers. Suppose num1 is 434 and num2 is 324 and we can replace digit 3 with 4 and digic 4 with 3. Then minimum sum will be − 333+323=656 and maximum sum will be 444+424=864.Let us understand with examples for digit replacement 3 with 4 and vice versa −Inputnum1=3224 num2=4321OutputMaximum sum is : 8645 ...
Read MoreCount Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +ny*y < n in C++
We are given a positive integer N. The goal is to count the pairs of distinct non-negative positive integers that satisfy the inequality: x*x + y*y < NWe will start from x=0 to x2 < N and y=0 to y2 < N . If any x2 + y2 < N, increase count of pairs.Inputn=4Outputdistinct pairs= 4Explanation − pairs will be (0, 0), (1, 1), (0, 1), (1, 0). All these satisfy the inequality x2 + y2 < 4Inputn=2Outputdistinct pairs= 3Explanation − pairs will be (0, 0), (0, 1), (1, 0). All these satisfy the inequality x2 + y2 < 2Approach ...
Read MoreCount how many times the given digital clock shows identical digits in C++
Suppose we have a digital clock of type HH:MM. Which shows the time in hours and minutes only. We are given a number of hours and minutes as input. The goal is to count the number of times all digits are the same. H=M.This happens 3 times a day, first at 00:00 midnight, then at 11:11 and last at 22:22. As time is represented in 24 hour format.InputInput: 12 hours 22 minutes.Output2Explanation − For times 00:00 and 11:11. Twice in 12 hours.InputInput: 48 hours 22 minutes.Output5Explanation − For times 00:00 and 11:11, 22:22 .Approach used in the below program is ...
Read MoreMaximum consecutive numbers present in an array in C++
We are given with an array of positive integers. The goal is to find the maximum number of consecutive numbers present in it. First of all we will sort the array and then compare adjacent elements arr[j]==arr[i]+1 (j=i+1), if difference is 1 then increment count and indexes i++, j++ else change count=1. Store the maximum count found so far stored in maxc.InputArr[]= { 100, 21, 24, 73, 22, 23 }OutputMaximum consecutive numbers in array : 4Explanation − Sorted array is − { 21, 22, 23, 24, 73, 100 } initialize count=1, maxcount=11. 22=21+1 count=2 maxcount=2 i++, j++ 2. 23=22+2 count=3 ...
Read MoreMaximize big when both big and small can be exchanged in C++
We are given a big object let’s say, ‘a’ and a small object let’s say, ‘b’. The choices for the object ‘a’ and ‘b’ depend upon the user. In the example below, we are taking objects to be toys which are big as well as small as per the size characteristics. The task is to calculate the maximum number of big toys that can be achieved by giving the small toys in return.Input − big_toys = 8, small_toys = 20, a = 6, b = 4Output − Maximize big when both big and small can be exchanged are − 11Explanation ...
Read More