We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
/*
* Complete the 'equal' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
private static int GetDistributionGap(int largestDistribution, int smallestDistribution) {
var gap = (largestDistribution - smallestDistribution);
if (gap >= 5) {
return 5;
}
if (gap >= 2) {
return 2;
}
return 1;
}
public static int equal(List<int> arr, int operations = 0)
{
var newDistributions = new List<int>(arr);
var nextOperations = operations;
var largestDistribution = arr.Max();
var smallestDistribution = arr.Min();
if (AnyDifferent(arr)) {
nextOperations = nextOperations + 1;
// var largestDistribution = arr.Max();
// var smallestDistribution = arr.Min();
var requiredDistribution = GetDistributionGap(largestDistribution, smallestDistribution);
for(int i = 0; i < arr.Count(); i++) {
var leftValue = arr[i];
if (leftValue < largestDistribution) {
newDistributions[i] = leftValue + requiredDistribution;
}
}
}
if (nextOperations != operations) {
return equal(newDistributions, nextOperations);
}
Console.WriteLine($"Iteration: {nextOperations}, Largest: {largestDistribution}, Smallest: {smallestDistribution}, Arr: {string.Join(", ", newDistributions)}");
return nextOperations;
}
public static bool AnyDifferent(List<int> arr) {
for(int i = 0; i < arr.Count; i++) {
var comparable = arr[0];
if (comparable != arr[i]) {
return true;
}
}
return false;
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int t = Convert.ToInt32(Console.ReadLine().Trim());
for (int tItr = 0; tItr < t; tItr++)
{
int n = Convert.ToInt32(Console.ReadLine().Trim());
List<int> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList();
int result = Result.equal(arr);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
Equal
You are viewing a single comment's thread. Return to all comments →
using System.CodeDom.Compiler; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text.RegularExpressions; using System.Text; using System;
class Result {
}
class Solution { public static void Main(string[] args) { TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
}