0% found this document useful (0 votes)
15 views

Question

The document outlines various algorithmic problems related to sequences and grid paths, including decoding strings and finding maximum sums under specific conditions. It provides Java code implementations for two problems: finding the longest increasing subsequence with a bitwise condition and calculating the maximum path sum in a grid with movement restrictions. Additionally, it mentions an upcoming problem titled 'Arithmetic Slice II.'

Uploaded by

dilusingh8084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Question

The document outlines various algorithmic problems related to sequences and grid paths, including decoding strings and finding maximum sums under specific conditions. It provides Java code implementations for two problems: finding the longest increasing subsequence with a bitwise condition and calculating the maximum path sum in a grid with movement restrictions. Additionally, it mentions an upcoming problem titled 'Arithmetic Slice II.'

Uploaded by

dilusingh8084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Important Question.

394. Decode String [stack]


1425. Constrained Subsequence Sum
300. Longest Increasing Subsequence.
1793. Maximum Score of a Good Subarray.
1043. Partition Array for Maximum Sum
368. Largest Divisible Subset
670. Maximum Swap
1498. Number of Subsequences That Satisfy the Given Sum Condition
395. Longest Substring with At Least K Repeating Characters
143. Reorder List [This contains, find middle, reverse, merge LinkedList]
Shortest Unsorted Continuous Subarray

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?

==> import java.util.*;


public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] grid = new int[n][2];

for (int i = 0; i < n; i++) {


for (int j = 0; j < 2; j++) {
grid[i][j] = sc.nextInt();
}
}

int maxSum = 0;

// Try starting from both (1, 1) and (1, 2)


for (int j = 0; j < 2; j++) {
maxSum = Math.max(maxSum, maxPath(grid, n, 0, j));
}

// Print the maximum sum modulo 10^9+7


System.out.println(maxSum % 1000000007);
}

public static int maxPath(int[][] grid, int n, int row, int col) {
if (row == n - 1) {
return grid[row][col];
}
int maxSum = 0;

for (int j = 0; j < 2; j++) {


if (grid[row + 1][j] > grid[row][col]) {
maxSum = Math.max(maxSum, maxPath(grid, n, row + 1, j));
}
}

return grid[row][col] + maxSum;


}
}

Next to be Solved
Arithmetic slice II.

You might also like