-
-
Save jianminchen/a377bac6ff6a2b459db462b0e2506470 to your computer and use it in GitHub Desktop.
HackerRank Stryker code sprint - https://www.hackerrank.com/contests/stryker-codesprint/challenges/minimum-index-difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace MinimumIndexDifference | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int n = Convert.ToInt32(Console.ReadLine().ToString()); | |
string[] arr1 = Console.ReadLine().ToString().Split(' '); | |
string[] arr2 = Console.ReadLine().ToString().Split(' '); | |
Console.WriteLine(findMinIndex(n, arr1, arr2)); | |
} | |
public static int findMinIndex(int n, string[] arr1, string[] arr2) | |
{ | |
Dictionary<string, int> data = new Dictionary<string, int>(); | |
int count = 0; | |
foreach(string s in arr2) | |
{ | |
data.Add(s, count); | |
count++; | |
} | |
int min = Int32.MaxValue; | |
int diff = Int32.MaxValue; // index difference | |
int index = 0; | |
foreach (string s in arr1) | |
{ | |
int index2 = data[s]; | |
int newD = Math.Abs(index2 - index); | |
int newV = Convert.ToInt32(s); | |
if((newD < diff) || (newD == diff && newV < min)) | |
{ | |
diff = newD; | |
min = newV; | |
} | |
index++; | |
} | |
return min; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment