linear search problem

hey! so I am writing a code that will determine the max profit from buying and selling stock prices. Example:
Sample Output: Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference/max profit = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price), which means you must buy the stock at $1 and sell at $6

I have the logic figured out except for determining where the min number and max number are located within the array. I need this because the min number must come before the max number. So like:
Input: [7, 6, 4, 3, 1] Output: 0
In this case, no transaction is done, max profit = 0.

I'm at a lost as to where to start. Also, if the min number is repeated twice, I need the first location! Any help is greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 #include <iostream>
using namespace std;

int main()
{
  int useranswer = 1;
  while (useranswer == 1)
 {
    int i = 0, n = 0, max = 0, min = 0, output = 0, largest = 0, locationmax = 0, locationmin = 0;
    int arr[10];

    cout << "Enter total number of stock prices: ";
    cin >> n;
    cout << endl;

    // Store number entered by the user
    for(i = 0; i < n; ++i)
    {
       cout << "Enter price of stock " << i + 1 << " : ";
       cin >> arr[i];
    }

   if(arr[0] < arr[1])
    {
      largest = arr[1];
      max = arr[0];
    }

    else
    {
      largest = arr[0];
      max = arr[1];
    }

    for (int i = 2; i < n; i ++)
    {
      if (arr[i] > largest)
      {
        max = largest;
        largest = arr[i];
      }
      else if (arr[i] > max && arr[i] != largest)
      {
        max = arr[i];
      }
    }

    for(i = 1;i < n; ++i)
    {
       if(arr[0] > arr[i])
           arr[0] = arr[i];
    }

    min = arr[0];


    if (locationmin < locationmax)
    {
      output = max - min;
      cout << "Max Profit is: " << output << endl;
      cout << "Would you like to run the program again? 1 for yes, 2 for no" << endl;
      cin >> useranswer;
    }
    else
    cout << "There is no transaction. Max Profit is O" << endl;
    cout << "Would you like to run the program again? 1 for yes, 2 for no" << endl;
    cin >> useranswer;
  }
    return 0;
}
Your algorithm is flawed. The maximum and minimum numbers have nothing to do with it. They will not necessarily be involved. In particular, what if the maximum is first and the minimum is last?

The obvious quadratic solution is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <limits.h>

int main() {
    int a[] = {7, 1, 5, 3, 6, 4};
    int size = sizeof a / sizeof *a;
    int m = INT_MIN;
    for (int i = 0; i < size - 1; i++)
        for (int j = i + 1; j < size; j++) {
            int d = a[j] - a[i];
            if (d > m) m = d;
        }
    printf("%d\n", m);
    return 0;
}

Topic archived. No new replies allowed.