Question

In: Computer Science

Given an unsorted array A of size N of integers, find a continuous sub-array which adds...

Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff.

input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.'

there may be more than one sub-array to be found in a given input.

example:

input: 6 10 first number represents 6 values to enter and 10 is the sum

3 5 8 2 3 5 6 input values for the array.

output: 8 2 and 235 these are continuous sub-array values that sum to 10

your input data sets: i would create an input file for the following.

6 10

358235

8 20

5 10 -3 3 10 -3 4 14

6 12

8 5 3 3 7 7

9 15

3 8 4 3 10 2 3 8 2

6 12

8 5 3 3 4 5

20 30

10 12 8 5 15 5 10 8 2 4 6 9 1 7 6 3 2 7 15 18 20 5 3 2 7 9 3 2 1 8 5

Solutions

Expert Solution

#include <iostream>
#include <fstream>

using namespace std;

void printArray(int *data, int i, int j) {
        for(int x=i; x<=j; x++) {
                cout << *(data + x) << " ";
        }
        cout << endl;
}
void subArray(int *data, int size, int target) {

        int currentSum = *data;
        int startIndex = 0;

        for(int i=1; i<=size; i++) {

                while(currentSum > target && startIndex < i-1) {  
                        currentSum -= *(data + startIndex);
                        startIndex++;
                }

                if(currentSum == target) {
                        printArray(data, startIndex, i-1);
                        return;
                }

                if(i < size) {
                        currentSum += *(data + i);
                }

        }

        // if we reach here, it means no solution
        cout << "no sum found" << endl;
}

int main() {
        string fName = "input.txt";

        ifstream f(fName.c_str());

        if(f.fail()) {
                cout << "Unable to find file." << endl;
                return 1;
        }

        int size;
        int target;

        while(f >> size) {
                f >> target;
                int *data = new int[size];
                for(int i=0; i<size; i++) {
                        f >> *(data + i);
                }

                subArray(data, size, target);
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

The main { string fName = "input.txt"; main.cpp input.txt ifstream f(fName.c_str(); clang version 7.0.0-3-ubuntu. 18.04.1 (tags/RELEASE_ ? clang++-7 -pthread -o main main.cpp } ./main 8 2 10 -3 3 10 no sum found 3 84 3 4 5 10 12 8 3 2 if(f.fail()) { cout << "Unable to find file." << endl; return 1; int size; int target; while(f >> size) { f > > target; int *data = new int[size]; for(int i=0; i<size; i++) { f » data[i]; subArray(data, size, target);


Related Solutions

Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
assume n = 2^100. consider the following problem. given an unsorted list of size n you...
assume n = 2^100. consider the following problem. given an unsorted list of size n you can choose to sort it or not first. after this step, you get m queries q1, q2, ... qm. one by one of the form "is qi in the list?" Describe whether you would sort and what algorithm you would use if 1.) m= 10, and 2) m = 1000
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT