-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/jayanpahuja20/Problem-Solvi…
- Loading branch information
Showing
37 changed files
with
1,047 additions
and
3 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
class Solution { | ||
public: | ||
bool find132pattern(vector<int>& nums) { | ||
//31412 | ||
stack<int>s; | ||
int temp=INT_MIN;//-13206 | ||
for(int i=nums.size()-1;i>=0;i--) | ||
{ | ||
if(nums[i]<temp)//2<-13206 | ||
{ | ||
return true; | ||
} | ||
while(!s.empty() && nums[i]>s.top()) //2>-13206 yes | ||
{ | ||
temp=s.top(); //temp=2 | ||
s.pop(); | ||
} | ||
s.push(nums[i]); //1 | ||
} | ||
return false; | ||
} | ||
}; |
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,41 @@ | ||
class Solution | ||
{ | ||
public: | ||
vector<vector<int>> fourSum(vector<int> &nums, int target) | ||
{ | ||
set<vector<int>> ans1; | ||
int n = nums.size(); | ||
sort(nums.begin(), nums.end()); | ||
long long int sum1; | ||
for (int i = 0; i < n - 3; i++) | ||
{ | ||
|
||
for (int j = i + 1; j < n - 2; j++) | ||
{ | ||
|
||
int l = j + 1, k = n - 1; | ||
while (l < k) | ||
{ | ||
sum1 = (nums[i] * 1LL) + nums[j] + nums[l] + nums[k]; | ||
|
||
if (sum1 > target) | ||
{ | ||
k--; | ||
} | ||
else if (sum1 == target) | ||
{ | ||
vector<int> v{nums[i], nums[j], nums[l], nums[k]}; | ||
ans1.insert(v); | ||
l++; | ||
} | ||
else | ||
l++; | ||
} | ||
} | ||
} | ||
vector<vector<int>> ans; | ||
for (auto it : ans1) | ||
ans.push_back(it); | ||
return ans; | ||
} | ||
}; |
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,44 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class BSTIterator { | ||
public: | ||
vector<int>v; | ||
int i=0; | ||
void inorder(TreeNode *root) | ||
{ | ||
if(root==NULL) | ||
{ | ||
return; | ||
} | ||
inorder(root->left); | ||
v.push_back(root->val); | ||
inorder(root->right); | ||
} | ||
BSTIterator(TreeNode* root) { | ||
inorder(root); | ||
} | ||
|
||
int next() { | ||
return v[i++]; | ||
} | ||
|
||
bool hasNext() { | ||
return i<v.size(); | ||
} | ||
}; | ||
|
||
/** | ||
* Your BSTIterator object will be instantiated and called as such: | ||
* BSTIterator* obj = new BSTIterator(root); | ||
* int param_1 = obj->next(); | ||
* bool param_2 = obj->hasNext(); | ||
*/ |
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,22 @@ | ||
class Solution { | ||
public: | ||
int maxArea(vector<int>& height) { | ||
int beginning=0, end=height.size()-1,max_area=0; | ||
|
||
|
||
while(beginning<=end) | ||
{ | ||
int minimum_height=min(height[beginning],height[end]); | ||
max_area=max(minimum_height * (end-beginning) , max_area); | ||
if(height[beginning]<=height[end]) | ||
{ | ||
beginning++; | ||
} | ||
else | ||
{ | ||
end--; | ||
} | ||
} | ||
return max_area; | ||
} | ||
}; |
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,13 @@ | ||
class Solution { | ||
public: | ||
string convertToTitle(int columnNumber) { | ||
string ans; | ||
while(columnNumber){ | ||
columnNumber--; | ||
char ch= 'A'+ (columnNumber%26); | ||
columnNumber/=26; | ||
ans= ch+ ans; | ||
} | ||
return ans; | ||
} | ||
}; |
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,20 @@ | ||
import java.util.Scanner; | ||
public class Fibonacci{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
System.out.print("Enter the Count :"); | ||
int count =sc.nextInt(); | ||
int n1=0,n2=1,n3,i; | ||
System.out.print(n1+" "+n2);//printing 0 and 1 | ||
|
||
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed | ||
{ | ||
n3=n1+n2; | ||
System.out.print(" "+n3); | ||
n1=n2; | ||
n2=n3; | ||
|
||
} | ||
} | ||
} |
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,42 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
vector<int> frequentK(vector<int> &nums, int k) | ||
{ | ||
int size = nums.size(); | ||
vector<int> ans; | ||
unordered_map<int, int> frequencyCount; | ||
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap; | ||
for (int i = 0; i < size; i++) | ||
{ | ||
frequencyCount[nums[i]]++; | ||
} | ||
for (auto i = frequencyCount.begin(); i != frequencyCount.end(); i++) | ||
{ // first is the number[1,2,3,4] second is the count | ||
// as we want to sort according to count we push second first | ||
// as we have iterator we use arrow dot wont work | ||
minHeap.push({i->second, i->first}); | ||
if (minHeap.size() > k) | ||
{ | ||
minHeap.pop(); | ||
} | ||
} | ||
while (!minHeap.empty()) | ||
{ | ||
// here the second is the min heap second denoting the maps first | ||
ans.push_back(minHeap.top().second); | ||
minHeap.pop(); | ||
} | ||
return ans; | ||
} | ||
int main() | ||
{ | ||
vector<int> nums = {1, 1, 1, 3, 2, 2, 4}; | ||
int k = 2; | ||
vector<int> ans = frequentK(nums, k); | ||
for (auto &it : ans) | ||
{ | ||
cout << it << " "; | ||
} | ||
|
||
return 0; | ||
} |
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,20 @@ | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
string intToRoman(int num) | ||
{ | ||
string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; | ||
string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; | ||
string hrns[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; | ||
string ths[] = {"", "M", "MM", "MMM"}; | ||
|
||
return ths[num / 1000] + hrns[(num % 1000) / 100] + tens[(num % 100) / 10] + ones[num % 10]; | ||
} | ||
int main() | ||
{ | ||
int num = 24; | ||
cout << "Enter the number: "; | ||
cin >> num; | ||
cout << intToRoman(num) << endl; | ||
} |
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,24 @@ | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
int count = 1, res = 0; | ||
HashSet<Integer> set = new HashSet<>(); | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
set.add(nums[i]); | ||
} | ||
|
||
|
||
for(int i = 0; i < nums.length; i++) { | ||
|
||
if(!set.contains(nums[i] - 1)) { | ||
count = 1; | ||
// Total lookups are 2n i.e. twice the size of the hash table. | ||
while(set.contains(nums[i]+ k)) { | ||
count++; | ||
} | ||
} | ||
res = Math.max(res, count); | ||
} | ||
return res; | ||
} | ||
} |
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,42 @@ | ||
import java.util.*; | ||
|
||
|
||
class LongestSubarrayWithGivenSum { | ||
|
||
static int longestSubarray(int n, int[] arr, int sum) { | ||
// We will use prefix sum here | ||
int pre_sum = 0, len = 0; | ||
HashMap<Integer,Integer> map = new HashMap<>(); | ||
|
||
for(int i = 0; i < n; i++) { | ||
pre_sum += arr[i]; | ||
// Condition if prefix sum starts from 0 index | ||
if(pre_sum == sum) len = Math.max(len, i+1); | ||
|
||
if(map.containsKey(pre_sum - sum)) { | ||
len = Math.max(len, i - map.get(pre_sum - sum)); | ||
} | ||
// Only 1st occurence of pre_sum is taken so as to consider longest subarray | ||
if(!map.containsKey(pre_sum)) map.put(pre_sum,i); | ||
|
||
} | ||
return len; | ||
} | ||
|
||
//Driver code | ||
public static void main (String[] args) { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
int sum = sc.nextInt(); | ||
int n = sc.nextInt(); | ||
int arr[] = new int[n]; | ||
|
||
for (int i = 0; i < n; i++) { | ||
arr[i] = sc.nextInt(); | ||
} | ||
System.out.println(longestSubarray(n, arr, sum)); | ||
sc.close(); | ||
} | ||
|
||
|
||
} |
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,28 @@ | ||
import java.util.Scanner; | ||
public class P26 | ||
|
||
{ | ||
public static void main(String[] args) | ||
{ | ||
int i, j, rows; | ||
Scanner sc = new Scanner(System.in); | ||
System.out.print("Enter the number of rows you want to print: "); | ||
rows = sc.nextInt(); | ||
for (i= 0; i<= rows-1; i++) | ||
{ | ||
for (j=0; j<=i; j++) | ||
{ | ||
System.out.print("*"+ " "); | ||
} | ||
System.out.println(""); | ||
} | ||
for (i=rows-1; i>=0; i--) | ||
{ | ||
for(j=0; j <= i-1;j++) | ||
{ | ||
System.out.print("*"+ " "); | ||
} | ||
System.out.println(""); | ||
} | ||
} | ||
} |
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,20 @@ | ||
import java.util.Scanner; | ||
class Palindrome{ | ||
public static void main(String args[]){ | ||
int r,sum=0,temp; | ||
Scanner sc=new Scanner(System.in); | ||
System.out.print("Enter the Count :"); | ||
int n =sc.nextInt();//It is the number variable to be checked for palindrome | ||
|
||
temp=n; | ||
while(n>0){ | ||
r=n%10; //getting remainder | ||
sum=(sum*10)+r; | ||
n=n/10; | ||
} | ||
if(temp==sum) | ||
System.out.println("Palindrome number "); | ||
else | ||
System.out.println("Not palindrome number"); | ||
} | ||
} |
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,26 @@ | ||
import java.util.Scanner; | ||
|
||
public class Pascal | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
int num; | ||
Scanner s = new Scanner(System.in); | ||
|
||
System.out.print("Enter the Row Size of Pascal Triangle: "); | ||
int row = s.nextInt(); | ||
|
||
for(int i=0; i<row; i++) | ||
{ | ||
for(int space=row; space>i; space--) | ||
System.out.print(" "); | ||
num=1; | ||
for(int j=0; j<=i; j++) | ||
{ | ||
System.out.print(num+ " "); | ||
num = num*(i-j)/(j+1); | ||
} | ||
System.out.print("\n"); | ||
} | ||
} | ||
} |
Oops, something went wrong.