Category: Java programming question
-
Move Zeroes
Problem Statement You are given an array of integers where some elements are zero. Your task is to move all the zeroes to the end of the array, maintaining the relative order of non-zero elements. The solution must be: Example Input: [4, 2, 0, 4, 0, 3, 0, 5, 0, 1]Output: [4, 2, 4, 3,…
-
Happy Number
Problem Statement You are given a positive integer n. Replace the number by the sum of the squares of its digits, and repeat the process until: Return true if n is a happy number. Otherwise, return false. What is a Happy Number? A happy number is a number that eventually reaches 1 when you repeatedly…
-
Clone Graph
1. Problem Statement You’re given a reference to a node in a connected, undirected graph. Each node contains: You must return a deep copy of the entire graph. 2. Node Definition 3. Key Idea This is a classic graph traversal + cloning problem. When you encounter a node: 4. DFS Approach with HashMap We use…
-
Best time to buy and sell stock
1. Problem Statement You are given an array prices[] where prices[i] is the stock price on the i-th day. You may: Goal: Return the maximum profit you can make. Example: Explanation: 2. Solution approach Let’s look at this example: We represent each day and compare it with the previous one: Profit Calculation: Total Profit =…
-
Symmetric Tree
1. Problem statement Given the root of a binary tree, check whether the tree is symmetric around its center. In simple terms:A binary tree is symmetric if the left subtree is a mirror of the right subtree. Example 1: Symmetric Tree Example 2: Not Symmetric 2. Recursive Java Solution 3. Explanation 4. Iterative Java Solution…
-
Decode ways
1. Problem statement You’ve intercepted a secret message encoded as a string of digits. Your task is to determine how many different ways this string can be decoded into letters, based on the following mapping: Rules: Example 1: Example 2: 2. How the Solution Works Imagine you’re walking along the string from left to right.…
-
Maximum Subarray
1. Problem statement Given an integer array nums, find the subarray with the largest sum, and return its sum. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1]Output: 1Explanation: The subarray [1] has the largest sum 1. 2. Core Idea of Kadane’s Algorithm At every…
-
Container with most water problem
1. Problem statement You are given an array height of length n, where each element represents the height of a vertical line drawn at that index i. These lines are drawn on the x-axis from (i, 0) to (i, height[i]). You need to find two lines such that, together with the x-axis, they form a…
-
Two Sum Problem
Problem Statement Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Examples: Solution 1: Brute Force Approach Logic: Drawback: Too slow for…
-
Rearrange Sorted Array in Max/Min Form
1. Problem Statement Given a sorted array in ascending order, rearrange it in an alternating max/min form. Explanation: 2. Approach Step 1: Choose a multiplier (maxElem) We need to store two values at each index: We choose: This ensures: Step 2: Initialization Step 3: Encode values one by one We use: Then, at the end:…
