Complexity is the analysis of how the time and space requirements of an algorithm vary according to the size of the input. Use this tag for reviews where the "Big O" is a concern.

learn more… | top users | synonyms

4
votes
4answers
338 views

Find element that has been removed from a shuffled array

For a function to determine which element has been removed from a shuffled array, I came up with: ...
1
vote
1answer
61 views

MaxCounters solution

I am doing this Codility problem You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all ...
1
vote
1answer
39 views

Finding the distance between every indices in an array that contain odd numbers

I'm doing this HackerRank problem which basically boils down to what I've written in the title. We have to find out the distance between all the odd values in the array. I've written this code below ...
4
votes
1answer
51 views

Lexicographically smallest wave like array without sorting

Given an array of integers, sort the array into a wave like array and return it. In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... Example: Given [...
3
votes
1answer
55 views

Taxicab numbers algorithm check

With the programming language skills that are available to me at the time, I've written this program to find the "taxicab numbers" (e.g. a number expressible as the sum of two cubes in two different ...
13
votes
7answers
413 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 ...
1
vote
1answer
30 views

Dividing a ThreadPool class into component classes to reduce/isolate complexity

I have a ThreadPool class which has grown to a point where it is becoming difficult to keep all of this unit of work in mind. It is also becoming harder to read. ...
3
votes
1answer
181 views

Finding line numbers of duplicate lines in a log file

I have a log file with a million lines, and my task is to code something that outputs the lines that have duplicates, and the line numbers. I've thought of two ways to approach this : 1) Use python'...
3
votes
0answers
55 views

Word Wrap via Dynamic Programming

Word Wrap problem in short: Given n words and a line length, break the words into lines in such a manner that minimizes the sum of the costs of all the lines. Consider the cost of each line being (...
1
vote
2answers
94 views

Massive file sorting algorithm

I was trying to sort a massive file of successive chars in C. I did some research and found a few file sorting algorithms that look the same. Their main idea is to read an amount of data to memory, ...
3
votes
1answer
58 views

Finding substrings within arrays

I need to find the substrings within my array. If I have an array: ["abc", "abcd", "abcde", "xyz"], my method should return the array members: ...
-1
votes
1answer
80 views

Complexities: Filtering out nested array inside an array

I am doing some iteration over an array which have another set of array (the nested array). I need to .map() the outer array in such a way that it should filter out ...
2
votes
1answer
109 views

Find the longest subarray with sum less than K

The problem consist of finding the longest contiguous subarray with a sum less than a given integer K. The input data are: K - ...
3
votes
1answer
503 views

Find the number of K-Complementary pairs in an array

Below is the program to find k-complementary pairs: K = A[i] + A[j];. ...
3
votes
1answer
118 views

Calculating binary tree height as asked in the interview

Input: The first line contains the number of vertices \$n\$. The second line contains \$n\$ integer numbers from the range \$\left[−1, n − 1\right]\$ representing the \$0\$-based index of the ...
0
votes
0answers
83 views

All combination of character alphabet

I write this code to find all combination of an alphabet z={A,B,C...N}. In DNA case,we use z={A,C,T,G},then the function Words and ...
0
votes
1answer
106 views

Subset sum simple iterative implementation

Here is the simplest iterative implementation of Subset sum problem that I could come up with1, as a follow up to this recursive implementation of the same problem: ...
1
vote
1answer
218 views

HackerRank: Left Array Rotation in Python

Here is the problem on HackerRank. I want to know how I can improve this code. I am mainly trying to improve the following skills: Documentation, functional programming (top-down approach), accurately ...
4
votes
1answer
99 views

Modified Z-Algorithm for string similarity with one char difference

I have modified a Z-Algorithm for string similarity in order to match strings which differ in 0 or 1 char. However, it seems much slower than original Z-Algorithm although from my point of view there ...
3
votes
1answer
53 views

Infinite Monkey Theorem Python Comparison

I wrote code to demonstrate the Infinite Monkey Theorem which states that if a monkey types at a keyboard for an infinite amount of time it will almost surely type a given text such as William ...
2
votes
2answers
106 views

'Zero out' a matrix using Python

Below is my code for an attempted solution to cracking the coding interview exercise 1.8 written in python 3.5. The problem statement is: Write an algorithm such that if an element in an MxN ...
3
votes
1answer
117 views

Rotate Matrix using Python

This is my solution to Exercise 1.7 from Cracking the Coding Interview. I would appreciate any feedback on coding style and algorithm efficiency. I do know that there is an existing function in ...
1
vote
2answers
511 views

Simple string compression in Python

This is my solution to exercise 1.6 in Cracking the Coding Interview. I am interested in receiving feedback on the coding style and time/space complexity. The exercise statement is: Implement a ...
4
votes
2answers
170 views

Check if two strings are one 'edit' apart using Python

The code below is my solution in python 3 to exercise 1.5 in Cracking the Coding Interview. I would appreciate feedback on both the algorithm (improvements to space and time complexity) and/or coding ...
3
votes
2answers
405 views

Check if a string is a permutation of a palindrome using Python

This is a solution to exercise 1.4 from Cracking the Coding Interview written using Python 3. Given a string, write a function to check if it is a permutation of a palindrome. Example: 'Tact Coa' ...
3
votes
3answers
169 views

URLIfy a character array using Python - follow-up

This is a follow up to my last code review: URLify a string using Python This problem has been reviewed in other languages, but I tried to solve this without looking at those answers. I am hoping ...
6
votes
2answers
301 views

URLify a string using Python

I have been working my way through the exercises in the book Cracking the Coding Interview. I wanted to get feedback for the following exercise. Write a method to replace all spaces in a string ...
5
votes
2answers
834 views

Check if one string is a permutation of another using Python

The code below is an attempt at a solution to an exercise from the book "Cracking the Coding Interview." I believe that the worst case time complexity of the code below is O(n), where n is the length ...
1
vote
1answer
74 views

Stack which finds the minimum element

Write a program to implement stack which implements min a method which gives the minimum element below the top element in the stack. ...
2
votes
1answer
110 views

Counting all substrings with exactly k distinct characters

Problem Statement : Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Example: Input: abc, k = 2 Output: 2 ...
1
vote
1answer
276 views

Finding a max element in an array in Java

Created the following method: ...
0
votes
0answers
52 views

Suffix tree code

I am always having confusion when it comes to run time complexity of an algorithm. I wrote this code for suffix array, I am not sure of the run time complexity of ...
2
votes
1answer
151 views

Iterative solution for generating all permutations of a string

I know the runtime for generating all permutations is supposed to be \$O(n!)\$. I'm not sure if the code I wrote for it runs in \$O(n!)\$ though. I'm having trouble analyzing its runtime. The ...
5
votes
2answers
87 views

Function to check truth value of a specific statement

I want to check the truth of A + B - (A & B) == A | B for N-bit numbers. For example, here's a "truth" table for 2-bit numbers. (I know I'm taking liberty with ...
7
votes
3answers
640 views

Algorithm to compute the n-th derivative of a polynomial in Python

As a personal exercise, I'm trying to write an algorithm to compute the n-th derivative of an ordered, simplified polynomial (i.e., all like terms have been combined). The polynomial is passed as an ...
11
votes
3answers
316 views

Finding Pythagorean triplet in array

We have an integer array as: private int[] arr = {1, 3, 5, 14, 18, 29, 78}; We have a function which takes three inputs of the array and checks whether: ...
3
votes
2answers
111 views

TopCoder problem “Substitute” - SRM 160 (Division II Level One)

I have solved the problem "Substitute" at Topcoder. A simple, easy to remember system for encoding integer amounts can be very useful. For example, dealers at flea markets put the information about ...
2
votes
0answers
62 views

Improving time efficiency of finding maximum area rectangles in a histogram

Here's my solution but I am looking for ways to improve the time complexity of solution. Also this question is categorized as being solved by stack so I would like to know how to approach it using ...
3
votes
1answer
102 views

Faster palindrome checker in Java

I wonder if there could be a faster palindrome checker in Java. The best one I have is O(n/2) which is O(n) eventually: ...
10
votes
2answers
167 views

Min sub array size with a given sum

How can the time complexity of the following code be improved (assuming it's \$O(N^2)\$)? What about style and patterns? This code is trying to find the minimum subarray size that adds up to given sum ...
3
votes
1answer
106 views

Finding the maximum distance from the starting node

I've tried everything to make my program faster but with 250 nodes my code takes around 9 seconds to print the result, and with 5000 nodes it took around 260 seconds. Is there a way to make my ...
3
votes
1answer
41 views

Creating List of String elements that occur a certain amount of times using only the List ADT

I am tasked with creating a list with no duplicates of all String elements, in this case first names, of an ArrayList. The method is passed a threshold, and the ...
3
votes
2answers
52 views

Given a set of random strings, write a function that returns a set that groups all the anagrams together in objective-c

I would like to get feedback on the solution and a question regarding Big O. I am first converting each character in the string into the array and that would be O(n) then i am sorting the array O(n ...
6
votes
6answers
876 views

Find maximum difference between values in an array

My task is to use the following pseudocode and improve it (make it run faster). Also I have to analyze the runtime of the given pseudocode and of my new code that i improved. What does this algorithm ...
1
vote
2answers
98 views

Summing all keys in the leaves of a tree

My program takes 35 sec to run example 2. How can I make my program faster? ...
-5
votes
1answer
86 views

Reverse a string [closed]

I use this for reversing a string, but it has O(n) : reverse(y.begin(),y.end()); How can I reverse a string in O(n/2) ?
4
votes
1answer
86 views

Local variable lookup table

I'm currently in the process of writing my own programming language and the Virtual Machine for the language is responsible for handing all of the local variable scopes. I need to support the ...
2
votes
2answers
3k views

JavaScript function to convert decimal number to binary

I was recently asked this in an interview for a front-end position and I came up with something like this which seems to work but is clunky. (Number.toString() was ...
1
vote
1answer
55 views

Return Object[] while connecting to DB once

I have written this code according to my requirements. But after finishing the code, it looks like there are too many loops. I have reduced it as much as I can. Can you find any improvements that I ...
3
votes
0answers
103 views

ACM-ICPC HackerRank challenge in C++

I was doing a programming question in C++ at HackerRank. Given a set of up to 500 bitstrings, each up to 500 bits long, the task is to find the pair a and b such that the bitwise OR contains the most ...