finding nth biggest element in multidimensional array - c#

how can I find 5th or 7th, or 15th biggest element in multidimensional array without existing methods (like list.Add)
I will be pleased if you write it in c#
int[,,,] x =new int[100, 20, 35, 200];
...
int indis = 0;
int toplam = 0;
int enss = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 20; j++) {
toplam = 0;
for (int k = 0; k < 35; k++) {
for (int l = 0; l < 200; l++) {
toplam += x[i, j, k, l];
}
}
if (toplam > enss) {
enss = toplam;
indis = j;
}
}
}

You can try to query with a help of Linq (let .NET use Add if it wants). The crucial thing is to treat multidimensional array T[,,,] as IEnumerable<T>; we can use OfType() for this. Having IEnumerable<T> we can solve the rest. To find max item, just call Max():
using System.Linq;
...
int[,,,] x = new int[100, 20, 35, 200];
...
int max = x.OfType<int>().Max();
If you want to find nth biggest item, you can sort, skip n - 1 items and take the first:
...
int n = 7;
int maxN = x
.OfType<int>()
.OrderByDescending(item => item)
.Skip(n - 1)
.First();

Using linq :
var table = Enumerable.Range(0, x.GetLength(0))
.SelectMany((a, i) => Enumerable.Range(0, x.GetLength(1))
.SelectMany((b, j) => Enumerable.Range(0, x.GetLength(2))
.SelectMany((c, k) => Enumerable.Range(0, x.GetLength(3)
.Select((d, l) => new { x = x[i, j, k, l], i = i, j = j, k = k, l = l }))))
.OrderByDescending(x => x.x);

Related

Digit difference sort

So I am trying to solve this task "Digit Difference Sort" on Codefights
Given an array of integers, sort its elements by the difference of their largest and smallest digits.
In the case of a tie, that with the larger index in the array should come first.
Example
For a = [152, 23, 7, 887, 243], the output should be digitDifferenceSort(a) = [7, 887, 23, 243, 152].
Here are the differences of all the numbers:
152: difference = 5 - 1 = 4;
23: difference = 3 - 2 = 1;
7: difference = 7 - 7 = 0;
887: difference = 8 - 7 = 1;
243: difference = 4 - 2 = 2.
23 and 887 have the same difference, but 887 goes after 23 in a, so in the sorted array it comes first.
I have an issue with two numbers having the same difference. Here's what I wrote so far:
int[] digitDifferenceSort(int[] a) {
return a.OrderBy(x => difference(x)).ToArray();
}
int difference(int x)
{
int min = 9, max = 0;
do
{
int tmp = x % 10;
min = Math.Min(min, tmp);
max = Math.Max(max, tmp);
} while ((x /= 10) > 0);
return max - min;
}
Didn't do much (for example the output is still [7, 23, 887, 243, 152] rather than [7, 887, 23, 243, 152])
How do I make element with larger index come first in result? What should I use instead of OrderBy?
I don't consider your difference method, i assume it works fine.
To your question: you have to keep revered order of the array (that the items with the same difference arrive will be sorted reverse). To do it, you could just reverse you input array: all items with not identical difference will be ordered correctly, and with the same differece will be ordered reversed:
int[] digitDifferenceSort(int[] a)
{
return a.Reverse().OrderBy(x => difference(x)).ToArray();
}
Following is my code for the above question digit difference sort. I am also getting output when running in Eclipse but when I paste the code on code signal it gives me a null pointer exception.
package NormalPrograms;
import java.util.ArrayList;
import java.util.Collections;
public class DigitDifferenceSort {
// For index wise sorting in descending order
public static int[] sortingnumberindexwise(int[] a, ArrayList<Integer> index) {
int k = 0;
int[] res = new int[index.size()];
int[] finalres = new int[index.size()];
for (int i = a.length - 1; i >= 0; i--) {
for (int j = 0; j < index.size(); j++) {
if (a[i] == (int) index.get(j)) {
res[k] = i;
index.remove(j);
k++;
break;
}
}
}
int g = 0;
k = 0;
for (int i = 0; i < res.length; i++) {
finalres[g] = a[res[k]];
g++;
k++;
}
return finalres;
}
public static int[] finddigitDifferenceandSort(int[] p) {
int[] finres = new int[p.length];
for (int i = 0; i < finres.length; i++) {
finres[i] = p[i];
}
// This finres array act as an temp array and reused to make final result array
int digit = 0;
ArrayList<Integer> A = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < 10; i++) {
B.add(new ArrayList<Integer>());
}
for (int i = 0; i < p.length; i++) {
int temp = 0;
temp = p[i];
while (p[i] > 0) {
digit = p[i] % 10;
p[i] /= 10;
A.add(digit);
}
int b = Collections.max(A);
int c = Collections.min(A);
int diff = b - c;
B.get(diff).add(temp);
A.clear();
}
for (int i = 0; i < B.size(); i++) {
if (B.get(i).size() > 1) {
ArrayList<Integer> C = new ArrayList<Integer>();
for (int k = 0; k < B.get(i).size(); k++) {
C.add(B.get(i).get(k));
}
B.get(i).clear();
for (int j : sortingnumberindexwise(finres, C)) {
B.get(i).add(j);
}
} else {
continue;
}
}
int k = 0;
for (int i = 0; i < B.size(); i++) {
for (int j = 0; j < B.get(i).size(); j++) {
if (B.get(i).size() == 0)
continue;
else {
finres[k] = B.get(i).get(j);
k++;
}
}
}
return finres;
}
public static void main(String[] args) {
int[] a = { 12, 21, 1, 1, 1, 2, 2, 3 };
for (int i : finddigitDifferenceandSort(a)) {
System.out.print(i + " ");
}
}
}

how to remake 2d array random unique numbers as 3d?

static void Main(string[] args)
{
Random r = new Random();
int[,] x = new int[10,8];
int[] temp = new int[x.Length];
// two dimensional array and i want for three dimensional array
for(int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 100);
for(int j = 0; j < i; j++)
{
if(temp[i] == temp[j])
{
i--;
break;
}
}
}
for(int i = 0, index = 0; i < x.GetLength(0); i++)
{
for(int j = 0; j < x.GetLength(1); j++)
{
x[i, j] = temp[index++]; //two dimensional array unique numbers
Console.Write(x[i, j] + " ");
}
}
// i want do it as for 3D and 4 D array unique numbers like that method what can i change or add?
It's pretty straight forward to do what you're doing for higher dimensions.
Here's my code for 3D:
var r = new Random();
int [,,] x = new int[10, 8, 8];
var count =
Enumerable
.Range(0, x.Rank)
.Select(y => x.GetLength(y))
.Aggregate((y, z) => y * z);
var values =
Enumerable
.Range(10, count)
.OrderBy(y => r.Next())
.ToArray();
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
x[i, j, k] = values[v++];
To change it to 4D these lines change:
int [,,,] x = new int[10, 8, 8, 12];
// ...
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
for (var l = x.GetLowerBound(3); l <= x.GetUpperBound(3); l++)
x[i, j, k, l] = values[v++];
Now, in this code I have explicitly called GetLowerBound as well as GetUpperBound as it is possible in .NET code to have a non-zero based array.
Also, rather than repeatedly re-try getting random numbers until you have unique numbers I simply generated a sequence of unique numbers and then randomly sorted them. That's a little different from your original code. You needed 80 (10 x 8) random values and you were choosing from values ranging from 10 to 99 inclusive. So you had some holes in your numbers.
Random r = new Random();
int[,,] x = new int[10, 8, 8];
int[] temp = new int[x.Length];
#region one dimensional array unique numbers.
for (int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 650);
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
i--;
break;
}
}
}
#endregion
for (int i = 0, index = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
for (int k = 0; k < x.GetLength(2); k++)
{
x[i, j, k] = temp[index++];
Console.Write(x[i, j, k] + " ");
}
Console.WriteLine();
}
}// i think it's correct code i've changed it

Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

I have five long integers p, q, s, m and x. An array numbers[] is created by the following formula.
numbers[0] = s;
for(int i=1; i<numbers.Length;i++){
numbers[i] = (p * numbers[i-1] + q) % m;
}
The first value of numbers (numbers[0]) is s.
What is the most efficient way to find index j where i < j and |numbers[j] - numbers[i]| <= x or |numbers[j] - numbers[i]| >= m-x.
For instance, in a case where p = 3, q= 7, s= 1, m= 29 en x= 1 the array will be:
numbers[0] = 1, numbers[1] = 10, numbers[2] = 8 and numbers[3] = 2.
In this case index j would be 3, because numbers[3] - numbers[0]<=x, because x is 1.
I thought about using something such as a variant of counting sort or radix sort but I can't get anything to work.
As i < j, then you need to grant that numbers has a length of at least 2.
You could do two nested loops, the outer one ranging from j = 1 to numbers.Length - 1 (granting the possible solution to be the smallest j) to i = 0 to i < j.
Then you compare both positions according your specs. If true, return j. If it finishes both loops, then there is no solution.
Edit: Code Sample
public int GetSmallestIndex(long[] numbers, long x, long m)
{
if (numbers.Length >= 2)
{
for (int j = 1; j < numbers.Length; j++)
{
for (int i = 0; i < j; i++)
{
long diff = Math.Abs(numbers[j] - numbers[i]);
if (diff <= x || diff >= m - x)
return j;
}
}
}
return -1; //If no solution is found, return -1 as convention
}
The only way to find out if something is more efficient is to benchmark it by using the StopWatch in System.Diagnostics.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
var stopWatch = new Stopwatch();
stopWatch.Start();
const long x = 1;
var numbers = new long[] {3, 7, 1, 29};
var theSmallestIndex = SmallestIndex(x, numbers);
stopWatch.Stop();
Console.WriteLine("Elapsed Time: {0}", stopWatch.Elapsed);
Console.WriteLine("Smallest Index: {0}", theSmallestIndex);
Console.ReadKey();
}
public static long SmallestIndex(long x, long[] numbers)
{
var values = ValuesMinusTheValueOfPreviousIndex(x, numbers.ToList());
var smallest = values.Values.OrderBy(n => n).FirstOrDefault();
var result = values.Where(n => n.Value.Equals(smallest));
return result.FirstOrDefault().Key;
}
public static Dictionary<int, long> ValuesMinusTheValueOfPreviousIndex(long x, List<long> numbers)
{
var results = new Dictionary<int, long>();
foreach (var number in numbers)
{
var index = numbers.IndexOf(number);
var previousNumber = index > 0 ? numbers.ElementAt(index - 1) : 0;
var result = number - previousNumber;
results.Add(index, result);
}
return results;
}
}
}
EDIT: Added Math.Abs as you requested in the comments
long p = 3, q = 7, s = 1, m = 29, x = 1;
long[] numbers = new long[10];
numbers[0] = s;
for (int i = 1; i < numbers.Length; i++)
{
numbers[i] = (p * numbers[i - 1] + q) % m;
}
// Find the smallest index j in numbers, where i < j &&
// (numbers[j] - numbers[i] <= x || numbers[j] - numbers[i] >= m-x)
int smallestIndex = 0;
long comparison;
for (int j = 1; j < numbers.Length; j++)
{
for (int i = 0; i < j; i++)
{
comparison = Math.Abs(numbers[j] - numbers[i]);
if (comparison <= x || comparison >= m - x)
{
smallestIndex = j;
break;
}
}
if (smallestIndex != 0) break;
}
if (smallestIndex == 0)
{
// No result matches the conditions
}
else
{
// j is the smallest index matching the conditions
// Before using Abs, in the example j is 2, because 8 - 10 = -2, lesser than x
// Now using absolute values, In the example j is 3
}

How to find a particular string in a two dimensional array of string?

I need to find a string in a two dimensional array and I don't know how. The code should look like this:
...
Random x = new.Random();
Random y = new.Random();
string[,] array = new string[10,10];
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = "";
}
}
}
array[x.Next(0,10),y.Next(0,10)] = "*";
...
The * symbol is always in a different spot and I'd like to know how do I find it. Thanks
You can find it by iterating through the array just like you did for initializing it, except instead of assigning the array index a value, you'll check it for equality:
int i = 0;
int j = 0;
bool found = false;
for (i = 0; i < 10 && !found; i++)
{
for (j = 0; j < 10; j++)
{
if (array[i, j] == "*")
{
found = true;
break;
}
}
}
if (found)
Console.WriteLine("The * is at array[{0},{1}].", i - 1, j);
else
Console.WriteLine("There is no *, you cheater.");
As an alterntive search query with LINQ:
Random xRnd = new Random(DateTime.Now.Millisecond);
Random yRnd = new Random(DateTime.Now.Millisecond);
string[,] array = new string[10, 10];
array[xRnd.Next(0, 10), yRnd.Next(0, 10)] = "*";
var result =
Enumerable.Range(0, array.GetUpperBound(0))
.Select(x => Enumerable.Range(0, array.GetUpperBound(1))
.Where(y => array[x, y] != null)
.Select(y => new { X = x, Y = y }))
.Where(i => i.Any())
.SelectMany(i => i)
.ToList();
result is a list of matches in the form of X,Y

Editting a 2D array with Linq and ForEach

I'm trying to solve this question in a Linqified way
"Finding diagonal in 2D array and replacing with 0 above it"
This is my Linqfied solution:
Random r = new Random();
//int[,] mas = new int[4, 5];
int[][] mas = new int[4][];
for (int i = 0; i < mas.Length; i++)
{
mas[i] = new int[5];
for (int j = 0; j < mas[i].Length; j++)
{
mas[i][j] = r.Next(1, 10);
Console.Write("{0}\t", mas[i][j]);
}
Console.WriteLine();
}
// my code starts here
for (int i = 0; i < mas.Length; i++)
{
mas[i] = mas[i].Select((c, ind) =>
{
if (ind > i)
c = 0;
return c;
}).ToArray();
}
It works, but I want to get rid of the for-loop, then I did:
mas.ToList().ForEach(m0 =>
{
m0 = m0.Select((c, ind) =>
{
if (ind > k)
c = 0;
return c;
}).ToArray();
k++;
});
Despite the inner elements m0 get the desired values changed to zero in debug time, the mas 2D array don't get its values changed.
You can use linq like so:
mas = mas
.Select((a, i) => a.Select((c, j) => j > i ? 0 : c).ToArray())
.ToArray();
You're changing the parameter to the method, which is a copy of the value in your arrays. Mutating that copy does not mutate the original array.
A proper LINQ-based solution to this problem would be to use LINQ operations such that the results of executing the query is your data set, rather than having the execution of the query mutate some other data set:
var random = new Random();
var query = Enumerable.Range(0, 4)
.Select(i => Enumerable.Range(0, 5)
.Select(j => j > i ? 0 : random.Next(1, 10))
.ToArray())
.ToArray();

Categories

Resources