Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added More Questions for Data Structures,Java,Android and System Design #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
876 changes: 458 additions & 418 deletions README.md

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions src/arrays/FindMaximumSumPathInTwoArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package arrays;


public class FindMaximumSumPathInTwoArrays
{

/*
* Given two sorted arrays, such that the arrays may have some common elements. Find the sum of the maximum sum path to reach from the beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements.
Note: The common elements do not have to be at the same indexes.
* Expected Time Complexity: O(m+n), where m is the number of elements in ar1[] and n is the number of elements in ar2[].
* Input: ar1[] = {2, 3, 7, 10, 12},ar2[] = {1, 5, 7, 8}
* Output: 35
* Step 1) Create some variables, result, sum1, sum2. Initialize result as 0. Also initialize two variables sum1 and sum2 as 0. Here sum1 and sum2 are used to store sum of element in ar1[] and ar2[] respectively.
* These sums are between two common points.
* Step 2) Now run a loop to traverse elements of both arrays. While traversing compare current elements of array 1 and array 2 in the following order: -
a)If current element of array 1 is smaller than current element of array 2, then update sum1, else if current element of array 2 is smaller, then update sum2.
b)If the current element of array 1 and array 2are same, then take the maximum of sum1 and sum2 and add it to the result. Also add the common element to the result.
c)This step can be compared to the merging of two sorted arrays, If the smallest element of the two current array indices is processed then it is guaranteed that
if there is any common element it will be processed together.So the sum of elements between two common elements can be processed.
* */

// Utility function to find maximum of two integers
int max(int x, int y)
{
return (x > y) ? x : y;
}

// This function returns the sum of elements on maximum path
// from beginning to end
int maxPathSum(int ar1[], int ar2[], int m, int n)
{
// initialize indexes for ar1[] and ar2[]
int i = 0, j = 0;

// Initialize result and current sum through ar1[] and ar2[].
int result = 0, sum1 = 0, sum2 = 0;

// Below 3 loops are similar to merge in merge sort
while (i < m && j < n)
{
// Add elements of ar1[] to sum1
if (ar1[i] < ar2[j])
sum1 += ar1[i++];

// Add elements of ar2[] to sum2
else if (ar1[i] > ar2[j])
sum2 += ar2[j++];

// we reached a common point
else
{
// Take the maximum of two sums and add to result
result += max(sum1, sum2);

// Update sum1 and sum2 for elements after this
// intersection point
sum1 = 0;
sum2 = 0;

// Keep updating result while there are more common
// elements
while (i < m && j < n && ar1[i] == ar2[j])
{
result = result + ar1[i++];
j++;
}
}
}

// Add remaining elements of ar1[]
while (i < m)
sum1 += ar1[i++];

// Add remaining elements of ar2[]
while (j < n)
sum2 += ar2[j++];

// Add maximum of two sums of remaining elements
result += max(sum1, sum2);

return result;
}

// Driver program to test above functions
public static void main(String[] args)
{
MaximumSumPath sumpath = new MaximumSumPath();
int ar1[] = {2, 3, 7, 10, 12, 15, 30, 34};
int ar2[] = {1, 5, 7, 8, 10, 15, 16, 19};
int m = ar1.length;
int n = ar2.length;
System.out.println("Maximum sum path is :" +
sumpath.maxPathSum(ar1, ar2, m, n));
}
}
63 changes: 63 additions & 0 deletions src/arrays/MergeTwoSortedArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package arrays;

import java.util.ArrayList;
import java.util.List;

public class MergeTwoSortedArrays
{


/*
* We are given two sorted array. We need to merge these two arrays such that the initial numbers (after complete sorting) are in the first array and the remaining numbers are in the second array. Extra space allowed in O(1).
* Expected Time Complexity: O(m+n), where m is the number of elements in ar1[] and n is the number of elements in ar2[].
* Input: ar1[] = {10},ar2[] = {2, 3}
* Output: ar1[] = {2},ar2[] = {3, 10}
*
* Extra space allowed in O(1)
* 1) Iterate through every element of ar2[] starting from last
element. Do following for every element ar2[i]
a) Store last element of ar1[i]: last = ar1[i]
b) Loop from last element of ar1[] while element ar1[j] is
smaller than ar2[i].
ar1[j+1] = ar1[j] // Move element one position ahead
j--
c) If any element of ar1[] was moved or (j != m-1)
ar1[j+1] = ar2[i]
ar2[i] = last
* */

static int arr1[] = new int[]{1, 5, 9, 10, 15, 20};
static int arr2[] = new int[]{2, 3, 8, 13};

static void merge(int m, int n)
{
// Iterate through all elements of ar2[] starting from
// the last element
for (int i = n - 1; i >= 0; i--)
{
/* Find the smallest element greater than ar2[i]. Move all
elements one position ahead till the smallest greater
element is not found */
int j, last = arr1[m - 1];
for (j = m - 2; j >= 0 && arr1[j] > arr2[i]; j--)
arr1[j + 1] = arr1[j];

// If there was a greater element
if (j != m - 2 || last > arr2[i])
{
arr1[j + 1] = arr2[i];
arr2[i] = last;
}
}
}

// Driver method to test the above function
public static void main(String[] args)
{
merge(arr1.length, arr2.length);
System.out.print("After Merging nFirst Array: ");
System.out.println(Arrays.toString(arr1));
System.out.print("Second Array: ");
System.out.println(Arrays.toString(arr2));
}
}
57 changes: 57 additions & 0 deletions src/arrays/Segregate0And1s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package arrays;

import java.util.ArrayList;
import java.util.List;

public class Segregate0And1s
{


/*
* You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.
* Input: ar[] = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
* Output: ar[] = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
* Steps-
* 1) Count the number of 0s. Let count be C.
2) Once we have count, we can put C 0s at the beginning and 1s at the remaining n – C positions in array.
* */
// function to segregate 0s and 1s

static void segregate0and1(int arr[], int n)
{
int count = 0; // counts the no of zeros in arr

for (int i = 0; i < n; i++)
{
if (arr[i] == 0)
count++;
}

// loop fills the arr with 0 until count
for (int i = 0; i < count; i++)
arr[i] = 0;

// loop fills remaining arr space with 1
for (int i = count; i < n; i++)
arr[i] = 1;
}

// function to print segregated array
static void print(int arr[], int n)
{
System.out.print("Array after segregation is ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}

// driver function
public static void main(String[] args)
{
int arr[] = new int[]{0, 1, 0, 1, 1, 1};
int n = arr.length;

segregate0and1(arr, n);
print(arr, n);

}
}
107 changes: 107 additions & 0 deletions src/linkedlist/ReverseDoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package linkedlist;


public class ReverseDoublyLinkedList
{

/*
* Runtime Complexity:
* Linear, O(n).
* As we can reverse the doubly linked list
* */


static Node head;

static class Node
{

int data;
Node next, prev;

Node(int d)
{
data = d;
next = prev = null;
}
}

/* Function to reverse a Doubly Linked List */
void reverse()
{
Node temp = null;
Node current = head;

/* swap next and prev for all nodes of
doubly linked list */
while (current != null)
{
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}

/* Before changing head, check for the cases like empty
list and list with only one node */
if (temp != null)
{
head = temp.prev;
}
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(int new_data)
{
/* allocate node */
Node new_node = new Node(new_data);

/* since we are adding at the beginning,
prev is always NULL */
new_node.prev = null;

/* link the old list off the new node */
new_node.next = head;

/* change prev of head node to new node */
if (head != null)
{
head.prev = new_node;
}

/* move the head to point to the new node */
head = new_node;
}

/* Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked list */
void printList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
}

public static void main(String[] args)
{
LinkedList list = new LinkedList();

/* Let us create a sorted linked list to test the functions
Created linked list will be 10->8->4->2 */
list.push(2);
list.push(4);
list.push(8);
list.push(10);

System.out.println("Original linked list ");
list.printList(head);

list.reverse();
System.out.println("");
System.out.println("The reversed Linked List is ");
list.printList(head);
}
}
Loading