Array Algorithms are defined as functional algorithms where each step of the algorithm results in a function being applied to an array, producing an array result

learn more… | top users | synonyms

1
vote
4answers
138 views

Programming test on algorithms? [on hold]

I was given this question in a programming test for an IT company. I will try my best to explain it. The problem is as follows: Given an Ant at origin (0,0) which moves only in clockwise direction( ...
0
votes
1answer
27 views

Can not get precise results for the hourglass algorithm

Here is the link for definition of the hourglass problem: https://www.hackerrank.com/challenges/30-2d-arrays I wrote the following program: package day11; import java.util.Scanner; public class ...
0
votes
1answer
52 views

Randomized quicksort C#

I am trying to analyze the quick sort algorithm with a random pivot on C#. This is the code I am trying to use this portion of code: //begeeben.wordpress.com/2012/08/22/randomized-quick-sort-in-c/ ...
-1
votes
3answers
287 views

Array Algorithms

I have one question to ask on Algorithms. I have been asked to write the algorithm on this: Not asking you to write the algo for me, but just let me know the efficient process what I need to do: ...
3
votes
7answers
3k views

Maximum element in array which is equal to product of two elements in array

We need to find the maximum element in an array which is also equal to product of two elements in the same array. For example [2,3,6,8] , here 6=2*3 so answer is 6. My approach was to sort the array ...
0
votes
1answer
42 views

replacing elements of one array from others to minimize the sum of first array

We are given two arrays , A[] of size n and another array B[] , of size m , we can replace any number of elements in A[] , using elements of B[] , each element of B[] can be used only once to replace ...
1
vote
0answers
41 views

Synchronous operation On Node.js (Large data Computation)

EDIT Changes made against the comments I have Four Arrays. commonOrdersList --length around 40k storeA --length close to 100k storeB --length around 110k storeC --length close to 100k of ...
0
votes
2answers
75 views

Convert integer to roman numerals - there must be a better way [closed]

I'm working through the intermediate algorithms in the FreeCodeCamp curriculum. One of them involves converting integers to roman numerals. My solution (presented below) works, however it is very much ...
0
votes
2answers
28 views

Insertion Sort - troubles reading MIT Intro to Algos

The MIT Intro to Algorithms describes insertion sort as: I wrote this in Python as: def sort(A): for j in range(1, len(A)): key = A[j]; i = j - 1; # i > 0 should be i ...
0
votes
1answer
92 views

Designing an algorithm to sort n strings in O(dn)

How can I design an algorithm to sort n strings in O(dn) where d is the #of characters in a longest string?
1
vote
1answer
58 views

How can I improve the speed of this shortest path/shortcut(array graph DS) solution?

Given a maze as an array of arrays where 1 is a wall and 0 is a passable area: Must include start node in distance, if you BFS this it will give you 21. [0][0] is the start point. | [ V [0,...
6
votes
3answers
1k views

Median of medians algorithm: why divide the array into blocks of size 5

In median-of-medians algorithm, we need to divide the array into chunks of size 5. I am wondering how did the inventors of the algorithms came up with the magic number '5' and not, may be, 7, or 9 or ...
3
votes
3answers
10k views

Sorting Coordinate Points c++

in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all have a score for ...
-6
votes
1answer
64 views

Interivew: Sweet Candies [closed]

You have been provided the sweetness level of N candies placed in a row. The sweetness level of each candy is represented as an integer. And yes, a candy can have negative sweetness level (it's bitter ...
-2
votes
1answer
49 views

repetition detection in O(Log n) in sorted array

Given a sorted integer array A of size n, where n is a multiple of 4. Could someone help me how to find an algorithm that decides whether not there exists an element that repeats at least n/4 times in ...
0
votes
0answers
38 views

pythonic quicksort. call stack exceeded

This is my attempted implementation of quicksort in python However, I notice that I have a max call stack exceeded error, likely because the length of my collection never goes down. When printing, I ...
0
votes
2answers
44 views

Iterate javascript array and sum attributes

Consider an array that contains objects. Each object of the array is composed of both label and value attributes. I want to be able to compare each object of the array, and if the labels are the same, ...
0
votes
2answers
65 views

Enumerate all possibilities in array with descending values

I'm trying to write a recursive approach to enumerate all possible values of an array of arbitrary length whose elements can all descend to one. More formally, given an array A with the elements, A_1, ...
0
votes
1answer
103 views

C++ Best way to read BitMap “Right Way up” into 1D Vector

I am trying to read a BitMap "The Right Way Up" into a 1D Vector. Here is my first attempt. It is pretty clunky: void BitMap::ReadBMP(const char* filename) { FILE* f = fopen(filename, "rb"); if(...
0
votes
2answers
48 views

Data Structures: If Heaps are trees, why are they implemented internally with Lists or arrays?

I'm giving myself a refresher course in data structures and algorithms (and learning new stuff -- I was an Information Systems major in college instead of Computer Science, so I didn't get a formal ...
-2
votes
3answers
80 views

Best way to find all elements in a list that are present more than k times

I just came across a problem and I was wondering what would be the best way to solve this. I have a List of Lists L = [[1, 2, 3, 4, 5, 6, 7], [2, 4, 6, 8, 10, 12], [3, 6, 9, 12, 15], ....] ...
0
votes
1answer
80 views

Peak finding algorithm - Why global maximum, what are the applications?

I came across the Peak Finding Algorithm from the MIT Intro to Algorithms class. I was wondering what are some of the practical applications of the algorithm, for both the 1D and 2D cases? Also, why ...
13
votes
4answers
3k views

Given an array [a1b2c3d4] convert to [abcd1234]

Constraints : O(1) space O(n) Time It is not a homework question just a interesting question I came across. Here are some solutions I could think of but nothing that does it in given constraints. ...
1
vote
4answers
100 views

Shift last element of the list

I'm looking for the efficient way to shift last element of the list in python to it's appropriate position. for example if we have list = [1, 3, 4, 5, 6, 2] we should get list = [1, 2, 3, 4, 5, 6]. ...
0
votes
1answer
38 views

Amortized Analysis [Dynamic Array]

Let x be the size of an empty array. If the array grows full, a new one will be created with a length k > x. The contents of the old array will be copied to the new one, and the new element will be ...
0
votes
3answers
79 views

What's the fastest way to remove duplicates from an array in Objective-C

Prepping for an interview. I am trying to practice by solving the following problem: Given an input array of NSNumbers where some of the numbers are duplicated, how can you create another array that ...
0
votes
1answer
70 views

Fastest way to eliminate consecutive duplicates from array in objective-C

Prepping for interview. Trying to work out a solution for the question "Fastest way to eliminate consecutive duplicates from array" using objective-C. I.e input =[1,2,2,1,2,3,3,4] output =[1,2,1,2,3,...
-4
votes
1answer
88 views

Algorithm to find all substrings from the given array of string

I need to find all sub-strings from the given array of strings and group them. Additional condition: If string S1 contains string S2, S1 contains S3, S2 contains S4 - all them should be in one group....
1
vote
2answers
44 views

Finding the distance between the two closest numbers in an array of n numbers. (Presorted Arrays)

The arrays are presorted making the algorithm θ(nlogn) [sorting+finding minimum distance], compared to bruteforce θ(n2). Both the codes does the same job but the first one shows time limit exceeded. I ...
-1
votes
1answer
133 views

Computing the union and intersection of two unsorted arrays in O(mlogn) time

Okay, so I need to devise the following algorithm (NO CODE NEEDED, just steps): Given two sets A and B with length m and n respectively where the numbers in each set are distinct, unsorted, and m<...
0
votes
0answers
57 views

Water between building in three dimensions

The question is quite similar to this question : Amazon: Water collected between towers But I need to fix a problem similar to above with extra dimension. There are towers with same base area, but ...
0
votes
1answer
46 views

Finding peak in 2D grid with traversal

We have a n-by-n grid such that the lower-left corner has coordinates (0,0) and the upper-right (n,n). The cells in the grid all have distinct values, and our goal is find a local peak, which is ...
2
votes
2answers
160 views

Why does mergesort have at most 6 n log n array accesses?

I'm watching the Coursera Princeton algorithms lecture on merge sort and I understand all of the analysis except for a merge being at most 6 n log n array accesses. Why 6?
-2
votes
1answer
45 views

How to implement a stack of integers in array in Java Collection Framework as a variable-size array?

The question is how to implement a stack of integers in an array in Java Collection Framework as a variable-size array?
0
votes
1answer
20 views

Complexities of the following -insertion sort, selection sort ,merge sort , radix sort and explain which one is best sorting algorithm and why?

Complexities of the following -insertion sort, selection sort ,merge sort , radix sort and explain which one is best sorting algorithm and why?
1
vote
1answer
213 views

Counting Sort Not Working

I am trying to implement the counting sort algorithm. Based on the psuedocode from Introduction to Algorithms (and if you don't know it it's just a book), I came up with this code: var countingSort = ...
2
votes
2answers
120 views

Finding continuous subsequence that minimizes the average of the rest of the array?

Suppose there's an integer array arr[0..n-1]. Find a subsequence sub[i..j] (i > 0 and j < n - 1) such that the rest of the array has the smallest average. Example: arr[5] = {5,1,7,8,2}; Remove {...
0
votes
2answers
55 views

Efficient Program to count values less then A[i] to the left of i

Can someone provide NlogN complexity efficient Program to count values less then A[i] to the left of i , I know how to do in n square. If possible provide link.
3
votes
3answers
2k views

datastructure behind browsing history

Im writing a QML file browser. Now, I want to implement a back and forward function. This function is similar to browser back and forward functionality. Example : I start in "/home/text/folder1" and ...
1
vote
1answer
159 views

Dynamic Programming: Why can't we solve minimum no. of coins required to form a change with the concept of 0/1 knapsack?

Concept of 0/1Knapsack- Fill a knapsack of W weight using given weights.Aim is to maximize the profit. Ans-Problem can be solved by either taking a particular weight or not taking a particular weight,...
4
votes
3answers
2k views

calcuating the sum of nodes in a single verticle line of a binary tree

For a binary tree i want to get the sum of all nodes that fall in a single verticle line.I want the sum of nodes in each verticle node A / \ B C / ...
0
votes
3answers
55 views

Sort array of numbers (first number will be dynamically selected and remaining array should be sorted ascending)

int[] array = {1,1,0,1,2,2,0,0}; int firstNumber = 1;// dynamic can be 0 or 1 or 2 int numberOfOccurances = 0; //Basic sort functionality for(int i = 0 ; i< array.length; ++i) { if(array[i] ==...
1
vote
1answer
479 views

How to remove the last input element in an array?

For this program, I have a user input elements into a 5 space array using the private static void add() method. After adding these elements, the user is then able to use the private static void delete(...
0
votes
1answer
45 views

Check correctness of the algorithm for minimum number of jumps in an array

I have an algorithm that is based on a problem where you need to find the minimum number of jumps to reach the end of the array.This problem was asked in geeks for geeks and they do not have a linear ...
2
votes
2answers
1k views

Longest Contiguous Subarray with Average Greater than or Equal to k

Consider an array of N integers. Find the longest contiguous subarray so that the average of its elements is greater (or equal) than a given number k. The obvious answer has O(n^2) complexity. Can we ...
3
votes
2answers
87 views

Looking for a limited shuffle algorithm

I have a shuffling problem. There is lots of pages and discussions about shuffling a array of values completely, like a stack of cards. What I need is a shuffle that will uniformly displace the ...
0
votes
5answers
2k views

Maximum of all possible subarrays of an array

How do I find/store maximum/minimum of all possible non-empty sub-arrays of an array of length n? I generated the segment tree of the array and the for each possible sub array if did query into ...
3
votes
1answer
92 views

Finding out two such elements out of an unsorted array which have minimum difference without sorting the array

We have been given an array of integer : 45, 10, 56, 42, 95, 78. We have to find the two elements which have the minimum difference. I did that in linear time complexity but it works only on the ...
-4
votes
3answers
94 views

How to replace the elements of a range less than k with k?

How do I replace the elements in the range of an array greater than k by k when the number of queries are high? Given that each query is given by the form l r k; where [l...r] is the range of the ...
-5
votes
2answers
133 views

Finding the largest subscript of an array in a unssorted and repetitive array

I have an unsorted & repetitive array like {1,1,2,2,3,3,5,5,4,4,4} , I would like to find the largest number in the array and its largest position .For example 5 is repaetd twice and i would like ...