## 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

## 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

## 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

## 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

## Find the number of pairs with odd XOR

Given an array of integers, write a program to find the number of pairs for which the XOR is an odd number. Example: Input[] = {3, 2, 1} Output: 2 Note: 1 XOR 2 = 3 and 2 XOR 3 = 1 Input[] = {3, 6, 9, 4} Output: 4 Naive Approach: Use nested loops … Read more

## Add digits until the number 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

## Remove Duplicates in a given 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

## Random character in a given string – Java

Given a string, write a function that returns a random character from the string.  Example: Given string = algorithms @ tutorial horizon Random character: s Random character: r Random character: h Random character: @ Random character: i Approach: Get the length of the string and then use the java random function to get the random … Read more