0% found this document useful (0 votes)
196 views4 pages

Adnanefs Symphony Codility

The document contains code to solve two problems: 1. Find the largest number that can be formed by rearranging the digits of a given number. It does this by generating all permutations of the digits and parsing/comparing them as integers. 2. Find the maximum number of distinct values occurring in a given tree. It copies the tree to a new data structure that tracks occurrence counts and visitation status. It recursively visits nodes, updating counts for the tree.

Uploaded by

mahirzukic2
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)
196 views4 pages

Adnanefs Symphony Codility

The document contains code to solve two problems: 1. Find the largest number that can be formed by rearranging the digits of a given number. It does this by generating all permutations of the digits and parsing/comparing them as integers. 2. Find the maximum number of distinct values occurring in a given tree. It copies the tree to a new data structure that tracks occurrence counts and visitation status. It recursively visits nodes, updating counts for the tree.

Uploaded by

mahirzukic2
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

//Zadatak 1

// you can also use imports, for example:


// import java.util.*;

// you can write to stdout for debugging purposes, e.g.


// System.out.println("this is a debug message");
import java.util.ArrayList;

class Sample {

public static ArrayList<String> permutation(String s) {


// The result
ArrayList<String> res = new ArrayList<String>();
// If input string's length is 1, return {s}
if (s.length() == 1) {
res.add(s);
} else if (s.length() > 1) {
int lastIndex = s.length() - 1;
// Find out the last character
String last = s.substring(lastIndex);
// Rest of the string
String rest = s.substring(0, lastIndex);
// Perform permutation on the rest string and
// merge with the last character
res = merge(permutation(rest), last);
}
return res;
}

/**
* @param list a result of permutation, e.g. {"ab", "ba"}
* @param c the last character
* @return a merged new list, e.g. {"cab", "acb" ... }
*/
public static ArrayList<String> merge(ArrayList<String> list, String c) {
ArrayList<String> res = new ArrayList<String>();
// Loop through all the string in the list
for (String s : list) {
// For each string, insert the last character to all possible postions
// and add them to the new list
for (int i = 0; i <= s.length(); ++i) {
String ps = new StringBuffer(s).insert(i, c).toString();
res.add(ps);
}
}
return res;
}

public int solution(int N) {


// write your code in Java SE 8
String number = N + "";
ArrayList<String> permutations = permutation(number);
Integer max = -1;
for(String num : permutations) {
try {
Integer temp = Integer.parseInt(num);
if (temp > max) {
max = temp;
}
} catch (Exception e) {

}
}
return max;
}
}

//Zadatak 2

// you can also use imports, for example:


// import java.util.*;

// you can write to stdout for debugging purposes, e.g.


// System.out.println("this is a debug message");

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Solution {

public class Tree {


public int x;
public Tree l;
public Tree r;
}

public class TreeAlternative {


public int x;
public TreeAlternative l;
public TreeAlternative r;
TreeAlternative parent;
public int numberOfDistinctUpToAndIncludingThisNode;
boolean visisted;
}

public Map<Integer, Integer> numberOccurences = new HashMap<Integer,


Integer>();
public Integer max = -1;
public Tree root;

public int solution(Tree T, Solution s) {


// write your code in Java SE 8
TreeAlternative newTree = new TreeAlternative();
copyTreeToTree(T, newTree, null);
TreeAlternative currentTree = newTree;
visitTreeRecursively(currentTree, s);

return max;
}

public Solution() {
root = new Tree();
root.x =4;
Tree rootL = new Tree();
rootL.x = 5;
Tree rootLL = new Tree();
rootLL.x = 4;
Tree rootLLL = new Tree();
rootLLL.x = 5;
rootLL.l = rootLLL;

Tree rootR = new Tree();


rootR.x = 6;
Tree rootRL = new Tree();
rootRL.x = 1;
rootR.l = rootRL;
Tree rootRR = new Tree();
rootRR.x = 6;
rootR.r = rootRR;

root.r = rootR;
}

public static void main(String[] args) {


Solution sol = new Solution();

System.out.println(sol.solution(sol.root, sol));
}

private void visitTreeRecursively(TreeAlternative currentTree, Solution s) {


Integer currentValue = currentTree.x;
Integer numberOccurencesOfX = numberOccurences.get(currentValue);
if (numberOccurencesOfX != null) {
numberOccurencesOfX ++;
numberOccurences.put(currentValue, numberOccurencesOfX);
} else {
numberOccurences.put(currentValue, 1);
}
currentTree.numberOfDistinctUpToAndIncludingThisNode =
countOccurences(s);
if (currentTree.numberOfDistinctUpToAndIncludingThisNode > max) {
max = currentTree.numberOfDistinctUpToAndIncludingThisNode;
}
currentTree.visisted = true;
if (currentTree.l != null && !currentTree.l.visisted) {
visitTreeRecursively(currentTree.l, s);
}
if (currentTree.r != null && !currentTree.r.visisted) {
visitTreeRecursively(currentTree.r, s);
}
if(currentTree.parent != null) {
reduceNumberOfOccurencesByOne(currentValue, s);
}
}

private void reduceNumberOfOccurencesByOne(Integer currentValue, Solution s)


{
Integer numberOccurencesOfX = s.numberOccurences.get(currentValue);
numberOccurencesOfX--;
if (numberOccurencesOfX > 0) {
s.numberOccurences.put(currentValue, numberOccurencesOfX);
} else {
s.numberOccurences.remove(currentValue);
}
}

private int countOccurences(Solution s) {


Set<Integer> set = new HashSet<Integer>();
for (int i : s.numberOccurences.keySet()) {
set.add(i);
}
return set.size();
}

private void copyTreeToTree(Tree t, TreeAlternative newTree, TreeAlternative


root) {
newTree.x = t.x;
newTree.numberOfDistinctUpToAndIncludingThisNode = -1;
newTree.visisted = false;
if (root != null) {
newTree.parent = root;
}
if (t.l != null) {
newTree.l = new TreeAlternative();
copyTreeToTree(t.l, newTree.l, newTree);
}
if(t.r != null) {
newTree.r = new TreeAlternative();
copyTreeToTree(t.r, newTree.r, newTree);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy