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...
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...
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...
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...
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.)
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT