Java Practice Set Fof FS
Java Practice Set Fof FS
Problem:
You are given an array of integers and a target sum. In one operation, you can select two distinct
elements from the array whose sum equals the target, remove them from the array, and repeat.
Return the maximum number of such operations you can perform.
Input Format:
• 1 ≤ n ≤ 10^5
• 1 ≤ array[i] ≤ 10^9
• 1 ≤ target ≤ 10^9
Sample Input:
6
1 2 3 4 3 6
6
Sample Output:
2
———————————————————————————————————————————
2. Reduce Array to 1
Problem:
Given an array of positive integers, in each operation, you can select two adjacent elements, remove
them, and replace them with their sum. Return the minimum number of operations required to
reduce the array to a single element.
Input Format:
• 2 ≤ n ≤ 10^5
Sample Input:
5
fi
fi
1 2 3 4 5
Sample Output:
4
———————————————————————————————————————————
Problem:
Given a binary array (containing only 0s and 1s), nd the length of the largest subarray that contains
equal numbers of 0s and 1s.
Input Format:
• The rst line contains an integer n (the length of the binary array).
• The second line contains n space-separated integers (the array values: 0s and 1s).
Constraints:
• 1 ≤ n ≤ 10^5
Sample Input:
6
1 0 0 1 0 1
Sample Output:
4
———————————————————————————————————————————
Problem:
Given an array of positive integers, in each operation, you can remove two elements from the array
whose sum is even. Return the maximum number of operations you can perform.
Input Format:
• 1 ≤ n ≤ 10^5
Sample Input:
6
1 2 2 3 4 6
fi
fi
fi
Sample Output:
3
———————————————————————————————————————————
Problem:
Given an array of integers, rearrange the array such that every adjacent pair of elements forms
consecutive numbers. If it is possible, return the rearranged array. If not, return -1.
Input Format:
• 1 ≤ n ≤ 10^5
Sample Input:
6
3 4 2 1 5 6
Sample Output:
1 2 3 4 5 6
1. String Compression
Problem:
Given a string, write a Java program to compress the string using the counts of repeated characters.
If the compressed string is longer than the original string, return the original string.
Input Format:
• 1 ≤ ∣s∣ ≤ 10^5
Sample Input:
aaabbccc
Sample Output:
fi
a3b2c3
———————————————————————————————————————————
Problem:
Given an array of integers, write a program to remove all duplicate elements while maintaining the
insertion order.
Input Format:
• 1 ≤ n ≤ 10^5
Sample Input:
7
1 2 2 3 4 5 3
Sample Output:
1 2 3 4 5
———————————————————————————————————————————
Problem:
Write a Java program to nd the rst non-repeating character in a given string.
Input Format:
• 1 ≤ ∣s∣ ≤ 10^5
Sample Input:
swiss
Sample Output:
w
———————————————————————————————————————————
fi
fi
fi
4. Custom Exception for Invalid Input
Problem:
Write a Java program that demonstrates custom exception handling. Implement an exception called
InvalidInputException that is thrown when the input is not a positive integer.
Input Format:
• A single integer n.
Sample Input:
-5
Sample Output:
Problem:
Write a Java program to merge two lists of integers and remove duplicates. Return the merged list in
sorted order.
Input Format:
• The rst line contains an integer n (the length of the rst list).
• The second line contains n space-separated integers (the rst list).
• The third line contains an integer m (the length of the second list).
• The fourth line contains m space-separated integers (the second list).
Constraints:
• 1 ≤ n,m ≤ 10^5
Sample Input:
3
1 2 3
4
3 4 5 6
Sample Output:
1 2 3 4 5 6
fi
fi
fi