# Post Only Coding Problems & Solutions Here | All Coding Guru's Dig In

**URL:** <https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680>\
**Category:** Discussion & Solutions\
**Tags:** business\
**Created:** [October 20, 2019, 9:23am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680 "2019-10-20T09:23:31Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![dEnOmInAtOr](https://onehack.st/user_avatar/onehack.st/denominator/32/24588_2.png) [@dEnOmInAtOr](https://onehack.st/u/dEnOmInAtOr)\
**Post date:** [October 21, 2019, 7:01pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/25 "2019-10-21T19:01:24Z")

</div>

We can use the same approach used to solve the above problem(problem 2).  
spoiler: logic ahead!

1. Save all array elements to hash
2. a,b = INT\_MAX
3. For current\_ele in array.  
other\_ele = X - current\_ele  
find if other\_ele is in hash:  
if(other\_ele \< b)  
b = other\_ele  
a = current\_ele
4. print a,b

Will work in O(N) time and space.  
There will surely exists a pair if array size is \>= 2

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 22, 2019, 7:42am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/28 "2019-10-22T07:42:26Z")

</div>

> [@dEnOmInAtOr](#):
>
> We can use the same approach used to solve the above problem(problem 2).  
> spoiler: logic ahead!
> 
> 1. Save all array elements to hash
> 2. a,b = INT\_MAX
> 3. For current\_ele in array.  
> other\_ele = X - current\_ele  
> find if other\_ele is in hash:  
> if(other\_ele \< b)  
> b = other\_ele  
> a = current\_ele
> 4. print a,b
> 
> Will work in O(N) time and space.  
> There will surely exists a pair if array size is \>= 2

**Problem 3**  
_testcase 1_  
if n is 3 , X is 1  
and elements are {5,7,6}  
the output must be {5,6}

can you tell me the correctness of you’re solution?

---

<div class="post-metadata">

**Author:** ![dEnOmInAtOr](https://onehack.st/user_avatar/onehack.st/denominator/32/24588_2.png) [@dEnOmInAtOr](https://onehack.st/u/dEnOmInAtOr)\
**Post date:** [October 22, 2019, 9:41am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/30 "2019-10-22T09:41:43Z")

</div>

Yep, It fails for down sums!

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 22, 2019, 2:21am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/27 "2019-10-22T02:21:18Z")

</div>

Good morning folks, here is our **Problem 3**  
Topics: Arrays/2-d arrays

This is a pretty standard problem in many coding interviews and may be easy for few of you guys but as i am just learning and most of us are on same boat so lets just put this out too.

**Given a 2-D array you will have to print it out in the spiral clockwise order:**  
For example:

 ![image](https://onehack.st/uploads/default/original/3X/1/7/17b4f56008a25c453f8f7e68460612a08fd97c7a.png)

Best of luck 🙂

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 22, 2019, 8:08am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/29 "2019-10-22T08:08:49Z")

</div>

Is there any constraints?

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 22, 2019, 10:54am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/31 "2019-10-22T10:54:40Z")

</div>

No , you may fill out aray values as you like. Here the question is to check implementation techniques.

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 22, 2019, 11:22am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/32 "2019-10-22T11:22:43Z")

</div>

the solution would be O(N) , not able to see any other soln atm.

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 22, 2019, 12:07pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/33 "2019-10-22T12:07:16Z")

</div>

Well it should be , because the question is to print all elements of the array.

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 22, 2019, 12:53pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/34 "2019-10-22T12:53:53Z")

</div>

Problem #4

Space Complexity O(MxN) + overhead  
Time Complexity O(N) , all MxN elements are visited  
Pseudocode  
the pattern of spiral clockwise is formed till M/2 rows  
(initially rowStart and colStart are 0,0)

the pattern as follows increase in currCol , once it reaches ColMax ,  
increase currRow to RowMax (by increment +1)  
now decrease col to ColStart (decrement by 1)  
and now decrement row to RowStart (decrement by 1)

edge case once you reach back currRow and currCol to rowStart and colStart respectively  
don’t print the element value { you can also remove this edge case by varying condition}

btw RowMax and ColMax are obtained from M-rowStart,N-colStart

the starting element is choosen from the position rowStart+1,ColStart +1 , rowStart,ColStart respectively.

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 23, 2019, 6:03am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/35 "2019-10-23T06:03:13Z")

</div>

Hi Guys i will post the my version of a detailed approach to solve problem 3 soon.  
However here is today’s **problem 4**

Given an unsorted array you will have to print out the largest & the smallest number in the array. **Topics: Arrays,Dynamic memory allocation, vectors**  
// You cannot sort the array here.

Inputs:

1. The first line N contain a integer showing number of elements in the array
2. user will enter an unsorted array

Output  
print out max & minimum integer value present in array.

Example:  
**Inputs**  
5  
6 4 10 1 60

**Output**  
Max=60  
Min=1

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 23, 2019, 8:42am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/37 "2019-10-23T08:42:16Z")

</div>

for problem #5  
time complexity O(N)

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 23, 2019, 9:17am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/38 "2019-10-23T09:17:46Z")

</div>

**problem # 6**

Time complexity O(N) preferred  
problem complexity medium

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.

For example, the length of your array of zeros n=10. Your list of queries is as follows:

```
a b k
1 5 3
4 8 7
6 9 1

```

Add the values of k between the indices a and b inclusive:

```auto
index-> 1 2 3 4 5 6 7 8 9 10
         [0,0,0, 0, 0,0,0,0,0, 0]
         [3,3,3, 3, 3,0,0,0,0, 0]
         [3,3,3,10,10,7,7,7,0, 0]
         [3,3,3,10,10,8,8,8,1, 0]

```

The largest value is **10** after all operations are performed.

**Function Description**

create the function _arrayManipulation_ It must return an integer, the maximum value in the resulting array.

arrayManipulation has the following parameters:

- _n_ - the number of elements in your array
- _queries_ - a two dimensional array of queries where each _queries[i]_ contains three integers, _a_ , _b_ , and _k_ .

```auto
long arrayManipulation(int n, int[][] queries){
//code here*
}

```

**Input Format**

The first line contains two space-separated integers _ **n** _ and _ **m** _ , the size of the array and the number of operations.  
Each of the next lines contains three space-separated integers **a** , **b** and **k** , the left index, right index and summed.

**Constraints**

- `3 <= n <= 10^7`
- `1 <= m <= 10^7`
- `1 <= a <= b <= n`
- `0 <= k <= 10^9`

**Output Format**

Return the integer maximum value in the finished array.

**Sample Input**

```auto
5 3
1 2 100
2 5 100
3 4 100

```

**Sample Output**

```auto
200

```

**Explanation**

After the first update list will be `100 100 0 0 0` .  
After the second update list will be `100 200 100 100 100` .  
After the third update list will be `100 200 200 200 100` .  
The required answer will be `200`

hint 1:  
try to solve in O(N^2).

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 23, 2019, 4:59pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/39 "2019-10-23T16:59:49Z")

</div>

Didnt understood the question ☹ 😕

---

<div class="post-metadata">

**Author:** ![goku\_pistol](https://onehack.st/user_avatar/onehack.st/goku_pistol/32/24327_2.png) [@goku\_pistol](https://onehack.st/u/goku_pistol)\
**Post date:** [October 27, 2019, 8:05pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/46 "2019-10-27T20:05:30Z")

</div>

My Solution to Problem 6:

Code in Python3

```
n, m = map(int, input().split())
array = [0] * (n+1)

for i in range(m):
    a, b, k = map(int, input().split())
    
    array[a] += k
    array[b] -= k
# print(array)

array_sum,array_max = 0,0
for i in range(n):
    array_sum += array[i]
    array_max = max(array_max,array_sum)

print()
print(array_max)

```

Increase the array[a] by k and decrease array[b] by k for each input a,b,k =\> time complexity: O(m)  
the maximum Iterative sum of resultant array is the output =\> time complexity: O(n)  
Final time complexity: O(m+n) =\> O(n)

---

<div class="post-metadata">

**Author:** ![Mayank](https://onehack.st/user_avatar/onehack.st/mayank/32/18032_2.png) [@Mayank](https://onehack.st/u/Mayank)\
**Post date:** [November 2, 2019, 5:53pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/63 "2019-11-02T17:53:23Z")

</div>

I’m not familar with the language there is a small bug in you’re code.

> [@goku\_pistol](#):
>
> ```auto
> array[a] += k 
> array[b] -= k // wont this be b+1?
> 
> ```

If this is not a bug , sorry about that ☀

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 24, 2019, 5:10pm UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/40 "2019-10-24T17:10:20Z")

</div>

will give hints PM me with problem number , we can proceed with problem # 7

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 25, 2019, 9:11am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/41 "2019-10-25T09:11:20Z")

</div>

problem # 7 (variation of closest)  
Given an array `nums` of _n_ integers and an integer `target` , find three integers in `nums` such that the sum is closest to `target` . Return the sum of the three integers. You may assume that each input would have exactly one solution.

**Example:**

Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 27, 2019, 6:24am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/42 "2019-10-27T06:24:53Z")

</div>

Can you please share the solution/logic ?

---

<div class="post-metadata">

**Author:** ![Summer\_Jance](https://onehack.st/user_avatar/onehack.st/summer_jance/32/19952_2.png) [@Summer\_Jance](https://onehack.st/u/Summer_Jance)\
**Post date:** [October 27, 2019, 8:37am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/43 "2019-10-27T08:37:37Z")

</div>

A naive approach would be O(n^3) , assuming you couldnt come up with naive approach.

better solution would be O(N^2) even better is O(NlogN).

**Naive Approach**  
Have a map (prefer sorted tree) to contain all the min found with abs(target-3sum) , with 3sum  
for i = 1 to n  
&nbsp;&nbsp;&nbsp;for j = i+1 to n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for k = j+1 to n  
3sum + nums[i]+nums[j]+nums[k]  
find min add to map  
end for k  
end for j  
end for i

return 3sum from tree found in the least value.

( **btw this is one of the approach for naive** )

---

<div class="post-metadata">

**Author:** ![Darklooter](https://onehack.st/user_avatar/onehack.st/darklooter/32/1882_2.png) [@Darklooter](https://onehack.st/u/Darklooter)\
**Post date:** [October 27, 2019, 9:22am UTC](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680/44 "2019-10-27T09:22:40Z")

</div>

**Problem 8** Topics: Arrays/String

Given a string print out the sub-string of the given string.  
Example:  
Input: abcd

output:  
a  
ab  
abc  
abcd  
b  
bc  
bcd  
c  
cd  
d

[Previous page](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680.md?page=1)

[Next page](https://onehack.st/t/post-only-coding-problems-solutions-here-all-coding-gurus-dig-in/60680.md?page=3)
