Skip to content

Commit

Permalink
Merge pull request #14 from TesseractCoding/master
Browse files Browse the repository at this point in the history
mpr
  • Loading branch information
JayantGoel001 authored Dec 9, 2020
2 parents 71069f0 + c16339f commit e027ac3
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions C-Plus-Plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ _add list here_
- [Swap Two Nibbles](other/SwapTwoNibbles.cpp)
- [Unique_Number_III](other/Unique_Number_III.cpp)
- [Least Common Multiple](other/lcm.cpp)
- [Generate all Subsets](other/subsets.cpp)
66 changes: 66 additions & 0 deletions C-Plus-Plus/other/subsets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//C++ Program to print all subsets of a given distinct array of positive integers
#include <bits/stdc++.h>
using namespace std;

void GetSubsets(vector<int> inputArray, vector<int> subsetArray, int pos)
{
if (pos == inputArray.size())
{
//the input array has been traversed when pos = array size
cout << "[";
for (int i = 0; i < subsetArray.size(); i++)
{
if (subsetArray[i] != 0) //ignoring empty values
cout << subsetArray[i] << ",";
}

cout << "]\n";
}
else
{
GetSubsets(inputArray, subsetArray, pos + 1); //Recursion tree branch 1

subsetArray[pos] = inputArray[pos];
GetSubsets(inputArray, subsetArray, pos + 1); //Recursion tree branch 2
}
}

//DRIVER FUNCTION
int main()
{
int size;
cout << "Enter size of the array: ";
cin >> size;
vector<int> inputArray(size); //initializing input array with user-given size
for (int i = 0; i < size; i++)
{
cin >> inputArray[i];
}

vector<int> subsetArray(size); //empty array for storing subsets
cout << "[";
GetSubsets(inputArray, subsetArray, 0); //prints the subsets
cout << "]";
}

/*Sample IO
Input:
3
1 2 3
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
TIME COMPLEXITY - O(2^n)
SPACE COMPLEXITY - O(n)
/*
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ _add list here_
- [Happiness Problem (cp sets question)](cp/HappinessProblem.java)
- [Prime Number With Recursion](cp/PrimeRecursion.java)
- [Prime Number without Recursion](cp/Prime.java)
- [Ugly Number With Recursion](cp/UglyRecursion.java)
- [PDDI Using Recursion](cp/ArmStrongRecursion.java)

## Cryptography

Expand Down
33 changes: 33 additions & 0 deletions Java/cp/ArmStrongRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Checking Armstrong number with Recursion

import java.util.Scanner;
class ArmStrongRecursion
{
static int p;
public static int armstrong(int i)
{ if(i<10) //base class
return (int)(Math.pow(i,p));
else
return((int)Math.pow(i%10,p)+armstrong(i/10));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
int n=sc.nextInt();
p=Integer.toString(n).length(); //calculating the number of digits in the number by converting it into String

if(n==armstrong(n))
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}
}

/*
Sample Input and Output :
N = 153
153 is an Armstrong number
Space Complexity: O(1)
Time Complexity : O(p)
*/
48 changes: 48 additions & 0 deletions Java/cp/UglyRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//finding nth ugly number using recursion
import java.util.Scanner;
class UglyRecursion
{

public static int ugly(int n)
{

if (n == 1) //base cases
return 1;
if (n <= 0)
return 0;
if (n % 2 == 0)
return (ugly(n / 2));

if (n % 3 == 0)
return (ugly(n / 3));

if (n % 5 == 0)
return (ugly(n / 5));

return 0;
}



public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N= ");
int n=sc.nextInt();
int i=1;
while(n>0)
{ if(ugly(i)==1)
n-=1;
i++;
}
System.out.println(i--); //to avoid the last increment,we decrement by one
}
}


/* Sample Input
Input:
N = 10
Output: 12
Time Complexity : O(n)
Space Complexity : O(1)*/
33 changes: 33 additions & 0 deletions Java/math/ArmStrongRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Checking Armstrong number with Recursion

import java.util.Scanner;
class ArmStrongRecursion
{
static int p;
public static int armstrong(int i)
{ if(i<10) //base class
return (int)(Math.pow(i,p));
else
return((int)Math.pow(i%10,p)+armstrong(i/10));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("N = ");
int n=sc.nextInt();
p=Integer.toString(n).length(); //calculating the number of digits in the number by converting it into String

if(n==armstrong(n))
System.out.println(n+" is an Armstrong number");
else
System.out.println(n+" is not an Armstrong number");
}
}

/*
Sample Input and Output :
N = 153
153 is an Armstrong number
Space Complexity: O(1)
Time Complexity : O(p)
*/

0 comments on commit e027ac3

Please sign in to comment.