0% found this document useful (0 votes)
3K views4 pages

Test Codility

This document contains code snippets in C# for solving problems related to finding the minimum absolute difference between two sums of arrays. It provides multiple solutions with varying scores, and tests different approaches for calculating the minimum difference between splitting an array into two parts and summing each part. The solutions get progressively better, receiving higher scores.

Uploaded by

Luis Beltran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views4 pages

Test Codility

This document contains code snippets in C# for solving problems related to finding the minimum absolute difference between two sums of arrays. It provides multiple solutions with varying scores, and tests different approaches for calculating the minimum difference between splitting an array into two parts and summing each part. The solutions get progressively better, receiving higher scores.

Uploaded by

Luis Beltran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

int solution(int X, int Y, int D) {

//write your code in C90


int compr=0, cuenta=0;
compr=X;
while(compr<=Y)
{
compr+=D;
cuenta++;
}
return cuenta;
}
1 Y 4
2 Y 3
3 Y 2
4 Y 1
ESCORE 50%
using System;
02.// you can also use other imports, for example:
03.// using System.Collections.Generic;
04.class Solution {
05.public int solution(int[] A) {
06.// write your code in C# 5.0 with .NET 4.5 (Mono)
07.int sumpp1=0; //Suma de la primera parte
08.int sumpp2=0; //Suma de la segunda parte
09.int diference=0,minimal=0;
10.int N=A.Length;
11.
12.for(int P=1; P<N;P++)
13.{
14.for(int aux=0;aux<=(P-1);aux++)
15.sumpp1+= A[aux];
16.for(int aux2=P;aux2<=(N-1);aux2++)
17.sumpp2+= A[aux2];
18.diference=Math.Abs(sumpp1-sumpp2);
19.if(P==1){minimal=diference;}
20.else if(minimal>diference){minimal=diference;}
21.sumpp1=0;
22.sumpp2=0;
23.}
24.return minimal;
25.}
26.}
OTRA SOLUCION ESCORE 66%
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution
{
public int solution(int[] A)
{
// write your code in C# 5.0 with .NET 4.5 (Mono)
int sumpp1 = 0; //Suma de la primera parte
int sumpp2 = 0; //Suma de la segunda parte
int diference = 0, minimal = 0;
int N = A.Length;
/*for(int P=1; P<N;P++)
{
for(int aux=0;aux<=(P-1);aux++)
sumpp1+= A[aux];
for(int aux2=P;aux2<=(N-1);aux2++)
sumpp2+= A[aux2];*/
int aux2 = 1, temp;
for (int aux = 0; aux <= (aux2 - 1); aux++)
{
sumpp1 += A[aux];
temp = aux2;
System.Console.Write("sumpp1_" + sumpp1 + "\n");
while (temp < N)
{
sumpp2 += A[temp];
temp++;
}
System.Console.Write("\tsumpp2_" + sumpp2 + "\n");
aux2++;
diference = Math.Abs(sumpp1 - sumpp2);
if (aux == 0) { minimal = diference; }
else if (minimal > diference) { minimal = diference; }
sumpp2 = 0;
if (aux2 == N)
{
break;
}
}
return minimal;
}
}
otra solucion
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution
{
public int solution(int[] A)
{
// write your code in C# 5.0 with .NET 4.5 (Mono)
int sumpp1 = 0; //Suma de la primera parte
int sumpp2 = 0; //Suma de la segunda parte
int diference = 0, minimal = 0;
int N = A.Length;
int aux2 = 1, temp; minimal = A[0];
for (int aux = 0; aux <= (aux2 - 1); aux++)
{
sumpp2 = 0;
sumpp1 += A[aux];
temp = aux2;
while (temp < N)
{
sumpp2 += A[temp];
temp++;
}
aux2++;
diference = Math.Abs(sumpp1 - sumpp2);
if (minimal > diference) { minimal = diference; }
if (aux2 == N)
{
break;
}
}
return minimal;
}
}
ultima ESCORE 100%
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution
{
public int solution(int[] A)
{
// write your code in C# 5.0 with .NET 4.5 (Mono)
int sumatotal=0;
int sumpp1 = 0; //Suma de la primera parte
int diference = 0, minimal = 0;
int N = A.Length;

for(int aux=0;aux<N;aux++){
sumatotal+=A[aux];
}
int aux2 = 1;
for (int aux = 0; aux <= (aux2 - 1); aux++)
{
sumpp1 += A[aux];
diference = Math.Abs((2*sumpp1) - sumatotal);

if (aux == 0) { minimal = diference; }
else if (minimal > diference) { minimal = diference; }

aux2++;
if (aux2 == N)
{
break;
}
}
return minimal;
}
}
TEST 3 ESCORE 80
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
// write your code in C# 5.0 with .NET 4.5 (Mono)
int resultbusca;
int N = A.Length;
int suma=0;
int a=N+1;
resultbusca=a*(a+1)/2;

for(int i=0;i<N;i++)
suma+=A[i];

return resultbusca-suma;

}
}
otra solucion ESCORE
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
class Solution {
public int solution(int[] A) {
// write your code in C# 5.0 with .NET 4.5 (Mono)
double resultbusca;
int N = A.Length;
double suma=0;
int a=N+1;
resultbusca=a*(a+1)/2;

for(int i=0;i<N;i++)
suma+=A[i];

return (int)(resultbusca-suma);

}
}

You might also like