Problem Statement :-
John has a sequence of integers. Brus is going to choose two different positions in John's sequence and swap the elements at those positions. (The two swapped elements may have the same value.) Return the number of different sequences Brus can obtain after he makes the swap.
My Solution :-
using System;
using System.Collections.Generic;
public class TheSwapsDivTwo {
public int find(int[] sequence) {
int c = 0;
bool hasDup = false;
for (int idx = 0; idx < sequence.Length; idx++)
{
for (int j = idx + 1; j < sequence.Length; j++)
{
if (sequence[idx] != sequence[j])
{
c++;
}
else
{
hasDup = true;
}
}
}
return c + ((hasDup)?1:0);
}
}
John has a sequence of integers. Brus is going to choose two different positions in John's sequence and swap the elements at those positions. (The two swapped elements may have the same value.) Return the number of different sequences Brus can obtain after he makes the swap.
Examples | |||||||
0) | |||||||
| |||||||
1) | |||||||
| |||||||
2) | |||||||
| |||||||
3) | |||||||
|
My Solution :-
using System;
using System.Collections.Generic;
public class TheSwapsDivTwo {
public int find(int[] sequence) {
int c = 0;
bool hasDup = false;
for (int idx = 0; idx < sequence.Length; idx++)
{
for (int j = idx + 1; j < sequence.Length; j++)
{
if (sequence[idx] != sequence[j])
{
c++;
}
else
{
hasDup = true;
}
}
}
return c + ((hasDup)?1:0);
}
}