Array based codes
Array based codes
Array based codes
int sum = 0;
//iterating over the array.
for (int i = 0 ; i < n ; i++)
{
//storing prefix sum.
sum += arr[i];
// Storing values of tallest bar from first index till ith index.
left[0] = arr[0];
for (int i = 1;i < n;i++) {
left[i] = max(left[i - 1], arr[i]);
}
// Storing values of tallest bar from last index till ith index.
right[n-1] = arr[n-1];
for (int i = n - 2;i >= 0;i--) {
right[i] = max(right[i + 1], arr[i]);
}
Example 1:
Input:
n=5
A[] = {1, 2, 3, 3, 4}
[a, b] = [1, 2]
Output: 1
Explanation: One possible arrangement is:
{1, 2, 3, 3, 4}. If you return a valid
arrangement, output will be 1.
Example 2:
Input:
n=3
A[] = {1, 2, 3}
[a, b] = [1, 3]
Output: 1
Explanation: One possible arrangement
is: {1, 2, 3}. If you return a valid
arrangement, output will be 1.
Your Task:
You don't need to read input or print anything. The task is to complete the function threeWayPartition()
which takes the array[], a, and b as input parameters and modifies the array in-place according to the
given conditions.
Constraints:
1 <= n <= 106
1 <= A[i] <= 106
Solution
class threeWayPartition{
//Function to partition the array around the range such
//that array is divided into three parts.
public void threeWayPartition(int array[], int a, int b)
{
int n = array.length;
//Using two pointers which help in finding the index with which
//the elements need to be swapped if they are not in correct position.
int start = 0, end = n-1;
Solution
class Solution{
public:
void multiply(int n, vector<int>& number) {
int carry = 0;
for (int i = 0; i < number.size(); i++) {
int num = n * number[i];
number[i] = (char)((num + carry) % 10);
carry = (num + carry) / 10;
}
while (carry) {
number.push_back(carry % 10);
carry /= 10;
}
}
vector<int> factorial(int N){
vector<int> number;
number.push_back(1);
for (int i = 2; i <= N; i++)
multiply(i, number);
reverse(number.begin(), number.end());
return number;
}
};
5. Minimum number of jumps
[Moonfrog Labs, Flipkart,Amazon,Microsoft,Housing.com,Walmart,Adobe,Google ]
Given an array of N integers arr[] where each element represents the maximum length of the jump that
can be made forward from that element. This means if arr[i] = x, then we can jump any distance y such
that y ≤ x.
Find the minimum number of jumps to reach the end of the array (starting from the first element). If an
element is 0, then you cannot move through that element.
Example 1:
Input:
N = 11
arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}
Output: 3
Explanation:
First jump from 1st element to 2nd
element with value 3. Now, from here
we jump to 5th element with value 9,
and from here we will jump to the last.
Example 2:
Input:
N=6
arr = {1, 4, 3, 2, 6, 7}
Output: 2
Explanation:
First we jump from the 1st to 2nd element
and then jump to the last element.
Your task:
You don't need to read input or print anything. Your task is to complete function minJumps() which
takes the array arr and it's size N as input parameters and returns the minimum number of jumps. If not
possible return -1.
Constraints:
1 ≤ N ≤ 107
0 ≤ arri ≤ 107
Solution
class MinJumps {
public:
int minJumps(int arr[], int n)
{
// The number of jumps needed to reach the starting index is 0
if (n <= 1)
return 0;
// initialization
int maxReach = arr[0]; // stores all time the maximal reachable index in the array.
int step = arr[0]; // stores the number of steps we can still take
int jump =1;//stores the number of jumps necessary to reach that maximal reachable position.
// updating maxReach
maxReach = max(maxReach, i+arr[i]);
return -1;
}
};
6. Find the median of an array
Given an array arr[] of N integers, calculate the median
Example 1:
Input: N = 5
arr[] = 90 100 78 89 67
Output: 89
Explanation: After sorting the array
middle element is the median
Example 2:
Input: N = 4
arr[] = 56 67 30 79
Output: 61
Explanation: In case of even number of elements, average of two middle elements
is the median.
Your Task:
You don't need to read or print anything. Your task is to complete the function
find_median() which takes the array as input parameter and returns the floor value of
the median.
Constraints:
1 <= Length of Array <= 100
1 <= Elements of Array <= 100
Solution
class Median
{
public:
public:
int find_median(vector<int> v)
{
sort(v.begin(),v.end());
int ans ;
// if size is odd
if(v.size() & 1)
ans = v[v.size() / 2];
// If size is even
else
ans = (v[v.size() / 2] + v[v.size() / 2 - 1]) / 2;
return ans;
}
};
7. Median of two sorted arrays of same size
There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median
of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The
complexity should be O(log(n))
Input: ar1[]={1,12,15,26,38}
ar2[]={2,13,16,30,45}
Output: 16
Explanation: After merging two arrays we get {1,2,12,13,15,17,26,30,38,45}. Middle
two elements are 15, 17. Average of middle two elements (15+17)/2=16
Note: Since the size of the set for which we are looking for the median is even (2n), we
need to take the average of the middle two numbers and return the floor of the
average
Solution
// A Simple Merge based O(n) solution to find median of two sorted arrays
#include <stdio.h>
/*Below is to handle case where all elements of ar2[] are smaller than
smallest(or first) element of ar1[]*/
else if (j == n)
{
m1 = m2;
m2 = ar1[0];
break;
}
/* equals sign because if two arrays have some common elements */
if (ar1[i] <= ar2[j])
{
m1 = m2; /* Store the prev median */
m2 = ar1[i];
i++;
}
else
{
m1 = m2; /* Store the prev median */
m2 = ar2[j];
j++;
}
}
return (m1 + m2)/2;
}
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};
int n1 = sizeof(ar1)/sizeof(ar1[0]);
int n2 = sizeof(ar2)/sizeof(ar2[0]);
if (n1 == n2)
printf("Median is %d", getMedian(ar1, ar2, n1));
else
printf("Doesn't work for arrays of unequal size");
return 0;
}
Method 2 (By comparing the medians of two arrays)
This method works by first getting medians of the two sorted arrays and then
comparing them.
Let ar1 and ar2 be the input arrays.
Algorithm :
1) Calculate the medians m1 and m2 of the input arrays ar1[] and ar2[] respectively.
2) If m1 and m2 both are equal then we are done, return m1 (or m2)
3) If m1 is greater than m2, then median is present in one of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
5) Repeat the above process until size of both the subarrays becomes 2.
6) If size of the two arrays is 2 then use below formula to get the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
Examples:
ar1[] = {1, 12, 15, 26, 38}
ar2[] = {2, 13, 17, 30, 45}
For above two arrays m1 = 15 and m2 = 17
For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of
the following two subarrays.
[15, 26, 38] and [2, 13, 17]
Let us repeat the process for above two subarrays:
m1 = 26 m2 = 13.
m1 is greater than m2. So the subarrays become
[15, 26] and [13, 17]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
= (max(15, 13) + min(26, 17))/2
= (15 + 17)/2
= 16
Solution
/* A Java program to divide and conquer based efficient solution to find median of two
sorted arrays of same size.*/
import java.util.*;
class medianSameSize {
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays Both have n elements */
static int getMedian(
int[] a, int[] b, int startA,
int startB, int endA, int endB)
{
if (endA - startA == 1) {
return (Math.max(a[startA],b[startB])+ Math.min(a[endA], b[endB]))/ 2;
}
/* get the median of the first array */
int m1 = median(a, startA, endA);
/* get the median of the second array */
int m2 = median(b, startB, endB);
/* If medians are equal then return either m1 or m2 */
if (m1 == m2) {
return m1;
}
/* if m1 < m2 then median must exist in ar1[m1....] and ar2[....m2] */
else if (m1 < m2) {
return getMedian(a, b, (endA + startA + 1) / 2,startB, endA,
(endB + startB + 1) / 2);
}
/* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */
else {
return getMedian(a, b, startA, (endB + startB + 1) / 2,
(endA + startA + 1) / 2, endB);
}
}
/* Function to get median of a sorted array */
static int median(int[] arr, int start, int end)
{
int n = end - start + 1;
if (n % 2 == 0) {
return (arr[start + (n / 2)] + arr[start + (n / 2 - 1)])/ 2;
}
else {
return arr[start + n / 2];
}
}
public static void main(String[] args)
{
int ar1[] = { 1, 2, 3, 6 };
int ar2[] = { 4, 6, 8, 10 };
int n1 = ar1.length;
int n2 = ar2.length;
if (n1 != n2) {
System.out.println("Doesn't work for arrays "+"of unequal size");
}
else if (n1 == 0) {
System.out.println("Arrays are empty.");
}
else if (n1 == 1) {
System.out.println((ar1[0] + ar2[0]) / 2);
}
else {
System.out.println("Median is "+ getMedian(ar1, ar2, 0, 0,
ar1.length - 1, ar2.length - 1));
}
}
}
8. Find minimum number of merge operations to make an array palindrome
Given an array of positive integers. We need to make the given array a ‘Palindrome’.
The only allowed operation is “merging” (of two adjacent elements). Merging two
adjacent elements means replacing them with their sum. The task is to find the
minimum number of merge operations required to make the given array a
‘Palindrome’.
To make any array a palindrome, we can simply apply merge operation n-1 times
where n is the size of the array (because a single-element array is always palindromic,
similar to single-character string). In that case, the size of array will be reduced to 1.
But in this problem, we are asked to do it in the minimum number of operations.
Example:
Input: arr[] = {15, 4, 15}
Output: 0
Array is already a palindrome. So we do not need any merge operation.
Input: arr[] = {1, 4, 5, 1}
Output: 1
We can make given array palindrome with minimum one merging (merging 4 and 5 to
make 9)
Algorithm
Let f(i, j) be minimum merging operations to make subarray arr[i..j] a palindrome. If i
== j answer is 0. We start i from 0 and j from n-1.
class mergePallindrome
{
// Returns minimum number of count operations
// required to make arr[] palindrome
static int findMinOps(int[] arr, int n)
{
int ans = 0; // Initialize result
// Start from two corners
for (int i=0,j=n-1; i<=j;)
{
// If corner elements are same, problem reduces arr[i+1..j-1]
if (arr[i] == arr[j])
{
i++;
j--;
}
// If left element is greater, then we merge right two elements
else if (arr[i] > arr[j])
{
// need to merge from tail.
j--;
arr[j] += arr[j+1] ;
ans++;
}
Examples:
// iterate over each element in the hash table and check their
frequency, // if it is more than n/k, print it.
for (Map.Entry m : y.entrySet()) {
Integer temp = (Integer)m.getValue();
if (temp > x)
System.out.println(m.getKey());
}
}
public static void main(String[] args)
{
int a[] = new int[] { 1, 1, 2, 2, 3, 5, 4, 2, 2, 3, 1, 1, 1 };
int n = 12;
int k = 4;
morethanNdK(a, n, k);
}
}
10. Chocolate Distribution Problem
Given an array A[ ] of positive integers of size N, where each value represents the
number of chocolates in a packet. Each packet can have a variable number of
chocolates. There are M students, the task is to distribute chocolate packets among M
students such that :
1. Each student gets exactly one packet.
2. The difference between maximum number of chocolates given to a student and
minimum number of chocolates given to a student is minimum.
Example 1:
Input:
N = 8, M = 5
A = {3, 4, 1, 9, 56, 7, 9, 12}
Output: 6
Explanation: The minimum difference between maximum chocolates and minimum
chocolates
is 9 - 3 = 6 by choosing following M packets:
{3, 4, 9, 7, 9}.
Example 2:
Input:
N = 7, M = 3
A = {7, 3, 2, 4, 9, 12, 56}
Output: 2
Explanation: The minimum difference between maximum chocolates and minimum
chocolates is 4 - 2 = 2 by choosing following M packets:
{3, 2, 4}.
Your Task:
You don't need to take any input or print anything. Your task is to complete the
function findMinDiff() which takes array A[ ], N and M as input parameters and returns
the minimum possible difference between maximum number of chocolates given to a
student and minimum number of chocolates given to a student.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1≤M≤N
Solution
class MinDiff {
public long findMinDiff (ArrayList<Integer> a, int n, int m)
{
Collections.sort(a);