Divide & Conquer Solutions
Divide & Conquer Solutions
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 n = arr2.length;
String[] arr3 = new String[m + n];
int idx = 0;
int i = 0;
int j = 0;
i++;
idx++;
}
else {
arr3[idx] = arr2[j];
j++;
idx++;
}
}
while (i < m) {
arr3[idx] = arr1[i];
i++;
idx++;
}
while (j < n) {
arr3[idx] = arr2[j];
j++;
idx++;
}
return arr3;
}
}
return false;
}
public static void main(String[] args) {
String[] arr = { "sun", "earth", "mars", "mercury" };
String[] a = mergeSort(arr, 0, arr.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
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++) {
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++;
}
}
return count;
}
return left;
}
System.out.println(majorityElement(nums));
}
}
(You can also find this problem at - https://leetcode.com/problems/majority-element/)
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));
}
}
int invCount = 0;
int temp[] = new int[(right - left + 1)];
i++;
}
else {
temp[k] = arr[j];
invCount += (mid - i);
k++;
j++;
}
}
j++;
}
https://telegram.me/+QGUyTripaP4zNTcx
https://telegram.me/+nEKeBr_yhXtmY2Yx
#TGSFamily