forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from TesseractCoding/master
mpr
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
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
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
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) | ||
/* |
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
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
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) | ||
*/ |
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
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)*/ |
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
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) | ||
*/ |