Questions tagged [arrays]

A sequential random-access data structure whose size can typically not be changed after creation.

Filter by
Sorted by
Tagged with
2
votes
1answer
40 views

array empty cell search algorithm

I'm Telecommunication student so I don't have Computer Science background, just in case if my question looks stupid. Here is my scenario: Consider an empty array named ...
-1
votes
1answer
32 views

how to optimise this

Say you are given a matrix $M (A \cdot B)$ when $A$ is the number of rows and $B$ is the number of columns. Now we are free to pick one element from each column. Lets say these elements are $E = \{ ...
1
vote
1answer
42 views

More efficient way to parse array into binary search tree

Let's assume I have array which I need to parse into binary tree ...
0
votes
0answers
68 views

Minimum no of swaps to be done in an array such that, no two adjacent elements are same

Given an array of size n, find minimum number of swaps required, so that no two adjacent elements are equal. For ex- n = 6, a[] = {1, 1, 5, 2, 5, 5}, answer = 1, ( swap a[0] with a[4] or a[5] ) n = 8,...
0
votes
0answers
86 views

Minimum Number of Refueling Stops with Dynamic Programming

This is a question from Leetcode and I wanted to solve it with Dynamic Programming. upon seeing the solution, I am not able to get the intuition behind the logic. I was able to understand the solution ...
0
votes
0answers
416 views

Find the magic value from an array of N numbers

I was asked this problem recently in an interview however, I failed to provide an optimal solution to it. Given an array $A$ of $n$ numbers, the magic factor of $i^{th}$ element is equal to the ...
1
vote
1answer
38 views

Map two arrays given a map of possible mappings

Given two integer arrays a and b and a map m which for each element ...
3
votes
1answer
33 views

Algorithm to Compute High Water Mark

I saw this algorithm question which is as below. There is a server that receives request Ids that are positive integers. The High Water Mark (HWM) for the server at any instant of time is defined as ...
1
vote
3answers
446 views

Minimum number of increment / decrement operations to make an array distinct?

I've been stuck on this problem for some time now.. Given an array A of size N that ranges between [1..N], a "move" is to increase or decrease an element (by 1). After each move the array ...
1
vote
1answer
45 views

Check array in linear time with a constant space complexity

Given an array of integers I have to return true if all the elements in the array are different or all the elements are the same. Otherwise, I have to return false. E.g. ...
0
votes
1answer
31 views

How to prove that a binary heap can't be sorted with complexity that better than O(nlogn)

I am trying to prove that you can't sort binary heap with better complexity than O(nlogn). We proved at class that every sort based on comparison can't have better cocmplexity than O(nlogn) and i ...
0
votes
1answer
45 views

In C++ how does “sizeof(array)/sizeof(array[0])” give the number of elements in an array? Why isn't it affected by the length of each element?

I'm playing around with C++ and was looking at how to loop through an array of unknown length. The following code was suggested and works perfectly from the little testing I've done: ...
2
votes
2answers
192 views

Sort an array with a constant number of unsorted elements

I am trying to sort an array that I know to have a constant number of unsorted elements (elements that are not in their right place). In the solution I looked at they say you can do this in $O(n)$ ...
0
votes
1answer
24 views

Are single-dimentional arrays better than multi-dimentional in the GPU?

I'm making simulations like Ant colony simulation, boids, and cellular automata in Go (to learn Go), but I believe this can apply to most languages. From what I've read (this), using 1D arrays is ...
0
votes
0answers
53 views

Multiplication of Upper triangular matrix and Lower triangular matrix

If I have an Upper triangular matrix A and Lower triangular matrix B, what could be the most efficient algorithm to get AxB and BxA. Also what would be the formula for total number of multiplication ...
0
votes
1answer
27 views

mapping function to map the index of Lower triangular matrix

If I have a Lower triangular matrix of order n and I want to store non-null elements of this matrix in 1D array from first row to last row and within a row from left to right. What could be the ...
0
votes
1answer
35 views

What is the time complexity of a linear search performed using 2 pointers?

For an array, I'm using a left pointer (pointing to 0) and a right pointer (pointing to end). For every iteration, if my search element is not found, I increment left and decrement right. This ...
1
vote
1answer
35 views

Why does Insertion sort take linear time in this case?

Remove any 5 elements from a sorted array of n items and replace them with 5 new elements. Why does running Insertion sort again on this array take linear O(n) time, I tried counting the number of ...
18
votes
3answers
3k views

Time complexity O(m+n) Vs O(n)

Consider this algorithm iterating over 2 arrays (A and B) size of $ A = n$ size of $ B = m$ Please note that $m \leq n$ The algorithm is as follows ...
0
votes
1answer
63 views

Find the minimum sum of distances between sets of points to a straight line in a plane

Given $n$ dots on a plane, such as: n couples ($x_i$,$y_i$) I would like to find a line parallel to y-axis ( $x=b$ ), such that the sum of all of the point's distances from that line will be minimal ...
1
vote
1answer
20 views
2
votes
1answer
104 views

Searching in sorted array with $O(\log n)$

Recently been practicing some recent exams, there was a problem I could not comprehend the given answer, the question is as follows: Suppose array $A[1..n]$ consist of $n$ distinct integers that is ...
1
vote
3answers
175 views

Intersection of two arrays

As part of a project i'm doing I have been looking for a way to find common values between 2 arrays including duplicates. For example; A = [2, 2, 3, 5] B = [2, 2, 5] would return the array; Common ...
1
vote
1answer
108 views

Sort a $d$-sorted array

An array is $d$-sorted if every key in the array is located at a distance at most $d$ from its location in the sorted array. I need to write an algorithm that get a $d$-sorted array of length $n$ and ...
1
vote
1answer
29 views

Arrays and pointers [closed]

so I know the name of the array is a pointer to the first element of the array. But I've noticed something weird Here's the code. Here is the result: How is it possible that the pointer is pointing ...
1
vote
1answer
42 views

Quickly determine if insertion sort or quick sort is better

I'm in a scenario where ~30% of the time, my array is almost completely sorted, and the other 70% of the time, it is basically completely random. I want to quickly determine if my list is almost ...
0
votes
0answers
37 views

Why is LRU cache not implemented using doubly linked list with just two nodes? Do we even need double linked list?

I don't understand why do we need to have more than two nodes in doubly linked list when all we need to maintain is the most recent and least recently accessed nodes. I was going through solutions for ...
0
votes
0answers
35 views

Array algorithm to fill an array in equal but alternate binary choices as close as possible given an already fixed configuration

Stuck on a problem with my roommate and can't seem to find any parallels to this. The problem is simple to understand: you have two possible binary options: T or F. You are given an array which ...
1
vote
1answer
31 views

Index variable takes up log(n) space?

In the definition of in-place algorithm, from Wiki, " However, this form is very limited as simply having an index to a length n array requires O(log n) bits." How does an array index ...
0
votes
0answers
21 views

Reordering similar elements together within an array

I have a requirement where I need to group similar elements together within an original list. For example: I/P array: ...
0
votes
0answers
31 views

Implement Immediate Smaller Element problem using Stack

Immediate Smaller Element Problem: Given an integer array arr of size $n$. For each element in the array, check whether the right adjacent element (on the next ...
0
votes
2answers
138 views

Given a sorted array with n integers, how to find the element that has no duplicates?

I am given a sorted array with n integers. All of the elements repeat exactly twice, while there is one element without duplicate. I need to solve it as efficient as it is possible by divide-and-...
1
vote
0answers
41 views

Dynamic array with 4x growth factor: Potential Method

I am curious on the use of the potential method for amortized analysis for a dynamic array which quadruples in size after it becomes full. I understand how the potential function is defined and used ...
1
vote
2answers
95 views

Is there a name for an “array with no holes”?

How would you call an array [1, 2, 3, 4, 5, ...] that has (and ensures) the property of not having any undefined values through all its length. "Dense" could work but it's not exactly what I ...
0
votes
1answer
41 views

Algorithm chalenge: find minimum range to cover all houses with light

N houses are located on a straight street which has K streetlights. Each streetlight has range R. Given N and K how to find minimum value for R? N is an array containing location of each house. K is ...
4
votes
2answers
165 views

Is there another type of list than arraylist and linked-list?

My question is not about how arraylists or linked-lists work, but why there aren't other types of lists. From my understanding, memory is accessed linearly by adresses. Thus, you have the choice to ...
0
votes
0answers
25 views

Subarray containing all integers from 1 to $m$

I have faced the following problem in an entrance examination, and need to verify a few results plus a question part that did not manage to get in time. Given an array $A$ of length $n$ which contains ...
0
votes
1answer
65 views

Recursive array

The following is a function to write a recursive function that accepts an array and a callback. Function returns true if a single value in array returns true when passed to the callback otherwise it ...
1
vote
1answer
85 views

Minimum number of swaps to make two arrays strictly increasing

I'm trying to understand the solution of the following problem (LC 801): Given two arrays, A and B, of the same nonzero length, find the minimum number of swaps (A[i] <--> B[i]) to make A and B ...
0
votes
2answers
64 views

Randomize List with guaranteed Index Change

I usually post on Stackoverflow, but I got stuck with a problem which I think fits better here and I hope someone can help me. The task ist pretty simple: Shuffle/Randomize an array/list conditions: ...
0
votes
1answer
49 views

LinkedList: Remove the kth node form the end

Question: Write a function that takes in the head of a Singly Linked List and an integer k and removes the kth node from the end of the list. The removal should be ...
1
vote
1answer
56 views

Using hashing to solve 4-sum problem

The famous 4-sum problem is to find 4 elements at unique indices in an array which sum to a given X. I was looking at one solution I found: ...
0
votes
0answers
65 views

0/1 Knapsack problem. How does the total weight does not exceed the limit?

I am trying to wrap my head around the knapsack problem algorithm. I understood the most of it except one tiny thing. On the left is the [val,weight] and on the ...
0
votes
1answer
107 views

design a strange data structure, is it possible?

I need a FIFO QUEUE that can do Insert and Remove from Queue in amortized $O(1)$ but extract min in $O(log n)$. is it possible? When just find min is important (not removing) there is lots of $O(1)$ ...
0
votes
0answers
57 views

Competitive programming: Good Subsets problem

An array of length K is called good if for each subarray of the array, the sum of the subarray is not divisible by K+1. You are given an array A of length N. Find the number of subsequences of length ...
0
votes
1answer
44 views

Find the intersection point between two sorted arrays with unknown lengths in lesser than O(n)

Two sorted arrays of positive integers, X[] and Y[] are given.But, the array sizes are unknown to us. We may assume that accessing any index beyond the last element of the array returns -1. The ...
0
votes
1answer
134 views

Product Sum of Special Arrays

Can someone please explain why this is correct: ...
0
votes
0answers
24 views

Counting elements in an array greater than a value and after the value

I have an array composed of integers from 1 through n. For each integer, I want to count the integers which are greater than that integer and occur after the integer's placement in the array in O(n*...
0
votes
0answers
14 views

Recursive Selection of Pivot from an Array

Suppose we have an array of integers $A$ of length $n$, which is assumed to be multiple of 5 for convenience in the discussion below. Now we try to pick a pivot $x$ from $A$ such that $x$ can divide $...
1
vote
2answers
46 views

A silly doubt on Computer Science Objects: Arrays, Lists, Strings, and Vectors, are they the same concept?

First of all I'm a Physics Student and I've been arguing with my brother concerning the usage and the fundamental meaning of a vector on Computer Science (CS). I) Vectors A vector have a axiomatic ...

1
2 3 4 5
10