An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
-5
votes
0answers
15 views
Can anyone solve this for array matrix by using java plz [on hold]
Create a class in java for a N x N array (matrix) which includes methods to do the
following:
1- To check whether the immediate neighbhours of central element are same or
not
2- To store immediate ...
-1
votes
1answer
33 views
QuickSort Median of Three V2
Unfortunately, my earlier question was put on hold due to "code that doesn't work", but I realised that there were holes in my question, so here it is again, revamped.
You probably know the QuickSort ...
3
votes
2answers
56 views
Dijkstra's algorithm using priority queue running slower than without PQ
I need to implement dijkstra's algorithm and I've done so using this Wikipedia page. I've done it both with priority queue and without.
Both versions work 100% correct, however I need the faster one ...
3
votes
0answers
28 views
Knapsack greedy algorithm in Python
I implemented the well-known knapsack problem and now I would like to improve it using list comprehension or lambda. I don't want to use NumPy. Could you help me?
...
5
votes
1answer
250 views
Get nth node from end in a linked list
Description:
You’re given the pointer to the head node of a linked list and a
specific position. Counting backwards from the tail node of the linked
list, get the value of the node at the given ...
4
votes
2answers
104 views
Bubble Sorting Algorithm
I made a simple Python program which takes a list of unique numbers and orders them from smallest to greatest. The algorithm (which is actually quite similar to bubble sort) works like this:
Take the ...
-1
votes
0answers
18 views
Depending on the frequency sort the array [on hold]
Depending on the frequency sort the array
I know the code is not optimised nor it works want to know a way to get the output
input = 3,5,3,5,3,4
output=3,3,3,5,5,4
...
1
vote
1answer
38 views
3 sum implementation in python 2.7
Working on below problem, looking for advice on code style, functional bug and time complexity improvements.
Also looking for advice in,
If array is not sorted, any solutions available for O(n^2) ...
-1
votes
0answers
8 views
Getting WA in SPOJ-PICAD [on hold]
I am trying to solve this problem.
PROBLEM STATEMENT
Sherlock Holmes is carrying out an investigation into the crime at Piccadily Circus. Holmes is trying to determine the maximal and minimal ...
7
votes
2answers
103 views
Find the Sum of Digit XOR till N?
The question is to find the sum of Digit XOR till \$N\$.
For eg:-
Digit XOR of a number 112
112 => 1 xor 1 xor 2 = 2
So we need to find
...
2
votes
1answer
88 views
Find smallest subset prefixes
Here is my code for this problem. Any bugs, performance in terms of algorithm time complexity, code style advice are appreciated.
The areas which I want to improve, but do not know how to improve are:...
3
votes
1answer
52 views
Huffman tree decoding
Description:
Huffman coding assigns variable length codewords to fixed length input
characters based on their frequencies. More frequent characters are
assigned shorter codewords and less ...
6
votes
6answers
210 views
Finding the most popular element with C# given O(1) space complexity requirement
I have tried to solve the following problem in C# with LINQ:
Given an integer array, find the most frequent number and it's count in the array. If multiple numbers have the same highest frequency ...
0
votes
1answer
38 views
Logarithmic growth counter
I'm looking for a way to have an logarithmic incremental counter. The counter goal is related to a numeric value than goes from 1 to Infinity. The counter maximum value should be 1000.
The sequence ...
0
votes
0answers
152 views
Drawing Book Hackerrank
Description:
Brie’s Drawing teacher asks her class to open their -page book to page
number . Brie can either start turning pages from the front of the
book (i.e., page number ) or from the back ...
0
votes
0answers
45 views
Parsing string and extracting meaningful information [closed]
Given a string containing user names(length 2) or Group names(length 2, 13) followed by the optional comment I have to parse the message and extract meaningful information from it.
Example:
Valid ...
5
votes
1answer
83 views
Markov Chain in Python
For learning purposes, I'm trying to implement a Markov Chain from scratch in Python.
The goal is, provided a file with a list of words, and some sequence, to predict the next letter according the ...
-4
votes
0answers
36 views
Solving a linear peg solitaire game
I'm trying to code a solution for Linear Peg Solitaire Game.
By linear, I mean that the board is a single dimensional array with 1 hole in it. And pieces only either move for left to right or right ...
-2
votes
1answer
42 views
1
vote
0answers
26 views
Optimizing string concatenations and checking for unique subsets
This code accurately completes my task. However, it does not scale very well. I have been doing my best to optimize the big O complexity; however, I'm not able to make any big improvements. I have the ...
3
votes
1answer
50 views
Find first bad version (part 2)
This is new code based on some new ideas to reduce calls to is_bad_version. Refer to the previous version of the code discussion from here.
Any smarter ideas for ...
2
votes
0answers
19 views
Sparse matrix compressed sparse row (CSR) in Python 2.7
Brief introduction for CSR:
The compressed sparse row (CSR) or compressed row storage (CRS) format
represents a matrix M by three (one-dimensional) arrays, that
respectively contain nonzero ...
0
votes
0answers
20 views
Minimal length of substring that forms cyclic string
there is a knows problem stated as "Cyclic string":
A string S has been recorded many times, and then from the resulting line took a substring, and give to you.Your task is to determine the minimum ...
6
votes
2answers
591 views
Algorithms to find statistical information of an array
I was working on a program that creates an array, then gives statistical information about that array such as std dev, median, mode average etc.
I have written a method for each statistical value, ...
1
vote
1answer
68 views
Implementing a queue using a circular buffer
I'm working on implementing a queue using a circular buffer problem. Any smarter ideas for better time complexity, any code bug or code style advice are highly appreciated.
...
1
vote
1answer
59 views
find first bad version
Working on below find first bad version problem, post my code in Python 2.7, any smarter ideas for better time complexity, any code bug or code style advice are highly appreciated. My major idea is ...
2
votes
1answer
56 views
Moving average in Rust
I worked on implementing a simple moving average function in Rust, that works on a collection of year/revenue tuples:
...
0
votes
0answers
30 views
Algorithm design for CodeChef - Base Attack
I was working on a CodeChef challenge: Base attack. I have a working solution but it is not fast enough. Could you recommend anything to make it faster?
...
4
votes
1answer
63 views
Simple Calculator in Java and Swing
I'm new at this and I'm almost done with learning basic Java. I made this just to see if I could do it. I just want to know if there's anything I can do to make it better.
...
-4
votes
1answer
39 views
4
votes
2answers
504 views
Count occurrences of a given number in a list
I have written a solution to find the occurrence of a common number in a n lists. I am just concerned if this is the best solution? Please suggest the best way to do it.
...
5
votes
2answers
51 views
Valid palindrome solution
I'm working on this valid palindrome problem. Any advice on code bug, better idea for low algorithm execution time complexity, code style, etc. are highly appreciated.
Problem
Given a string, ...
3
votes
1answer
45 views
Time limit exceeded on finding out the GCD and LCM of a Python list
I'm doing this HackerRank exercise which expands on the problem on finding the GCD and LCM of a list.
Assuming lists a and b as ...
3
votes
3answers
86 views
Circular shift string
So,basically I am working on an algorithm for circular shifting a string upto a position.There are two parameters required here.
String of text to shift
Number of shifts required.
For example:
<...
6
votes
1answer
86 views
Classic merge sort (part 2)
(Initial discussion from Classic merge sort, since it is new code, I start a new thread)
Post my code below, my major question is, I have to create another array ...
2
votes
1answer
50 views
The fastest “find”er of them all?
For part of a project, I was given this directive:
The find command will use the directory (dirent) function calls, but
will require you to start at a directory and then determine if a file
is ...
0
votes
1answer
23 views
Codilty, binary gap get the highest gap
I have written a solution for the codility task which is Binary Gap.
finding the highest gap in Binary. for example 1041 is the input number and convert it to ...
3
votes
1answer
61 views
Serialize and deserialize binary tree
Here is my code of serialize and de-serialize a binary tree, looking for advice. One issue in my mind which I cannot resolve is, I am using a global variable index, wondering if more elegant solutions ...
2
votes
2answers
51 views
Finding the longest path in an equally-weighted tree
Its purpose is to find the longest path in an equally-weighted tree denoted as such:
3
1 2
2 3
The first number denotes both the number of vertices and the ...
5
votes
1answer
51 views
Search a file (simple grep clone)
For part of a project, I was given this directive:
The grep command will take an exact string and an exact filename as
the 2 arguments and you will need to search through the filename for
the ...
3
votes
2answers
83 views
Percolation using quick union connectivity algorithm
This is my attempt to solve the percolation problem for the Princeton Algorithm-I course, the problem is well-defined here. My implementation is in C++ not Java and the following procedure is followed:...
6
votes
1answer
109 views
List directories (with information)
For part of a project, I was given this directive:
The “ls” command will require you to use directory functions to list
out what the filenames are in the directory. Functions required to
make a ...
3
votes
2answers
66 views
Dijkstra's algorithm using a specific structure
I have implemented Dijkstra's algorithm using a slightly modified version of the structure and class posted here. Unfortunately, I have ruined the efficiency. I am intent on using this structure. I ...
14
votes
1answer
161 views
QuickMergeSort — The power of internal buffering
Amongst the lesser-known unstable sorting algorithms is the QuickXsort family of algorithms, as described in QuickXsort: Efficient Sorting with n log n - 1.399n +o(n) Comparisons on Average by Stefan ...
3
votes
0answers
19 views
Closest points using Rabin randomizing approach
I was told to use the following Rabin algorithm to find the shortest distance between 2 points in 2D:
Randomly choose sqrt(n) and brute force to find the closest ...
2
votes
1answer
106 views
HackerRank NCR codesprint: Spiral Message
Problem statement:
https://www.hackerrank.com/contests/ncr-codesprint/challenges/spiral-message
You've intercepted an encoded spy message! The message originated as a
single line of one or more ...
1
vote
1answer
42 views
Quicklyish finding primes by trial division
I ask because it seems to be taking waaaay more space/time than it should. Also, I know that if I want to be super efficient I should use Java's BitSet and a Sieve of Eratosthenes, but I wrote it like ...
2
votes
0answers
65 views
HackerRank - Matrix Rotation
Problem statement
Given a matrix (up to 300 × 300), rotate each element R steps anti-clockwise along concentric rectangular paths (R up to 109).
The algorithm is rated as hard on HackerRank.
On ...
2
votes
1answer
121 views
Given an unsorted integer array, find the first missing positive integer
Here is the problem, any advice about code improvement, more efficient implementation in terms of time complexity, functional bugs, etc. are appreciated.
Given an unsorted integer array, find the ...
0
votes
1answer
50 views
Sort Stack using just an additional stack
The problem is from the cracking coding Interview programming book.
The problem is that:
Sort Stack: Write a program to sort a stack such that the smallest
items are on the top. You can use an ...