Find an extra element in two almost similar arrays

Given two arrays where the second array has all the elements from the first array and one extra element.  Example:  arrA = [1, 4, 2, 5, 7] arrB = [1, 2, 4, 1, 7, 5] Output: 1 arrA = [1, 8, 2, 0] arrB = [1, 8, 4, 2, 0] Output: 4 Solution: This problem … Read more

find the Arithmetic Progression sequence

Given a first element a and common difference d of an arithmetic progression. Write a program to print the first n elements of progression.  wiki: An arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression … Read more

Find the Nth-term in a given arithmetic progression

Given a first element a and common difference d of an arithmetic progression. Write a program to find the nth term in that progression.  wiki: An arithmetic progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression … Read more

Find The Minimum time difference

Minimum time difference 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 … Read more

Departure and Destination Cities in a given itinerary

You are on a business trip and traveling from one city to another. you have a stack of unsorted flight boarding passes. The only departure city and destination city are on the boarding pass. how do you find the first departure city and your final destination city, Example:  [Dallas, Austin], [Houston, Dallas], [Austin, Seattle] Output: … Read more

Given an array, rank its elements

Given an array of numbers, write a program to rank the array elements. 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: … Read more

Find Three Consecutive Odd Numbers in an array

GIven an array of numbers, find out if array contains three consecutive odd numbers 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 = … Read more

Convert to Non-decreasing Array with one change

Given an array of numbers, you need to find out whether an array can be converted to a non-decreasing array where you are allowed to modify the maximum one element in the array. Non-decreasing array: Array is called a non-decreasing array when you traverse the array from left to right, each element on the left … Read more

In an array, Duplicate the zeroes without expanding it

Given an array of numbers, you need to duplicate the zero’s in the array. Do not extend the size of array, just discard the remaining elements (had array gets extended, those elements) after the array size. Example:  Input: [1, 0, 2, 3, 0, 4, 5, 0] Output: [1, 0, 0, 2, 3, 0, 0, 4] … Read more

Maximum Depth of Valid Nested Parentheses in an arithmetic expression

Given an arithmetic expression which operators, operands and brackets. You need to find out the maximum depth of valid nested parentheses. If parentheses are not valid ( means not well formed ) then return -1.  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: … Read more

Convert a Decimal number to its representation

Given a decimal number, convert that number to its binary representation. 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 … Read more

Maximum Consecutive Ones in a given array

Given a binary array (contains only 0’s and 1’s), find out the number of maximum consecutive ones.  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: … Read more

Duplicate even elements in an array

Given an array of numbers, write a program to duplicate the even numbers and return the new array. Example: Input: [2, 4, 1, 5, 2, 3, 8, 9, 10, 4] Output: [2, 2, 4, 4, 1, 5, 2, 2, 3, 8, 8, 9, 10, 10, 4, 4] Input: [2, 4, 6, 8] Output: [2, 2, … Read more

Minimum Increments to make all array elements unique

Given a sorted array of integers, Write an algorithm to make all array elements distinct or unique by doing minimum increments. Example: Given Input: [2, 2, 3, 5, 6, 6] Unique Array: [2, 3, 4, 5, 6, 7], Minimum Increments: 3 Explanation: Increment 2 to 3, 3 to 4 and 6 to 7. Given Input: … Read more

In a number, add digits until it becomes a single digit

Write a program to add the digits of the given number until the number becomes a single digit number. Example: Input: 256, Output: 4 256 -> 13 -> 4 Input: 15, Output: 6 15 -> 6 Input: 82, Output: 1 82 -> 10 -> 1 Input: 242239, Output: 4 242239 -> 22 -> 4 Approach: … Read more