Document of Java Arrays
Document of Java Arrays
import java.math.BigInteger;
import java.util.;
<p>For a non-negative integer X, the array-form of X is an array of its digits in left to right
<p>Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
<p>Example 1:
<p>Input: A = [2,7,4], K = 181 Output: [4,5,5] Explanation: 274 + 181 = 455 Example 3:
<p>Input: A = [2,1,5], K = 806 Output: [1,0,2,1] Explanation: 215 + 806 = 1021 Example 4:
<p>1 <= A.length <= 10000 0 <= A[i] <= 9 0 <= K <= 10000 If A.length > 1, then A[0] != 0
System.out.println(answer);
for (int a : A) {
sb.append(a);
list.add(Integer.parseInt(String.valueOf(a)));
return list;
package array;
import java.util.;
integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]],
<p>Suppose the first element in S starts with the selection of element A[i] of index = i, the
next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right
<p>Example 1:
= 1, A[5] = 6, A[6] = 2.
<p>One of the longest S[K]: S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
<p>Note:
<p>N is an integer within the range [1, 20,000]. The elements of A are all distinct. Each element
System.out.println(new ArrayNesting().arrayNesting(A));
}
Set<Integer> done;
int count;
int max = 0;
if (!done.contains(i)) {
count = 0;
dfs(i, nums);
return max;
done.add(i);
count++;
int n = nums[i];
if (!done.contains(n)) {
dfs(n, nums);
package array;
import java.util.;
these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of
Note: n is a positive integer, which is in the range of [1, 10000]. All the integers in the array
<p>Solution: O(n log n) General idea is to pair the smallest with the next smallest value inorder
System.out.println(new ArrayPartitionI().arrayPairSum(A));
Arrays.sort(nums);
int sum = 0;
return sum;
package array;
Created by gouthamvidyapradhan on 12/08/2017. Given an 2D board, count how many battleships are
in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may
<p>You receive a valid board, made of only battleships or empty slots. Battleships can only be
placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row,
N columns) or Nx1 (N rows, 1 column), where N can be of any size. At least one horizontal or
vertical cell separates between two battleships - there are no adjacent battleships. Example:
X..X ...X ...X In the above board there are 2 battleships. Invalid Example: ...X XXXX ...X This
is an invalid board that you will not receive - as battleships will always have a cell separating
between them.
<p>Follow up: Could you do it in one-pass, using only O(1) extra memory and without modifying the
<p>Solution: The below solution works in one pass using only O(1) memory. Iterate through each
cell and add one to count if and only if the current cell equals 'X' and its adjacent upper and
Main method
@param args
@throws Exception
char[][] board = {{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}};
System.out.println(new BattleshipsInABoard().countBattleships(board));
int count = 0;
if (board[i][j] == 'X') {
if (j - 1 >= 0) {
if (board[i][j - 1] == 'X') {
continue;
count++;
}
}
return count;
package array;
Created by gouthamvidyapradhan on 10/03/2019 A group of two or more people wants to meet and
minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks
the home of someone in the group. The distance is calculated using Manhattan Distance, where
<p>Example:
<p>Input:
<p>1 - 0 - 0 - 0 - 1 | | | | | 0 - 0 - 0 - 0 - 0 | | | | | 0 - 0 - 1 - 0 - 0
<p>Output: 6
<p>Explanation: Given three people living at (0,0), (0,4), and (2,2): The point (0,2) is an ideal
<p>Solution: O(N ^ 2 + M ^ 2) + O(N x M): Calculate the total number of persons in each row and
each column and then take a minimum of cartesian product of each row and each column.
Main method
@param args
if (grid[i][j] == 1) {
countR[i]++;
countC[j]++;
if (countR[j] != 0) {
if (countC[j] != 0) {
return min;
package array;
<p>Suppose you have a long flowerbed in which some of the plots are planted and some are not.
However, flowers cannot be planted in adjacent plots - they would compete for water and both
would die.
<p>Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means
not empty), and a number n, return if n new flowers can be planted in it without violating the
no-adjacent-flowers rule.
[1,0,0,0,1], n = 2 Output: False Note: The input array won't violate no-adjacent-flowers rule.
The input array size is in the range of [1, 20000]. n is a non-negative integer which won't
Main method
@param args
@throws Exception
}
public boolean canPlaceFlowers(int[] flowerbed, int n) {
T[0] = 1;
T[T.length - 1] = 1;
if (T[i] == 0) count++;
else {
count = 0; // reset
package array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
<p>On a table are N cards, with a positive integer printed on the front and back of each card
(possibly different).
<p>We flip any number of cards, and after we choose one card.
<p>If the number X on the back of the chosen card is not on the front of any card, then this
number X is good.
<p>What is the smallest number that is good? If no number is good, output 0.
<p>Here, fronts[i] and backs[i] represent the number on the front and back of card i.
<p>A flip swaps the front and back numbers, so the value on the front is now on the back and vice
versa.
<p>Example:
<p>Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3] Output: 2 Explanation: If we flip the second
card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3]. We choose the second card, which
has number 2 on the back, and it isn't on the front of any card, so 2 is good.
<p>Note:
<p>1 <= fronts.length == backs.length <= 1000. 1 <= fronts[i] <= 2000. 1 <= backs[i] <= 2000.
numbers.add(fronts[i]);
numbers.add(backs[i]);
Collections.sort(numbers);
success = false;
break;
}
}
if (success) {
return n;
return 0;
package array;
Created by gouthamvidyapradhan on 28/03/2019 We stack glasses in a pyramid, where the first row
has 1 glass, the second row has 2 glasses, and so on until the 100th row. Each glass holds one
<p>Then, some champagne is poured in the first glass at the top. When the top most glass is full,
any excess liquid poured will fall equally to the glass immediately to the left and right of it.
When those glasses become full, any excess champagne will fall equally to the left and right of
those glasses, and so on. (A glass at the bottom row has it's excess champagne fall on the
floor.)
<p>For example, after one cup of champagne is poured, the top most glass is full. After two cups
of champagne are poured, the two glasses on the second row are half full. After three cups of
champagne are poured, those two cups become full - there are 3 full glasses total now. After four
cups of champagne are poured, the third row has the middle glass half full, and the two outside
<p>Now after pouring some non-negative integer cups of champagne, return how full the j-th glass
poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will
be no excess liquid so all the glasses under the top glass will remain empty.
<p>Example 2: Input: poured = 2, query_glass = 1, query_row = 1 Output: 0.5 Explanation: We
poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is
one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share
the excess liquid equally, and each will get half cup of champange.
<p>Note:
<p>poured will be in the range of [0, 10 ^ 9]. query_glass and query_row will be in the range of
[0, 99].
<p>Solution: Calculate for every glass and for each row at a time. Use the value from the
@see PascalsTriangle
Main method
@param args
A[0][0] = poured;
if (j == 0) continue;
if (A[i - 1][j - 1] > 1.0) {
package array;
import java.util.ArrayList;
import java.util.List;
<p>Each employee has a list of non-overlapping Intervals, and these intervals are in sorted
order.
<p>Return the list of finite intervals representing common, positive-length free time for all
There are a total of three employees, and all common free time intervals would be [-inf, 1], [3,
4], [10, inf]. We discard any intervals that contain inf as they aren't finite. Example 2: Input:
representing Intervals in the form [x, y], the objects inside are Intervals, not lists or arrays.
defined.)
<p>Also, we wouldn't include intervals like [5, 5] in our answer, as they have zero length.
<p>Note:
<p>schedule and schedule[i] are lists with lengths in range [1, 50]. 0 <= schedule[i].start <
<p>Solution: O(L X N x N) Where L is the number of schedules, N is the length of schedules for
each employee. Merge all the intervals to form a single merged array, now by using this array
form a result array with the intervals which form the free time.
int start;
int end;
Interval() {
start = 0;
end = 0;
Interval(int s, int e) {
start = s;
end = e;
Main method
@param args
schedule.add(ints1);
schedule.add(ints3);
return result;
return list;
result.add(interval);
result.add(list.get(j));
return result;
result.add(curr);
result.add(interval);
return result;
package array;
import java.util.;
Created by gouthamvidyapradhan on 06/08/2019 Given an array of integers nums, write a method that
<p>We define the pivot index as the index where the sum of the numbers to the left of the index
<p>If no such index exists, we should return -1. If there are multiple pivot indexes, you should
<p>Example 1:
<p>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.
<p>Example 2:
<p>Input: nums = [1, 2, 3] Output: -1 Explanation: There is no index that satisfies the
<p>Note:
<p>The length of nums will be in the range [0, 10000]. Each element nums[i] will be an integer in
<p>Solution: O(N) maintain a prefix and posfix sum array and then use this to arrive at the
answer.
if (nums.length == 1) return 0;
left[0] = nums[0];
int l, r;
if (i == 0) {
l = 0;
} else {
l = left[i - 1];
if (i == nums.length - 1) {
r = 0;
} else {
r = right[i + 1];
if (l == r) return i;
return -1;
package array;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
<p>Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may
exist one celebrity. The definition of a celebrity is that all the other n - 1 people know
<p>Now you want to find out who the celebrity is or verify that there is not one. The only thing
you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of
whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as
<p>You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement
a function int findCelebrity(n), your function should minimize the number of calls to knows.
<p>Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's
Main method
@param args
// initialize relationship
map.get(0).add(3);
map.get(0).add(1);
map.get(0).add(2);
map.get(0).add(4);
map.get(0).add(5);
map.get(0).add(6);
map.get(1).add(0);
map.get(1).add(3);
map.get(1).add(2);
map.get(2).add(1);
map.get(2).add(3);
map.get(2).add(4);
map.get(4).add(2);
map.get(4).add(3);
map.get(4).add(5);
map.get(5).add(4);
map.get(5).add(3);
map.get(5).add(6);
map.get(6).add(5);
map.get(6).add(3);
map.get(6).add(0);
System.out.println(new FindTheCelebrity().findCelebrity(0));
Find celebrity
@param n
@return
while (i < n) {
i = next;
next = i + 1;
} else if ((!knows(i, next) && !knows(next, i)) || (knows(i, next) && knows(next, i))) {
i = next + 1;
next = i + 1;
} else {
next++;
candidate = i;
candidate = -1;
break;
return candidate;
return map.get(a).contains(b);
package array;
Created by gouthamvidyapradhan on 24/06/2017. Given an unsorted integer array, find the first
<p>Your algorithm should run in O(n) time and uses constant space.
private int L;
System.out.println(new FirstMissingPositive().firstMissingPositive(nums));
L = nums.length;
int v = nums[i];
nums[i] = -1;
replace(v, nums);
}
}
if (nums[i] != i + 1) return i + 1;
return L + 1;
nums[i - 1] = i;
replace(v, nums);
package array;
import java.util.Stack;
Created by gouthamvidyapradhan on 02/03/2019 In a row of trees, the i-th tree produces fruit with
type tree[i].
<p>You start at any tree of your choice, then repeatedly perform the following steps:
<p>Add one piece of fruit from this tree to your baskets. If you cannot, stop. Move to the next
tree to the right of the current tree. If there is no tree to the right, stop. Note that you do
not have any choice after the initial choice of starting tree: you must perform step 1, then step
<p>You have two baskets, and each basket can carry any quantity of fruit, but you want each
<p>What is the total amount of fruit you can collect with this procedure?
<p>Example 1:
<p>Input: [0,1,2,2] Output: 3 Explanation: We can collect [1,2,2]. If we started at the first
<p>Input: [1,2,3,2,2] Output: 4 Explanation: We can collect [2,3,2,2]. If we started at the first
started at the first tree or the eighth tree, we would only collect 4 fruits.
<p>Note:
Main method
@param args
System.out.println(new FruitIntoBaskets().totalFruit(trees));
if (i == t1 || i == t2) {
countAndMax(stack, i);
} else {
if (t1 == -1) {
t1 = i;
countAndMax(stack, i);
t2 = i;
countAndMax(stack, i);
} else {
count = 0;
t1 = stack.pop();
countAndMax(temp, t1);
countAndMax(temp, stack.pop());
t2 = i;
stack = temp;
countAndMax(stack, i);
return max;
count++;
stack.push(i);
package array;
/
Created by gouthamvidyapradhan on 12/12/2017. Given an array of citations (each citation is a
<p>According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her
N papers have at least h citations each, and the other N − h papers have no more than h cita ons
each."
<p>For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in
total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher
has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations
<p>Note: If there are several possible values for h, the maximum one is taken as the h-index.
<p>Solution O(n) Replace all the citations which are greater than n with n, the result will not
change with this operation. Maintain a count array with count of each citations. Sum up all the
counts from n -> 0 and store this in a array S. Value in array index Si is number of papers
<p>The first value at index i, from right to left in array S which has i <= Si is the answer.
System.out.println(new HIndex().hIndex(A));
int n = citations.length;
if (citations[i] > n) {
citations[i] = n;
}
count[citation]++;
S[n] = count[n];
if (i <= S[i]) {
return i;
return 0;
package array;
scale of an image, you need to design a smoother to make the gray scale of each cell becomes the
average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less
<p>Example 1: Input: [[1,1,1], [1,0,1], [1,1,1]] Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Explanation: For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0 For the point
(0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0 For the point (1,1): floor(8/9) =
floor(0.88888889) = 0 Note: The value in the given matrix is in the range of [0, 255]. The length
and width of the given matrix are in the range of [1, 150].
int numCount = 0;
int totalCount = 1;
if (newR >= 0 && newC >= 0 && newR < M.length && newC < M[0].length) {
if (M[newR][newC] > 0) {
numCount += M[newR][newC];
totalCount++;
if (M[i][j] == 1) numCount++;
return result;
package array;
import java.util.Arrays;
<p>Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] <
arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. Your algorithm should run in O(n) time
Main method
@param args
@throws Exception
System.out.println(new IncreasingTripletSubsequence().increasingTriplet(A));
Arrays.fill(A, Integer.MAX_VALUE);
A[0] = num;
A[1] = num;
return true;
return false;
}
package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
<p>You may assume that the intervals were initially sorted according to their start times.
[1,2],[3,10],[12,16].
<p>Solution: O(n): Iterate through each interval and check for each overlapping case
int start;
int end;
Interval() {
start = 0;
end = 0;
Interval(int s, int e) {
start = s;
end = e;
/
Main method
@param args
@throws Exception
return result;
result.add(curr);
} else {
result.add(newInterval);
return result;
result.add(newInterval);
return result;
int l = intervals.size();
while (i < l) {
result.add(intervals.get(i));
i++;
package array;
import java.util.TreeSet;
Created by gouthamvidyapradhan on 01/01/2018. There is a garden with N slots. In each slot, there
is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly
one flower blooming and it will be in the status of blooming since then.
<p>Given an array flowers consists of number from 1 to N. Each number in the array represents the
<p>Also given an integer k, you need to output in which day there exists two flowers in the
status of blooming, and also the number of flowers between them is k and these flowers are not
blooming.
<p>Example 1: Input: flowers: [1,3,2] k: 1 Output: 2 Explanation: In the second day, the first
and the third flower have become blooming. Example 2: Input: flowers: [1,2,3] k: 1 Output: -1
<p>Solution: O(n log n). Maintain a tree-set of bloomed flowers and for every element in the
array find the upper and lower bound bloomed flowers and calculate their difference with the
current. If the difference is k return the current day, if none found then return -1
Main method
@param args
@throws Exception
if (lowerBound != null) {
if ((Math.abs(flowers[i] - lowerBound) + 1) == k) {
return i + 1;
if (upperBound != null) {
if ((Math.abs(flowers[i] - upperBound) + 1) == k) {
return i + 1;
set.add(flowers[i]);
return -1;
package array;
<p>Find whether the largest element in the array is at least twice as much as every other number
in the array.
<p>If it is, return the index of the largest element, otherwise return -1.
<p>Example 1:
<p>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.
<p>Example 2:
<p>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.
<p>Note:
<p>nums will have a length in the range [1, 50]. Every nums[i] will be an integer in the range
[0, 99].
Main method
@param args
@throws Exception
max = nums[i];
index = i;
if (i == index) continue;
return -1;
return index;
}
package array;
<p>The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is
<p>Return the answer as a string of length 5. If no valid time can be made, return an empty
string.
<p>Example 1:
<p>Note:
<p>A.length == 4 0 <= A[i] <= 9 Solution O(N ^ 4) Check all combinations of time possible and
System.out.println(new LargestTimeForGivenDigits().largestTimeFromDigits(A));
if (k == i || k == j) continue;
if (l == i || l == j || l == k) continue;
max = value;
return result;
package array;
<p>Given an unsorted array of integers, find the length of longest continuous increasing
subsequence (subarray).
subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence,
it's not a continuous one where 5 and 7 are separated by 4. Example 2: Input: [2,2,2,2,2] Output:
1 Explanation: The longest continuous increasing subsequence is [2], its length is 1. Note:
/
public class LongestIncreasingSubsequence {
Main method
@param args
@throws Exception
System.out.println(new LongestIncreasingSubsequence().findLengthOfLCIS(A));
if (nums.length == 0) return 0;
count++;
i++;
j++;
} else {
count = 0;
i = j;
j = i + 1;
return max;
package array;
Example: Input: [[0,1,1,0], [0,1,1,0], [0,0,0,1]] Output: 3 Hint: The number of elements in the
<p>Solution O(N x M) for each cell keep track of maximum value possible horizontally, vertically
and diagonally. Start iterating from left-right and top-bottom and repeat the same from
right-left and top to bottom to get max for anti-diagonal and return the max value.
int[][] M = {
{1, 1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 0, 0, 1, 1, 1},
{0, 1, 0, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 0, 1, 0, 1, 1, 1, 1}
};
System.out.println(new LongestLineofConsecutiveOneinMatrix().longestLine(M));
class Cell {
int h, v, d;
this.h = h;
this.v = v;
this.d = d;
}
}
if (M.length == 0) return 0;
int max = 0;
int h = 0, v = 0, d = 0;
if (M[i][j] == 1) {
h = 1;
v = 1;
d = 1;
if (j - 1 >= 0) {
if (left.h > 0) {
h += left.h;
if (i - 1 >= 0) {
if (top.v > 0) {
v += top.v;
if (diagonal.d > 0) {
d += diagonal.d;
}
}
int h = 0, v = 0, d = 0;
if (M[i][j] == 1) {
h = 1;
v = 1;
d = 1;
if (j + 1 < M[0].length) {
if (left.h > 0) {
h += left.h;
if (i - 1 >= 0) {
if (top.v > 0) {
v += top.v;
if (diagonal.d > 0) {
d += diagonal.d;
}
}
return max;
package array;
import java.util.;
Created by gouthamvidyapradhan on 15/08/2019 We are given a matrix with R rows and C columns has
cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
<p>Additionally, we are given a cell in that matrix with coordinates (r0, c0).
<p>Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from
smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2)
is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that
<p>Example 1:
distances from (r0, c0) to other cells are: [0,1,1,2] The answer [[0,1],[1,1],[0,0],[1,0]] would
The distances from (r0, c0) to other cells are: [0,1,1,2,2,3] There are other answers that would
also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
<p>Note:
<p>1 <= R <= 100 1 <= C <= 100 0 <= r0 < R 0 <= c0 < C
<p>Solution: O (log (R x C)) Straight forward solution, find the Manhattan distance from the
given cell to all the cells in the grid and sort by min distance and return their coordinates.
//
class Cell {
int max, i, j;
this.max = max;
this.i = i;
this.j = j;
A[i][0] = list.get(i).i;
A[i][1] = list.get(i).j;
return A;
package array;
Created by gouthamvidyapradhan on 08/05/2019 Given a binary array, find the maximum number of
<p>Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three
<p>The input array will only contain 0 and 1. The length of input array is a positive integer and
//
int max = 0;
int count = 0;
if (nums[i] == 1) {
if (!flag) {
flag = true;
count++;
} else {
count = 0;
flag = false;
return max;
package array;
Created by gouthamvidyapradhan on 08/05/2019 Given a binary array, find the maximum number of
<p>Example 1: Input: [1,0,1,1,0] Output: 4 Explanation: Flip the first zero will get the the
maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4.
Note:
<p>The input array will only contain 0 and 1. The length of input array is a positive integer and
will not exceed 10,000 Follow up: What if the input numbers come in one by one as an infinite
stream? In other words, you can't store all numbers coming from the stream as it's too large to
<p>Solution: O(N) Maintain a left and right auxiliary array with counts of contagious 1's from
both directions. Now, iterate through the array and flip a 0 to 1 and sum up left and right
contagious sum of 1's and return the max sum as the answer
//
int count = 0;
int max = 0;
if (nums[j] == 1) {
if (!flag) {
flag = true;
count++;
L[j] = count;
} else {
count = 0;
flag = false;
L[j] = count;
flag = false;
count = 0;
if (nums[j] == 1) {
if (!flag) {
flag = true;
count++;
R[j] = count;
} else {
count = 0;
flag = false;
R[j] = count;
return max;
package array;
import java.util.;
the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L
and M. (For clarification, the L-length subarray could occur before or after the M-length
subarray.)
<p>Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1]
<p>0 <= i < i + L - 1 < j < j + M - 1 < A.length, or 0 <= j < j + M - 1 < i < i + L - 1 <
A.length.
<p>Example 1:
<p>Note:
<p>L >= 1 M >= 1 L + M <= A.length <= 1000 0 <= A[i] <= 1000 Solution O(N ^ 2) Find prefix sum of
array of length L and array of length M and keep track of their begin and end indices. Now,
brute-force compare pairs of prefix array sum where their indices don't overlap and return the
class MaxWithIndex {
int max, i, j;
this.max = max;
this.i = i;
this.j = j;
int max = 0;
}
return max;
int i = 0, j = L;
int sum = 0;
sum += A[i];
sum += A[j];
return list;
package array;
Created by gouthamvidyapradhan on 10/12/2017. Given a non-negative integer, you could swap two
digits at most once to get the maximum valued number. Return the maximum valued number you could
get.
<p>Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example
2: Input: 9973 Output: 9973 Explanation: No swap. Note: The given number is in the range [0, 108]
<p>Solution O(n): Create a array of digit index. Iterate through the digits starting from left
and in each iteration check if there is any digit which is greater than the current digit and
appearing after the current index, if found then swap and return the new integer.
System.out.println(new MaximumSwap().maximumSwap(2736));
char[] A = String.valueOf(num).toCharArray();
D[A[i] - '0'] = i;
if (D[j] > i) {
A[D[j]] = temp;
found = true;
break;
if (found) break;
return Integer.parseInt(String.valueOf(A));
package array;
import java.util.Arrays;
Created by gouthamvidyapradhan on 27/06/2017. Given an integer array, find three numbers whose
the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
System.out.println(new MaxProductOfThreeNumbers().maximumProduct(A));
Arrays.sort(nums);
package array;
import java.util.Arrays;
of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all
meetings.
<p>Solution: Sort the interval based on the start interval. Then, for every interval check if the
int start;
int end;
Interval() {
start = 0;
end = 0;
Interval(int s, int e) {
start = s;
end = e;
System.out.println(new MeetingRooms().canAttendMeetings(intervals));
return true;
package array;
import java.util.;
Created by gouthamvidyapradhan on 19/11/2019 Given the availability time slots arrays slots1 and
slots2 of two people and a meeting duration duration, return the earliest time slot that works
<p>If there is no common time slot that satisfies the requirements, return an empty array.
<p>The format of a time slot is an array of two elements [start, end] representing an inclusive
<p>It is guaranteed that no two availability slots of the same person intersect with each other.
That is, for any two time slots [start1, end1] and [start2, end2] of the same person, either
<p>Example 1:
[60,68] Example 2:
[]
<p>Constraints:
< slots1[i][1] slots2[i][0] < slots2[i][1] 0 <= slots1[i][j], slots2[i][j] <= 10^9 1 <= duration
<= 10^6
System.out.println();
int s, e, type;
this.s = s;
this.e = e;
this.type = type;
PriorityQueue<Node> pq =
new PriorityQueue<>(
if (r == 0) {
} else return r;
});
while (!pq.isEmpty()) {
if (prev == null) {
prev = node;
} else {
if (prev.type != node.type) {
package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
overlapping intervals.
<p>Solution: O(N log N) where N is the number of intervals 1. Sort the intervals based on start
index 2. Mark the first interval as the current interval 3. For every ith interval starting 1 ->
N, if the ith interval overlaps with the current interval then create a new current interval.
Else, add the current interval to result set and begin a new current interval.
int start;
int end;
Interval() {
start = 0;
end = 0;
Interval(int s, int e) {
start = s;
end = e;
Interval I = intervals.get(i);
&& I.start <= curr.end) { // check if the new interval overlaps with the current
} else {
result.add(curr);
curr = I;
result.add(curr);
return result;
package array;
/
Created by gouthamvidyapradhan on 29/07/2017. Given two sorted integer arrays nums1 and nums2,
<p>Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to
hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m
and n respectively.
int[] A = {0};
int[] B = {1};
int i = m + n - 1, j = m - 1, k = n - 1;
while (j >= 0 && k >= 0) nums1[i--] = (nums1[j] > nums2[k]) ? nums1[j--] : nums2[k--];
package array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Created by gouthamvidyapradhan on 11/04/2018. Suppose Andy and Doris want to choose a restaurant
for dinner, and they both have a list of favorite restaurants represented by strings.
<p>You need to help them find out their common interest with the least list index sum. If there
is a choice tie between answers, output all of them with no order requirement. You could assume
there always exists an answer.
<p>Example 1: Input: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at
Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] Output: ["Shogun"] Explanation: The only
restaurant they both like is "Shogun". Example 2: Input: ["Shogun", "Tapioca Express", "Burger
King", "KFC"] ["KFC", "Shogun", "Burger King"] Output: ["Shogun"] Explanation: The restaurant
they both like and have the least index sum is "Shogun" with index sum 1 (0+1). Note: The length
of both lists will be in the range of [1, 1000]. The length of strings in both lists will be in
the range of [1, 30]. The index is starting from 0 to the list length minus 1. No duplicates in
both lists.
<p>Solution:O(N) maintain index of each restaurant in a list using a HashMap, find the min sum of
indices and list all the restaurants which match the min sum of indices
Main method
@param args
System.out.println(s);
String s = list1[i];
if (index.containsKey(s)) {
String s = list1[i];
if (index.containsKey(s)) {
if (sum == min) {
result.add(s);
result.toArray(resArr);
return resArr;
package array;
import java.util.Arrays;
Created by gouthamvidyapradhan on 17/02/2018. Given a non-empty integer array, find the minimum
number of moves required to make all array elements equal, where a move is incrementing a
<p>Input: [1,2,3]
<p>Output: 2
<p>Explanation: Only two moves are needed (remember each move increments or decrements one
element):
<p>Solution: O(n log n): Sort the array and find the median of the array. Use the median of array
to increment/decrement other value of array. Sum up the difference and return the answer.
Main method
@param args
@throws Exception
System.out.println(new MinimumMovesToEqualArray().minMoves2(A));
if (nums.length == 1) return 0;
Arrays.sort(nums);
int median;
if ((nums.length % 2) == 1) {
int sum = 0;
return sum;
} else {
median = (nums.length / 2) - 1;
int sum = 0;
int min;
min = sum;
sum = 0;
return min;
package array;
import java.util.;
Created by gouthamvidyapradhan on 23/10/2019 Given a binary array data, return the minimum number
of swaps required to group all 1’s present in the array together in any place in the array.
<p>Example 1:
<p>Input: [1,0,1,0,1] Output: 1 Explanation: There are 3 ways to group all 1's together:
[1,1,1,0,0] using 1 swap. [0,1,1,1,0] using 2 swaps. [0,0,1,1,1] using 1 swap. The minimum is 1.
Example 2:
<p>Input: [0,0,0,1,0] Output: 0 Explanation: Since there is only one 1 in the array, no swaps
needed. Example 3:
<p>Input: [1,0,1,0,1,0,0,1,1,0,1] Output: 3 Explanation: One possible solution that uses 3 swaps
is [0,0,0,0,0,1,1,1,1,1,1]. Solution: O(N) All the 1s to be grouped together would mean that all
1s should occupy a small window in a array, this window could be in any part of the array - a
//
int one = 0;
int zero = 0;
if (data[i] == 1) {
one++;
} else zero++;
if (one == 0) return 0;
one = 0;
zero = 0;
int i = 0, j = window - 1;
if (data[k] == 1) {
one++;
} else zero++;
i++;
j++;
int min = zero;
if (data[j] == 0) {
zero++;
} else one++;
if (data[i - 1] == 0) {
zero--;
} else one--;
return min;
package array;
import java.util.;
import java.util.stream.Collectors;
"Hour:Minutes" format, find the minimum minutes difference between any two time points in the
list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given
list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to
23:59.
<p>Solution: O(N log N) convert each time value of the form hh:mm to minutes and sort the array.
For every pair (i, j) where j = i + 1 (also for the case where i = 0 and j = N - 1) check the
minute difference and return the minimum time difference as the answer.
System.out.println(new MinimumTimeDifference().findMinDifference(list));
}
public int findMinDifference(List<String> timePoints) {
List<Integer> timeInMinutes =
timePoints
.stream()
.map(
t -> {
})
.sorted(Integer::compareTo)
.collect(Collectors.toList());
return min;
package array;
from 0, 1, 2, ..., n, find the one that is missing from the array.
System.out.println(new MissingNumber().missingNumber(nums));
int sum = 0;
int n = nums.length;
sum += num;
package array;
import java.util.;
<p>Your class will have one method, book(int start, int end). Formally, this represents a booking
on the half open interval [start, end), the range of real numbers x such that start <= x < end.
<p>A K-booking happens when K events have some non-empty intersection (ie., there is some time
<p>For each call to the method MyCalendar.book, return an integer K representing the largest
integer such that there exists a K-booking in the calendar.
<p>Your class will be called like this: MyCalendarThree cal = new MyCalendarThree();
The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking. The
third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking. The
remaining events cause the maximum K-booking to be only a 3-booking. Note that the last event
locally causes a 2-booking, but the answer is still 3 because eg. [10, 20), [10, 40), and [5, 15)
int n, index;
this.n = n;
this.index = index;
return n;
return index;
public MyCalendarThree() {
max = 0;
}
Main method
@param args
System.out.println(calendar.book(10, 20));
System.out.println(calendar.book(50, 60));
System.out.println(calendar.book(10, 40));
System.out.println(calendar.book(5, 15));
System.out.println(calendar.book(5, 10));
System.out.println(calendar.book(25, 55));
events.sort((Comparator.comparing(Node::getN).thenComparing(Node::getIndex)));
int count = 0;
break;
return max;
package array;
/
Created by gouthamvidyapradhan on 09/02/2018. You are given two arrays (without duplicates) nums1
and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for
<p>The Next Greater Number of a number x in nums1 is the first greater number to its right in
<p>Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] Explanation: For
number 4 in the first array, you cannot find the next greater number for it in the second array,
so output -1. For number 1 in the first array, the next greater number for it in the second array
is 3. For number 2 in the first array, there is no next greater number for it in the second
array, so output -1. Example 2: Input: nums1 = [2,4], nums2 = [1,2,3,4]. Output: [3,-1]
Explanation: For number 2 in the first array, the next greater number for it in the second array
is 3. For number 4 in the first array, there is no next greater number for it in the second
array, so output -1. Note: All elements in nums1 and nums2 are unique. The length of both nums1
Main method
@param args
@throws Exception
System.out.println(result);
int nF = 0;
if (nums2[j] == n) {
found = true;
if (found) {
if (nums2[j] > n) {
nF = nums2[j];
break;
if (found) {
result[i] = nF;
} else {
result[i] = -1;
return result;
package array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
<p>Note: Could you optimize your algorithm to use only O(k) extra space?
System.out.println(new PascalsTriangle().getRow(3));
int k = rowIndex;
if (k == 0) return Arrays.asList(1);
result.add(2);
k = k - 2;
int p, c;
p = 1;
int i = 0;
c = result.get(i);
result.set(i, p + c);
p = c;
result.add(p + 1);
result.add(0, 1);
result.add(1);
return result;
}
package array;
representing the height of the terrain at that index. The width at each index is 1. After V units
<p>Water first drops at index K and rests on top of the highest terrain or water at that index.
<p>If the droplet would eventually fall by moving left, then move left. Otherwise, if the droplet
would eventually fall by moving right, then move right. Otherwise, rise at it's current position.
Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in
that direction. Also, "level" means the height of the terrain plus any water in that column. We
can assume there's infinitely high terrain on the two sides out of bounds of the array. Also,
there could not be partial water being spread out evenly on more than 1 grid block - each unit of
<p>When moving left or right, the water can only move to the same level or a lower level. (By
level, we mean the total height of the terrain plus any water in that column.) Since moving left
will eventually make it fall, it moves left. (A droplet "made to fall" means go to a lower height
<p>Since moving left will not make it fall, it stays in place. The next droplet falls:
<p># # # w # ## w# ### ######### 0123456
<p>Since the new droplet moving left will eventually make it fall, it moves left. Notice that the
droplet still preferred to move left, even though it could move right (and moving right makes it
fall quicker.)
<p>After those steps, the third droplet falls. Since moving left would not eventually make it
fall, it tries to move right. Since moving right would eventually make it fall, it moves right.
<p>Finally, the fourth droplet falls. Since moving left would not eventually make it fall, it
tries to move right. Since moving right would not eventually make it fall, it stays in place:
[2,3,3,4] Explanation: The last droplet settles at index 1, since moving further left would not
<p>heights will have length in [1, 100] and contain integers in [0, 99]. V will be in range [0,
<p>Solution: Check first left and then right to see if there are any lower levels, if yes then
drop the water at this point. Else maintain the drop at the start position
Main method
@param args
@throws Exception
heights[K] += 1;
int index = K;
break;
min = heights[i] + 1;
index = i;
if (index == K) {
break;
index = i;
if (index != K) {
heights[K]--;
heights[index]++;
return heights;
package array;
<p>Given an array of n integers where n > 1, nums, return an array output such that output[i] is
<p>Follow up: Could you solve it with constant space complexity? (Note: The output array does not
result[i] = temp;
temp = nums[i];
temp = nums[i];
return result;
package array;
Created by gouthamvidyapradhan on 09/12/2017. The API: int read4(char buf) reads 4 characters at
<p>The return value is the actual number of characters read. For example, it returns 3 if there
<p>By using the read4 API, implement the function int read(char buf, int n) that reads n
<p>Note: The read function will only be called once for each test case.
Main method
@param args
/
public static void main(String[] args) {}
@param buf
@param n
@return
int i = 0;
int r = read4(temp);
buf[i] = temp[j];
i++;
if (r < 4) break;
package array;
import java.util.ArrayList;
import java.util.List;
ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal",
"5"] Explanation: The first three athletes got the top three highest scores, so they got "Gold
Medal", "Silver Medal" and "Bronze Medal". For the left two athletes, you just need to output
their relative ranks according to their scores. Note: N is a positive integer and won't exceed
class Position {
this.score = score;
this.poisition = position;
Main method
@param args
@throws Exception
for (String i : S) {
System.out.println(i);
if (i == 0) {
} else if (i == 1) {
} else if (i == 2) {
} else {
return S;
package array;
import java.util.;
Created by gouthamvidyapradhan on 05/12/2019 Given two arrays arr1 and arr2, the elements of arr2
<p>Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in
arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
<p>Example 1:
<p>Constraints:
<p>arr1.length, arr2.length <= 1000 0 <= arr1[i], arr2[i] <= 1000 Each arr2[i] is distinct. Each
arr2[i] is in arr1.
/
//
set.add(i);
map.putIfAbsent(i, 0);
result.add(i);
if (!set.contains(k)) {
notPresent.add(k);
result.addAll(notPresent);
int[] resA = new int[result.size()];
resA[i] = result.get(i);
return resA;
package array;
import java.util.ArrayDeque;
import java.util.Arrays;
Created by gouthamvidyapradhan on 12/08/2019 In a deck of cards, every card has a unique integer.
<p>Initially, all the cards start face down (unrevealed) in one deck.
<p>Now, you do the following steps repeatedly, until all cards are revealed:
<p>Take the top card of the deck, reveal it, and take it out of the deck. If there are still
cards in the deck, put the next top card of the deck at the bottom of the deck. If there are
still unrevealed cards, go back to step 1. Otherwise, stop. Return an ordering of the deck that
<p>The first entry in the answer is considered to be the top of the deck.
<p>Example 1:
<p>Input: [17,13,11,2,3,5,7] Output: [2,13,3,11,5,17,7] Explanation: We get the deck in the order
[17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. After reordering, the deck starts
as [2,13,3,11,5,17,7], where 2 is the top of the deck. We reveal 2, and move 13 to the bottom.
The deck is now [3,11,5,17,7,13]. We reveal 3, and move 11 to the bottom. The deck is now
[5,17,7,13,11]. We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. We reveal
7, and move 13 to the bottom. The deck is now [11,17,13]. We reveal 11, and move 17 to the
bottom. The deck is now [13,17]. We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17. Since all the cards revealed are in increasing order, the answer is correct.
<p>Note:
<p>1 <= A.length <= 1000 1 <= A[i] <= 10^6 A[i] != A[j] for all i != j
<p>Solution: O(N) General idea is to start from the last element and build the array of element
in the backwards order. Use a doubly-ended queue which allows you to poll from either end of a
queue.
Arrays.sort(deck);
queue.offer(deck[i]);
if (i == 0) break;
queue.offer(temp);
int i = 0;
while (!queue.isEmpty()) {
answer[i++] = queue.pollLast();
return answer;
}
}
package array;
/
Created by gouthamvidyapradhan on 01/08/2017. Rotate an array of n elements to the
right by k
steps.
<p>Note: Try to come up as many solutions as you can, there are at least 3
different ways to
solve this problem.
<p>Hint: Could you do it in-place with O(1) extra space? Related problem: Reverse
Words in a
String II
/
public class RotateArray {
/
Main method
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
int[] A = {1, 2, 3, 4, 5, 6};
new RotateArray().rotate(A, 2);
for (int i : A) System.out.print(i + " ");
}
public void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
private void reverse(int[] nums, int s, int e) {
for (int i = s, j = e; i < j; i++, j--) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
package array;
/
Created by gouthamvidyapradhan on 21/03/2017. You are given an n x n 2D matrix
representing an
image.
<p>Rotate the image by 90 degrees (clockwise).
<p>Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is
even, i is even.
<p>You may return any answer array that satisfies this condition.
<p>Example 1:
<p>Note:
<p>2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000 Solution: O(N)
straight forward
linear solution, keep track of odd and even indices and increment by 2 every time a
value is
added at the index.
/
public class SortArrayByParityII {
public static void main(String[] args) {
//
}
public int[] sortArrayByParityII(int[] A) {
int[] R = new int[A.length];
int i = 0, j = 1;
for (int a : A) {
if (a % 2 == 0) {
R[i] = a;
i += 2;
} else {
R[j] = a;
j += 2;
}
}
return R;
}
}
package array;
/
Created by gouthamvidyapradhan on 03/12/2017.
<p>You may assume that A's column number is equal to B's row number.
<p>Example:
<p>B = [ [ 7, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 1 ] ]
<p>| 1 0 0 | | 7 0 0 | | 7 0 0 | AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | | 0 0 1 |
<p>Solution: Skip the rows and columns which you already know would result in zero
after
multiplication.
/
public class SparseMatrixMultiplication {
/
Main method
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
int[][] A = {{1, 0, 0, 1}};
int[][] B = {{7, 0, 0}, {0, 0, 0}, {0, 0, 1}, {0, 0, 1}};
int[][] C = new SparseMatrixMultiplication().multiply(A, B);
}
public int[][] multiply(int[][] A, int[][] B) {
boolean[] AH = new boolean[A.length]; // Metadata for matrix A
boolean[] BH = new boolean[B[0].length]; // Metadata for matrix A
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[0].length; j++) {
if (A[i][j] != 0) {
AH[i] = true;
break;
}
}
}
for (int i = 0; i < B[0].length; i++) {
for (int j = 0; j < B.length; j++) {
if (B[j][i] != 0) {
BH[i] = true;
break;
}
}
}
int[][] C = new int[A.length][B[0].length];
for (int i = 0; i < C.length; i++) {
if (AH[i]) {
for (int j = 0; j < C[0].length; j++) {
if (BH[j]) {
int sum = 0;
for (int k = 0; k < A[0].length; k++) {
sum += A[i][k] B[k][j];
}
C[i][j] = sum;
}
}
}
}
return C;
}
}
package array;
import java.util.HashMap;
import java.util.Map;
/
Created by gouthamvidyapradhan on 03/01/2018. Given an array of integers and an
integer k, you
need to find the total number of continuous subarrays whose sum equals to k.
<p>Solution: O(n) Maintain a hash-map of prefix sum and its count and check for
range sum for
each element.
/
public class SubarraySumEqualsK {
/
Main method
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
int[] A = {1, 2, 1, -2, 3, -1, -1};
System.out.println(new SubarraySumEqualsK().subarraySum(A, 2));
}
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
for (int i : nums) {
sum += i;
Integer count = map.get(sum);
if (count == null) {
map.put(sum, 1);
} else {
map.put(sum, count + 1);
}
}
sum = 0;
int result = 0;
for (int i : nums) {
int key = sum + k;
if (map.containsKey(key)) {
int count = map.get(key);
result += count;
}
sum += i;
if (map.containsKey(sum)) {
int count = map.get(sum);
if (count - 1 > 0) {
map.put(sum, count - 1);
} else {
map.remove(sum);
}
}
}
return result;
}
}
package array;
/
Created by gouthamvidyapradhan on 30/04/2019 On a N N grid, we place some 1 1 1
cubes.
<p>Example 1:
<p>Note:
<p>Solution: O(N x M) For each cell, check each adjacent cell and sum the value of
(current cell
- adjacent cell) if the current cell value is greater than adjacent cell. For every
cell which
has value grater then 0, the top surface area is by default 1 therefore add one to
the sum of
each cell.
/
public class SurfaceAreaOfThreeDShapes {
private final int[] R = {0, 0, -1, 1};
private final int[] C = {1, -1, 0, 0};
/
Main method
@param args
/
public static void main(String[] args) {
int[][] A = {{2}};
System.out.println(new SurfaceAreaOfThreeDShapes().surfaceArea(A));
}
public int surfaceArea(int[][] grid) {
int sum = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int cell = grid[i][j];
for (int k = 0; k < 4; k++) {
int newR = i + R[k];
int newC = j + C[k];
if (newR >= 0 && newC >= 0 && newR < grid.length && newC < grid[0].length) {
int adjacent = grid[newR][newC];
if (cell > adjacent) {
sum += (cell - adjacent);
}
} else if (newR < 0 || newR >= grid.length || newC < 0 || newC >= grid[0].length) {
sum += cell;
}
}
if (cell > 0) {
sum += 2;
}
}
}
return sum;
}
}
package array;
/
Created by gouthamvidyapradhan on 25/03/2017. Given a non-empty array of integers,
return the
third maximum number in this array. If it does not exist, return the maximum
number. The time
complexity must be in O(n).
<p>Output: 1
<p>Output: 2
<p>Explanation: The third maximum does not exist, so the maximum (2) is returned
instead. Example
3: Input: [2, 2, 3, 1]
<p>Output: 1
<p>Explanation: Note that the third maximum here means the third maximum distinct
number. Both
numbers with value 2 are both considered as second maximum.
/
public class ThirdMaximumNumber {
/
Main method
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
int[] a = {1, 2};
System.out.println(new ThirdMaximumNumber().thirdMax(a));
}
public int thirdMax(int[] nums) {
long[] max = {Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE};
int count = 0;
for (int num : nums) {
for (int j = 0; j < 3; j++) {
if (max[j] > num) continue;
else if (max[j] == num) break;
int k = j;
long temp1, temp2;
temp1 = num;
count++;
while (k < 3) {
temp2 = max[k];
max[k] = temp1;
temp1 = temp2;
k++;
}
break;
}
}
System.out.println(Integer.MIN_VALUE);
return (count >= 3) ? (int) max[2] : (int) max[0];
}
}
package array;
/
Created by gouthamvidyapradhan on 18/03/2017. Given an array of integers that is
already sorted
in ascending order, find two numbers such that they add up to a specific target
number.
<p>The function twoSum should return indices of the two numbers such that they add
up to the
target, where index1 must be less than index2. Please note that your returned
answers (both
index1 and index2) are not zero-based.
<p>You may assume that each input would have exactly one solution and you may not
use the same
element twice.
<p>The board is a 3 x 3 array, and consists of characters " ", "X", and "O". The "
" character
represents an empty square.
<p>Players take turns placing characters into empty squares (" "). The first player
always places
"X" characters, while the second player always places "O" characters. "X" and "O"
characters are
always placed into empty squares, never filled ones. The game ends when there are 3
of the same
(non-empty) character filling any row, column, or diagonal. The game also ends if
all squares are
non-empty. No more moves can be played if the game is over. Example 1: Input: board
= ["O ", " ",
" "] Output: false Explanation: The first player always plays "X".
<p>Example 2: Input: board = ["XOX", " X ", " "] Output: false Explanation: Players
take turns
making moves.
<p>Example 4: Input: board = ["XOX", "O O", "XOX"] Output: true Note:
<p>board is a length-3 array of strings, where each string board[i] has length 3.
Each
board[i][j] is a character in the set {" ", "X", "O"}.
<p>Solution: Do a brute-force check for each row, column and diagonals and keep
track of count of
'X' and 'O'
/
public class ValidTicTacToeState {
/
Main method
@param args
/
public static void main(String[] args) {
String[] board = {"XXX", "XOO", "OO "};
System.out.println(new ValidTicTacToeState().validTicTacToe(board));
}
public boolean validTicTacToe(String[] board) {
boolean xWon = hasWon(board, 'X');
boolean oWon = hasWon(board, 'O');
int xcount = 0, ocount = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i].charAt(j) == 'X') {
xcount++;
} else if (board[i].charAt(j) == 'O') {
ocount++;
}
}
}
if (xWon && oWon) return false;
if (xWon) {
return ((xcount - 1 == ocount));
} else if (oWon) {
return ((xcount == ocount));
} else {
return (xcount == ocount || xcount - 1 == ocount);
}
}
private boolean hasWon(String[] board, char c) {
boolean diagnol =
((board[0].charAt(0) == c && board[1].charAt(1) == c && board[2].charAt(2) == c)
|| (board[0].charAt(2) == c && board[1].charAt(1) == c && board[2].charAt(0) == c));
if (diagnol) return true;
for (int i = 0; i < 3; i++) {
if (board[i].charAt(0) == c && board[i].charAt(1) == c && board[i].charAt(2) == c)
return true;
}
for (int i = 0; i < 3; i++) {
if (board[0].charAt(i) == c && board[1].charAt(i) == c && board[2].charAt(i) == c)
return true;
}
return false;
}
}
-