I have a sorted array of numbers with array length as n. For a given item k find index i in the sorted array where elements from index i to n are greater than the given item k. If there is no index present then return -1 This is my program: The time complexity of this approach is O(n), and

# Tag: algorithm

## Algorithm to get the submultiples of each number in a range

I have this exercise in mind, I keep trying to solve it in Java but I’m not able to create a correct algorithm to get the submultiples of each number in a range, the start and the end of the range is user defined. Besides, the number 1 doesn’t count as a submultiple. For example, in a range between 3

## How to update Sorting Algorithm Visualization?

I have been making a sorting algorithm visualizer in Java using Java Swing and AWT and coded insertion sort to see if it would be able to work. The algorithm works in the sense that when this program is run you can shuffle and sort the algorithm using insertion short but you don’t actually see the algorithm in action. It

## Moving character efficiently on a row

Given input (items = 6, position = 3) creates a row of 6 items and a character positioned on item 3 {0,1,2,[3],4,5} A call to left() moves the character two positions to the left and the item at position 3 is removed {0,[1],2,4,5} The next call to right() moves the character two positions to the right and the item at

## Find the first Recurring letter in Java

I wrote the code as follows but it doesn’t return the first recurring letter properly. Example: In the word “statistics” the recurring letters are s, t, and i. But the letter t recurs sooner than the letter s and i, but my program returns s instead of t. What do I need to do for it to return t, using

## Time limit exceeded in hackerearth exercise

I’m trying to solve this exercise in HackerEarth. But I have an error of time limit exceeded. This is the code that I wrote : Answer Your solution is a little bit slow because of the bad implementation. I rewrote your solution in better implementation with the same logic and time complexity of your solution and get Accepted and none

## AlgoExpert: Validate Subsequence, not passing all test cases

Question: Validate Subsequence on AlgoExpert. Given two non-empty arrays of integers write a function that determines whether the second array is a subsequence of the first one. My code is not passing all test cases, what am I doing wrong? Answer Thanks for everyone’s help! Updated Solution:

## How does parameters of a method get stored in stack during a recursive call?

I was doing a leetcode question https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/ and I am confused on how parameters for a method get stored during a recursive call. If a node gets visited, I wanted to save it’s state that it was visited. When I send two variables, when the stack is getting unwinded the updated variable values are being lost. But if I send

## Check if array is sorted using recursion

I am getting true as answer even for unsorted(isNonDescending) arrays. Where is the bug? I want to break the array into smaller problem from the start of the array only. //Check for isNonDescending. Answer Instead of passing the size of the array as a parameter, which makes no sense anyway, because you can simply call arr.length, you should pass a

## Code that takes a string and recognizes the number of consecutive letters

To do my job, I need a code that takes a word from the user, then recognizes the number of consecutive letters and outputs it in such a way that it prints the letter and the number of times it is repeated. Example 1 input: Example 1 output: Example 2 input: Example 2 output: Answer Here is one way. You