Saturday, June 24, 2023

Unmix My Strings

Unmix My Strings


lPaeesh le pemu mnxit ehess rtnisg! Oh, sorry, that was supposed to say: Please help me unmix these strings! Somehow my strings have all become mixed up; every pair of characters has been swapped. Help me undo this so I can understand my strings again. 
 unmix("123456") ➞ "214365" 
 unmix("hTsii s aimex dpus rtni.g") ➞ "This is a mixed up string." 
 unmix("badce") ➞ "abcde" 


Input: hTsii s aimex dpus rtni.g, Output: This is a mixed up string. 
Input: 214365, Output: 123456 
Input: badce, Output: abcde 



Question Reference: https://edabit.com/challenge/XRAGxXj4KtakkvD3F

Largest Gap

Given an array of integers, return the largest gap between the sorted elements of the array.

For example, consider the array:

[9, 4, 26, 26, 0, 0, 5, 20, 6, 25, 5] ... in which, after sorting, the array becomes:

[0, 0, 4, 5, 5, 6, 9, 20, 25, 26, 26] ... so that we now see that the largest gap in the array is between 9 and 20 which is 11.



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