Question

In: Computer Science

Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...

Array with Pointers

Find Continuous Sub-Array C++

Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax
(no [ ]’s or (ptr+i) 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 found’. 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 2 3 5     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

3 5 8 2 3 5

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

30 20

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 18 5

                                              

Solutions

Expert Solution

#include <iostream>

using namespace std;

void subarray(int arr[], int n)

{

  int MAX_start = 0;

  int max_end = 0;

  int start = 0, end = 0;

  int borrow = 0;

  for (int i = 0; i < n; i++)

  {

    max_end = max_end+ arr[i];

    if (max_end < 0)

    {

      max_end = 0;  

      borrow = i + 1;

    }

  if (MAX_start < max_end)

    {

      MAX_start = max_end;

      start = borrow;

      end = i;

    }

  }

  cout << "The sum of contiguous subarray is " <<

      MAX_start << endl;

  cout << "The contiguous subarray is";

  for (int i = start; i <= end; i++)

    cout << arr[i] << " ";

}

// main function

int main()

{

  int arr[] = { 6,10 };

  int n = sizeof(arr)/sizeof(arr[0]);

  subarray(arr, n);

  return 0;

}

PLEASE GIVE A THUMBS UP!!!!!!!!!


Related Solutions

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...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
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.
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
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...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
given an array A = {a1, a2, ... , an} find the number of the continuous...
given an array A = {a1, a2, ... , an} find the number of the continuous subarrays of gcd one continuous subarrays of gcd one means gcd(ai, ai+1, ... , aj) = 1 for example {2, 4, 6, 3} or {1} are continuous subarrays of gcd one
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10...
C++ Program 1–Implement a Priority Queue(PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill...
Find the minimum sample size n needed to estimate μ for the given values of​ c,...
Find the minimum sample size n needed to estimate μ for the given values of​ c, σ​, and E. c=0.95, σ=7.2, and E=1 Assume that a preliminary sample has at least 30 members. n=___ (Round up to the nearest whole​ number.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT