0

I'm having trouble with int[] arrays and adding them to a List<>. I'd like to add the values of my int[] array to something each loop but every time I do this my "something" gets the same value for every element I add. Very annoying. I understand arrays are always reference vars. However even the "new" key word doesn't seem to help. What needs to happen is to add result to some enumerated object like a List or Array or ArrayList.

Here's the codility question:

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increased by 1,
  • max_counter − all counters are set to the maximum value of any counter.

A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:

  • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  • if A[K] = N + 1 then operation K is max_counter.

For example, given integer N = 5 and array A such that:

A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4

the values of the counters after each consecutive operation will be:

(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)

The goal is to calculate the value of every counter after all operations.

I copied some code from others and the variable "result" does indeed load the data correctly. I just wanted to copy it back to the main program so I could see it. The only method that works is += add it into a string. Thus losing any efficiency I might have gained.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testarray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = new int[7];
            A[0] = 3;
            A[1] = 4;
            A[2] = 4;
            A[3] = 6;
            A[4] = 1;
            A[5] = 4;
            A[6] = 4;
            List<int[]> finish = solution(5, A);

        }
        public static List<int[]> solution(int N, int[] A)
        {
            int[] result = new int[N];
            int maximum = 0;
            int resetlimit = 0;
            int iter = 0;
            List<int[]> collected_result = new List<int[]>;

            for (int K = 0; K < A.Length; K++)
            {
                if (A[K] < 1 || A[K] > N + 1)
                {
                    throw new InvalidOperationException();
                }

                if (A[K] >= 1 && A[K] <= N)
                {
                    if (result[A[K] - 1] < resetlimit)
                    {
                        result[A[K] - 1] = resetlimit + 1;
                    }
                    else
                    {
                        result[A[K] - 1]++;
                    }

                    if (result[A[K] - 1] > maximum)
                    {
                        maximum = result[A[K] - 1];
                    }
                }
                else
                {
                    resetlimit = maximum;
                    result = Enumerable.Repeat(maximum, result.Length).ToArray<int>();

                }

                collected_result.Add(result);

            }
                  //    for (int i = 0; i < result.Length; i++)
                  //result[i] = Math.max(resetLimit, result[i]);

            return collected_result;
        }

    }
}

This doesn't work, the collected_result ends up like:

(0,0,1,2,0)
(0,0,1,2,0)
(0,0,1,2,0)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)
(3,2,2,4,2)

I know it's the line collected_result.Add(result); adding the reference each time to every instance of result in the List<>. Bother. I've tried adding "new" which is a compiler error. Finally in desperation I just added everything to a very long string. Can someone help me figure out how to properly load an object to pass back to main?

1
  • Do not use weird and complex solution. There are more than one way to reach your goal. Commented May 18, 2014 at 5:23

2 Answers 2

2

Easiest way to go:

Get a copy of your array before adding it to list:

collected_result.Add(result.ToArray());
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, that was elegant. I could not see how to stop referencing the reference. The method ToArray() must act as kind of a "new". I really appreciate the help.
0

Here is a Python solution: def solution(A, N): lenA = len(A) k = 0 max_counter_value = 0 counters = [0 for x in range(0, N)]

for k in range(0, lenA):
    if A[k] >= 1 and A[k] <= N:
        counters[A[k] - 1] += 1
        max_counter_value = max(counters)
    if A[k] == N + 1:
        counters = [max_counter_value for x in range(0, N)]
    print counters

A = [3, 4, 4, 6, 1, 4, 4] N = 5

solution(A, N)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.