A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3We can split this tape in four places:
- P = 1, difference = |3 − 10| = 7
- P = 2, difference = |4 − 9| = 5
- P = 3, difference = |6 − 7| = 1
- P = 4, difference = |10 − 3| = 7
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [−1,000..1,000].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
var p = -1;
for (var i = 0; i < length - 1; i++)
{
var difference = Math.Abs(2 * A[i] - A[length - 1);
if (difference == 0)
{
return i + 1;
}
if (difference < minAbsoluteDifference)
{
minAbsoluteDifference = difference;
p = i + 1;
}
return p;
}
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(29,61): error CS1525: Unexpected symbol `)', expecting `,' or `]'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
var p = -1;
for (var i = 0; i < length - 1; i++)
{
var difference = Math.Abs(2 * A[i] - A[length - 1]);
if (difference == 0)
{
return i + 1;
}
if (difference < minAbsoluteDifference)
{
minAbsoluteDifference = difference;
p = i + 1;
}
return p;
}
}
}
Compilation failed: 1 error(s), 1 warnings Solution.cs(10,16): error CS0161: `Solution.solution(int[])': not all code paths return a value Solution.cs(27,41): warning CS0162: Unreachable code detected
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
var p = -1;
for (var i = 0; i < length - 1; i++)
{
var difference = Math.Abs(2 * A[i] - A[length - 1]);
if (difference == 0)
{
return i + 1;
}
if (difference < minAbsoluteDifference)
{
minAbsoluteDifference = difference;
p = i + 1;
}
}
return p;
}
}
[3, 1, 2, 4, 3]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
var p = -1;
for (var i = 0; i < length - 1; i++)
{
var difference = Math.Abs(2 * A[i] - A[length - 1]);
if (difference == 0)
{
return 0;
}
if (difference < minAbsoluteDifference)
{
minAbsoluteDifference = difference;
}
}
return difference;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(43,16): error CS0103: The name `difference' does not exist in the current context
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
[3, 1, 2, 4, 3]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
[3, 1, 2, 4, 3]
[2, -3, 0, -5, 1]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
[3, 1, 2, 4, 3]
[2, -3, 0, -5, 1]
[0, -1, -7, -9, 7, 65, 21, 4, -5, -13, 6, 2, 33, 98, 2, -7]
function result: 1
function result: 3
function result: 10
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
[3, 1, 2, 4, 3]
[2, -3, 0, -5, 1]
[0, -1, -7, -9, 7, -9, 4, 4, -5, -3, 6, 2, 6, 4, 2, -7]
function result: 1
function result: 3
function result: 4
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
[3, 1, 2, 4, 3]
[2, -3, 0, -5, 1]
[0, -1, -7, -9, 7, -9, 4, 4, -5, -3, 6, 2, 6, 4, 2, -7]
function result: 1
function result: 3
function result: 4
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
var length = A.Length;
if (length < 3)
{
throw new Exception("The array should have at least three elements according to the requirements.");
}
for (var i = 1; i < length; i++)
{
A[i] = A[i - 1] + A[i];
}
var minAbsoluteDifference = int.MaxValue;
for (var i = 0; i < length - 1; i++)
{
minAbsoluteDifference = Math.Min(minAbsoluteDifference, Math.Abs(2 * A[i] - A[length - 1]));
if (minAbsoluteDifference == 0)
{
return 0;
}
}
return minAbsoluteDifference;
}
}
The following issues have been detected: runtime errors.
Unhandled Exception: System.Exception: The array should have at least three elements according to the requirements. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: The array should have at least three elements according to the requirements. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.Exception: The array should have at least three elements according to the requirements. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: The array should have at least three elements according to the requirements. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0