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

Java Practice Set Fof FS

Uploaded by

subham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Practice Set Fof FS

Uploaded by

subham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Problem-Solving Questions:

1. Pair Sums to Target

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:

• The rst line contains an integer n (the length of the array).


• The second line contains n space-separated integers (the array).
• The third line contains an integer target.
Constraints:

• 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:

• The rst line contains an integer n (the length of the array).


• The second line contains n space-separated integers (the array).
Constraints:

• 2 ≤ n ≤ 10^5

Sample Input:

5
fi
fi
1 2 3 4 5

Sample Output:

4
———————————————————————————————————————————

3. Largest Subarray with Equal 0s and 1s

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
———————————————————————————————————————————

4. Maximize Array by Removing Pairs

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:

• The rst line contains an integer n (the length of the array).


• The second line contains n space-separated integers.
Constraints:

• 1 ≤ n ≤ 10^5

Sample Input:

6
1 2 2 3 4 6
fi
fi
fi
Sample Output:

3
———————————————————————————————————————————

5. Rearrange Array into Consecutive Pairs

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:

• The rst line contains an integer n (the length of the array).


• The second line contains n space-separated integers.
Constraints:

• 1 ≤ n ≤ 10^5

Sample Input:

6
3 4 2 1 5 6

Sample Output:

1 2 3 4 5 6

Java (Collections, String, Exception Handling) Questions:

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:

• A single line containing the string s.


Constraints:

• 1 ≤ ∣s∣ ≤ 10^5

Sample Input:

aaabbccc

Sample Output:
fi
a3b2c3
———————————————————————————————————————————

2. Remove Duplicates in an Array (Using Collections)

Problem:
Given an array of integers, write a program to remove all duplicate elements while maintaining the
insertion order.

Input Format:

• The rst line contains an integer n (the length of the array).


• The second line contains n space-separated integers.
Constraints:

• 1 ≤ n ≤ 10^5

Sample Input:

7
1 2 2 3 4 5 3

Sample Output:

1 2 3 4 5
———————————————————————————————————————————

3. Find the First Non-Repeated Character (Using HashMap)

Problem:
Write a Java program to nd the rst non-repeating character in a given string.

Input Format:

• A single line containing the string s.


Constraints:

• 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:

Exception caught: Invalid input - must be a positive integer.


———————————————————————————————————————————

5. Merge Two Lists (Using Collections)

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

You might also like