2
\$\begingroup\$

I have a assignment to find which season has the biggest temperature amplitude. Input par is the int array with temperatures:

int T[]= {-1,-10,10,5,30,15,20,-10,30,10,29,20}

which means:

winter: -1,-10,10 spring: 5,30,15 summer: 20,-10,30 autumn: 10,29,20
  • temp is always an int,
  • int[] T - T.length%4 is equal to 0 (same number of temperatures for every season, could be 2, 3 etc.)
{

    public String solution(int[] T){
    int length = T.length;
    int count = length/4;
    int i=0,j=0,indx=0;
    String name="";
    int maxAmpl;

    int[] winter = new int[count];
    int[] spring = new int[count];
    int[] summer = new int[count];
    int[] autumn = new int[count];
    int[] diff = new int[4];

    for(j=0,i=0;j<count; j++,i++){
        winter[i] = T[j];
    }

    for(j=count,i=0;j<count*2; j++,i++){
        spring[i] = T[j];
    }

    for(j=count*2,i=0;j<count*3; j++,i++){
        summer[i] = T[j];
    }

    for(j=count*3,i=0;j<count*4; j++,i++){
        autumn[i] = T[j];
    }

    Arrays.sort(winter);
    Arrays.sort(spring);
    Arrays.sort(summer);
    Arrays.sort(autumn);

    diff[0] = winter[count-1]-winter[0];
    diff[1] = spring[count-1]-spring[0];
    diff[2] = summer[count-1]-summer[0];
    diff[3] = autumn[count-1]-autumn[0];

    maxAmpl=diff[0];

    for(int k=1; k<4; k++){
        if (diff[k]>maxAmpl){
            maxAmpl = diff[k];
            indx = k;
        }
    }

    switch(indx){
        case 0:
            name = "WINTER";
            break;
        case 1:
            name = "SPRING";
            break;
        case 2:
            name = "SUMMER";
            break;
        case 3:
            name = "AUTUMN";
            break;
    }
    return name;
}
\$\endgroup\$
3
\$\begingroup\$

Some key points:

  • No need to sort the array to find min/max value
  • No need to copy the array, can search in the original array using an offset count * N

Have a look at this:

public String solution(int[] T) {
    final int count = T.length / 4;
    int indx = 0;
    int last = Integer.MIN_VALUE;
    for (int i = 0; i < 4; ++i) {
        int diff = IntStream.of(T).skip(count * i).limit(count).max().getAsInt()
                 - IntStream.of(T).skip(count * i).limit(count).min().getAsInt();
        if (diff > last) {
            indx = i;
            last = diff;
        }
    }
    final String[] seasons = { "WINTER", "SPRING", "SUMMER", "AUTUMN" };
    return seasons[indx];
}
\$\endgroup\$
1
\$\begingroup\$

Steps to solve this problem:

  • Get slice of array related to season
  • Sort slice (we could use custom for loop to get min and max but sorting is quite similar in performance and takes one line instead of few)
  • Get first and last value from sorted slice
  • Calculate amplitude and save this result in Array or List

Example implementation (C#):

using System;
using System.Linq;

namespace application
{
    class Program
    {
        static void Main(string[] args)
        { 
            var T1 = new int[] { 13, 2, 3, 4, 1, 6, 11, 8, 2, 10, 11, 4 };
            var T2 = new int[] { 26, 2, 3, 4, 1, 6, 11, 33, 2, 10, 11, 4 };
            var T3 = new int[] { 13, 2, 3, 27, 1, 6, 11, 8, 2, 10, 11, 4 };
            var T4 = new int[] { 13, 2, 3, 4, 1, 6, 11, 8, 2, 10, 11, 33 };

            Console.WriteLine($"Solution: {Solution(T1)}");
            Console.WriteLine($"Solution: {Solution(T2)}");
            Console.WriteLine($"Solution: {Solution(T3)}");
            Console.WriteLine($"Solution: {Solution(T4)}");
        }

        static string Solution(int[] T)
        {
            const int numberOfSeasons = 4;
            int offset = T.Length / numberOfSeasons;
            var amplitudes = new int[numberOfSeasons];
            var seasons = new string[] { "WINTER", "SPRING", "SUMMER", "AUTUMN" };
            for (int i = 0; i < numberOfSeasons; i++)
            {
                var slice = new ArraySegment<int>(T, i * offset, offset).ToArray();
                Array.Sort(slice);
                var min = slice[0];
                var max = slice[slice.Length - 1];
                var amplitude = max - min;
                amplitudes[i] = amplitude;
            }
            var index = Array.IndexOf(amplitudes, amplitudes.Max());
            return seasons[index];
        }
    }
}

Output:

Solution: WINTER
Solution: SUMMER
Solution: SPRING
Solution: AUTUMN
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer. \$\endgroup\$ Nov 24 '21 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.