Hard Arrays

A basic linear data structure that holds a fixed number of containers to store data.

Section Menu

How do I use this section?

1. LESSON

What is Computer Programming and What Are Its Applications?

Definition When we ask ourselves a question such as what computer programming is, it's tempting to just look up the definition. We all sort of know what it is, but we want to get it precisely right. Let's not do that this time - let's build the definition from scratch. So, if you were to ask a layman what programmers do, they might say th...

2. LESSON

How to Learn Python: Installation and Basics

In this lesson, we will provide an introduction to installing and working with Python, with a focus on the following key points: Installation and setup of the language on your machine. An overview of some basic features of Python programming with code examples. Python is one of the most p...

3. LESSON

Introduction to Code Variables and Assignment

In this lesson, we will discuss the use of variables and assignment operators in Python, with a focus on the following key points: What are variables and the assignment of variables in the Python programming language? Working with variables and assignment of variables in an introductory programming l...

4. LESSON

Boolean Algebra and Logic Gates

The field of mathematics known as Boolean algebra deals with logistical operations on binary variables. In other words, the functions and variables in Boolean algebra can have only one of two values – 0 or 1, where 0 represents False, and 1 represents True. Boolean algebra has been used mostly in computer programming languages. Set...

5. LESSON

Initializing Variables and Basic Math Operations

In this lesson, we will discuss basic elements in programming, with a focus on the following key points: What are variables, and how are they used in a program? Learn to work with math operations in an introductory programming language; Python. _This is a general purpose tutorial for multiple la...

6. LESSON

Constants and Literals: When and How to Use Them

In the realm of programming, constants and literals are fundamental building blocks that play a crucial role in constructing robust and reliable software applications. Understanding the distinction between these two concepts and employing them effectively is essential for aspiring software engineers. public class Main { public stati...

7. LESSON

What are Strings and String Operations?

In this lesson, we will discuss one of the basic types in programming languages, with a focus on the following key points, What are strings, and common string operations? Working with strings in one of a few introductory programming languages. _This is a general purpose tutorial for multiple lang...

8. LESSON

Introduction to Arrays and Array Operations

In this lesson, we will discuss the concept of arrays, with a focus on the following key points, What are arrays, and how are they implemented? Working with array operations in Python. For the Javascript version of this lesson, [please click here] (https://algodaily.com/lessons/introduction-array...

9. LESSON

How to Find the Minimum and Maximum Value in An Array

In this tutorial, we'll learn to find out the minimum and maximum values in an array using the following techniques. Built-in Methods Linear Search Method Divide and Conquer Single Loop Trick/ Comparison in Pairs We'll be using a few different languages to solve the problem. So without further ado, let's get started! ![Min and Max in Ar...

10. LESSON

Control Flow: If/Else and Try/Catch Statements

In this lesson, we will discuss the flow of execution of programs, with a focus on the following key points, How can the execution flow of a program be controlled? Working with if/else and try/catch statements to establish control flow in programs. For the Javascript version of this lesson, [plea...

11. LESSON

Loops and Iterations in Programming

In this lesson, we will learn about loops and iterations in programming, with a focus on the following key points, What are iterations in programming? Working with loops and related concepts in Python. For the Javascript version of this lesson, [please click here] (https://algodaily.com/lessons/l...

12. LESSON

Calling Functions: Calling, Parameters, Returning

In this lesson, we will learn about functions in a program, with a focus on the following key points, What are functions, and why do we use them? Working with function parameters, return, and call statements. _For the Javascript version of this lesson, [please click here] (https://algodaily.com/...

13. LESSON

Dictionaries and Key-Value Stores

In this lesson, we will learn about a new type of data structure called dictionaries, with a focus on the following key points, What are dictionaries and how are they used? Working with dictionaries in Python. For the Javascript version of this lesson, [please click here](https://algodaily.com/le...

14. LESSON

Understanding Mutable vs. Immutable Objects

Before discussing the difference between mutable and immutable objects, it's important that we properly define what an Object is in Object-Oriented Programming (OOP). This will serve as one of the foundations/basic concepts to fully understand the topic. So to start, an Object is a piece...

15. CHALLENGE

Longest Increasing Subsequence

The Longest Increasing Subsequence (LIS) is a subsequence within an array of numbers with an increasing order. The numbers within the subsequence have to be unique and in an ascending manner. It's important to note that the items of the sequence do not have to be in consecutive locations within the array. Can you write an effi...

16. CHALLENGE

Nth Smallest Number in a Stream

Many modern programming languages have the notion of a stream. A stream is a resource that's broken down and sent (usually over a network) in small chunks. Imagine a video that's playing-- it would be inefficient to need to download an entire 1 gigabyte movie before being able to watch it. Instead, browsers will gradually down...

17. CHALLENGE

Matrix Operations

Here's a fun one: let's say we have a 2D array matrix that is a size of m rows and n columns. It is initially prefilled with 0s and looks like the following: `js [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] ` <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/hard-arrays/matrix-operations-...

18. CHALLENGE

Subsets Summing Zero

Given an array of positive and negative integers like the following: `js const arr = [3, 5, -2, -4, 7, -1, 6, 8, -8, 4]; ` Can you write a method findSumZeroSubsets to return the starting and ending indexes of all subarrays in it that sum up to zero? Note: There are 3 terms that you should be able to distingui...

19. CHALLENGE

Sum of Perfect Squares

A perfect square is a number made by squaring a whole number. Some examples include 1, 4, 9, or 16, and so on -- because they are the squared results of 1, 2, 3, 4, etc. For example: ` 1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 ... ` However, 15 is not a perfect square, because the square root of 15 is...

20. CHALLENGE

The Coin Change Problem

If I give you coins of denominations {3, 7, 9} (a coin worth 3 cents, a coin worth 7 cents, etc.), can you tell me the minimum number of coins that are needed to make a total of 37? You can assume that an infinite supply of all these coins are available to you. <img src="https://storage.googleapis.com/algodailyrandomassets/...

21. CHALLENGE

How Many Strongly Connected

Given a directed graph represented via an adjacency list, write a class StronglyConnectedComponents with a count() method that would return the number of strongly connected components in a graph. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/hard-arrays/how-m...

22. CHALLENGE

Max Rectangle in a Histogram

We're given a histogram like the following, where contiguous (sharing a common border or touching) bars are made up of different heights. Let's assume all bars have the same width. The histogram is converted to an...

23. CHALLENGE

Lamps and Houses

On a given street, there's a bunch of houses and lamps nearby. It looks kind of like the following (H being houses, L being lamps): L H L H H 0 1 2 3 4 We decide to represent this with two arrays: one being the positions of the houses, and the other the positions of the lamps. From the above example, the arrays would...

24. CHALLENGE

Dutch National Flag Problem

This problem is named the "Dutch national flag problem" because the flag of the Netherlands is comprised of the colors red, white, and blue in separate parts. Although we won't be using colors, the premise of the challenge is to develop a sorting algorithm that performs some form of separations of three kinds of elements. <img s...

25. CHALLENGE

Matrix Encryption

Encryption means encoding a string using some pre-defined formula. It is widely used for securing and protecting sensitive information over the internet from theft and data breaches. Suppose an English text has to be encrypted using the following procedure for encoding: Remove all the whitespaces from the text. Then, the ch...

26. CHALLENGE

Traverse a Matrix in Spiral Order

Given an m * n matrix array, can you print all its elements in a spiral order as shown in the figure below? Try to use only O(1) space! Constraints Total elements in the matrix <= 100000 The values in...

27. CHALLENGE

Maximum Sum of Absolute Difference of Values and Indices

The Puzzle in Simple Terms You have a list of numbers called A. Your task is to pick two numbers from this list, let's call them A[i] and A[j]. Additionally, you will take note of their positions, i and j, in the list. You are to find the biggest result you can get from a specific formula. The Formula to Under...

28. CHALLENGE

Find in Shifted Sorted Matrix

Understanding the Matrix Shift Problem Suppose you have a matrix where each row and column is sorted in ascending order. Your task is to apply two specific types of shifts, described below, and locate a given element x in the transformed matrix. Shift Types Left Circular Shift (l): Definition: Moves the f...

29. CHALLENGE

Combination Sum

This challenge is all about a well-known problem in programming. You might have come across it in a job interview as part of a test, or even as part of a competition or exam. The problem is called Combination Sum and there are some variations in how it is presented, but this is the most common one: Given an array of distinct inte...

Cheat Sheet

  • Quick summary: a collection that stores elements in order and looks them up by index.
  • Also known as: fixed array, static array.
  • Important facts:
    • Stores elements sequentially, one after another.
    • Each array element has an index. Zero-based indexing is used most often: the first index is 0, the second is 1, and so on.
    • Is created with a fixed size. Increasing or decreasing the size of an array is impossible.
    • Can be one-dimensional (linear) or multi-dimensional.
    • Allocates contiguous memory space for all its elements.
  • Pros:
    • Ensures constant time access by index.
    • Constant time append (insertion at the end of an array).
  • Cons:
    • Fixed size that can't be changed.
    • Search, insertion and deletion are O(n). After insertion or deletion, all subsequent elements are moved one index further.
    • Can be memory intensive when capacity is underused.
  • Notable uses:
    • The String data type that represents text is implemented in programming languages as an array that consists of a sequence of characters plus a terminating character.
  • Time complexity (worst case):
    • Access: O(1)
    • Search: O(n)
    • Insertion: O(n) (append: O(1))
    • Deletion: O(n)
  • See also: