The post Find Nth-term of an arithmetic progression first appeared on Algorithms.

]]>**Example: **

a = 1, d = 2, n = 5 output: 9 a = 2, d = 5, n = 4 output: 17

**Solution: **

**Formula for finding nth term in an arithmetic progression:**

Nth term = a + (n-1)*d.

**Code:**

**Output:**

a = 1, d = 2, n = 5 5th term: 9 a = 2, d = 5, n = 4 4th term: 17

The post Find Nth-term of an arithmetic progression first appeared on Algorithms.

]]>The post Minimum time difference first appeared on Algorithms.

]]>Given a list of 24-hour clock time points in “HH:MM” format. Write a program to find the minimum minutes difference between any two time-points in the list.

**Example: **

Given hours: [00:00, 03:00, 22:30] minimum time difference: 90 Given hours: [01:59, 03:00, 21:50, 22:30] minimum time difference: 40

**Solution:**

- Convert all the given times to minutes and have these stored in a list.
- Now sort the minutes list in ascending order.
- Now get the difference between every consecutive time and keep track of the minimum.
- To cover the edge case when one time is before and after the 24:00 hours, in that case add 1440 (there are 1440 minutes in 24 hours ) to time which is before 24:00. For example if the first time is 1:00 and the second time is 23:30. So minutes are 60 and 1410 respectively. Now add 1440 to 60 which becomes 1500. Now the difference is 1500-1410 = 90 mins.
- Return the minimum difference.
- See the code below for more understanding.

**Code:**

**Output:**

Given hours: [01:59, 03:00, 21:50, 22:30] minimum time difference: 40

**Reference: **Leetcode

The post Minimum time difference first appeared on Algorithms.

]]>The post Find departure and destination cities from the itinerary first appeared on Algorithms.

]]>**Example: **

[Dallas, Austin], [Houston, Dallas], [Austin, Seattle] Output: Departure: Houston, Destination: Seattle [Dallas, New York], [Dallas, Austin], [Austin, Dallas], [New York, Seattle], [Seattle, Houston] Output: Departure: Dallas, Destination: Houston

**Solution: **

Construct a map with a city name as a key and an integer as a value. For any boarding, subtract the 1 from the value against the departure city and add the 1 to the value against the destination city in the map. Once you process all the boarding passes, on the map you will have only one city with count as -1 and only one city with count as +1. These are the first departure and final destination cities respectively.

**Code:**

**Output:**

Departure: Dallas, Destination: Houston

**Reference: **careercup

The post Find departure and destination cities from the itinerary first appeared on Algorithms.

]]>The post Rank Array Elements first appeared on Algorithms.

]]>**Example:**

Given array: [22, 11, 44, 66, 55] Rank: [2, 1, 3, 5, 4] Given array: [15, 12, 11, 10, 9] Rank: [5, 4, 3, 2, 1] Given array: [10, 20, 30, 40, 50] Rank: [1, 2, 3, 4, 5] Given array: [10, 10, 10, 10, 20] Rank: [1, 1, 1, 1, 2]

**Approach: **

- Copy the given into a temp array.
- Sort the temp array.
- Now iterate the temp array and store the element and its index into a Hashmap.
- Initialize the rank array.
- Now iterate the original array and for each element, get the index from the hashmap and store it in the rank array.
- Print the rank array.
- See the code below for more understanding.

Time Complexity: **O(nlogn)**

**Complete Code:**

**Output:**

Given array: [22, 11, 44, 66, 55] Rank: [2, 1, 3, 5, 4] ----------------------- Given array: [15, 12, 11, 10, 9] Rank: [5, 4, 3, 2, 1] ----------------------- Given array: [10, 20, 30, 40, 50] Rank: [1, 2, 3, 4, 5] ----------------------- Given array: [10, 10, 10, 10, 20] Rank: [1, 1, 1, 1, 2]

The post Rank Array Elements first appeared on Algorithms.

]]>The post Three Consecutive Odd Numbers first appeared on Algorithms.

]]>**Example**:

[2, 4, 1, 3, 4, 1, 3, 6] Three consecutive odds: false [2, 4, 1, 3, 4, 1, 3, 6] Three consecutive odds: true [2, 4, 1, 3, 4, 1, 3, 6] Three consecutive odds: true

**Solution:**

Initialize count = 0. Now iterate the array from left to right and for each element if the element is odd then increment the count by 1 else if the element is even then reset the count to 0. So during iteration if any time count becomes 3 that means there are three consecutive elements that are odd else count would have been set to 0. If iteration gets completed that means no we have not found three consecutive odd elements, so return false.

**Code:**

**Output:**

[2, 4, 1, 3, 4, 1, 3, 6], three consecutive odds: false [2, 4, 1, 3, 4, 1, 3, 6], three consecutive odds: true [2, 4, 1, 3, 4, 1, 3, 6], three consecutive odds: true

**Reference: **Leetcode

The post Three Consecutive Odd Numbers first appeared on Algorithms.

]]>The post Non-decreasing Array with one allowed change first appeared on Algorithms.

]]>**Non-decreasing array: **Array is called non-decreasing array when you traverse array from left to right, each element on the left is less than equal to the element on the right. So for any element at index i, input[i-1]<=input[i]<=input[i+1].

**Example:**

Input[] = [4, 5, 1, 7] Output: true Change 1 to 6 or 5 or 7 Input[] = [10, 5, 2] Output: false Input: [1, 2, 1, 5] Output: true Change 2 to 1. Input: [1, 1, 1, 1] Output: true No Change required

**Solution: **

We are allowed to modify one element and we need to check both the possibilities of increasing or decreasing of a number. For example, [4, 5, 1, 7], we will increase the element 1 to 6 where as in example [1, 5, 4], we will decrease the element 5 to 2 or 3. So Iterate the array from right to left and count how many elements we need to decrease if the element on the left is greater than the element at the right. Then iterate the array from left to right and check how many elements we need to increase if the element on the right is less than the element on the left. An important point to remember is – we will increase to decrease the element so that it will just pass the non-decrement condition so increase to decrease the element to make it equal to its neighbor element. Example – [1, 2, 1, 5], decrement the element 2 to 1 so make it equal otherwise we cannot make the array non-decreasing .

Time Complexity: **O(N)**

**Code:**

**Output:**

true false true true

The post Non-decreasing Array with one allowed change first appeared on Algorithms.

]]>The post Duplicate zero’s without expanding the array. first appeared on Algorithms.

]]>**Example: **

Input: [1, 0, 2, 3, 0, 4, 5, 0] Output: [1, 0, 0, 2, 3, 0, 0, 4] Input: [1, 0, 0, 0, 3, 4, 5] Output: [1, 0, 0, 0, 0, 0, 0] Input: [1, 2, 3] Output: [1, 2, 3] Input: [0, 0, 0] Output: [0, 0, 0]

**Approach:**

Initialize previous = null. Now iterate the array from left to right and whenever you encounter 0, set previous = 0 and whenever previous = 0, right shift the array from current index to the last index. Now place 0 at the current index and reset the previous to 0. See the code below for more understanding.

**Code: **

**Output:**

Input: [1, 0, 2, 3, 0, 4, 5, 0] Output: [1, 0, 0, 2, 3, 0, 0, 4] ------------------------- Input: [1, 0, 0, 0, 3, 4, 5] Output: [1, 0, 0, 0, 0, 0, 0] ------------------------- Input: [1, 2, 3] Output: [1, 2, 3] ------------------------- Input: [0, 0, 0] Output: [0, 0, 0] -------------------------

The post Duplicate zero’s without expanding the array. first appeared on Algorithms.

]]>The post Maximum Depth of Valid Nested Parentheses first appeared on Algorithms.

]]>**Example:**

**Solution:**

Input: 1 + (2*((3+4)/(2-1)))/2+56 Maximum Depth: 3 Input: 1 + (4/2) Maximum Depth: 1 Input: ((1)) Maximum Depth: 2 Input: 5 Maximum Depth: 0 Input: )1 + (2*((3+4)/(2-1)))/2+56( Maximum Depth: -1 (not well formed)

Earlier we solved a similar problem – Valid Brackets. Please read before continuing. We will use a similar approach with some enhancements. Use variables `currentDepth`

and `maxDepth`

. Whenever there is an open parenthesis, increment the count of currentDepth by 1 and compare it with maxDepth and update the maxDepth with currentDepth if maxDepth<currentDepth. Similarly, whenever there is a close parenthesis, decrement the currentDepth by 1. If anytime you find that parentheses are not well-formed, set maxDepth = -1 and break the loop. See the code below for more understanding.

**Complete Code:**

**Output:**

Input: 1 + (2*((3+4)/(2-1)))/2+56, Maximum Depth: 3 Input: 1 + (4/2), Maximum Depth: 1 Input: ((1)), Maximum Depth: 2 Input: 5, Maximum Depth: 0 Input: )1 + (2*((3+4)/(2-1)))/2+56(, Maximum Depth: -1

The post Maximum Depth of Valid Nested Parentheses first appeared on Algorithms.

]]>The post Decimal to Binary first appeared on Algorithms.

]]>**Example: **

Number: 5, binary representation: 101 Number: 8, binary representation: 1000 Number: 105, binary representation: 1101001

**Solution: **

Initialize a result string and while the given number is greater than 0, keep dividing it by 2 and append the remainder of division to the result string. Once the number is 0, reverse the result string and this will be the binary representation of the given number.

**Complete Code:**

**Output:**

Number: 5, binary representation: 101 Number: 8, binary representation: 1000 Number: 105, binary representation: 1101001

The post Decimal to Binary first appeared on Algorithms.

]]>The post Maximum Consecutive Ones first appeared on Algorithms.

]]>**Example:**

Input: [0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1] Max consecutive ones: 4 Input: [0, 1, 1, 0, 1, 1], Max consecutive ones: 2 Input: [0, 0, 0, 0] Max consecutive ones: 0

**Solution:**

Initialize **max** = 0, **currentOnes** = 0.Now iterate the given input array and if the current element is 1 then do currentOnes++. If current element is 0 then check if max<currentOnes, if yes then do **max = currentOnes** and reset currentOnes=0. Once the iteration is over, handle the special case when the last element is 1, again check currentOnes with max and update max if necessary.

Time Complexity: **O(N)**

**Complete Code:**

**Output:**

Input: [0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1], Max consecutive ones: 4

The post Maximum Consecutive Ones first appeared on Algorithms.

]]>