Skip to content
Merged
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
42 changes: 42 additions & 0 deletions Array/findLongestSubArrayBySum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Find longest sub array by sum in linear time
let sum = 10
array = [1,2,4,3,0,0,0,0,10] longest subarray = 12340000

algo used
1. take two pointers
2. left = start of the array
3. right = start of the array
4. While right < End.
5. calculate sum += array[right]
6. check if {sum > sum_required} => True => subtract sum - array[left] and increment left++
7. if {sum == sum_required} than check {right-left > stored right - stored left}
8. store the value to return
9. increment right and repeat steps 5 to 8
*/

func findLongestSubArray(array:[Int],s:Int)->[Int]{
var left = 0
var right = 0
var subArrayStart = -1
var subArrayEnd = -1
var sum = 0

while(right <= (array.count - 1)){

sum += array[right]
if(sum > s){
sum = sum - array[left]
left += 1
}else
if (sum == s) && ((right - left) > ((subArrayEnd) - (subArrayStart))){
subArrayEnd = right
subArrayStart = left
}

right += 1
}

return [subArrayStart,subArrayEnd]
}

print(findLongestSubArray(array: [1,2,4,3,0,0,0,0,10], s: 11))
73 changes: 73 additions & 0 deletions Array/floyedTortoieAndHareDuplicateDetection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Floyed tortoie and Hare Cycle Detection

Contraint:
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one

Example 1:

Input: [1,3,4,2,2]
Output: 2


Example 2:
Input: [3,1,3,4,2]
Output: 3


Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.


Algo
1. Create two pointers slow pointer and fast pointer
2. slow pointer will take one step i.e num[slowPointer]
3. fast pointer will take two steps i.e num[num[fast pointer]]
4. check for the interceptor point where slowPointer == fastPointer

5. we will try to find the entry point
6. we will take pointer1 at index 0
7. we will take pointer2 at slowpointer
6. now we will move pointer1 at num[pointer1]
7. pointer2 at num[pointer2]
8. check if pointer1 == pointer2
9. if both values are equal that becomes the entry point and is the duplicate value

complexity
1. it takes only constant O(1) extra space
2. time complexity = O(n)
*/

func findDuplicate(num:[Int])->Int{
// slow pointer moves 1 step i.e num[slowPointer]
var slowPointer = num[0]
// fast pointer moves 2 steps i.e num[num[fastPointer]]
var fastPointer = num[0]


while (true){
slowPointer = num[slowPointer]
fastPointer = num[num[fastPointer]]

// Check for condition when both have equal values and breaks the loop
if (slowPointer==fastPointer){break}
}

// find entry Point
var pointer1 = num[0]
// sets to slow pointer
var pointer2 = slowPointer

// both moves one step until they both have equal values
while(pointer1 != pointer2){
pointer1 = num[pointer1]
pointer2 = num[pointer2]
}

// returns the duplicate value
return pointer1
}

findDuplicate(num: [1,3,4,2,2])
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Writing this project for reference, fun and learning purposes. Have started with
- [Reverse Digits](Array/ReverseDigits.swift)
- [Sum of Integer digits](Array/SumOfDigits.swift)
- [Shuffle Array](Array/ShuffleArray.swift)
- [Find Duplicate - Floyed - Tortoie and Hare algo ](Array/floyedTortoieAndHareDuplicateDetection.swift)
- [Longest SubArray by Sum](Array/findLongestSubArrayBySum.swift)

### Strings

Expand Down