Last active
April 8, 2021 10:44
-
-
Save viniston/f94d7389dfa68f1102d1b6f0a9717729 to your computer and use it in GitHub Desktop.
HackerEarth Programming Test: Merchant is allowed to travel from one village (Village i) to another village (Village j) to make more profit but with 2 conditions. Condition 1: if i < j and profit from village j (Pj) is a multiple of profit from village i (Pi). Example 6 villages with profit order 1, 2, 3, 4, 9, 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApp5 | |
{ | |
class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var inputs = new[] { 1, 2, 3, 4, 9, 8 }; | |
var numbers = 6; | |
Console.WriteLine(TotalProfit(inputs, numbers)); | |
Console.ReadLine(); | |
} | |
private static int TotalProfit(int[] p, int n) | |
{ | |
var moves = new List<int>(); | |
for (var i = 0; i < n - 1; i++) | |
{ | |
var iterator = i + 1; | |
while (iterator < n) | |
{ | |
Compare(p[i], p[iterator], ref moves); | |
iterator++; | |
} | |
} | |
return moves.Sum(); | |
} | |
static void Compare(int i, int j, ref List<int> moves) | |
{ | |
if (i >= j || j != (i + i)) return; | |
if (!moves.Contains(i)) | |
moves.Add(i); | |
if (!moves.Contains(j)) | |
moves.Add(j); | |
} | |
} | |
} |
It is C# code
this is the wrong code ..
correct code is as follows
/* written in java */
public class VILLAGE {
public static void main(String[] args) {
int inputs[] = new int[] { 2, 3, 4, 7, 8, 9, 21, 1 };
int numbers = inputs.length;
int s = village1(inputs, numbers);
System.out.println(s);
}
public static int village1(int ar[], int p) {
int ss;
for (int h = 0; h < p - 1; h++) {
for (int d = h + 1; d < p; d++) {
if (ar[h] > ar[d]) {
ss = ar[d];
ar[d] = ar[h];
ar[h] = ss;
}
}
}
int big = 0;
int sum[] = new int[100];
int f = 0;
int temp;
for (int i = 0; i < p - 2; i++) {
p: for (int k = i + 1; k < p; k++) {
if (!(ar[k] % ar[i] == 0)) {
continue p;
} else {
sum[f] = ar[i] + ar[k];
temp = ar[k];
}
if (ar[k] != ar[p - 1]) {
for (int j = k + 1; j < p; j++) {
if (ar[j] % temp == 0) {
sum[f] = sum[f] + ar[j];
temp = ar[j];
}
}
}
f++;
}
}
big = sum[0];
for (int u = 1; u < sum.length; u++) {
if (big < sum[u]) {
big = sum[u];
}
}
return big;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this Java code??