## 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. | Set – 1

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

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

## Overlapping sum of two arrays.

Given two arrays of integers. Write a program to find the sum of all the overlapping elements between two arrays. You can consider that there will not be any duplicates in each array individually. Example: A [] : [6, 5, 1, 9, 2, 8, 3] B [] : [3, 7, 9, 2, 4] Overlapping sum … Read more

## Number of Contiguous Parking Areas

Problem: Given a building with parking slots. If a spot is free then it is marked by 1 and if the spot is taken by a vehicle then it is marked by 0. Write a program to find the number of free contiguous parking areas in the building. One free parking area can have one … Read more

## Pascal Triangle – Get specific row K

Given a row index K, write a program to print the Kth of Pascal triangle. Note: row index starts from 0. Pascal Triangle: Example:  K = 2 Output: 1, 1 K= 5 Output: 1, 4, 6, 4, 1 Simple Approach: As we discussed here – Pascal triangle, starting calculating the rows from 1 to K … Read more

## Override Arrays Sort Method – Java

Many times we have an array of custom objects and we need to sort these objects. In such cases we need to override the compare function for Arrays.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

## Pascal Triangle

Given a number N, write a program to print the first N lines of the Pascal triangle. Pascal Triangle: Note: In Pascal’s triangle, each number is the sum of the two numbers directly above it.  (Image reference: Wiki) Approach: Initialize list row and list of lists as all_rows.  We will keep updating the list row … Read more

## Minimum number of adjacent swaps to sort the given array

Given an array of integers, you are allowed to swap only adjacent elements in the array. write a program to find the minimum number of swaps to sort the given array. Example: Input[] : [2, 20, 15, 6, 10] Minimums adjacent swaps required sort the array: 5 Input[] : [10, 3, 4, 2, 5, 7, … Read more

## Check if the given playlist of programs is valid

Problem Statement: Given a playlist that contains the list of intervals with the start time, end time and program id. Write a program to validate whether the playlist is valid or not based on certain conditions.  The playlist will be considered invalid if any of the following is true – If any two programs overlap. … Read more

## Maximum meetings in one room

You have one meeting room at your company. There are N meeting needs to take place. Every meeting has a start time and end time along with the meeting title. Your task is to schedule as many meetings as possible in that conference room with no conflicts.  Example: Meetings: A[ Start Time: 1, End Time: … Read more

## Find the number of pairs with even XOR

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

## Given an array, Find the number of all pairs with even sum

Objective: Given an array of integers, write a program to find the number of pairs with even sum. Example: Given Input: [1, 2, 3, 4] Number of even pairs: 2 Note: (1, 3) and (2, 4) Given Input: [6, 7, 1, 3, 2, 5, 4] Number of even pairs: 9 Naive approach: Use nested loops … Read more

## The number of cycles in a given array of integers.

Objective: Given an array of size N which contains integers from range 0 to N-1. (No duplicates). Write a program to find the number of cycles in the array.  Cycles in Array: Since the array is of size N and elements are from 0 to N-1 without any duplicates means all the elements appear exactly … Read more

## Check if given sudoku is valid or not

Problem: Given a filled sudoku, write a program to check if sudoku is valid or following all of its rules. SUDOKU rules:  Each column contains all of the digits from 1 to 9 only once. Each row contains all of the digits from 1 to 9 only once. Each of the nine 3×3 sub-grid contains … Read more