Friday 16 June 2023

Reverse Java Array with Two-Pointer Technique

 To reverse an array in Java with an optimized approach, you can use a two-pointer technique. Here's an example implementation:

java
public class ReverseArray
public static void reverse(int[] arr)
int left = 0
int right = arr.length - 1;
while (left < right) { 
// Swap elements at left and right indices int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; 
// Move the pointers towards the center 
 left++; right--; 
 } 
 } 
public static void main(String[] args)
int[] arr = {1, 2, 3, 4, 5}; 
 System.out.println("Original Array: " + Arrays.toString(arr)); reverse(arr); System.out.println("Reversed Array: " + Arrays.toString(arr)); 
 } 
}

In this example, the reverse method takes an array of integers (arr) as input. It initializes two pointers, left and right, pointing to the first and last elements of the array, respectively. It then iterates through the array, swapping the elements at the left and right indices using a temporary variable. After each swap, the left pointer is incremented and the right pointer is decremented, moving towards the center of the array. This process continues until the left pointer crosses the right pointer, resulting in a reversed array.

The example demonstrates the reversal of the array {1, 2, 3, 4, 5}. The output will be:

javascript
Original Array: [1, 2, 3, 4, 5
Reversed Array: [5, 4, 3, 2, 1]

Using the two-pointer technique ensures that each element in the array is swapped only once, resulting in an optimized approach for reversing the array.

Find TwoSum from array in java

This one is very favourite question of java interviewers, In which you have to find the two numbers in an array that add up to a specific target sum, you can use the Two Sum problem solution. Here's an example implementation in Java:

java
import java.util.HashMap;
import java.util.Map; 
public class TwoSum
public static int[] findTwoSum(int[] nums, int target) { 
 Map<Integer, Integer> map = new HashMap<>(); 
for (int i = 0; i < nums.length; i++) { 
int complement = target - nums[i]; if (map.containsKey(complement)) { 
return new int[]{map.get(complement), i}; 
 } 
 map.put(nums[i], i); 
 }
return new int[]{-1, -1}; // If no solution is found 
 } 
public static void main(String[] args)
int[] nums = {2, 7, 11, 15}; 
int target = 9
int[] result = findTwoSum(nums, target); 
if (result[0] != -1 && result[1] != -1) { 
 System.out.println("Two numbers found: " + nums[result[0]] + " and " + nums[result[1]]); 
 } else
 System.out.println("No two numbers found that add up to the target sum."); } 
 } 
}

In this example, the findTwoSum method takes an array of integers (nums) and a target sum as parameters. It uses a HashMap to store the complement of each element (the difference between the target and the current number) and its corresponding index. It iterates through the array, checking if the complement exists in the HashMap. If found, it returns the indices of the two numbers that add up to the target sum. If no solution is found, it returns [-1, -1].