array of integer problem

Hi all

I am trying to solve this problem in C++. Below is what I am trying to do, but has some logical issues.

https://www.chegg.com/homework-help/questions-and-answers/strugacarro-planet-whose-year-divided-four-seasons-winter-spring-summer-autumn-order-year--q101365105

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
#include <iostream>
#include <vector>

string solution(vector<int>& T)
{
  int maxtempw = T[0];
  int max = T[0];
  int pos = 1;
  int n =  T.size();
  for(int k = 1; k < n; k++)
  {
    if(T[k] < maxtempw)
    {
      pos = k+1;
      maxtempw = max;
    }
    else if (T[k] > max) {
        max = T[k];
    }
    return pos;
}



Below is the JAVA code for this solution, but I do not understand JAVA. Can anyone help me to understand this code?

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
72
73
74
75
76
77
78
79
80
function solution() {
}

solution.prototype.solve = function (temps) {
// console.log("Input types is: " + typeof arguments);
// console.log(arguments);

let daysInYear = temps.length;

// create clean clusters for each season
let clusters = {};
for (let i = 0; i < temps.length; i++) {
let currentTemperature = temps[i];

let currentSeason = getSeason(i + 1, daysInYear);
// console.log(currentSeason);

if (!clusters[currentSeason]) {
clusters[currentSeason] = [];
}

clusters[currentSeason].push(currentTemperature);
}

// console.log(clusters);
let highest = null;
let highestSeason = null;
Object.keys(clusters).map(function (season) {

let seasonTemps = clusters[season];
let min = Math.min(...seasonTemps);
let max = Math.max(...seasonTemps);
let amplitude = max - min;

if (highest == null) {
highest = amplitude;
}

if (amplitude > highest) {
highest = amplitude;
highestSeason = season;
}
});

// console.log("highest ", highest);
// console.log("season ", highestSeason);
// console.log("--- CLUSTERS ---");

// console.log(clusters);

function getSeason(currentDay, numberOfDays) {
let x = currentDay / numberOfDays;

// console.log(currentDay, numberOfDays, x);

if (x <= 0.25) {
return "WINTER"
}

if (x > 0.25 && x <= 0.5) {
return "SPRING"
}

if (x > 0.5 && x <= 0.75) {
return "SUMMER"
}

if (x > .75) {
return "AUTUMN"
}

throw new Error("out of range");
}

return highestSeason;

};

module.exports = Solution;

Last edited on
That's Javascript I believe, not Java.

With 4 seasons this code is arguably nearing the point where one should remove the repetition, but I opted not to.
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
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

string solution(vector<int> t)
{
  auto const season_days = t.size() / 4;
  auto const winter = minmax_element(t.begin() + season_days * 0, t.begin() + season_days * 1);
  auto const spring = minmax_element(t.begin() + season_days * 1, t.begin() + season_days * 2);
  auto const summer = minmax_element(t.begin() + season_days * 2, t.begin() + season_days * 3);
  auto const autumn = minmax_element(t.begin() + season_days * 3, t.begin() + season_days * 4);
  
  struct { std::string name; int dt; } dts[4] = 
    { { "WINTER", *winter.second - *winter.first }, { "SPRING", *spring.second - *spring.first },
      { "SUMMER", *summer.second - *summer.first }, { "AUTUMN", *autumn.second - *autumn.first }, };

  return max_element(dts, dts + 4, [](auto a, auto b){ return a.dt < b.dt; })->name; 
}

int main()
{
  std::cout << solution({-3, -14, -5, 6, 8, 42, 8, 3}) << '\n'; 
  std::cout << solution({2, -3, 3, 1, 10, 8, 2, 5, 13, -5, 3, -18}) << '\n';   
}

Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <vector>
#include <iostream>

std::string solution(const std::vector<int>& t) {
	constexpr static size_t num_seasons { 4 };
	constexpr static const char* months[num_seasons] { "WINTER", "SPRING", "SUMMER", "AUTUMN" };
	const auto season_days { t.size() / num_seasons };
	int mx[num_seasons] {};

	for (size_t i {}; i < num_seasons; ++i) {
		const auto s { std::minmax_element(t.begin() + season_days * i, t.begin() + season_days * (i + 1)) };

		mx[i] = *s.second - *s.first;
	}

	return months[std::max_element(std::begin(mx), std::end(mx)) - std::begin(mx)];
}

int main() {
	std::cout << solution({ -3, -14, -5, 6, 8, 42, 8, 3 }) << '\n';
	std::cout << solution({ 2, -3, 3, 1, 10, 8, 2, 5, 13, -5, 3, -18 }) << '\n';
}



SUMMER
AUTUMN

Thanks everyone for your valuable inputs.
Topic archived. No new replies allowed.