Untitled
unknown
plain_text
2 years ago
918 B
9
Indexable
You are given an array A of size n and an integer b. You have to find outall pairs of elements from array A whose sum is equal to b and print them. Inthe first line, you are given 2 integers, n (size of array) and b. In the next line,you are given n integers (the elements of array A). You have to output all pairsof elements from array A whose sum is equal to b. Output each new pair in anew line.Example:Input:5 5 where n = 5 b = 51 2 3 4 5Output1 42 3
#include <stdio.h>
void findPairs(int arr[], int n, int b) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == b) {
printf("%d %d\n", arr[i], arr[j]);
}
}
}
}
int main() {
int n, b;
scanf("%d %d", &n, &b);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findPairs(arr, n, b);
return 0;
}Editor is loading...