deep logic probllems
questions:
Here’s a list of intermediate-level array logic-building questions that will make you think beyond basic looping and indexing:
1. Rotation & Shifting
-
Rotate an array k times to the left or right without using an extra array.
-
Shift all zeroes to the end of the array while preserving the order of other elements.
2. Frequency & Counting
-
Find the most frequent element in the array without using a map/dictionary.
-
Count the number of pairs whose sum is equal to a given number.
3. Pattern Recognition
-
Check if the array is a rotation of another given array.
-
Determine if the array is a mountain array (strictly increasing then strictly decreasing).
4. Subarrays & Sums
-
Find the maximum sum subarray (Kadane’s Algorithm).
-
Count all subarrays with sum equal to 0.
5. Rearrangement Challenges
-
Rearrange the array so that positive and negative numbers alternate (order not important).
-
Rearrange the array so that the first half is ascending and the second half is descending.
6. Search & Optimization
-
Find the second smallest and second largest element in the array in one pass.
-
Find the smallest missing positive integer in the array.
7. Advanced Index Problems
-
Given an array where every element appears twice except one, find that element in O(1) space.
-
Find the length of the longest consecutive sequence in an unsorted array.
8. Matrix-like Thinking (1D to 2D Concept)
-
Treat the array as a matrix and rotate it 90 degrees (given it has N×N elements).
-
Given a sorted array, convert it into a wave array (like
a1 >= a2 <= a3 >= a4...).
If you want, I can make you a progressive practice set where the difficulty increases step-by-step — so you build logic gradually without getting stuck too early.
Would you like me to prepare that set?
Q1)
Index Product Except Self
Given an array, create a new array where each element is the product of all other elements except the one at that index.
No division allowed.
Example: [1, 2, 3, 4] → [24, 12, 8, 6]
class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = arr.length;
int[] result = new int[n];
// Step 1: Calculate left products directly in result
result[0] = 1;
for (int i = 1; i < n; i++) {
result[i] = result[i - 1] * arr[i - 1];
}
// Step 2: Multiply with right products (calculated in reverse)
int right = 1; // running product of elements to the right
for (int i = n - 1; i >= 0; i--) {
result[i] *= right;
right *= arr[i];
}
// Output
for (int val : result) {
System.out.println(val);
}
}
}
Comments
Post a Comment