Write a function named findingpair that takes a value to search for and a list o
ID: 3870951 • Letter: W
Question
Write a function named findingpair that takes a value to search for and a list of pairs. This function must search for the given value as the first element of a pair in the list. If such a pair is found, then it is returned (as a SOME option). If no such pair is found, then the NONE option is returned. For example, -findingpair 1 [(2, 3), (4, 1), (1, 9)]: SOME (1,9) - findingpair "key SOME ("key",3) -findingpair "bob" [("three", 3), ("shoe", 1), ("key", 9)]; [("key", 3), ("key", 1), ("key", 9)1: NONEExplanation / Answer
# include <stdio.h>
# define bool int
void quickSort(int *, int, int);
bool hasArrayTwoCandidates(int A[], int arr_size, int sum)
{
int l, r;
quickSort(A, 0, arr_size-1);
l = 0;
r = arr_size-1;
while (l < r)
{
if(A[l] + A[r] == sum)
return 1;
else if(A[l] + A[r] < sum)
l++;
else // A[i] + A[j] > sum
r--;
}
return 0;
}
int main()
{
int A[] = {1, 4, 45, 6, 10, -8};
int n = 16;
int arr_size = 6;
if( hasArrayTwoCandidates(A, arr_size, n))
printf("Array has two elements with sum 16");
else
printf("Array doesn't have two elements with sum 16 ");
getchar();
return 0;
}
void exchange(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition(int A[], int si, int ei)
{
int x = A[ei];
int i = (si - 1);
int j;
for (j = si; j <= ei - 1; j++)
{
if(A[j] <= x)
{
i++;
exchange(&A[i], &A[j]);
}
}
exchange (&A[i + 1], &A[ei]);
return (i + 1);
}
void quickSort(int A[], int si, int ei)
{
int pi;
if(si < ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi - 1);
quickSort(A, pi + 1, ei);
}
}
#include <stdio.h>
#define MAX 100000
void printPairs(int arr[], int arr_size, int sum)
{
int i, temp;
bool binMap[MAX] = {0};
for (i = 0; i < arr_size; i++)
{
temp = sum - arr[i];
if (temp >= 0 && binMap[temp] == 1)
printf("Pair with given sum %d is (%d, %d) n",
sum, arr[i], temp);
binMap[arr[i]] = 1;
}
}
int main()
{
int A[] = {1, 4, 45, 6, 10, 8};
int n = 16;
int arr_size = sizeof(A)/sizeof(A[0]);
printPairs(A, arr_size, n);
getchar();
return 0;
}
int getPairsCount(int arr[], int n, int sum)
{
unordered_map<int, int> m;
for (int i=0; i<n; i++)
m[arr[i]]++;
int twice_count = 0;
for (int i=0; i<n; i++)
{
twice_count += m[sum-arr[i]];
if (sum-arr[i] == arr[i])
twice_count--;
}
return twice_count/2;
}
int main()
{
int arr[] = {1, 5, 7, -1, 5} ;
int n = sizeof(arr)/sizeof(arr[0]);
int sum = 6;
cout << "Count of pairs is "
<< getPairsCount(arr, n, sum);
return
// C/C++ program to find a pair with the given difference
#include <stdio.h>
// The function assumes that the array is sorted
bool findPair(int arr[], int size, int n)
{
// Initialize positions of two elements
int i = 0;
int j = 1;
// Search for a pair
while (i<size && j<size)
{
if (i != j && arr[j]-arr[i] == n)
{
printf("Pair Found: (%d, %d)", arr[i], arr[j]);
return true;
}
else if (arr[j]-arr[i] < n)
j++;
else
i++;
}
printf("No such pair");
return false;
}
int main()
{
int arr[] = {1, 8, 30, 40, 100};
int size = sizeof(arr)/sizeof(arr[0]);
int n = 60;
findPair(arr, size, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.