Find departure and destination cities from the 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 Find departure and destination cities from the itinerary

Three Consecutive Odd Numbers

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 Three Consecutive Odd Numbers

Non-decreasing Array with one allowed 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 non-decreasing array when you traverse array from left to right, each element on the left is less … Read more Non-decreasing Array with one allowed change

Duplicate zero’s without expanding the array.

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 Duplicate zero’s without expanding the array.

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 Duplicate even elements in an array

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 Minimum Increments to make all array elements unique

Maximum Surpasser in the given array

The “surpasser” of an element in an array is defined as the number of elements that are to the “right” and bigger than itself. Write an algorithm to Find the maximum surpasser of the array. Example: Input array: [2, 7, 5, 5, 2, 7, 0, 8, 1] The “surpassers” are [5, 1, 2, 2, 2, … Read more Maximum Surpasser in the given array

Remove Duplicates from Sorted Array

Given a sorted array of integers, write a program to remove duplicates in-place by modifying the given array such that all unique integers will be at the beginning of the array, and do not worry about other indexes after the new length. Example: Given Input: [1, 1, 2, 3, 3] Output: [1, 2, 3, 3, … Read more Remove Duplicates from Sorted Array

Replace Elements with Greatest Element on Right

Given an array of numbers nums[], write ment a function to replace each element of the array with the greatest element present to its right side. Replace the last element with -1.  Example: Input: [4, 5, 2, 25, 13, 16, 8] Output: [25, 25, 25, 16, 16, 8, -1] Input: [4, 5, 2, 25, 13, … Read more Replace Elements with Greatest Element on Right

Count number of pairs which has sum equal to K

Given an array of integers and number K, write a program to find the number of pairs which has sum equal to K. Example: int input [] = {6, 3, 2, 9, 2, 2, 2, 1} int K = 4 Output: 7 int input [] = {5, 5, 5, 5} int K = 10 Output: … Read more Count number of pairs which has sum equal to K

Count similar words in a given array

Given an array of strings, write a program to count all the similar words.  Similar words: Two words are similar if Has spaces in-between or at the end. Ex: “Apple” and ” A pp le” are similar. Has upper or lower cases. Ex: “APPle” and “apple” are similar. Has special characters. Ex: “app%^L&e” and “@@apple” … Read more Count similar words in a given array

Maximum distance from the nearest person.

Given a bench with n seats and few people sitting, You are going to sit on a vacant seat such that the distance between you and the nearest person to you is maximum. On the beach, the occupied seats are represented by 1 and vacant seats are represented by 0. Example: Input: [1, 0, 1, … Read more Maximum distance from the nearest person.

Sort 0’s, the 1’s and 2’s in the given array – Dutch National Flag algorithm | Set – 2

Given an array of numbers, that consists only of three types of integers, which are 0, 1, and 2. Write an algorithm to sort the given array. Example: Input: {2, 1, 2, 0, 1, 0} Output: {0, 0, 1, 1, 2, 2} Input: [0, 0, 2, 0, 2, 1, 0, 1, 2] Output: [0, 0, … Read more Sort 0’s, the 1’s and 2’s in the given array – Dutch National Flag algorithm | Set – 2