Posts

Showing posts from March, 2020

InterviewBit - Colorful Number Solution - The Coding Shala

Home >> Programming Questions >> Colorful Number InterviewBit Colorful Number Solution In this post, you will learn how to solve InterviewBit's Colorful Number Problem and its solution in Java. A colorful number is if a number can be broken into different contiguous sub-subsequence parts. Suppose, a number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245. And this number is a COLORFUL number since the product of every digit of a contiguous subsequence is different. Example: N = 23 2 3 23 2 -> 2 3 -> 3 23 -> 6 this number is a COLORFUL number since the product of every digit of a sub-sequence is different. Output: 1 Colorful number java Program Approach: We will use HashSet to check if the number is a colorful number or not. We will generate every possible number and find their product and will check if the current product is in HashSet or not. Java Program:  public class Solution {

Minimum Swaps 2 Solution - The Coding Shala

Home >> Programming Questions >> Minimum Swaps 2 Minimum Swaps 2 Solution In this post, you will learn how to solve the Minimum Swaps 2 Problem and its solution in Java. You are given an unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order. For example, given the array arr = [7,1,3,2,4,5,6] we perform the following steps: i   arr                          swap (indices) 0   [7, 1, 3, 2, 4, 5, 6]   swap (0,3) 1   [2, 1, 3, 7, 4, 5, 6]   swap (0,1) 2   [1, 2, 3, 7, 4, 5, 6]   swap (3,4) 3   [1, 2, 3, 4, 7, 5, 6]   swap (4,5) 4   [1, 2, 3, 4, 5, 7, 6]   swap (5,6) 5   [1, 2, 3, 4, 5, 6, 7] It took  5 swaps to sort the array. Minimum Swaps 2 Java Program All the given elements of the array are from 1 to n and there are duplicates so we can solve this pro

How to Check if given Sudoku is Valid or not - The Coding Shala

Image
Home >> Interview Questions >> Valid Sudoku How to Check if given Sudoku is Valid or not In this post, you will learn how to check if given Sudoku is valid or not. We will write a Java program to check if the given sudoku board is valid. Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Example: For grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'],            ['.', '.', '6', '.', '.', '.', '.', '.', '.'],