C
C#5mo ago
Greya

Confused on why my simple leetcode solution works

No description
12 Replies
Greya
GreyaOP5mo ago
lets use the first data test case 1 is listed twice so it returns true but my second loop tests for i+1. so j= i +1 right lets assume i is at number 3 in the first test case. then j (which is i+1) would be at 1 3 and 1 are not the same so why does it return true?
Angius
Angius5mo ago
Did you use the debugger? It will let you see what each value is at any given point
Greya
GreyaOP5mo ago
the other solutions are like this to im just confused on how its correct if i said j = i+3 then that would make sense because if i is at 1. then j would be at 1 again but i said j = i+1 so i and j should never be the same number yet it still works
Angius
Angius5mo ago
i=0 => j=1,2,3 i=1 => j=2,3 i=2 => j=3 i=3 => j= It never compares numbers on the same position
Greya
GreyaOP5mo ago
wait what so j
Angius
Angius5mo ago
numbers[x] will never be compared with numbers[x]
Greya
GreyaOP5mo ago
is all the numbers past i? not just the number infront of i
Angius
Angius5mo ago
Well, yes, you start with a j that is one higher than i
Greya
GreyaOP5mo ago
if i have an array {1,2,3,4,5} and i is at number 2. then j would be number 3 no? assuming j is i+1?
MODiX
MODiX5mo ago
Angius
REPL Result: Success
int[] nums = {34, 45, 56, 67};
for (var i = 0; i < nums.Length; i++)
{
for (var j = i + 1; j < nums.Length; j++)
{
var even = nums[i] == nums[j];
Console.WriteLine($"[i={i}, j={j}]: {nums[i]} == {nums[j]} => ${even}");
}
}
int[] nums = {34, 45, 56, 67};
for (var i = 0; i < nums.Length; i++)
{
for (var j = i + 1; j < nums.Length; j++)
{
var even = nums[i] == nums[j];
Console.WriteLine($"[i={i}, j={j}]: {nums[i]} == {nums[j]} => ${even}");
}
}
Console Output
[i=0, j=1]: 34 == 45 => $False
[i=0, j=2]: 34 == 56 => $False
[i=0, j=3]: 34 == 67 => $False
[i=1, j=2]: 45 == 56 => $False
[i=1, j=3]: 45 == 67 => $False
[i=2, j=3]: 56 == 67 => $False
[i=0, j=1]: 34 == 45 => $False
[i=0, j=2]: 34 == 56 => $False
[i=0, j=3]: 34 == 67 => $False
[i=1, j=2]: 45 == 56 => $False
[i=1, j=3]: 45 == 67 => $False
[i=2, j=3]: 56 == 67 => $False
Compile: 494.046ms | Execution: 70.268ms | React with ❌ to remove this embed.
MODiX
MODiX5mo ago
Angius
REPL Result: Success
int[] nums = {34, 45, 56, 34};
for (var i = 0; i < nums.Length; i++)
{
for (var j = i + 1; j < nums.Length; j++)
{
var even = nums[i] == nums[j];
Console.WriteLine($"[i={i}, j={j}]: {nums[i]} == {nums[j]} => ${even}");
}
}
int[] nums = {34, 45, 56, 34};
for (var i = 0; i < nums.Length; i++)
{
for (var j = i + 1; j < nums.Length; j++)
{
var even = nums[i] == nums[j];
Console.WriteLine($"[i={i}, j={j}]: {nums[i]} == {nums[j]} => ${even}");
}
}
Console Output
[i=0, j=1]: 34 == 45 => $False
[i=0, j=2]: 34 == 56 => $False
[i=0, j=3]: 34 == 34 => $True
[i=1, j=2]: 45 == 56 => $False
[i=1, j=3]: 45 == 34 => $False
[i=2, j=3]: 56 == 34 => $False
[i=0, j=1]: 34 == 45 => $False
[i=0, j=2]: 34 == 56 => $False
[i=0, j=3]: 34 == 34 => $True
[i=1, j=2]: 45 == 56 => $False
[i=1, j=3]: 45 == 34 => $False
[i=2, j=3]: 56 == 34 => $False
Compile: 504.328ms | Execution: 69.859ms | React with ❌ to remove this embed.
Angius
Angius5mo ago
Ignore that $, a JS-ism found it's way in lol

Did you find this page helpful?