Server Side Programming Articles - Page 1682 of 2646

Count elements such that there are exactly X elements with values greater than or equal to X in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:07:40

629 Views

We are given with an array of integers. The goal is to find the count of elements in the array that satisfy the following condition −For each element the count of numbers greater than or equal to it present in the array should be exactly equal to it. Excluding the element itself. If element is X then array has exactly X numbers which are greater or equal to X. (Excluding the element).InputArr[]= { 0, 1, 2, 3, 4, 9, 8 }OutputElements exactly greater than equal to itself : 1Explanation − Elements and numbers >= to it −Arr[0]: 6 elements are ... Read More

Count elements less than or equal to a given value in a sorted rotated array in C++

Ravi Ranjan
Updated on 10-Jun-2025 17:50:57

589 Views

A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated. Example Here is an example of counting elements less than or equal to a given value in the sorted and rotated array: Case 1 The given array is: 30 40 50 10 20 Given value = 25 Output Count: 2 (10, 20) Case 2 The given array is: 10 20 30 40 ... Read More

Maximum count of pairs which generate the same sum in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:04:40

769 Views

We are given with an array of integers. The goal is to find the maximum number of pairs in the array that when added produce the same sum. We have to find the maximum count of such pairs.InputArr[]= { 1, 2, 3, 4, 2 }OutputMaximum count of pairs with same sum : 3Explanation − Sum of pairs of numbers −{1, 2}, {1, 2} Sum:3 {1, 3}, {2, 2} Sum:4 {1, 4}, {2, 3}, {3, 2} Sum:5 {2, 4} Sum:6 {3, 4} Sum:7 Maximum count of pairs with same sum is 3 ( for sum = 5 )InputArr[]= { 5, 3, ... Read More

Maximum count of equal numbers in an array after performing given operations in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:02:35

1K+ Views

We are given with an array of integers. The goal is to find the maximum numbers in array that are equal after performing given operations −Select two elements a[i] and a[j] such that i != j andIncrement a[i] and decrement a[j] ( a[i]++, a[j]-- )We will take the sum of the array and divide it by the number of elements. If N is size of array thenIf sum is divisible by N then equal numbers will also be N otherwise Equal numbers will be N-1.InputArr[]= { 1, 2, 3 }OutputMaximum count of equal numbers : 3Explanation − After first step ... Read More

Maximum consecutive numbers present in an array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:01:47

2K+ Views

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 More

Maximum array sum that can be obtained after exactly k changes in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:59:01

717 Views

We are given with an array of positive and negative integers and a number K. The task is to find the maximum sum of the array after K changes in it’s elements. The single change operation here multiplies the single element by -1.Approach used will be to convert every negative number to positive. If there are N negative numbers then, for this we will sort the array −If NK then change the sign of K negative numbers and add the array. Sum will be maximum.InputArr[]= { 0, -2, 6, 4, 8, 2, -3 } K=4OutputMaximum array sum is : 25Explanation ... Read More

Maximum and minimum sums from two numbers with digit replacements in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:57:06

377 Views

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 More

Maximum and Minimum Product Subsets in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:54:51

427 Views

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 More

Count Balanced Binary Trees of Height h in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:52:45

428 Views

We are given with the height H of a binary tree. The goal is to find the number/count of balanced Binary Trees of given height.A binary tree − is a tree data structure in which each node has at most two children, which are the left child and the right child.Height-balanced binary tree − is defined as a binary tree in which the depth of the two subtrees of every node differ by 1 or 0 only. That is the height of the left subtree and the right subtree at every node has a maximum difference of 1.Following figure contains ... Read More

Sets of pairs in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:50:15

8K+ Views

Set in C++ is an associative container and contains unique elements. All the elements once added to a specific cannot be modified. One can only remove and add elements in order to change them.Pair is defined under header and is used to couple together two pair values. The pair can have values of different or same type. The class has member functions first() and second() to individually access the values in a pair.The order of pair elements is fixed (first, second). We can use pair to combine two heterogeneous values of different types.To access any element we use variable_name.first ... Read More

Advertisements