_20.10_Divide _ Conquer Solutions
_20.10_Divide _ Conquer Solutions
_20.10_Divide _ Conquer Solutions
abhaykumar.gv@gmail.com
Solution 1:
class Solution {
//function to mergeSort 2 arrays
public static String[] mergeSort(String[] arr, int lo, int hi) {
if (lo == hi) {
String[] A = { arr[lo] };
return A;
}
int mid = lo + (hi - lo) / 2;
String[] arr1 = mergeSort(arr, lo, mid);
String[] arr2 = mergeSort(arr, mid + 1, hi);
int idx = 0;
int i = 0;
int j = 0;
abhaykumar.gv@gmail.com
i++;
idx++;
}
while (j < n) {
arr3[idx] = arr2[j];
j++;
idx++;
}
return arr3;
}
Solution 2:
Approach 1 - Brute Force Approach
Idea : Count the number of times each number occurs in the array & find the largest count.
Time complexity - O(n^2)
class Solution {
public static int majorityElement(int[] nums) {
int majorityCount = nums.length/2;
for (int i=0; i<nums.length; i++) {
int count = 0;
for (int j=0; j<nums.length; j++) {
abhaykumar.gv@gmail.com
if (nums[j] == nums[i]) {
count += 1;
}
}
if (count > majorityCount) {
return nums[i];
}
}
return -1;
}
public static void main(String args[]) {
int nums[] = {2,2,1,1,1,2,2};
System.out.println(majorityElement(nums));
}
}
Here, we apply a classical divide & conquer approach that recurses on the left and right
halves of an array until an answer can be trivially achieved for a length-1 array. Note that
because actually passing copies of subarrays costs time and space, we instead pass lo and
hi indices that describe the relevant slice of the overall array. In this case, the majority
element for a length-1 slice is trivially its only element, so the recursion stops there. If the
current slice is longer than length-1, we must combine the answers for the slice's left and
right halves. If they agree on the majority element, then the majority element for the overall
slice is obviously the same[1]. If they disagree, only one of them can be "right", so we need to
count the occurrences of the left and right majority elements to determine which subslice's
answer is globally correct. The overall answer for the array is thus the majority element
between indices 0 and n.
class Solution {
private static int countInRange(int[] nums, int num, int lo, int hi) {
int count = 0;
for (int i = lo; i <= hi; i++) {
if (nums[i] == num) {
count++;
}
}
abhaykumar.gv@gmail.com
return count;
}
abhaykumar.gv@gmail.com
Solution 3 :
Approach 1 - Brute Force Approach
Idea : Traverse through the array, and for every index, find the number of smaller elements on
its right side of the array. This can be done using a nested loop. Sum up the counts for all
indexes in the array and print the sum.
● Traverse through the array from start to end
● For every element, find the count of elements smaller than the current number up to
that index using another loop.
● Sum up the count of inversion for every index.
● Print the count of inversions.
Time complexity - O(n^2)
class Solution {
public static int getInvCount(int arr[]) {
int n = arr.length;
int invCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
invCount++;
}
}
}
return invCount;
}
public static void main(String[] args) {
int arr[] = {1, 20, 6, 4, 5};
System.out.println("Inversion Count = "+ getInvCount(arr));
}
}
abhaykumar.gv@gmail.com
Let’s consider two subarrays involved in the merge process:
abhaykumar.gv@gmail.com
Algorithm:
● Split the given input array into two halves, left and right similar to merge sort
recursively.
● Count the number of inversions in the left half and right half along with the inversions
found during the merging of the two halves.
● Stop the recursion, only when 1 element is left in both halves.
● To count the number of inversions, we will use a two pointers approach. Let us
consider two pointers i and j, one pointing to the left half and the other pointing
towards the right half.
● While iterating through both the halves, if the current element A[i] is less than A[j], add
it to the sorted list, else increment the count by mid – i.
abhaykumar.gv@gmail.com
Time complexity - O(n* logn)
abhaykumar.gv@gmail.com
private static int mergeSort(int arr[], int left, int right) {
int invCount = 0;