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

TCS Coding

Tcs code question previous year

Uploaded by

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

TCS Coding

Tcs code question previous year

Uploaded by

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

ItsRunTym

25 Repeating Array Ques

1. Find the smallest number in an array


2. Find the largest number in an array
3. Find the second smallest and second largest element in an array
4. Reverse a given array
5. Count the frequency of each element in an array
6. Rearrange the array in increasing-decreasing order
7. Calculate the sum of the elements of the array
8. Rotate an array by K elements - Block Swap Algorithm
9. Find the average of all elements in an array
10. Find the median of the given array
11. Remove duplicates from a sorted array
12. Remove duplicates from an unsorted array
13. Add an element in an array
14. Find all repeating elements in an array
15. Find all non-repeating elements in an array
16. Find all symmetric pairs in an array
17. Find the maximum product subarray in an array
18. Replace each element of the array by its rank in the array
19. Sort the elements of an array by frequency
20. Rotate the elements of an array (left and right)
21. Find the equilibrium index of an array
22. Find the circular rotation of an array by K positions
23. Sort an array according to the order defined by another array
24. Search for an element in an array
25. Check if an array is a subset of another array or not

1. Find the smallest number in an array


java
public static int findSmallest(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
• Time Complexity: O(n) — We scan the entire array once.
• Space Complexity: O(1) — We use only a few extra variables.

2. Find the largest number in an array


java

public static int findLargest(int[] arr) {


int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
• Time Complexity: O(n) — We scan the entire array once.
• Space Complexity: O(1) — Only one variable is used for storing the maximum.

3. Find the second smallest and second largest element in an array


java

public static int[] findSecondSmallestAndLargest(int[] arr) {


Arrays.sort(arr);
return new int[]{arr[1], arr[arr.length - 2]};
}
• Time Complexity: O(n log n) — Due to sorting.
• Space Complexity: O(1) — Only two variables are used.

4. Reverse a given array


java

public static void reverseArray(int[] arr) {


int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
• Time Complexity: O(n) — Each element is swapped once.
• Space Complexity: O(1) — We are swapping elements in place.

5. Count the frequency of each element in an array


java

public static Map<Integer, Integer> countFrequency(int[] arr) {


Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
return freqMap;
}
• Time Complexity: O(n) — We traverse the array once.
• Space Complexity: O(n) — Space is required for the frequency map.

6. Rearrange the array in increasing-decreasing order


java

public static void rearrangeIncDec(int[] arr) {


Arrays.sort(arr);
int[] result = new int[arr.length];
int left = 0, right = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
result[i] = arr[right--];
} else {
result[i] = arr[left++];
}
}
System.arraycopy(result, 0, arr, 0, arr.length);
}
• Time Complexity: O(n log n) — Due to sorting.
• Space Complexity: O(n) — We use an extra array for rearranging.

7. Calculate the sum of the elements of the array


java

public static int sumOfArray(int[] arr) {


int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
• Time Complexity: O(n) — We traverse the array once.
• Space Complexity: O(1) — No extra space required.

8. Rotate an array by K elements (Block Swap Algorithm)


java

public static void rotateArray(int[] arr, int k) {


int n = arr.length;
k = k % n;
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
reverse(arr, 0, n - 1);
}

public static void reverse(int[] arr, int start, int end) {


while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
• Time Complexity: O(n) — Each element is reversed a constant number of times.
• Space Complexity: O(1) — In-place modification.

9. Find the average of all elements in an array


java

public static double findAverage(int[] arr) {


return (double) sumOfArray(arr) / arr.length;
}
• Time Complexity: O(n) — We reuse the sum calculation.
• Space Complexity: O(1) — Only constant space used.

10. Find the median of the given array


java

public static double findMedian(int[] arr) {


Arrays.sort(arr);
int n = arr.length;
if (n % 2 == 0) {
return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
} else {
return arr[n / 2];
}
}
• Time Complexity: O(n log n) — Sorting the array.
• Space Complexity: O(1) — Sorting is done in place.
11. Remove duplicates from a sorted array
java

public static int[] removeDuplicatesFromSorted(int[] arr) {


if (arr.length == 0) return new int[0];
int j = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return Arrays.copyOf(arr, j + 1);
}
• Time Complexity: O(n) — One pass through the array.
• Space Complexity: O(1) — No extra space needed except the result.

12. Remove duplicates from an unsorted array


java

public static int[] removeDuplicatesFromUnsorted(int[] arr) {


Set<Integer> set = new LinkedHashSet<>();
for (int num : arr) {
set.add(num);
}
int[] result = new int[set.size()];
int i = 0;
for (int num : set) {
result[i++] = num;
}
return result;
}
• Time Complexity: O(n) — We iterate over the array and use a set.
• Space Complexity: O(n) — The set takes extra space.

13. Add an element in an array


java

public static int[] addElement(int[] arr, int element) {


int[] newArr = new int[arr.length + 1];
System.arraycopy(arr, 0, newArr, 0, arr.length);
newArr[arr.length] = element;
return newArr;
}
• Time Complexity: O(n) — Copying the entire array.
• Space Complexity: O(n) — Extra space for the new array.

14. Find all repeating elements in an array


java

public static List<Integer> findRepeating(int[] arr) {


Set<Integer> seen = new HashSet<>();
List<Integer> repeating = new ArrayList<>();
for (int num : arr) {
if (!seen.add(num)) {
repeating.add(num);
}
}
return repeating;
}
• Time Complexity: O(n) — We traverse the array and use a set.
• Space Complexity: O(n) — Set and list both take linear space.

15. Find all non-repeating elements in an array


java

public static List<Integer> findNonRepeating(int[] arr) {


Map<Integer, Integer> countMap = new HashMap<>();
for (int num : arr) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
if (entry.getValue() == 1) {
result.add(entry.getKey());
}
}
return result;
}
• Time Complexity: O(n) — Two passes: one to count, another to filter.
• Space Complexity: O(n) — HashMap to store counts.

16. Find all symmetric pairs in an array


java

public static List<int[]> findSymmetricPairs(int[][] arr) {


Map<Integer, Integer> map = new HashMap<>();
List<int[]> result = new ArrayList<>();
for (int[] pair : arr) {
int first = pair[0], second = pair[1];
if (map.containsKey(second) && map.get(second) == first) {
result.add(new int[]{second, first});
} else {
map.put(first, second);
}
}
return result;
}
• Time Complexity: O(n) — Traverse the array once.
• Space Complexity: O(n) — Map takes linear space.

17. Find the maximum product subarray in an array


java

public static int maxProductSubarray(int[] arr) {


int max = arr[0], min = arr[0], result = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < 0) {
int temp = max;
max = min;
min = temp;
}
max = Math.max(arr[i], max * arr[i]);
min = Math.min(arr[i], min * arr[i]);
result = Math.max(result, max);
}
return result;
}
• Time Complexity: O(n) — One pass through the array.
• Space Complexity: O(1) — Constant extra space.

18. Replace each element of the array by its rank in the array
java

public static int[] rankArray(int[] arr) {


int[] sorted = arr.clone();
Arrays.sort(sorted);
Map<Integer, Integer> rankMap = new HashMap<>();
for (int i = 0; i < sorted.length; i++) {
rankMap.putIfAbsent(sorted[i], i + 1);
}
for (int i = 0; i < arr.length; i++) {
arr[i] = rankMap.get(arr[i]);
}
return arr;
}
• Time Complexity: O(n log n) — Sorting the array.
• Space Complexity: O(n) — Space for the rank map.

19. Sort the elements of an array by frequency


java

public static int[] sortByFrequency(int[] arr) {


Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
return Arrays.stream(arr)
.boxed()
.sorted((a, b) -> {
int freqCompare = freqMap.get(b).compareTo(freqMap.get(a));
return freqCompare != 0 ? freqCompare : Integer.compare(a, b);
})
.mapToInt(i -> i)
.toArray();
}
• Time Complexity: O(n log n) — Sorting the array by frequency.
• Space Complexity: O(n) — Map and result array.

20. Rotate the elements of an array (left and right)


java

public static void rotateLeft(int[] arr, int k) {


k = k % arr.length;
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
reverse(arr, 0, arr.length - 1);
}

public static void rotateRight(int[] arr, int k) {


k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
• Time Complexity: O(n) — Reversing the elements.
• Space Complexity: O(1) — No extra space used.

21. Find the equilibrium index of an array


java

public static int findEquilibriumIndex(int[] arr) {


int totalSum = 0, leftSum = 0;
for (int num : arr) totalSum += num;
for (int i = 0; i < arr.length; i++) {
totalSum -= arr[i];
if (leftSum == totalSum) return i;
leftSum += arr[i];
}
return -1;
}
• Time Complexity: O(n) — Single pass through the array.
• Space Complexity: O(1) — Only a few extra variables.

22. Find the circular rotation of an array by K positions


java

public static void circularRotate(int[] arr, int k) {


k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
• Time Complexity: O(n) — Reversing elements.
• Space Complexity: O(1) — In-place reversal.

23. Sort an array according to the order defined by another array


java

public static int[] sortByOrder(int[] arr, int[] order) {


Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
int index = 0;
for (int num : order) {
if (freqMap.containsKey(num)) {
int count = freqMap.get(num);
while (count-- > 0) {
arr[index++] = num;
}
freqMap.remove(num);
}
}
for (int num : freqMap.keySet()) {
int count = freqMap.get(num);
while (count-- > 0) {
arr[index++] = num;
}
}
return arr;
}
• Time Complexity: O(n + m) — n is the size of the array, m is the size of the order
array.
• Space Complexity: O(n) — Extra space for the frequency map.

24. Search for an element in an array


java

public static int searchElement(int[] arr, int key) {


for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) return i;
}
return -1;
}
• Time Complexity: O(n) — Scan through the array.
• Space Complexity: O(1) — No extra space.

25. Check if an array is a subset of another array or not


java

public static boolean isSubset(int[] arr1, int[] arr2) {


Set<Integer> set = new HashSet<>();
for (int num : arr1) {
set.add(num);
}
for (int num : arr2) {
if (!set.contains(num)) {
return false;
}
}
return true;
}
• Time Complexity: O(n + m) — n is the size of the first array, m is the size of the
second array.
• Space Complexity: O(n) — Extra space for the set.

You might also like