
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count maximum elements of an array whose absolute difference does not exceed K in C++
We are given an array let’s say, arr[] of integer elements of any given size and a positive integer k and the task is to calculate the count of those element pairs whose absolute difference doesn’t exceed the given integer k.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
For example
Input − int arr[] = {2, 3, 6, 12, 14}, k= 5 Output − count is : 3
Explanation − the pairs with maximum absolute difference not more then k i.e. 5 in this example the pairs so formed are: (2, 3), (2, 6), (3,6) i.e {2, 3, 6} therefore the count is 3.
Input − int arr[] = {2, 3, 6, 12, 14}, k= 10 Output − count is : 4
Explanation − the pairs with maximum absolute difference not more then k i.e. 10 in this example the pairs so formed are: (2, 3), (2, 6), (3,6), (2, 12), (3, 12), (6, 12) i.e {2, 3, 6, 12} therefore the count is 4 as the maximum elements are 4.
Input − int arr[] = {2, 3, 6, 12, 14}, k= 0 Output − count is : 0
Explanation − As there is no pair with difference as 0 so count is 0.
Approach used in the below program is as follows
Create an array let’s say, arr[] and a positive integer k
Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.
Take a temporary variable that will store the count of elements.
Declare two temporary variables lets say, first and last and initialise with 0
Call the sort method to sort an array and pass array and size of an array as an argument to the function.
Start loop for i to 0 and i less than size of an array
Inside the loop, start while j<size AND arr[j] <= arr[i] + k
Inside while, check IF count < j-i then set count to j - i and first to i and last to j
Return the count
Print the result.
Example
#include <iostream> #include <algorithm> using namespace std; int countmax(int arr[], int size, int K){ int result = 0; int i = 0, j = 0; int beg = 0; int end = 0; // Sort the array sort(arr, arr + size); // Find max elements for (i = 0; i < size; i++) { // Count all elements which are in the range while (j < size && arr[j] <= arr[i] + K) j++; if (result < (j - i)) { result = (j - i); beg = i; end = j; } } // Return the max count return result; } // main function int main(){ int arr[] = { 2, 3, 6, 12, 14 }; int size = sizeof(arr) / sizeof(arr[0]); int K = 5; cout <<"count is "<<countmax(arr, size, K) << endl; return 0; }
Output
If we run the above code we will get the following output −
count is 3
- Related Articles
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Count of subarrays whose maximum element is greater than k in C++
- Maximum difference between the group of k-elements and rest of the array in C
- Find the node whose absolute difference with X gives maximum value in C++
- MongoDB query for documents whose array elements does not have a specific value
- Find k maximum elements of array in original order in C++
- Count the number of elements in an array which are divisible by k in C++
- Count numbers < = N whose difference with the count of primes upto them is > = K in C++
- Absolute sum of array elements - JavaScript
- Count pairs in array whose sum is divisible by K in C++
- JavaScript Program to Find k maximum elements of array in original order
