
- 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
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 Articles
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Find largest d in array such that a + b + c = d in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd 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++
- Write five pairs of integers(a,b) such that a=b=-5 where one such pair (-25,5)because -25+5=-5
- Find number of pairs in an array such that their XOR is 0 using C++.
- Count all pairs of an array which differ in K bits in C++
- Write five pairs of integers $(a, b)$ such that $a ÷ b=-3$. One such pair is $(6, -2)$ because $6 ÷ (-2) = (-3)$.
- K-diff Pairs in an Array in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- Count of pairs (x, y) in an array such that x < y in C++

Advertisements