Skip to content

Commit

Permalink
Sherlock and array problem:C# solution
Browse files Browse the repository at this point in the history
Added a new solution for sherlock and array problem using C# programming language
  • Loading branch information
rasik210 committed May 26, 2018
1 parent 513f785 commit 0567784
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions Algorithms/Search/SherlockandArray/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Problem: https://www.hackerrank.com/challenges/sherlock-and-array/problem
C# Language Version: 7.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
- for cases when array lenth is 1 or 2, the decision is straightforward
- for cases when array length > 2
- first create the sum of all the elements in the array.
- Now iterate the elements. Now for each element, sum of left and right elements can be calculated
easily using simple maths as the sum of all the elements of the array is already available.
Time Complexity: O(n) //actually it is O(2n) as effectively we traverse the entire array twice.
Space Complexity: O(n) //we need to store the input elements in an array for processing.
*/
using System;

class Solution
{
static void Main(String[] args)
{

var testCaseCount = int.Parse(Console.ReadLine());
for (var a0 = 0; a0 < testCaseCount; a0++)
{
//No need to capture the size of array. We can use array's length property instead.
Console.ReadLine();
var a_temp = Console.ReadLine().Split(' ');
var inputNumbers = Array.ConvertAll(a_temp, int.Parse);
if (inputNumbers.Length == 1)
Console.WriteLine("YES");
else if (inputNumbers.Length == 2)
Console.WriteLine("NO");
else
{
var totalSum = 0;
foreach (var item in inputNumbers)
totalSum += item;

//calculations for element at 1st index as 0th index element can never satisfy the criterion
var leftSum = inputNumbers[0];
var rightSum = totalSum - inputNumbers[1] - inputNumbers[0];
if (leftSum == rightSum)
Console.WriteLine("YES");
else
{
var i = 2;
for (; i < inputNumbers.Length; i++)
{
leftSum += inputNumbers[i - 1];
rightSum -= inputNumbers[i];
if (leftSum == rightSum)
{
Console.WriteLine("YES");
break;
}
}
if (i == inputNumbers.Length)
Console.WriteLine("NO");
}
}
}

}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
| | [Minimum Loss](https://www.hackerrank.com/challenges/minimum-loss)| <ul><li>[Java](./Algorithms/Search/Minimum%20Loss/Solution.java)</li><ul> | _O(n log(n))_ | _O(n)_ | Medium | 35| ||
| | [KnightL on a Chessboard](https://www.hackerrank.com/challenges/knightl-on-chessboard)| <ul><li>Java</li></ul> | | | Medium | 35 | ||
| | [Pairs](https://www.hackerrank.com/challenges/pairs)| <ul><li>Java</li> <li>[C++](./Algorithms/Search/Pairs/Solution.cpp)</li><ul> | _O(n log(n))_ | _O(n)_ | Medium | 50 | ||
| | [Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array)| <ul><li>Java</li> <li>[C++](./Algorithms/Search/SherlockandArray/Solution.cpp)</li><ul> | | | Easy | 40 | ||
| | [Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array)| <ul><li>Java</li> <li>[C++](./Algorithms/Search/SherlockandArray/Solution.cpp)</li><li>[C#](./Algorithms/Search/SherlockandArray/Solution.cs)</li><ul> | _O(n)_ | _O(n)_| Easy | 40 | ||
| | [Maximum Subarray Sum](https://www.hackerrank.com/challenges/maximum-subarray-sum)| <ul><li>Java</li></ul> | | | Hard | 65 | ||
| | [Connected Cells in a grid](https://www.hackerrank.com/challenges/connected-cell-in-a-grid)| <ul><li>Java</li></ul> | | | Medium | 50 | ||
| | [Short Palindrome](https://www.hackerrank.com/challenges/short-palindrome)| <ul><li>Java</li></ul> | | | Medium | 40 | ||
Expand Down

0 comments on commit 0567784

Please sign in to comment.