Reversing a number

I was working through a programming challenge I found on Reddit and it seemed easy enough: Find all numbers less than \$10^n\$ where the number and its reverse (the reverse of \$123\$ is \$321\$) are both divisible by \$7\$ and then sum them all together. def challenge_229(power): numbers = [] for x in range(0, 10**power, … Read more

HackerRank Implement Queue using two stacks Solution

Challenge Problem Statement – Implement a Queue using two Stacks I implemented dequeue() in O(1) at the cost of enqueue() in O(n) #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <stack> using namespace std; struct Queue{ stack<int> s1,s2; int dequeue(){ int x = s1.top(); s1.pop(); return x; } void peek(){ cout<< s1.top()<<“\n”; … Read more

CodeFights Quora bot

This isn’t any homework questions or assignments, only my practice. In the CodeFights Quora bot. My code cannot pass even the first hidden input because of the execution time. I’m only a first-month programmer in university. Here is the detailed question: For the purposes of this problem, suppose that Quora has n questions, and question … Read more

C++ 8-Sliding Puzzle Solver is very slow

I have made a sliding puzzle solver which is currently for 8-puzzles only. It works too slowly even though it is based on an A* algorithm whose f-score is calculated on the basis of Manhattan Distance and Linear Conflicts. But it solves all the cases correctly. I would like to improve the time and space … Read more

KQuery SPOJ problem, using merge sort segment tree

Problem : KQUERY Summary: For each query consisting of \$(i, j, k)\$, return the number of elements greater than \$k\$ in the subsequence \$a_i, a_{i+1}, …, a_j\$. My code has complexity of \$O(n\log^2{n})\$. I am only interested in segment tree based solution of Kquery. Please let me know why am I getting TLE using merge … Read more

Terminated due to timeout Mapping phone numbers

I have a code based on the following problem “Mapping phone numbers to names”, the problem is that when they are going to process large data(+100.000) the program ends by timeout. my code and problem is this, how can I optimize my code to consume less time? my code function processData(input) { var r=input.split(/\n/),o=Number(r[0]),c=r.length-1,x=[]; r.splice(0,o+1); … Read more

Project Euler Problem 12 in Python 3 (Find triangle number with 500 divisors)

I am doing Project Euler, and 12 is the one that takes significantly longer then everything else. Here is my code: # The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be # 1 + 2 + 3 + 4 + 5 + 6 + 7 … Read more

Checking if the whole number is prime, and if all its digits are too

That was an easy one to solve but the problem lies in the efficiency since it checks every single digit of the number while converting to integers. I’m receiving TLE (i.e. Time Limit Exceeded) on the site so I have to increase its performance. So its hard to explain but the exercise asks for the … Read more