Need in C Given a sorted array of n distinct values as well

**Need in C

Given a sorted array of n distinct values as well as a target value T, determine in O(n) time whether or not there exist two distinct values in the array that sum to T. For example, if the array contained 3, 5, 6, 7, and 9 and T = 14, then you should report that a pair of values in the array exists that adds up to T. If T = 17, you should indicate that no such pair exists.

1.1 Inputs -There are two basic inputs, the input file name, passed via the command line, and the input file data defined below.

1.1.1 Command Line arguments-The input file name will be input as follows:

• targetSum filename.ext

In the event that the input file is not available or there is an error finding the file, an appropriate error message shall be displayed. Use the example below for guidance. • targetSum ERROR: File “bogusFilename” not found

1.1.2 Input File fields-The first line of the file will have a single positive integer k, representing the number of test cases in the file. Each test case will follow. The first line of each test case will have two space separated positive integers: n(n 106 ), the size of the array, and 1 t(t 109 ), the target for the input case. The following n distinct positive integers will be listed in ascending order, separated by white space. The input file, shown below, will NOT include comments. The comments and line spacing shown below are for clarification.

2 //Number of test cases

5 14 //First digit is the count of numbers in the list

//Second digit is the target sum for this list

3 5 6 //First three numbers of the 5 in the list

7 9 //Fourth and fifth numbers of the list

3 11 //The count of numbers and the target sum for this list

1 3 6 //Three of three numbers in the list

The actual file’s data would be as shown below:

2

5 14

3 5 6

7 9

3 11

1 3 6

The output of the program will be a simple YES or NO for however many test cases were tested. So for the data shown above the outputs are shown below.

YES
NO

Solution

// C code

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100000

void findtargetPair(int a[], int n, int target)
{
int i, t;
bool map[MAX] = {0};
for (i = 0; i < n; i++)
{
t = target - a[i];
if (t >= 0 && map[t] == 1)
{
    printf(\"YES\ \");
    return;
}
map[a[i]] = 1;
}

printf(\"NO\ \");
}

int main (int argc, char const *argv[])
{
FILE *infile;

infile = fopen (argv[1],\"r\");
if (infile == NULL || argc < 2)
{
fprintf(stderr, \"\ ERROR: File “bogusFilename” not found\ \ \");
exit (1);
}

int k,n,target;
int i;
while (1)
{
        fscanf(infile,\"%d\",&k);
        while(k--)
        {
            fscanf(infile,\"%d\",&n);
            fscanf(infile,\"%d\",&target);

            int a[n];
            for (i = 0; i < n; ++i)
            {
               fscanf(infile,\"%d\",&a[i]);               
            }

            findtargetPair(a,n,target);
        }
       
        if(feof(infile))
        {
   break;
   }
}
return 0;
}

**Need in C Given a sorted array of n distinct values as well as a target value T, determine in O(n) time whether or not there exist two distinct values in the
**Need in C Given a sorted array of n distinct values as well as a target value T, determine in O(n) time whether or not there exist two distinct values in the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site