# Questions tagged [algorithm]

An algorithm is a sequence of well-defined steps that defines an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

92,978
questions

**0**

votes

**0**answers

5 views

### Pseudocode, Algorithm and Flowchart Questions

Computer programming concepts - algorithm, pseudocode and flowchart
I am new to programming. For the following questions, I have researched online and watch a few YouTube videos to attempt the ...

**1**

vote

**0**answers

20 views

### How could I make my lineOFsight and Theta star algorithm take the right descion and more flexible?

I'm working on implementing the Theta star, by considering the height of obstacles and divided the height into three categories. First category: higher than my Drone_height, second one: risk zone, ...

**0**

votes

**0**answers

10 views

### How to calculate passed (driven) distance -real time- with android and without Google MAP API

I am trying to develop an android app for drivers.
I want drivers would be able to check how many kilometers or meters passed (in real-time fashion). I know google map has API for that, but I want to ...

**0**

votes

**0**answers

24 views

### how to manage multiple sources of truth (i.e. offline apps) [on hold]

offline strategy conundrum
device A has items 1, 2 and 3
device B has items 1, 2 and 3
database has items 1, 2 and 3
both devices work offline and can create new items
device A deletes item 3 and ...

**1**

vote

**1**answer

37 views

### Build a min max stack using a linkedlist

Problem
The idea is to construct a MIN MAX stack that can do the following operations in constant time.
Push
Pop
Peek
getMinValue
getMaxValue
My Approach
My idea is that I have ...

**-1**

votes

**0**answers

29 views

### Java HashMap GET Method [duplicate]

Map<String, Word> map = new HashMap<>();
for(String wd : words){
if(map.containsKey(word)) map.get(wd).cnt++;
}
When I try to change the value of the HashMap through map.get method, ...

**2**

votes

**0**answers

73 views

### How to make a recursion function on a complicated equation? [on hold]

I need to make a recursive program which can find the answer to the following equation:
P1(k) is a simple binomial distribution equation.
And the equation, Pn(k), will recursively call itself until ...

**0**

votes

**2**answers

82 views

### Find a number by given digit

Let¡¯s say I have a series of numbers like:
12345678910111213141516... (until unlimited)
Then I would like to get a number from it by given digit. For example:
Digit 10th: 1
Digit 17th: 3
...
I ...

**0**

votes

**0**answers

24 views

### Is there a more efficient way to encode and contract a sparse tensor?

I have a sparse rank-3 tensor beta which I need to contract repeatedly against a rank-2 tensor alpha. I am currently encoding the nonzero tensor elements as a linear list over which I can easily ...

**-5**

votes

**1**answer

38 views

### I am teaching myself data structures and algorithms. Can you plz tell me the topics I need to cover? [on hold]

Can youplz tell me the topics that I need to learn in data structures. I wish to know this because I am learning from the internet and need to make sure that I have covered all the topics. Also can ...

**-4**

votes

**0**answers

32 views

### The way of thinking in multiply 2 natural numbers (problem solving¡±)

Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants c ¡Ý 2.
function multiply(y,z) comment Return the product yz.
1. if z = 0 then ...

**0**

votes

**1**answer

29 views

### Whats the good way check and compare data (time limit) from database in Java?

So, I have a mysql db, and lets say we have donation table, and I have a Java application.
My donation table have columns such as: user_id, tier, start_date, and end_date. Both start_date and end_date ...

**0**

votes

**1**answer

48 views

### The Randomized algorithm Parody

I was reading CLRS and I was stuck at question 5.1-2.
Describe an implementation of the procedure RANDOM(a,b) that only makes calls to RANDOM(0,1).What is the expected running time of your procedure, ...

**2**

votes

**1**answer

115 views

### Haskell - Grouping specific nearest neighbours in a cartesian grid

I'm after some direction in this puzzle where I need to group specific nearest neighbours together.
my input data is:
myList :: Map (Int, Int) Int
myList =
fromList
[((-2,-2),0),((-2,-1),0),((-...

**-3**

votes

**0**answers

20 views

### 0-1 knapsack problem with multiple weight and value pairs in each item [on hold]

standard 0-1 knapsack problem has a knapsack and a number of item, and all the items put together is basically a dict, each item has one weight and value pair. Suppose now I have multiple weight and ...

**-1**

votes

**1**answer

25 views

### Conditional Multiplication and Cumulative sum

Recently I came across a coding question and I wanted to find the algorithm for it.
Array is 1-based array.
Query types:
1 L R X K : Multiply from L to R with K if it is less than X
2 L R: Print ...

**-3**

votes

**0**answers

30 views

### Firefly algorithm for bin packing problem [on hold]

How can I apply firefly algorithm to optimize the solution of bin packing problem? I want to know how can I set the parameters of bin packing problem in firefly algorithm and what should I consider a ...

**0**

votes

**3**answers

54 views

### At most one field out of n is not null

I have n fields in a class. At most one of them is allowed to be set, and so I need to throw an exception if that's not the case. I know the value of n.
I can do this the obvious way:
if (field1 ...

**0**

votes

**2**answers

44 views

### Find the nth largest element in an unsorted read only data struture with no extra space

I need to find the nth largest element in a range, which is not random access range, with O(1) extra space. The heap method takes too much space. I found a solution How to find the Kth smallest ...

**-1**

votes

**1**answer

45 views

### MinAbsSum: Given array of integers, find the lowest absolute sum of elements [on hold]

I get stuck with a problem in the Codility that I want to solve and also, collected a solution for the problem. The problem is provided below,
For a given array A of N integers and a sequence S of ...

**3**

votes

**1**answer

67 views

### How to delete consecutive elements in a linked list which add up to 0

I'm writing a Python code to delete those consecutive elements in a linked list, which add up to 0
The linked list is defined as follows:
class Node:
def __init__(self, val, next=None):
...

**-1**

votes

**1**answer

22 views

### Preventing data loss in client authoritative database writes

A project I'm working on requires users to insert themselves into a list on a server. We expect a few hundred users over a weekend and while very unlikely, a collision could happen in which two users ...

**-2**

votes

**0**answers

21 views

### How to create a combinatory non binary tree? [on hold]

I have a problem (search algorithm) that I would like to explore using DFS (depth first search) to review all the possible outcomes of combinations of L elements from a list distributed across G ...

**-1**

votes

**0**answers

23 views

### How do you do Quickselect using Lomuto Partition properly? [on hold]

I am doing a problem in class using QuckSelect using the Lomuto Algorithm: No Coding is needed - Just need to show how to do the work . I'm not sure if I misunderstood or what I am doing wrong but ...

**1**

vote

**1**answer

63 views

### Detecting diagonal in a row win - tic-tac-toe, gomoku

Within a game on Gomoku a player has to get 5 in a row to win. Detecting diagonal win is a problem.
I have tried the code below which searches a 2d matrix from top right until it finds a player token ...

**1**

vote

**2**answers

49 views

### How can I input data to array and print them through function?

I am trying to input data to an array and then print them by using a function but I am not sure how to organize the code.
#include <iostream>
using namespace std;
void showGrade(double grade[],...

**-1**

votes

**1**answer

28 views

### Regex number generator for certain time range?

I'm trying to get a regex number generator for a range of 9 a.m to 9 p.m.
I've tried this (9|1[0-9]|2[01]) but this is 9 to 21 (which is 9pm military time). The solution I have may be the answer but ...

**-1**

votes

**1**answer

40 views

### Is there an easy way to work out the Big O value for time and space complexity? [duplicate]

I have been learning about sorting algorithms and understand the idea of time and space complexity. I was wondering if there was a fairly quick and easy way of working out the complexities given the ...

**0**

votes

**1**answer

35 views

### Algorithm to find change in direction in raster

I have a raster containing 0 and 1 as a numpy array, all pixels with a 1 are connected forming a line. This line is 1 pixel thick at all places. Using a pathfinding algorithm I have a list of the ...

**-1**

votes

**0**answers

82 views

### maximum array length of '1' after flipping at most k, '0' using recursion if array has values only '1' or '0' [on hold]

Given an array of size n with 0s and 1s , flip at most k 0s to get the longest possible subarray of 1s.
I am trying to think a recursive approach to solve this problem.
#include <bits/stdc++.h>...

**-3**

votes

**1**answer

37 views

### how to implement an engine that do NOT limit the order count of an user? [on hold]

I found that some exchange does not limit the order count of an user. e.g.
an user can create 10000 orders in "ethusdt" market.
If there are 1000 such users, the total orders count will increase to ...

**1**

vote

**1**answer

47 views

### How could I calculate the distance between the parent and neighbors node when there is a line of sight between two points?

I have completed my Astar algorithm in Python and now I need to convert it to a Theta star algorithm, I have built my line of sight algorithm below, but when I come to my Theta star algorithm I face ...

**0**

votes

**0**answers

62 views

### implement custom comparator to sort words in java? [on hold]

I was asked below question in an interview:
Implement a method that sort words, but instead of using the normal
alphabet a, b, c, ..., x, y, z we have ch that goes between h and i
in the sort ...

**0**

votes

**1**answer

64 views

### How to fix problem with array overload in while loop

I'm trying to fix a SIGSEGV error in my program. I am not able to locate the site of error. The program compiles successfully in Xcode but does not provide me the results.
The goal of the program is ...

**0**

votes

**1**answer

43 views

### Get coordinates of polygon outline and polygon mask given vertex coordinates

Given a matrix containing a polygon mask (here a small and simplistic case):
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
...

**1**

vote

**1**answer

41 views

### Why do we need to check for a pattern match everytime the hash value is same in Rabin Karp algorithm

I don't see the reason why we need to check for a substring match every time the hash returns the same value for the pattern and the text. Isn't the hash value returned unique for a string?

**1**

vote

**2**answers

66 views

### Sorting using two arrays

There are two arrays:
The first one contain numbers, and the second one contains "weight" of the first array values.
It works like this:
arr1 = [56,65,100,89,180,90];
"Weight" of the numbers are ...

**2**

votes

**1**answer

47 views

### Time and space complexity of Ruby Anagram algorithm

I have written this algorithm here and I am trying to evaluate its time and space complexity in terms of Big-O notation. The algorithm determines if two given strings are anagrams.
def anagram(str1, ...

**-1**

votes

**1**answer

24 views

### How change location of the TextView depending on content

I have two TextView side by side in a LinearLayout horizontally
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:...

**0**

votes

**1**answer

52 views

### How to generate all combinations given an array of elements using backtracking? [on hold]

Given an array, generate all combinations
For example:
Input: {1,2,3}
Output: {1}, {2}, {3}, {1,2}, {2,1}, {1,3}, {3,1}, {2,3}, {3,2}, {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, {3,2,1}
I am ...

**0**

votes

**3**answers

55 views

### How can I increase the period of this PRNG?

I found this pseudo random number generator on Wikipedia and gave it a try, it's fast and works great for what I intend to use, but I would like it to have a bigger period, is there a way of improving ...

**-1**

votes

**0**answers

18 views

### Can we categorize the analogy of “using predefined library in your project” as applying dynamic programming in real life [on hold]

I was just going through real life applications of dynamic programming.
I am using predefined library for my project. Is my analogy of classifying this as real life application of dynamic programming ...

**-3**

votes

**1**answer

49 views

### How to calculate the overhead of an amount

I would like an algorithm to calculate the overhead of an amount based on
Scale table :
< $10,000 => 13%
$10,001 - $100,000 => 8%
$100,001 - $1,000,000 => 6%
$1,000,001 - $5,000,000 => 4....

**3**

votes

**2**answers

55 views

### Ugly Number - Mathematical intuition for dp

I am trying find the "ugly" numbers, which is a series of numbers whose only prime factors are [2,3,5].
I found dynamic programming solution and wanted to understand how it works and what is the ...

**-1**

votes

**2**answers

110 views

### Is it possible to check if word stored on stack is palyndrome without ANY additonal data structures in C++

In C++ programme, let's analyse stack data structure.
My question is : Is it possible to check if the given stack contains palindrome or not WITHOUT ANY additional data structure and without ...

**-1**

votes

**1**answer

20 views

### Shortest path in an edge-weighted DAG with multiple source vertices?

Given an algorithm A that computes the longest path from a source vertex s in a DAG G with non-negative edge weights. What is the minimum number of times required to run the algorithm A to find the ...

**0**

votes

**0**answers

48 views

### Algorithm for avoiding name collision

I have a collection of objects in an STL vector that are sorted on a member of those objects. Let¡¯s say for the sake of simplicity that this vector is as follows, sorted on integer ¡°string¡± values:
[...

**0**

votes

**2**answers

33 views

### Python - Searching items from one list in another [on hold]

I have a N x 2 list of whereabouts (distance from a reference point) and a second list of approximate places, of the same type (error on these approximate locations is not known). I need to match the ...

**-3**

votes

**0**answers

39 views

### I develop program to find first day of given year with respect to Gregorian Calendar [on hold]

I want to know is it safe or accurate if I use following method to find first day of given year, using GCC with Code block
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (...

**-1**

votes

**0**answers

38 views

### highest song volume using array

We are given a count of songs to be played ¨C n, highest volume allowed ¨C h, initial volume ¨C i, and list of allowed volume change A[] of size n. The singer can either increase/decrease the volume of ...