Skip to content
DATA STRUCTURE
DATA STRUCTURE

Learn everything about Data structures.

  • Algorithms
  • DSA
  • Array
  • Stack
  • Queue
  • Matrix
  • Programs
DATA STRUCTURE

Learn everything about Data structures.

HackerRank Dynamic Array Solution in Python

YASH PAL, May 21, 2025May 21, 2025

Today we are going to solve HackerRank Dynamic Array problem solution in python programming.

we need to declare a 2 dimensional array with empty array all zero indexed and then we need to declare an integer variable and then need to initialize it to 0. after that we need to process two types of queries.

we need to complete the dynamic array function that has a input denotes the number of empty arrays to initialize in array and the queries in 2D array of integers.

Hackerrank Dynamic array solution in python
Dynamic array

Dynamic array problem solution in Python3

N, Q = input().strip().split(' ')
N, Q = int(N), int(Q)
seq = [[] for i in range(N)]
lastans = 0
#print(N, Q, lastans, seq)
for q in range(Q):
    t, x, y = input().strip().split(' ')
    t, x, y = int(t), int(x), int(y)
    x = (x^lastans) % N
    if t == 1:
        seq[x].append(y)
    else:
        y = y % len(seq[x])
        print(seq[x][y])
        lastans = seq[x][y]
    #print(x, seq[x])

Problem solution in Python 2

# Enter your code here. Read input from STDIN. Print output to STDOUT
n, q = map(int, raw_input().split())

arrs = []
for _ in range(n):
    arrs.append([])
    
lastans = 0
for _ in range(q):
    _type, x, y = map(int, raw_input().split())
    if _type == 1:
        arrs[(x ^ lastans) % n].append(y)
    elif _type == 2:
        arr_i = (x ^ lastans) % n
        size = len(arrs[arr_i])
        lastans = arrs[arr_i][y % size]
        print lastans
array DS problems arraysDSA

Post navigation

Previous post
  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • About US
  • Contact US
  • Data Structures and algorithms tutorials
  • Digital Communication Tutorials
  • DMCA
  • HackerRank All Algorithms problems solutions
  • HackerRank C problems solutions
  • HackerRank C++ problems solutions
  • HackerRank Java solutions
  • HackerRank Python solutions
  • Human Values Tutorials
  • Internet of Things Tutorials
  • Privacy Policy
©2025 DATA STRUCTURE | WordPress Theme by SuperbThemes