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

HackWithInfy_Java_Practice_Problems

The document contains Java coding practice problems, including solutions for finding a missing number in an array, determining minimum insertions to make a string a palindrome, calculating the length of the longest consecutive subsequence, generating a character pattern based on position, and finding the maximum subarray sum with one deletion allowed. Each problem is accompanied by a code implementation and a main method for testing. These problems are designed to enhance coding skills and algorithmic thinking.

Uploaded by

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

HackWithInfy_Java_Practice_Problems

The document contains Java coding practice problems, including solutions for finding a missing number in an array, determining minimum insertions to make a string a palindrome, calculating the length of the longest consecutive subsequence, generating a character pattern based on position, and finding the maximum subarray sum with one deletion allowed. Each problem is accompanied by a code implementation and a main method for testing. These problems are designed to enhance coding skills and algorithmic thinking.

Uploaded by

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

HackWithInfy Java Coding Practice Problems

1. Missing Number in Array


Find the missing number from an array of n-1 integers ranging from 1 to n.

import java.util.*;

public class MissingNumber {


public static int findMissing(int[] arr, int n) {
int total = n * (n + 1) / 2;
int sum = 0;
for (int num : arr) {
sum += num;
}
return total - sum;
}

public static void main(String[] args) {


int[] arr = {1, 2, 4, 5};
int n = 5;
System.out.println("Missing number is: " + findMissing(arr, n));
}
}

2. Minimum Insertions to Make a String Palindrome


Find the minimum number of insertions to make a string a palindrome.

public class MinInsertPalindrome {


public static int minInsertions(String s) {
int n = s.length();
int[][] dp = new int[n][n];

for (int gap = 1; gap < n; gap++) {


for (int l = 0, r = gap; r < n; l++, r++) {
if (s.charAt(l) == s.charAt(r)) {
dp[l][r] = dp[l+1][r-1];
} else {
dp[l][r] = Math.min(dp[l+1][r], dp[l][r-1]) + 1;
}
}
}
return dp[0][n-1];
}

public static void main(String[] args) {


String s = "abcda";
System.out.println("Minimum insertions: " + minInsertions(s));
}
}

3. Longest Consecutive Subsequence


Return the length of the longest sequence of consecutive elements.

import java.util.*;

public class LongestConsecutive {


public static int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) set.add(num);

int longest = 0;
for (int num : set) {
if (!set.contains(num - 1)) {
int currentNum = num;
int count = 1;

while (set.contains(currentNum + 1)) {


currentNum++;
count++;
}

longest = Math.max(longest, count);


}
}

return longest;
}

public static void main(String[] args) {


int[] nums = {100, 4, 200, 1, 3, 2};
System.out.println("Longest Consecutive Sequence Length: " +
longestConsecutive(nums));
}
}

4. String Pattern Generator


Print a pattern with each character repeated based on its position.

public class PatternPrinter {


public static void main(String[] args) {
String s = "abc";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
for (int j = 0; j <= i; j++) {
System.out.print(c);
}
System.out.println();
}
}
}

5. Maximum Subarray Sum with One Deletion Allowed


Find the maximum sum of a subarray with at most one deletion.

public class MaxSumOneDeletion {


public static int maximumSum(int[] arr) {
int n = arr.length;
int[] f = new int[n]; // max sum ending at i
int[] b = new int[n]; // max sum starting at i

f[0] = arr[0];
int maxSum = arr[0];

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


f[i] = Math.max(arr[i], f[i - 1] + arr[i]);
maxSum = Math.max(maxSum, f[i]);
}

b[n - 1] = arr[n - 1];


for (int i = n - 2; i >= 0; i--) {
b[i] = Math.max(arr[i], b[i + 1] + arr[i]);
}

for (int i = 1; i < n - 1; i++) {


maxSum = Math.max(maxSum, f[i - 1] + b[i + 1]);
}

return maxSum;
}

public static void main(String[] args) {


int[] arr = {1, -2, 0, 3};
System.out.println("Maximum sum with one deletion: " + maximumSum(arr));
}
}

You might also like