C
C#2y ago
shawski.

✅ need help with a problem

i know what im supposed to do but im having trouble implementing it. i need to add the sum of a list except for the minimum value and do the same but this time except the maximum value
82 Replies
shawski.
shawski.2y ago
i could do
arr.Sum()-arr.Min();
arr.Sum()-arr.Min();
that would get me one of them it has to be in the form x y ill give an example
1 2 3 4 5
1 2 3 4 5
1+2+3+4 = 10 2+3+4+5 = 14 answer: 10 14
public static void miniMaxSum(List<int> arr)
{
int a = arr.Sum() - arr.Max();
int b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}
public static void miniMaxSum(List<int> arr)
{
int a = arr.Sum() - arr.Max();
int b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}
im curious why this worked for 3/14 tests, but failed the rest
Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Linq.Enumerable.Sum(IEnumerable`1 source)
at Result.miniMaxSum(List`1 arr) in /tmp/submission/20221204/04/00/hackerrank-a6dbcc82c252190428c82ae94e977449/code/Solution.cs:line 26
at Solution.Main(String[] args) in /tmp/submission/20221204/04/00/hackerrank-a6dbcc82c252190428c82ae94e977449/code/Solution.cs:line 41
Aborted (core dumped)
Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Linq.Enumerable.Sum(IEnumerable`1 source)
at Result.miniMaxSum(List`1 arr) in /tmp/submission/20221204/04/00/hackerrank-a6dbcc82c252190428c82ae94e977449/code/Solution.cs:line 26
at Solution.Main(String[] args) in /tmp/submission/20221204/04/00/hackerrank-a6dbcc82c252190428c82ae94e977449/code/Solution.cs:line 41
Aborted (core dumped)
the error ^
Deneri
Deneri2y ago
Overflow means you went past the end of int. Probably added two very large ints. That went beyond the valid range of int
shawski.
shawski.2y ago
hmm ill send some of the numbers being tested @Deneri
256741038 623958417 467905213 714532089 938071625
256741038 623958417 467905213 714532089 938071625
this is 1 of the 14 inputs being tested against my algorithm they are adding massive numbers. the question is, what do i do about that? all the tests past if the numbers were much lower
shawski.
shawski.2y ago
shawski.
shawski.2y ago
that passed for example if the problem calls for me to add the numbers, but the numbers are too large... it kinda seems impossible from my pov. maybe there are some solutions to this though, but i cant think of any
Deneri
Deneri2y ago
Don't store the result in an int. An int is too small. Store the result in something bigger.
shawski.
shawski.2y ago
alright
shawski.
shawski.2y ago
shawski.
shawski.2y ago
i stored it as a long same tests pass same ones fail i guess the calculations are still using an integer im stumped
custard
custard2y ago
Don't forget to change the type of the arr parameter to List<long>
shawski.
shawski.2y ago
yeah i tried that. it seems to break the whole program.
Kouhai
Kouhai2y ago
What's the expected method signature?
shawski.
shawski.2y ago
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 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/

public static void miniMaxSum(List<int> arr)
{
long a = arr.Sum() - arr.Max();
long b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}

}

class Solution
{
public static void Main(string[] args)
{

List<int> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList();

Result.miniMaxSum(arr);
}
}
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 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/

public static void miniMaxSum(List<int> arr)
{
long a = arr.Sum() - arr.Max();
long b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}

}

class Solution
{
public static void Main(string[] args)
{

List<int> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList();

Result.miniMaxSum(arr);
}
}
Kouhai
Kouhai2y ago
Okay, what's the current problem With long there should be no overflows
shawski.
shawski.2y ago
the results of both calculations are being stored in the variable but the list is an
int
int
Kouhai
Kouhai2y ago
Oh I didn't notice it's a int You can modify main or no?
shawski.
shawski.2y ago
i dont believe im supposed to i mean i can fix it if i edit main i could give that a go, just im not sure if i m supposed to do that
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 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/

public static void miniMaxSum(List<long> arr)
{
long a = arr.Sum() - arr.Max();
long b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}

}

class Solution
{
public static void Main(string[] args)
{

List<long> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt64(arrTemp)).ToList();

Result.miniMaxSum(arr);
}
}
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 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/

public static void miniMaxSum(List<long> arr)
{
long a = arr.Sum() - arr.Max();
long b = arr.Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}

}

class Solution
{
public static void Main(string[] args)
{

List<long> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt64(arrTemp)).ToList();

Result.miniMaxSum(arr);
}
}
edited verison @Kouhai it passed all the 14 tests i couldve done that the whole time. i guess i was just under the impression that i wasnt supposed to edit main
Kouhai
Kouhai2y ago
If you don't want to edit main you can do this
public static void miniMaxSum(List<int> arr)
{
long a = arr.Sum(m => (long)m) - arr.Max();
long b = arr.Sum(m => (long)m) - arr.Min();

Console.WriteLine(a + " " + b);
}
public static void miniMaxSum(List<int> arr)
{
long a = arr.Sum(m => (long)m) - arr.Max();
long b = arr.Sum(m => (long)m) - arr.Min();

Console.WriteLine(a + " " + b);
}
shawski.
shawski.2y ago
im curious, what is |

(m => (long)m)


(m => (long)m)

doing?
Kouhai
Kouhai2y ago
It casts the int in arr to long before summing
shawski.
shawski.2y ago
does the "m" mean something specifically?
Kouhai
Kouhai2y ago
No, m is just the name I chose randomly It's basically a variable name
shawski.
shawski.2y ago
oh where was it initialized? or i guess it was initizilized in that that scope ive never seen that so im pretty confused on which each component is doing
Kouhai
Kouhai2y ago
That syntax is for lambda expressions in C# If you don't know what lambda expressions are, they are basically an anonymous function that can be executed. (If this is more confusing and makes it harder, reading the docs might help) https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions
Lambda expressions - Lambda expressions and anonymous functions
C# lambda expressions that are used to create anonymous functions and expression bodied members.
shawski.
shawski.2y ago
alright thank you 😄
Kouhai
Kouhai2y ago
But also you can just do
public static void miniMaxSum(List<int> arr)
{
long a = arr.Cast<long>().Sum() - arr.Max();
long b = arr.Cast<long>().Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}
public static void miniMaxSum(List<int> arr)
{
long a = arr.Cast<long>().Sum() - arr.Max();
long b = arr.Cast<long>().Sum() - arr.Min();

Console.WriteLine(a + " " + b);
}
shawski.
shawski.2y ago
what does Cast do?
Kouhai
Kouhai2y ago
Cast<T> as the name suggests casts an IEnumerable<T> to another IEnumerable<T>
shawski.
shawski.2y ago
@Kouhai quick question, how can i check whether a number is odd or even? nvm
shawski.
shawski.2y ago
shawski.
shawski.2y ago
im curious why this worked
shawski.
shawski.2y ago
24 / 2 doesnt = 0
shawski.
shawski.2y ago
it failed 2/8 tests the rest did fine wait it isnt dividing it
Kouhai
Kouhai2y ago
%
shawski.
shawski.2y ago
thats what "/" would do yeah
Kouhai
Kouhai2y ago
% is modulo operator
shawski.
shawski.2y ago
but im curious why 2 tests failed but 6 passed 20 % 2 = 0
shawski.
shawski.2y ago
shawski.
shawski.2y ago
so why is it printing "Weird" here
Kouhai
Kouhai2y ago
20 % 2 will return 0, I'm not sure if another part of the code is causing that error
shawski.
shawski.2y ago
shawski.
shawski.2y ago
i ran the code in vsc and it prints "Not Weird" i dont think the problem lies with my code..
Kouhai
Kouhai2y ago
Yeah, if Weird should be printed for odd numbers, then the test case above is invalid
shawski.
shawski.2y ago
im not sure how i can pass this problem then
shawski.
shawski.2y ago
Kouhai
Kouhai2y ago
Can you show the description of the problem?
shawski.
shawski.2y ago
shawski.
shawski.2y ago
why is it giving me 2 answers? it isnt giving me an error or anything if that's what you mean
Kouhai
Kouhai2y ago
No, I mean what are the requirements?
shawski.
shawski.2y ago
oh Task Given an integer, , perform the following conditional actions: If n is odd, print Weird If n is even and in the inclusive range of 2 to , 5 print Not Weird If n is even and in the inclusive range of 6 to , 20 print Weird If n is even and greater than 20, print Not Weird Print Weird if the number is weird; otherwise, print Not Weird.
shawski.
shawski.2y ago
shawski.
shawski.2y ago
wait i think there is a little bit more i have to do i didnt read this part If n is odd, print Weird If n is even and in the inclusive range of 2 to , 5 print Not Weird If n is even and in the inclusive range of 6 to , 20 print Weird If n is even and greater than 20, print Not Weird ok i have to nest some if statements. i know how to do this now alright i believe i fixed everything nvm...
If n is even and in the inclusive range of 6 to , 20 print Weird
If n is even and in the inclusive range of 6 to , 20 print Weird
shawski.
shawski.2y ago
from reading that, isnt this supposed to print 18
shawski.
shawski.2y ago
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());

if (N % 2 == 0)
{
if (N < 2 && N > 5)
{
Console.WriteLine("Not Weird");
}
else if (N < 6 && N > 20)
{
Console.WriteLine("Weird");
}
else if (N > 20)
{
Console.WriteLine("Not Weird");
}

}
else
{
Console.WriteLine("Weird");
}
}
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());

if (N % 2 == 0)
{
if (N < 2 && N > 5)
{
Console.WriteLine("Not Weird");
}
else if (N < 6 && N > 20)
{
Console.WriteLine("Weird");
}
else if (N > 20)
{
Console.WriteLine("Not Weird");
}

}
else
{
Console.WriteLine("Weird");
}
}
thats my updated code
Servator
Servator2y ago
How can a number smaller than 6 yet bigger than 20 ?
shawski.
shawski.2y ago
oh wait i know what i need to do it should be || it's still failing 3/8 tests im not really sure what to do If n is even and in the inclusive range of 2 to , 5 print Not Weird
shawski.
shawski.2y ago
why is this wrong then?
Servator
Servator2y ago
What's the problem exactly ? I mean assignment oh wait i saw mb
shawski.
shawski.2y ago
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());

if (N % 2 == 0)
{
if (N < 2 || N > 5)
{
Console.WriteLine("Not Weird");
}
else if (N < 6 || N > 20)
{
Console.WriteLine("Weird");
}
else if (N > 20)
{
Console.WriteLine("Not Weird");
}

}
else
{
Console.WriteLine("Weird");
}
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());

if (N % 2 == 0)
{
if (N < 2 || N > 5)
{
Console.WriteLine("Not Weird");
}
else if (N < 6 || N > 20)
{
Console.WriteLine("Weird");
}
else if (N > 20)
{
Console.WriteLine("Not Weird");
}

}
else
{
Console.WriteLine("Weird");
}
wait i see a couple mistakes i gtg for a little. i think i know how to solve it
Servator
Servator2y ago
What is the expected result of 0 ? btw if number is expected in range (inclusive) It should be n >= 2 && n <= 5
var result = (number, number % 2 == 0) switch
{
(_, false) => "Weird",
(>= 2 and <= 5, true) => "Not Weird",
(>= 6 and <= 20, true) => "Weird",
(> 20, _) => "Not Weird",
_ => "I don't know"
};

Console.WriteLine(result);
var result = (number, number % 2 == 0) switch
{
(_, false) => "Weird",
(>= 2 and <= 5, true) => "Not Weird",
(>= 6 and <= 20, true) => "Weird",
(> 20, _) => "Not Weird",
_ => "I don't know"
};

Console.WriteLine(result);
shawski.
shawski.2y ago
yeah i mixed around the the greater than and less than symbols i havent seen a switched used like that hmm i thought it had to be
switch ()
{

}
switch ()
{

}
so result is a bool that checks if the number is % by 2 == 0 what does the underscore and "=>" mean i have seen those symbols but i dont really understand what they do
Servator
Servator2y ago
It's lambda and switch expression so even numbers supposed to be Output "Weird" right ? sorry odd
shawski.
shawski.2y ago
yeah i think so
Servator
Servator2y ago
var result = number switch
{
_ when number % 2 != 0 => "Weird",
_ when number % 2 == 0 && number is >= 2 and <= 5 => "Not Weird",
_ when number % 2 == 0 && number is >= 6 and <= 20 => "Weird",
_ when number % 2 == 0 && number > 20 => "Not Weird",
_ => "I don't know"
};
var result = number switch
{
_ when number % 2 != 0 => "Weird",
_ when number % 2 == 0 && number is >= 2 and <= 5 => "Not Weird",
_ when number % 2 == 0 && number is >= 6 and <= 20 => "Weird",
_ when number % 2 == 0 && number > 20 => "Not Weird",
_ => "I don't know"
};
can you try this ? If this doesnt work
var result = number switch
{
_ when number % 2 == 0 && number is >= 2 and <= 5 => "Not Weird",
_ when number % 2 == 0 && number is >= 6 and <= 20 => "Weird",
_ when number % 2 == 0 && number > 20 => "Not Weird",
_ when number % 2 != 0 => "Weird",
_ => "I don't know"
};
var result = number switch
{
_ when number % 2 == 0 && number is >= 2 and <= 5 => "Not Weird",
_ when number % 2 == 0 && number is >= 6 and <= 20 => "Weird",
_ when number % 2 == 0 && number > 20 => "Not Weird",
_ when number % 2 != 0 => "Weird",
_ => "I don't know"
};
This should work
shawski.
shawski.2y ago
i got past the problem a few hours ago i shouldve mentioned that
ero
ero2y ago
var result = number % 2 switch
{
0 => number switch
{
(>= 2 and <= 5) or (> 20) => "Not Weird",
_ => "Weird"
},
_ => "Weird"
};
var result = number % 2 switch
{
0 => number switch
{
(>= 2 and <= 5) or (> 20) => "Not Weird",
_ => "Weird"
},
_ => "Weird"
};
think this is more concise, at least to me
Servator
Servator2y ago
more concise less readable :&
shawski.
shawski.2y ago
what does the _ mean there
ero
ero2y ago
"default"
shawski.
shawski.2y ago
oh
Servator
Servator2y ago
last _ is default
shawski.
shawski.2y ago
what about the one that is in the middle
Servator
Servator2y ago
others are discarded parameter/variable
ero
ero2y ago
conciseness is basically a synonym for readability Not really for "shortness" Plus it doesn't have an "I don't know" path
shawski.
shawski.2y ago
i mean @Samarichitane version was definitely more readable but it doesnt really matter thanks for the help both of you 😄
Servator
Servator2y ago
He got shortened tho
shawski.
shawski.2y ago
yeah nice to see all the different ways you guys took it apart
ero
ero2y ago
I'd agree that their version makes it more clear what's happening
Servator
Servator2y ago
You can close thread with /close @shawski
ero
ero2y ago
Think mine's just more efficient
shawski.
shawski.2y ago
alright
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server
More Posts