Skip to content

Commit 2761148

Browse files
committed
add can make arithmetic progression
1 parent f5edc00 commit 2761148

File tree

5 files changed

+247
-235
lines changed

5 files changed

+247
-235
lines changed

Algorithm Solutions In Swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1F21EEE72F54613100E0274E /* MakeArithmeticProgression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */; };
1011
1F7037522F3E5618002CFFE1 /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037512F3E5618002CFFE1 /* Algorithms */; };
1112
1F7037552F3E5642002CFFE1 /* BasicContainers in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037542F3E5642002CFFE1 /* BasicContainers */; };
1213
1F7037572F3E5642002CFFE1 /* BitCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037562F3E5642002CFFE1 /* BitCollections */; };
@@ -332,6 +333,7 @@
332333
/* End PBXCopyFilesBuildPhase section */
333334

334335
/* Begin PBXFileReference section */
336+
1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeArithmeticProgression.swift; sourceTree = "<group>"; };
335337
1F85E42D2F500D060097933E /* RecursiveBubbleSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecursiveBubbleSort.swift; sourceTree = "<group>"; };
336338
1F85E4302F500D210097933E /* RecusriveInsertionSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecusriveInsertionSort.swift; sourceTree = "<group>"; };
337339
1F85E4352F5062420097933E /* SortedRotated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortedRotated.swift; sourceTree = "<group>"; };
@@ -598,6 +600,14 @@
598600
/* End PBXFrameworksBuildPhase section */
599601

600602
/* Begin PBXGroup section */
603+
1F21EEE52F54611E00E0274E /* Can Make Arithmetic Progression */ = {
604+
isa = PBXGroup;
605+
children = (
606+
1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */,
607+
);
608+
path = "Can Make Arithmetic Progression";
609+
sourceTree = "<group>";
610+
};
601611
1F85E42C2F500D010097933E /* Recursive Bubble Sort */ = {
602612
isa = PBXGroup;
603613
children = (
@@ -1350,6 +1360,7 @@
13501360
6A88CEEA26C3132700A3746B /* Merge Intervals */,
13511361
6A88CEE626C056BD00A3746B /* 3 Sum Closest */,
13521362
1F85E42F2F500D1D0097933E /* Recursive Insertion Sort */,
1363+
1F21EEE52F54611E00E0274E /* Can Make Arithmetic Progression */,
13531364
);
13541365
path = LeetCode;
13551366
sourceTree = "<group>";
@@ -2993,6 +3004,7 @@
29933004
6A651A4C2907866D000731BB /* FirstUniqueCharacter.swift in Sources */,
29943005
6AF5788626A2E67F007B37CA /* TwoNumberSum.swift in Sources */,
29953006
6AF5790926A6E0FC007B37CA /* BinarySearchTrees.swift in Sources */,
3007+
1F21EEE72F54613100E0274E /* MakeArithmeticProgression.swift in Sources */,
29963008
6AF578BC26A34C97007B37CA /* RemoveDuplicatesFromLinkedList.swift in Sources */,
29973009
6AF5795A26AC287A007B37CA /* ThreeNumberSort.swift in Sources */,
29983010
6A502306285C8C4600688107 /* ReconstructBST.swift in Sources */,

Algorithm Solutions In Swift/main.swift

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,5 @@ import Foundation
22

33

44

5-
func spiralOrder1(_ matrix: [[Int]]) -> [Int] {
6-
var startRow = 0
7-
var endRow = matrix.count - 1
8-
9-
var startCol = 0
10-
var endCol = matrix[0].count - 1
11-
12-
var traversed = [Int]()
13-
14-
while startRow <= endRow, startCol <= endCol {
15-
for col in stride(from: startCol, through: endCol, by: 1) {
16-
traversed.append(matrix[startRow][col])
17-
}
18-
19-
if startRow == endRow { break }
20-
21-
for row in stride(from: startRow + 1, through: endRow, by: 1) {
22-
traversed.append(matrix[row][endCol])
23-
}
24-
25-
if startCol == endCol { break }
26-
for col in stride(from: endCol - 1, through: startCol, by: -1) {
27-
traversed.append(matrix[endRow][col])
28-
}
29-
30-
for row in stride(from: endRow - 1, through: startRow + 1, by: -1) {
31-
traversed.append(matrix[row][startCol])
32-
}
33-
34-
startRow += 1
35-
endRow -= 1
36-
37-
startCol += 1
38-
endCol -= 1
39-
}
40-
return traversed
41-
}
42-
var array = [[1, 2, 3]]/*[[1,2,3,4],[5,6,7,8],[9,10,11,12]]*/
43-
print(spiralOrder1(array))
5+
var array = [3, 5, 7]
6+
print(canMakeArithmeticProgression(array))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// MakeArithmeticProgression.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 01/03/26.
6+
//
7+
8+
func canMakeArithmeticProgression(_ arr: [Int]) -> Bool {
9+
// In a valid AP, d = (max - min) / (n - 1) where n is the total number of elements in AP
10+
let min = arr.min()!, max = arr.max()!
11+
// if max - min is zero, then all the elements are same, so return true
12+
if max - min == 0 { return true }
13+
// if there is a remainder in (max - min) % (n - 1), means AP can't be created as diff will be in fraction
14+
if (max - min)%(arr.count - 1) != 0 { return false }
15+
let diff = (max - min) / (arr.count - 1)
16+
// create set and using diff check if all the elements exist
17+
let hashSet = Set(arr)
18+
for i in 0..<arr.count {
19+
if !hashSet.contains(min + i * diff) {
20+
return false
21+
}
22+
}
23+
return true
24+
}

0 commit comments

Comments
 (0)