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.

Decimal to Binary

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 Decimal to Binary

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

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 Find the number of pairs with odd XOR

Student Attendance Reward

In school a student gets rewarded if he has an attendance record without being absent for more than once or being late for 3 times continuously.  Given a student’s attendance record represented by a string. The record only contains the following three characters: ‘A’ : Absent. ‘L’ : Late. ‘O’ : On-Time. Check whether the … Read more Student Attendance Reward

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

Get a random character from the given string – Java Program

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 Get a random character from the given string – Java Program

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

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

Override List Collections Sort Method

Many times we have a list of custom objects and we need to sort these objects. In such cases we need to override the compare function for Collections.sort method. In this article we discuss how to do that. Say we have a list of employees. Each employee has id, name, and salary. Write a function … Read more Override List Collections Sort Method