Skip to content

Commit

Permalink
super digit
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Jan 5, 2025
1 parent e4799ae commit b85e7b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g

[Find ZigZag Sequence](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/hackerRank/week2/FindZigZagSequence.java)

[Super Digit]()

# AWS Challenges

[Review Score](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/ReviewScore.java)
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/hackerRank/week2/SuperDigit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class SuperDigit {

public static int superDigit(String n, int k) {

LinkedList<Integer> list = new LinkedList<>();

for (int i=0; i< n.length(); i++) {
list.add(Integer.valueOf(String.valueOf(n.charAt(i))));
}

Long sum = 0L; // huge numbers
boolean firstLoop = true;

while(!list.isEmpty()) {
int i =list.pop();
sum+=i;

if(list.isEmpty()) {
if(firstLoop) {
sum = sum * k; // avoid concatenation
firstLoop = false;
}

if(sum<10) {
return sum.intValue();
}

String v = String.valueOf(sum);
for (int j=0; j<v.length(); j++) {
list.add(Integer.valueOf(String.valueOf(v.charAt(j))));
}
sum = 0L;
}
}

return -1;
}

}

0 comments on commit b85e7b8

Please sign in to comment.