Skip to content

Commit

Permalink
Solve Question 2974
Browse files Browse the repository at this point in the history
  • Loading branch information
febinjoy committed Jan 1, 2024
1 parent 7246471 commit 60ff067
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions LeetCodeSolutions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static void Main(string[] args)
ExecuteQuestion(26);
ExecuteQuestion(9);
ExecuteQuestion(35);
ExecuteQuestion(2974);

//Medium

Expand Down
67 changes: 67 additions & 0 deletions LeetCodeSolutions/Questions/Easy/Q2974.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using LeetCodeSolutions.Classes;
using LeetCodeSolutions.Enums;
using LeetCodeSolutions.Interfaces;

namespace LeetCodeSolutions.Questions.Easy
{
internal class Q2974 : IQuestion
{
public int QuestionNumber { get; private set; }
public string QuestionText { get; private set; }
public string QuestionLink { get; private set; }
public Difficulty Difficulty { get; private set; }

public Q2974()
{
QuestionNumber = 2974;
QuestionText = "Minimum Number Game";
QuestionLink = "https://leetcode.com/problems/minimum-number-game/description/";
Difficulty = Difficulty.Easy;
}

public IResult Execute()
{
int[] nums = new int[] { 5, 4, 2, 3 };
int[] expectedNums = new int[] { 3, 2, 5, 4 };
int[] result = NumberGame(nums);

for (int i = 0; i < nums.Length; i++)
{
if (expectedNums[i] != result[i])
{
return new ErrorResult($"Expected number: {expectedNums[i]}; But was {result[i]}");
}
}

nums = new int[] { 2, 5 };
expectedNums = new int[] { 5, 2 };
result = NumberGame(nums);

for (int i = 0; i < nums.Length; i++)
{
if (expectedNums[i] != result[i])
{
return new ErrorResult($"Expected number: {expectedNums[i]}; But was {result[i]}");
}
}

return new SuccessResult();
}

public int[] NumberGame(int[] nums)
{
int[] sortedArray = nums.OrderBy(i => i).ToArray();
var result = new int[nums.Length];

for (int i = 0; i < nums.Length; i = i + 2)
{
result[i] = sortedArray[i + 1];
result[i + 1] = sortedArray[i];
}

return result;
}

}
}

0 comments on commit 60ff067

Please sign in to comment.