Sort by

recency

|

2014 Discussions

|

  • + 0 comments

    С# solution (Half of difficulty its figure out what the task is actually...)

    Task explanation(if someone needed): We have an input : [[type, x, y]] , where type - type of queries( if 1 - add y in list(using formula), if 2 - extract element using second formula and add to lastAnswer ). x - parametr, what we using in formula which calculate index where we need to insert/extract y. y - parameter, what we insert/extract.

    {
        List<List<int>> seqList = new List<List<int>>();
        for (int i = 0; i < n; i++)
        {
            seqList.Add(new List<int>());
        }
    
        List<int> results = new List<int>();
        int lastAnswer = 0;
    
        foreach (var query in queries)
        {
            int type = query[0];
            int x = query[1];
            int y = query[2];
            int seqIndex = (x ^ lastAnswer) % n;
    
            if (type == 1)
            {
                seqList[seqIndex].Add(y);
            }
            else if (type == 2)
            {
                int size = seqList[seqIndex].Count;
                lastAnswer = seqList[seqIndex][y % size];
                results.Add(lastAnswer);
            }
    
        }
    
        return results;
    }
    
  • + 0 comments

    this is the code from in java but totally with normal array not ArrayList.

    
    

    import java.util.Scanner;

    public class HourGlass { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[][] array = new int[6][6];

        // Read input
        for (int x = 0; x <= 5; x++) {
            for (int y = 0; y <= 5; y++) {
                array[x][y] = s.nextInt();
            }
        }
    
        // Calculate and print the maximum hourglass sum
        int max_sum = hourglassSum(array);
        System.out.println(max_sum);
    }
    
    public static int hourglassSum(int[][] arr) {
        int max_sum = Integer.MIN_VALUE; // Start with the smallest possible integer
    
        // Loop through all potential top-left corners of hourglasses
        for (int x = 0; x <= 3; x++) {
            for (int y = 0; y <= 3; y++) {
                // Calculate the hourglass sum for the current position
                int sum = arr[x][y] + arr[x][y + 1] + arr[x][y + 2] // Top row
                        + arr[x + 1][y + 1]                         // Middle element
                        + arr[x + 2][y] + arr[x + 2][y + 1] + arr[x + 2][y + 2]; // Bottom row
    
                // Update max_sum if the current hourglass sum is greater
                if (sum > max_sum) {
                    max_sum = sum;
                }
            }
        }
    
        return max_sum;
    }
    

    }

    
    
  • + 0 comments

    Giving those of us trying to understand the problem code examples of the solution isn't really ideal. This challenge is incredibly perscriptive rather than descriptive of some logical problem to solve. The perscription isn't very well written either. It would be incredibly useful if someone were to re-write an actual problem description rather than a solution.

  • + 0 comments

    Took me a while to understand the problem and the input description. Here is the code in golang:

        var lastAnswer int32
        lastAnswer = 0
        var answers []int32
    		
    // you need to use the n parameter to generate the size of arr
        arr := make([][]int32, n)
    
    // range the queries params
        for i := 0; i < len(queries); i++ {
    		
    // the first index (0) of the queries will always be the type of query
    // example {1 0 3}
    // 1 is the queryType, 0 is the x and 3 is the y
            queryType := queries[i][0]
    				
    // the x will be the index (1)
            x := queries[i][1]
    				
    // and the y will be de value or index (2)
            y := queries[i][2]
    				
    // this is the formula given by the problem to get the index to append
            idx := ((x ^ lastAnswer) % n)
    
            if queryType == 1 {
                arr[idx] = append(arr[idx], y)
            }
    
            if queryType == 2 {
                lastAnswer = arr[idx][y%int32(len(arr[idx]))]
                answers = append(answers, lastAnswer)
            }
    				
    
        }
    
        return answers
    
  • + 1 comment
    def dynamicArray(n, q):
        # Write your code here
        l=0
        la=[]
        a=[]
        for i in range(n):
            a.append(list())
        for i in range(len(q)):
            if q[i][0]==1:
                idx=((q[i][1] ^ l)%n)
                a[idx].append(q[i][2])
            if q[i][0]==2:
                idx=((q[i][1] ^ l)%n)
                l=a[idx][q[i][2]%len(a[idx])]
                la.append(l)
        return la