
- 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 of pairs (x, y) in an array such that x < y in C++
We are given an integer array and the task is to count the total number of pairs (x, y) that can be formed using the given array values such that the integer value of x is less than y.
Input − int arr[] = { 2, 4, 3, 1 }
Output − Count of pairs (x, y) in an array such that x < y are − 6
Explanation −
X | Y | X < Y |
2 | 4 | true |
2 | 3 | true |
2 | 1 | false |
4 | 3 | false |
4 | 1 | false |
4 | 2 | false |
3 | 2 | false |
1 | 2 | true |
3 | 4 | true |
1 | 4 | true |
3 | 1 | false |
1 | 3 | false |
Approach used in the below program is as follows
Input an array of integer elements to form an pair
Calculate the size of an array pass the data to the function for further processing
Create a temporary variable count to store the pairs having x less than y
Start loop FOR from i to 0 till the size of an array
Inside the loop, start another loop FOR from j to 0 till the size of an array
Inside the loop, check IF arr[i] < arr[j] == TRUE then increment the count by 1
Return the count
Print the result
Example
#include <iostream> using namespace std; int X_Less_Y(int arr[],int size){ int count = 0; for (int i = 0; i < size; i++){ for (int j = 0; j < size; j++){ if (arr[i] < arr[j]){ count++; } } } return count; } int main(){ int arr[] = { 2, 4, 3, 1 }; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs (x, y) in an array such that x < y are: "<<X_Less_Y(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs (x, y) in an array such that x < y are: 6
- Related Articles
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Solve the following pairs of equations:\( \frac{2 x y}{x+y}=\frac{3}{2} \)\( \frac{x y}{2 x-y}=\frac{-3}{10}, x+y ≠ 0,2 x-y ≠ 0 \)
- Solve the following pairs of equations:\( x+y=3.3 \)\( \frac{0.6}{3 x-2 y}=-1,3 x-2 y ≠ 0 \)
- Verify : (i) \( x^{3}+y^{3}=(x+y)\left(x^{2}-x y+y^{2}\right) \)(ii) \( x^{3}-y^{3}=(x-y)\left(x^{2}+x y+y^{2}\right) \)
- If \( 3^{x}=5^{y}=(75)^{z} \), show that \( z=\frac{x y}{2 x+y} \).
- Verify that |x+y|
- Solve the following pairs of equations:\( 43 x+67 y=-24 \)\( 67 x+43 y=24 \)
- Factorize:$4(x - y)^2 - 12(x -y) (x + y) + 9(x + y)^2$
- Factorize:\( x^{2}+y-x y-x \)
- Solve the following pairs of equations:\( 4 x+\frac{6}{y}=15 \)\( 6 x-\frac{8}{y}=14, y ≠ 0 \)
- Find larger of x^y and y^x in C++
- If \( x+y+z=0 \), show that \( x^{3}+y^{3}+z^{3}=3 x y z \).
