How to get the sum of values in array in swift - arrays

I have an array with elements like a = [5,6,8,9,2,6,8]. So when I do the first iteration I skip the first index and then add the remaining elements in the array and if I do the second iteration I skip the second element and then add the remaining elements. ex: for the second iteration I skip the second element and then add the remaining elements as 5 + 8 + 9 + 2 + 6 + 8 = 38 like this I want to add the elements and find the highest sum of the elements and minimum sum of the elements. How to do that in swift?

Try this:
let array: [Int] = [1, 2, 3, 4, 5]
var min: Int!
var max: Int!
for (index, _) in array.enumerated() {
var auxArray = array
auxArray.remove(at: index)
let sum = auxArray.reduce(0, +)
if min == nil || max == nil {
min = sum
max = sum
} else if sum < min {
min = sum
} else if sum > max {
max = sum
}
}
print("MIN: \(min!), MAX: \(max!)")

You can do it with one iteration
let a = [5,6,8,9,2,6,8]
var sum = 0
var smallestNumber = a[0]
var largestNumber = a[0]
for number in a {
sum += number
if number < smallestNumber {
smallestNumber = number
}
if number > largestNumber {
largestNumber = number
}
}
let smallestSum = sum - largestNumber
let largestSum = sum - smallestNumber

Related

Smallest subarray with sum equal to k

I want to find the length smallest subarray whose sum is equal to k.
Input: arr[] = {2, 4, 6, 10, 2, 1}, K = 12
Output: 2
Explanation:
All possible subarrays with sum 12 are {2, 4, 6} and {10, 2}.
Input: arr[] = { 1, 2, 4, 3, 2, 4, 1 }, K = 7
Output: 2
Here's a solution using JavaScript.
It could be made more efficient, for sure, but I've coded it to work.
function lengthOfShortestSubArrayOfSumK(array, k) {
var combos=[];
for(var i=0; i<Math.pow(2, array.length); i++) {
var bin=("0".repeat(array.length)+i.toString(2)).slice(-array.length).split("");
var ones=bin.reduce((count, digit)=>{count+=digit=="1";return count;},0);
var sum=bin.reduce((sum, digit, index)=>{sum+=digit=="1"?array[index]:0;return sum;},0);
combos.push([bin, ones, sum]);
};
return combos.filter(combo=>combo[2]==k).sort((a, b)=>a[1]-b[1])[0][1];
}
var arraysAndKs=[
{array:[2, 4, 6, 10, 2, 1], k:12},
{array:[1, 2, 4, 3, 2, 4, 1], k:7}
];
for(arrayAndK of arraysAndKs)
console.log("Length of shortest sub array of ["+arrayAndK.array.join(", ")+"] with sum "+arrayAndK.k+" is : "+lengthOfShortestSubArrayOfSumK(arrayAndK.array, arrayAndK.k));
The Binary number between 0 and array.length squared will give us a representation of included array items in the sum.
We count how many "ones" are in that Binary number.
We sum array items masked by those "one"s.
We save into combos array an array of the Binary number, "one"s count, and sum.
We filter combos for sum k, sort by count of "one"s, and retrun the first's "one"s count.
I'm sure this can be translated to any programming language.
You can use an algorithm that finds a subset in size K, and save another variable that stores the number of members that make up such a subarray.
The algorithm for finding a K subarray is:
initialize an array of size K, Each place (idx) indicates whether there is a subarray that amounts to idx (I used a dictionary)
Go over any number (i) in the array, and any sum (j) we can reach in the previous iteration now we can reach j + i.
If in the K place it is marked TRUE, then there is a subarray that amounts to K.
Here's the solution in Python
def foo(arr,k):
dynamic = {0:0}
for i in arr:
temp = {}
for j, l in dynamic.items():
if i + j <= k: # if not it's not interesting us
# choose the smallest subarray
temp[i+j] = min(l+1,dynamic.get(i+j,len(arr)))
dynamic.update(temp)
return dynamic.get(k,-1)
the complexity is O(N*K).
I assumed that the subarray refers to any possible combinations of original array.
Here is a Python code that solves the problem under the condition that the subset must be contiguous:
in O(N) complexity
def shortest_contiguous_subarray(arr,k):
if k in arr:
return 1
n = len(arr)
sub_length = float('inf')
sub = arr[(i:=0)]
j = 1
while j < n:
while sub < k and j < n:
sub += arr[j]
j += 1
while sub > k:
sub -= arr[i]
i += 1
if sub == k:
# print(arr[i:j],j-i)
sub_length = min(sub_length,j-i)
sub -= arr[i]
i += 1
return sub_length if sub_length <= n else -1
This answer works for any array of positive numbers, and can be modified to work with arrays that have zero or negative elements if an O(n) pre-processing pass is performed (1. find the minimum element m, m <= 0, 2. make the whole array positive by adding -m+1 to all elements, 3. solve for sum + n*(1-m))
function search(input, goal) {
let queue = [ { avail: input.slice(), used: [], sum: 0 } ]; // initial state
for (let qi = 0; qi < queue.length; qi ++) {
let s = queue[qi]; // like a pop, but without using O(n) shift
for (let i = 0; i < s.avail.length; i++) {
let e = s.avail[i];
if (s.sum + e > goal) continue; // dead end
if (s.sum + e == goal) return [...s.used, e]; // eureka!!
queue.push({ // keep digging
avail: [...s.avail.slice(0, i), ...s.avail.slice(i+1)],
used: [...s.used, e],
sum: s.sum + e
});
}
}
return undefined; // no subset of input adds up to goal
}
console.log(search([2, 4, 6, 10, 2, 1], 12))
This is a classic breadth-first-search that does a little bit of pruning when it detects that we are already over the target sum. It can be further optimized to avoid exploring the same branch several times (for example, [4,2] is equivalent to [2,4]) - but this would require extra memory to keep a set of "visited" states. Additionally, you could add heuristics to explore more promising branches first.
I have done this by using unordered_map in c++. Hope this helps .
`
/* smallest subarray of sum k*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> v = {2,4,6,10,2,12};
int k=12;
unordered_map<int,int>m;
int start=0,end=-1;
int len=0,mini=INT_MAX;
int currsum=0;
for(int i=0;i<v.size();i++){
currsum+=v[i];
if(currsum==k){
start=0,end=i;
len=end-start+1;
mini=min(mini,len);
}
if(v[i]==k){
mini=min(mini,1);
}
if(m.find(currsum-k)!=m.end()){
end=i;
start=m[(currsum-k)]+1;
len=end-start+1;
mini=min(mini,len);
}
m[currsum]=i;
}
cout<<mini;
return 0;
}`
class Solution
{
static int findSubArraySum(int arr[], int N, int k)
{
// code here
// i use prefix sum and hashmap approach
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0,1);
// this is bcoz when 1st element is valid one
int count=0;
int sum=0;
for(int i=0;i<N;i++){
sum += arr[i];
// prefix sum
if(map.containsKey(sum-k)){
count += map.get(sum-k);
}
map.put(sum, map.getOrDefault(sum,0)+1);
}
return count;
}
}
// this approach even for -ve numbers
// i came to dis solution by prefix sum approach
This version finds the entire optimal sub-array, not only its length. It's based on a recursion. It will test each number of the array against the optimal sub-array of the rest.
const bestSum = (targetSum, numbers) => {
var shortestCombination = null
for (var i = 0; i < numbers.length; i++) {
var current = numbers[i];
if (current == 0) {
continue
}
if (current == targetSum) {
return [current]
}
if (current > targetSum) {
continue;
}
// "remove" current from array
numbers[i] = 0;
// now the recursion:
var rest = bestSum(targetSum - current, numbers)
if (rest && (!shortestCombination || rest.length + 1 < shortestCombination.length)) {
shortestCombination = [current].concat(rest);
}
// restore current to array
numbers[i] = current
}
return shortestCombination
}
console.log(bestSum(7, [5, 3, 4, 7])) // Should be 7, not [3, 4]
This is my code in Python 3. I used the same idea of find the longest subarray with a sum equal to K. But in the below code for every prefix sum I am storing the recent index.
def smallestSubArraySumLength(a, n, k):
d=defaultdict(lambda:-1)
d[0]=-1
psum=0
maxl=float('inf')
for i in range(n):
psum+=a[I]
if psum-k in d:
maxl=min(maxl, i-d[psum-k])
d[psum]=i
return maxl

swift: average consecutive positive number

We have an array, for example:
let numbers = [-1.0,1.0,3.0,4.0,-1.0,-2.0,2.0]
We know how to get maximum consecutive positive number:
let pos = numbers.map({ () -> (Double) -> Int in var c = 0; return { c = $0 > 0 ? c + 1 : 0; return c } }())
// [0, 1, 2, 3, 0, 0, 1]
let maxConsecutivePos = pos.max()!
//3
How can we find average consecutive positive number the same way, using closures and pos array in our case? For this example, we divide sum (3 + 1) by 2 -> 2 is expected output.
A possible solution: Split the array into slices of consecutive
positive numbers, then compute the average slice length:
let numbers = [-1.0, 1.0, 3.0, 4.0, -1.0, -2.0, 2.0]
let slices = numbers.split(whereSeparator: { $0 <= 0 })
// --> [ArraySlice([1.0, 3.0, 4.0]), ArraySlice([2.0])]
let avg = Double(slices.reduce(0, { $0 + $1.count })) / Double(slices.count)
print(avg) // 2.0
You basically compare each element with the next one and check, if it the next one is less. If it is less, then it's a max and you use it to calculate the average.
let array = [0, 1, 2, 3, 0, 0, 1]
func maxConsAvg(array: [Int]) -> Double? {
guard array.count > 1 else {
return array.first.flatMap({ Double($0) })
}
let array = array + [0]
let maxs = zip(array.dropLast(), array.dropFirst())
.flatMap { (first, second) -> Int? in
return first > second ? first : nil
} //[3, 1]
return Double(maxs.reduce(0, +)) / Double(maxs.count)
}
maxConsAvg(array: array) //2

[Swift 2.2]: analog arc4random for range with negative numbers

I wrote a function that finds the greatest difference among all elements of the array. But I need the restriction on the input elements array [-5..20]. Unfortunately it does not support UInt32. What are similar to a solution to fill the array randomly from the range [-5..20]?Thank you!
func highDifferenceV ( n: Int) ->String{
var a = [Int]() //array
var dif = 0 // max difference
var k = 0
for _ in 0..<n {
a.append(Int(arc4random_uniform(UInt32(20)))) // fill array
}
while k < a.count { //search the greatest difference
for i in 0..<n {
if a[k] - a[i] > dif {
dif = a[k] - a[i]
}
}
k++
}
print(a)
return "Maximum difference =\(dif)"
}
highDifferenceV(75)
To fill an array with values from -5...20, generate an number in the range 0...25 and then subtract 5:
for _ in 0..<n {
a.append(Int(arc4random_uniform(UInt32(26)))-5) // fill array
}
In general, to generate a value in the range min...max, call arc4random_uniform with max - min + 1 and then add min.

Each array to come up with a number, calculate out the location of the array

var a = [1,2,3]
var b = [1,2,3]
var c = [1,2,3,4]
Each array inside take out a number of a three digits.
A total of 36 kinds of combination of the above.
I want to compute the position of the each combination.
[1,1,1] position is 1
[1,1,2] position is 2
[2,2,4] position is?
I want to ask how much the position of a combination of them, is there a specific formula?
Write three nested for loops. First one is for array a,second for b and third for c. That way you will first change the c for a new permutation than b, than a. Before going to for loop declare a variable called count which is 1. At the third loop increase that variable by one. Example:
int first,second,third,count=1;
for(int i=0;i<a.size();i++)
{
first=a[i];
for(int k=0;k<b.size();k++)
{
second=b[k];
for(int g=0;g<c.size();g++)
{
third=c[g];
count=count+1; //count++
}
}
}
This is written in C++. But you get the idea.You can put if statements inbetween to find the number of the permutation that you are looking for.
var a = [1,2,3];
var b = [1,2,3];
var c = [1,2,3,4];
var all = [a,b,c];
var allCount = [b.length * c.length, c.length, 0];
function getIndex(combo) {
var count = 0;
for (var i = 0; i < combo.length; i++) {
var pos = all[i].indexOf(combo[i]);
if (pos < 0) {
throw new Error(combo[i] + "Not effective elements");
}
count += allCount[i] * pos;
}
count += combo[combo.length - 1];
return count;
}
console.log("index of [2,2,4] is " + getIndex([2,2,4]));
console.log("index of [1,1,1] is " + getIndex([1,1,1]));
console.log("index of [3,3,4] is " + getIndex([3,3,4]));
console.log("index of [1,2,3] is " + getIndex([1,2,3]));
console.log("index of [3,2,1] is " + getIndex([3,2,1]));
The output:
index of [2,2,4] is 20
index of [1,1,1] is 1
index of [3,3,4] is 36
index of [1,2,3] is 7
index of [3,2,1] is 29

Retrieve each value from array and add them together, code cannot iterate over whole array?

For a code question:
What it wants is for you to make a for loop that will run down the length of the array it provides. We're going to keep track of where we are in the array with our counter variable. At the end, we should have the sum of all the numbers in that array.
I don't understand why my 2nd loop cannot iterate over the whole array, also how do I check the sum, would that just be printing the sum?
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
while sum < numbers.count {
print(numbers[sum])
sum += 1
}
while counter < numbers.count {
sum = sum + numbers[counter]
print(numbers[counter])
counter += 1
}
If all you want is the sum of all the numbers in the array then you can do this...
let numbers = [2,8,1,16,4,3,9]
var sum = 0
for number in numbers {
sum += number
}
print (sum)
or even easier...
sum = numbers.reduce(0, combine: +)
Strictly following the question statement, I would say the following code will be better option
let numbers = [2, 8, 1, 16, 4, 3, 9]
var sum = 0
for counter in 0 ..< numbers.count {
sum += numbers[counter]
print("Counter: \(counter) Sum: \(sum)")
}

Resources