Posts

Diagonal Traverse Java Solution - The Coding Shala

Image
Home >> Interview Questions >> Diagonal Traverse Diagonal Traverse Problem:  Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [  [ 1, 2, 3 ],  [ 4, 5, 6 ],  [ 7, 8, 9 ] ] Output:  [1,2,4,7,5,3,6,8,9] Explanation: Diagonal Traverse Java Solution Approach:  Traverse the Matrix. Java Code::  class Solution { public int [] findDiagonalOrder ( int [][] matrix ) { int row = matrix . length ; if ( row == 0 ) return ( new int [ 0 ]); //if empty matrix int col = matrix [ 0 ]. length ; int [] ans = new int [ row * col ]; int index = 0 ; int i = 0 , j = 0 ; while ( i < row && j < col ){ while ( i >= 0 && j < col ){ //moving up ...

Plus One Java Solution - The Coding Shala

Home >> Interview Questions >> Plus One Plus One Problem: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. Example 1: Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Plus One Java Solution Approach:  Here the simple idea is if the number in the array is less than 9 then we don't have to forward carry 1 to the next number.  if all the numbers are 9 in the array then we have to add 1 at the beginning of the array[need to create new array]. java Code::  class Solution...

Largest Number At Least Twice of Others Java Solution - The Coding Shala

Home >> Interview Questions >> Largest Number at least twice of others Largest Number At Least Twice of Others Problem: In a given integer array nums, there is always exactly one largest element. Find whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, otherwise, return -1. Example 1: Input: nums = [3, 6, 1, 0] Output: 1 Explanation: 6 is the largest integer and for every other number in the array x, 6 is more than twice as big as x.  The index of value 6 is 1, so we return 1. Example 2: Input: nums = [1, 2, 3, 4] Output: -1 Explanation: 4 isn't at least as big as twice the value of 3, so we return -1. Largest Number At Least Twice of Others Java Solution Approach:  In this problem, we just need to find out the largest and 2nd largest numbers present in the array. Now if the largest element is more th...

Find Pivot Index Java Solution - The Coding Shala

Home >> Interview Questions >> Find Pivot Index Find Pivot Index Problem: Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. Example 1: Input:  nums = [1, 7, 3, 6, 5, 6] Output: 3 Explanation:  The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. Also, 3 is the first index where this occurs. Example 2: Input:  nums = [1, 2, 3] Output: -1 Explanation:  There is no index that satisfies the conditions in the problem statement. Find Pivot Index Java Solution Approach: At every index, we can check if the total ...

Introduction to Dynamic Array - The Coding Shala

Home >> Data Structures >> Introduction to Dynamic Array Introduction to Dynamic Array Arrays can be dynamic in size or I can say sometimes we don't know the size of an array at the starting point. In this case, we need to declare an array of variable size. For that, we use ArrayList in Java(vector in C++). NOTE: ArrayList is a part of Collections. The following example explains the basics operation of dynamic Array or ArrayList:  import java.util.ArrayList ; import java.util.Arrays ; import java.util.Collections ; import java.util.List ; //ArrayList Example public class Main { public static void main ( String [] args ) { //initializing an ArrayList ArrayList < Integer > arr0 = new ArrayList <>(); //inserting elements into arrayList for ( int i = 1 ; i <= 5 ; i ++) { arr0 . add ( i ); //add method } //accessing elements from an arrayList for ( int i = 0 ; i < 5 ; i...

Introduction to Array - The Coding Shala

Home >> Data Structures >> Introduction to Array Introduction to Array An Array is a data structure used to store a collection of elements sequentially. we store elements of the same type in an array for example elements can be int, char or String, etc. We can access elements in array randomly or by index.  The weakness of array is array must be implemented in contiguous memory. NOTE:  The index of an array starts with zero(0). Arrays are two types:  One-dimensional array(liner array) Multi-dimensional array The following example explains the basic operations of an Array:  import java.util.Arrays ; //Array Example public class Main { public static void main ( String [] args ) { int [] arr = new int [ 5 ]; //array arr with size 5 type int int [] arr2 = { 4 , 2 , 1 }; //we can decalre like this also //we access element with index(0 to length) int len1 = arr . length ; //get length of arr...

Java static keyword - The Coding Shala

Home >> Learn Java >> Java Static Keyword Java static keyword In Java, a static keyword is a non-access modifier. We can use Java static keyword with blocks, variables, methods, and classes(nested classes). When we create a static member, we can access it directly from static methods without reference to any object. The main() is also declared as static so it can be accessed without any object.  We can access static methods without creating any object. The following example explains it:  class Main { static void Display () { System . out . println ( "Hello from static method" ); } public static void main ( String [] args ) { Display (); } } Output:  Hello from static method In this chapter, we will discuss Java static variables, static methods, static blocks.  Java static variables In Java, when we declared a variable as static, then only a single copy of that variable is created and shared ...