- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
We are given an array of n positive numbers.The goal is to count the ordered pairs (i,j) such that arr[i]*arr[j] > arr[i]+arr[j] and 0<=i<j<n. Where n is the number of elements in the array.
We will traverse the array using two for loops for each number of pairs. Now calculate the sum and product of arr[i] and arr[j]. If the product is greater than sum increment count.
Let’s understand with examples.
Input − Arr[]= { 1,1,2,3 } N=4
Output − Count of pairs − 1
Explanation − Only valid pair is − (2,3)
2*3=6 > 2+3=5
Input − Arr[]= { 2,2,2 } N=3
Output − Count of pairs − 0
Explanation − both 2*2 and 2+2 is 4. No pairs where product>sum
Approach used in the below program is as follows
We take an integer array arr[] initialized with positive numbers.
Take a variable n which stores the length of Arr[].
Function countPairs(int arr[], int n) takes an array, its length as input and prints the count of pairs with product>sum.
Traverse array using two for loops for each element of the pair.
Outer Loop from 0<=i<n-1, inner loop i<j<n
Check if arr[i]*arr[j]>arr[i]+arr[j]. Increment count if true.
At the end of all loops count will have a total number of pairs that have product > sum
Return count as result.
Example
#include <bits/stdc++.h> #include <math.h> using namespace std; int countPairs(int arr[], int n){ int count=0; int sum=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if(arr[i]*arr[j]>arr[i]+arr[j]) //condition { count++; } } } return count; } int main(){ int arr[] = { 1,2,3,2 }; int len = sizeof(arr) / sizeof(int); cout<<"Count of number of pairs :"<<countPairs(arr, len); return 0; }
Output
If we run the above code it will generate the following output −
Count of number of pairs :2
- Related Articles
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Rearrange an array such that arr[i] = i in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
