Skip to content

Commit

Permalink
java solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishika6 committed Dec 17, 2020
1 parent 550e29a commit dc0aaec
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 3 deletions.
56 changes: 56 additions & 0 deletions Algorithms/Implementation/Find Digits/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//Q->Fing Digits Given an integer, for each digit that makes up the integer
// determine whether it is a divisor. Count the number of divisors occurring within the integer.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {


static int findDigits(int n)
{
int t=n;
int count=0; //initial ans
while(t>0)
{
int r=t%10; //exytracting the last digit
if(r!=0){
if(n%r==0) //checking if it divides n
count++;
}
t=t/10; //discarding the last digit
}

return count; //returning final ans


}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int t = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int tItr = 0; tItr < t; tItr++) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int result = findDigits(n);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}

bufferedWriter.close();

scanner.close();
}
}
73 changes: 73 additions & 0 deletions Algorithms/Implementation/Sequence Equation/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Q-->Sequence equation , algorithms(implementation)
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {


static int[] permutationEquation(int[] p) {

ArrayList<Integer> ll=new ArrayList<>(); //to store values
for(int i = 0;i < p.length;i++)
p[i]--; //decrementing p values by one according to indices

// using double for loop we match for p(p(x))=y
// first for x and second for y
// as we have started from index 0, then we push index

for(int i = 0; i < p.length; i++) { //for y
for (int j = 0; j < p.length; j++) { //for x
if (p[p[j]] == i) {
ll.add(j+1);
break;
}
}
}
int ans[]=new int[ll.size()]; //ans array to be returned
for(int i=0;i<ans.length;i++){
ans[i]=ll.get(i);
}
return ans;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int[] p = new int[n];

String[] pItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int pItem = Integer.parseInt(pItems[i]);
p[i] = pItem;
}

int[] result = permutationEquation(p);

for (int i = 0; i < result.length; i++) {
bufferedWriter.write(String.valueOf(result[i]));

if (i != result.length - 1) {
bufferedWriter.write("\n");
}
}

bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}

53 changes: 53 additions & 0 deletions Algorithms/Implementation/Utopian Tree/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Q->Utopian Tree , Algorithms(implementation)

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {


static int utopianTree(int n) {

int r = 1; // starting period , initial height = 1
for (int i = 1; i <= n; i++) {

// (period%2==0 )--> height + 1
// (period%2!=0) ----> height * 2;

if (i % 2 == 0)
r++;
else
r = r * 2;
}
return r;

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int t = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int tItr = 0; tItr < t; tItr++) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int result = utopianTree(n);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}

bufferedWriter.close();

scanner.close();
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
||[Xor sequence](https://www.hackerrank.com/challenges/xor-se/problem)|Medium|[View](/Algorithms/Bit%20Manipulation/Xor%20sequence/solution.cpp)|[View](Algorithms/Bit%20Manipulation/Xor%20sequence/solution.java)|[View](/Algorithms/Bit%20Manipulation/Xor%20sequence/solution.py)|
||[Sherlock and the Valid String](https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem)|Medium||[View](Algorithms/Strings/Sherlock%20and%20the%20Valid%20String/solution.java)|[View](Algorithms/Strings/Sherlock%20and%20the%20Valid%20String/solution.py)||
||[Bigger is Greater](https://www.hackerrank.com/challenges/bigger-is-greater/problem)|Medium|[View](Algorithms/Implementation/Bigger%20is%20Greater/solution.cpp)|[View](Algorithms/Implementation/Bigger%20is%20Greater/solution.java)|[View](Algorithms/Implementation/Bigger%20is%20Greater/solution.py)|
||[Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree/problem)|Easy|[View](Algorithms/Implementation/Utopian%20Tree/solution.cpp)|||
||[Sequence Equation](https://www.hackerrank.com/challenges/permutation-equation/problem)|Easy|[View](Algorithms/Implementation/Sequence%20Equation/solution.cpp)|||
||[Find Digits](https://www.hackerrank.com/challenges/find-digits/problem)|Easy|[View](Algorithms/Implementation/Find%20Digits/solution.cpp)|||
||[Utopian Tree](https://www.hackerrank.com/challenges/utopian-tree/problem)|Easy|[View](Algorithms/Implementation/Utopian%20Tree/solution.cpp)|[View](Algorithms/Implementation/Utopian%20Tree/solution.java)||
||[Sequence Equation](https://www.hackerrank.com/challenges/permutation-equation/problem)|Easy|[View](Algorithms/Implementation/Sequence%20Equation/solution.cpp)|[View](Algorithms/Implementation/Sequence%20Equation/solution.java)||
||[Find Digits](https://www.hackerrank.com/challenges/find-digits/problem)|Easy|[View](Algorithms/Implementation/Find%20Digits/solution.cpp)|[View](Algorithms/Implementation/Find%20Digits/solution.java)||
||[Modified Kaprekar Numbers](https://www.hackerrank.com/challenges/kaprekar-numbers/problem)|Easy|[View](Algorithms/Implementation/Modified%20Kaprekar%20Numbers/solution.cpp)|||[View](Algorithms/Implementation/Modified%20Kaprekar%20Numbers/solution.py)
||[Taum and B'day](https://www.hackerrank.com/challenges/taum-and-bday/problem)|Easy|[View](Algorithms/Implementation/Taum%20and%20B'day/solution.cpp)|||[View](Algorithms/Implementation/Taum%20and%20B'day/solution.py)
||[Lonely Integer](https://www.hackerrank.com/challenges/lonely-integer/problem)|Easy|[View](/Algorithms/Lonely%20Integer/solution.cpp)|[View](/Algorithms/Lonely%20Integer/solution.java)|[View](/Algorithms/Lonely%20Integer/solution.py)|
Expand Down

0 comments on commit dc0aaec

Please sign in to comment.