Thursday, June 27, 2019

Maximum Loot Problem

Maximum Loot Problem

More details about the problem are mentioned at:


Input:
5 5 10 100 10 5

Output:
110
Input:
1 2 3

Output:
4


Wednesday, May 1, 2019

Maximum sum of hour glass in matrix

Rather than explaining something more in detail, following sample input, output and explanation would help to understand what do we need to do for this problem.




Input:
int[][] input1 = {
    {1, 2, 3, 4, 5},
    {0, 5, 1, 5, 2},
    {0, 0, 0, 1, 3},
    {4, 1, 1, 0, 1},
    {0, 0, 0, 0, 3},
};

Output:
3   4   5
    5
0   1   3

Max Sum for input1 is: 21

Input:
int[][] input2 = { {3, 0, 0, 5}, {2, 5, 5, 5}, {0, 0, 0, 1}, {4, 4, 0, 0}, {0, 0, 3, 0}, };
Output:
2   5   5
    0
4   4   0

Max Sum for input2 is: 20
Input:
int[][] input3 = { {-1, -1, 0, -9, -2, -2}, {-2, -1, -6, -8, -2, -5}, {-1, -1, -1, -2, -3, -4}, {-1, -9, -2, -4, -4, -5}, {-7, -3, -3, -2, -9, -9}, {-1, -3, -1, -2, -4, -5} };

Output:
-1   -1   0
    -1
-1   -1   -1

Max Sum for input3 is: -6

Stack and Queue using Deque



Deque is double ended queue, which like the doubly linked list with having frond/head in the start and last/tail in the end of the list.






Stack using Deque

Input:
push(1)
push(2)
push(3)
push(4)

Output:
4
3->2->1



Queue using Deque

Input:
enqueue(1)
enqueue(2)
enqueue(3)
enqueue(4)

Output:
1
2->3->4



Sunday, April 28, 2019

Implementing Stack Using Queue

Implementing stack using two queues, where we push element to q2 and then move pop and add all the to q1. Swap q2 to q1 to proceed with pop or another push operation.

Input:
push(1)
push(2)
push(3)

pop()
pop()
pop()
pop()

Output:
1 :3
2 :2
3 :1
Queue Empty
4 :null


Queue Using Stack Data Structure

Implementing Queue Using Stack


Two approaches
1) Using Multiple Stacks

Input:
enque(4)
dequeue()
enque(2)
dequeue()
enque(7)
dequeue()

Output:
1: 4
2: 2
3: 7




2) Using Single Stack and Recursive Method

Input:
enque(4)
dequeue()
enque(8)
enque(2)
dequeue()
enque(7)
dequeue()

Output:
1: 4
2: 2
3: 7
4: 8



Sunday, April 21, 2019

Find the first circular tour that visits all gas station or petrol bunks

Find the first circular tour that visits all gas station or petrol bunks where there are N gas stations are there in a circle. 

You are given two sets of data:
1. The amount of gas/petrol that every petrol pump has.
2. Distance from that gas/petrol pump to the next petrol pump.
Node node1 = new Node("1", 6, 4);

"1" is the gas station name.
6 is amount of gas can be filled at gas station "1"
4 is the distance from current gas station to next one.

Calculate the first point from where a truck will be able to complete the circle 

Input 1
Node node1 = new Node("1", 6, 4);
Node node2 = new Node("2", 3, 6);
Node node3 = new Node("3", 7, 3);
Output
Execution 1: 3

Input 2
Node node1 = new Node("1", 4, 6);
Node node2 = new Node("2", 6, 5);
Node node3 = new Node("3", 7, 3);
Node node4 = new Node("4", 4, 5);
Output
Execution 2: 2