

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find all pairs (a, b) in an array such that a % b = k in C++
Suppose we have an array A, from that array, we have to get all pairs (a, b) such that the a%b = k. Suppose the array is A = [2, 3, 4, 5, 7], and k = 3, then pairs are (7, 4), (3, 4), (3, 5), (3, 7).
To solve this, we will traverse the list and check whether the given condition is satisfying or not.
Example
#include <iostream> using namespace std; bool displayPairs(int arr[], int n, int k) { bool pairAvilable = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i] % arr[j] == k) { cout << "(" << arr[i] << ", "<< arr[j] << ")"<< " "; pairAvilable = true; } } } return pairAvilable; } int main() { int arr[] = { 2, 3, 4, 5, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; if (displayPairs(arr, n, k) == false) cout << "No paira found"; }
Output
(3, 4) (3, 5) (3, 6) (3, 7) (7, 4)
- Related Questions & Answers
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Find largest d in array such that a + b + c = d in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Count of all possible values of X such that A % X = B in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Find FIRST & FOLLOW for the following Grammar. S → A a A | B b B A → b B B → ε
- Find three element from different three arrays such that that a + b + c = sum in Python
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Larger of a^b or b^a in C++
Advertisements