Question
Question
1. Bitwise subsequence
You have an array A of N integers A1 A2 .. An. Find the longest increasing
subsequence Ai1 Ai2 .. Ak
(1 <= k <= N) that satisfies the following condition:
For every adjacent pair of numbers of the chosen subsequence Ai[x] and Ai[x+1] (1 <
x < k), the
expression( Ai[x] & Ai[x+1] ) * 2 < ( Ai[x] | Ai[x+1] ) is true .
Note: '&' is the bitwise AND operation, ' | ' is the bit-wise OR operation
==>import java.util.*;
public class Main
{
public static void main(String[] args) {
int nums[]={17, 16, 12, 2, 8, 17, 17};
int dp[]=new int[nums.length];
int max=1;
if(nums.length==0 || nums==null){
System.out.println("0");
}
Arrays.fill(dp, 1);
for(int i=1;i<nums.length;i++){
for(int j=0;j<i;j++){
if(nums[i]>nums[j]){
if((nums[i] & nums[j]) *2 < (nums[i] | nums[j])){
dp[i]=Math.max(dp[i], dp[j]+1);
}
}
}
max=Math.max(dp[i], max);
}
System.out.println(max);
}
}
2..Grid Path
Given a grid. Each cell of the grid contains a single integer value. These values
are described by 2D
integer array a with N rows and 2 columns, where a[i][j] is the value in the cell
located in row i,
column j.
Standing in (i; j), the player can move to any cell of the next row (i+1; j2) under
the condition that
a[i+1][j2] > a[i][j]. In other words, the value in the next cell of the player's
path should be strictly
greater than the value in the current cell of the player's path.
The player can't make any moves if he's already in the last row.
If the player starts in any cell of the first row (either (1; 1) or (1; 2)), what
is the maximum possible
total sum of values in all cells he can visit on his path?
int maxSum = 0;
public static int maxPath(int[][] grid, int n, int row, int col) {
if (row == n - 1) {
return grid[row][col];
}
int maxSum = 0;
Next to be Solved
Arithmetic slice II.