DSA Problems for Coding Interviews
DSA Problems for Coding Interviews
DSA
FOR
CRACKING SHEET
INTERNSHIP
ARSH
BY
GOYAL
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
Problem Statement
1. Easy Level-Find the Duplicate Number.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int dupl(vector<int>&num)
{
int n=[Link]();
unordered_map<int,int>m;
for(int i=0;i<n;i++)
{
m[num[i]]++;
if(m[num[i]]>1)
return num[i];
}
return 0;
}
int main()
{
vector<int>num={1,3,4,2,2};
cout<<dupl(num)<<endl;
return 0;
}
Code:
#include <bits/stdc++.h>
using namespace std;
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
{
int lo = 0;
int hi = n - 1;
int mid = 0;
case 0:
swap(a[lo++], a[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(a[mid], a[hi--]);
break;
}
}
}
int main()
{
int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
sort(arr, n);
printArray(arr, n);
return 0;
}
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
return 0;
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
if(col==false)
for(int i=0; i<m; i++)
matrix[i][0]=0;
if(row==false)
for(int j=0; j<n; j++)
matrix[0][j]=0;
}
int main()
{
vector<vector<int>>matrix={{1,1,1},{1,0,1},{1,1,1}};
setZeroes(matrix);
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
}
return 0;
}
int k = 0;
if (A[i] != 0) {
A[k++] = A[i];
}
}
int main(void)
{
int A[] = { 6, 0, 8, 2, 3, 0, 4, 0, 1 };
int n = sizeof(A) / sizeof(A[0]);
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
reorder(A, n);
return 0;
}
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
int n=sizeof(a)/sizeof(a[0]);
int m=3;
cout<<minimumdistribution(a,n,m);
return 0;
}
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
8. Two Sum
Code:
#include <bits/stdc++.h>
#include <iostream>
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]+a[j]==target)
return 0;
int main()
int a[]={2,7,11,15};
int n=sizeof(a)/sizeof(a[0]);
SHIVANI PATEL 8
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
int target=9;
cout<<sumoftwo(a,n,target);
return 0;
#include <bits/stdc++.h>
#include <iostream>
int diff=0;
for(int i=1;i<n;i++)
if(prices[i]>prices[i-1])
diff=diff+prices[i]-prices[i-1];
SHIVANI PATEL 9
DSA Sheet By Arsh
Solution of Array Easy level Problem
shivani patel
return diff;
int main()
int prices[]={7,1,5,3,6,4};
int n=sizeof(prices)/sizeof(prices[0]);
cout<<maxProfit(prices,n);
return 0;
SHIVANI PATEL 10
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
{
m[a[i]]++;
}
for(auto it:m)
{
if([Link]>1)
{
cout<<[Link]<<" ";
}
}
cout<<"\n";
return 0;
}
int main()
{
int a[]={4,3,2,7,8,2,3,1};
int n=sizeof(a)/sizeof(a[0]);
cout<<findalldupl(a,n);
return 0;
}
3. Medium Level-Container With Most Water
Code:
#include <bits/stdc++.h>
#include <iostream>
int maxwater(vector<int>&v)
{
int left=0;
int right=[Link]()-1;
int maxarea=0;
while(left<right){
int area=min(v[left],v[right])*(right-left);
maxarea=max(maxarea,area);
if(v[left]<v[right])
left++;
else
right--;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
}
return maxarea;
}
int main()
{
vector<int>v={1,8,6,2,5,4,8,3,7};
int n=[Link]();
cout<<maxwater(v);
return 0;
}
4. 3Sum (Brute as well as Optimal)
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
have = true;
}
}
}
}*/
bool have = false;
unordered_set<int> s;
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
}
int main()
{
}
5. Medium Level-Maximum Points You Can Obtain from Cards
Code:
#include <bits/stdc++.h>
#include <iostream>
int ans=0;
for(int i=0;i<k;i++){
sum+=a[i];
}
ans=sum;
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
int i=k-1,j=n-1;
while(i>=0 && j>=n-k){
sum-=a[i];
sum+=a[j];
i--;
j--;
ans=max(sum,ans);
}
return ans;
}
int main()
{
int a[]={1,2,3,4,5,6,1};
int n=sizeof(a)/sizeof(a[0]);
int k=3;
cout<<findpoint(a,n,k);
return 0;
}
6. Medium Level-Subarray Sum Equals K
Code:
#include <bits/stdc++.h>
#include <iostream>
int count=0;
unordered_map<int,int>prevSum;
int sum=0;
for(int i=0;i<n;i++){
sum+=nums[i];
if(sum==k)
count++;
if([Link](sum-k)!=[Link]()){
count+=prevSum[sum-k];
}
prevSum[sum]++;
}
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
return count;
}
int main()
{
int nums[]={1,1,1};
int n=sizeof(nums)/sizeof(nums[0]);
int k=2;
cout<<subarraySum(nums,n,k);
return 0;
}
7. Medium Level-Spiral Matrix
Code:
#include <bits/stdc++.h>
#include <iostream>
int T,B,L,R,dir;
T=0;
B=[Link]()-1;
L=0;
R=matrix[0].size()-1;
dir=0;
vector<int>res;
while(T<=B and L<=R)
{
if(dir==0)
{
for(int i=L;i<=R;i++)
res.push_back(matrix[T][i]);
T++;
}
else if(dir==1)
{
for(int i=T;i<=B;i++)
res.push_back(matrix[i][R]);
R--;
}
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
else if(dir==2)
{
for(int i=R;i>=L;i--)
res.push_back(matrix[B][i]);
B--;
}
else if(dir==3)
{
for(int i=B;i>=T;i--)
res.push_back(matrix[i][L]);
L++;
}
dir=(dir+1)%4;
}
return res;
}
int main()
{
vector<vector<int>> matrix{{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
for(int x:spiralOrder(matrix))
{
cout << x << " ";
}
return 0;
}
8. Medium Level-Word Search
Code:
bool dfs(vector<vector<char>>& board, string &word,int i,int j){
//base case
if([Link]()==0) return true;
if(i<0 || j<0 || i>=[Link]() || j>= board[0].size() ||
board[i][j]!=word[0]) return false;
char c = board[i][j];
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
board[i][j] ='X';
string s = [Link](1);
//dfs call
bool res = dfs(board,s,i+1,j)||dfs(board,s,i-
1,j)||dfs(board,s,i,j+1)||dfs(board,s,i,j-1);
//backtrack
board[i][j] =c;
return res;
}
bool exist(vector<vector<char>>& board, string word) {
int m = [Link]();
int n = board[0].size();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(dfs(board,word,i,j)) return true;
}
}
return false;
}
9. Medium Level-Jump Game
Code:
#include <bits/stdc++.h>
#include <iostream>
return false;
reach=max(reach,i+a[i]);
SHIVANI PATEL 8
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
}
return true;
}
int main()
{
int a[]={2,3,1,1,4};
int n=sizeof(a)/sizeof(a[0]);
cout<<canJump(a,n)<<endl;
return 0;
}
10. Medium Level-Merge Sorted Array.
Code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int arr1[] = {1, 3, 5, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int arr3[n1+n2];
mergeArrays(arr1, arr2, n1, n2, arr3);
return 0;
}
11. Medium Level-Majority Element.
Code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int>m;
int n=[Link]();
for(int i=0;i<[Link]();i++)
{
m[nums[i]]++;
if(m[nums[i]]>(n/2))
return nums[i];
}
return 0;
}
int main()
{
SHIVANI PATEL 10
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
vector<int>nums={3,2,3};
int n=[Link]();
cout<<majorityElement(nums);
return 0;
}
12. Medium Level-Reverse Pairs.
Code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
void mergeArray(vector<int> &arr, int low, int mid, int high, int
&cnt)
{
}
};
int main()
{
Solution ob;
vector<int> v = {2,8,7,7,2};
cout << ([Link](v));
}
13. Medium Level-Print all possible combinations of r elements in a
given array of size n.
Code:
#include <bits/stdc++.h>
int data[r];
SHIVANI PATEL 12
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
if (index == r)
return;
if (i >= n)
return;
SHIVANI PATEL 13
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
data[index] = arr[i];
int main()
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCom(arr, n, r);
return 0;
{
return 0;
}
return 1;
}
if(life(board,i-1,j)==1)
{
k++;
}
if(life(board,i,j-1)==1)
{
k++;
}
if(life(board,i+1,j+1)==1)
{
k++;
}
if(life(board,i+1,j)==1)
{
k++;
}
if(life(board,i-1,j-1)==1)
{
k++;
}
if(life(board,i,j+1)==1)
{
k++;
}
if(life(board,i+1,j-1)==1)
{
k++;
}
if(life(board,i-1,j+1)==1)
{
SHIVANI PATEL 15
DSA Sheet By Arsh
Solution of Array Medium Level Problem
Shivani Patel
k++;
}
if(board[i][j]==0 and k==3)
{
return 1;
}
if(board[i][j]==1 and (k==2||k==3))
{
return 1;
}
return 0;
}
void gameOfLife(vector<vector<int>>& board) {
vector<vector<int>>a([Link](),vector<int>(board[0].size(),0));
for(int i=0;i<[Link]();i++){
for(int j=0;j<board[0].size();j++){
a[i][j]=checklive(board,i,j);
}
}
board=a;
}
};
SHIVANI PATEL 16
DSA Sheet By Arsh
Solution Of String Easy Level Problem
shivani patel
//stack st;
for (int i=0;i<[Link]();i++){
if(s[i]=='('||s[i]=='{'||s[i]=='['){
[Link](s[i]);
}
else{
if([Link]()==0) return false;
if(s[i]==')'&& [Link]()=='('||s[i]=='}'&&
[Link]()=='{'||s[i]==']'&&
[Link]()=='['){
[Link]();
}
else {return false;}
}
}
if([Link]()==0) {return true;}
return false;
}
int main()
{
string s="()[]{}";
if(isValid(s))
cout<<"Valid";
else
cout<<"Not valid";
return 0;
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution Of String Easy Level Problem
shivani patel
}
2. Easy Level-Print all the duplicates in the input string.
Code:
#include <bits/stdc++.h>
#include <iostream>
}
}
int main()
{
string s="shivanishivi";
dupl(s);
return 0;
}
3. Easy Level- Implement strStr()
Code:
#include <bits/stdc++.h>
#include <iostream>
shivani patel
int main()
{
string haystack = "hello", needle = "ll";
int res = impstr(haystack, needle);
if (res == -1)
cout << "Not present";
else
cout << "Present at index " << res;
return 0;
}
4. Easy Level- Longest Common Prefix.
Code:
#include <bits/stdc++.h>
#include <iostream>
string longestCommonPrefix(vector<string>& s)
{
if([Link]()==0)
return " ";
else
{
string s1=s[0];
for(int i=1;i<[Link]();i++)
{
for(int j=0;j<[Link]();j++)
{
if(j==s[i].size() or s1[j]!=s[i][j])
{
s1=[Link](0,j);
break;
}
}
}
return s1;
}
}
int main()
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution Of String Easy Level Problem
shivani patel
{
vector<string>s={"flower","flow","flight"};
string res=longestCommonPrefix(s);
cout<<res;
return 0;
}
5. Easy Level- Valid Palindrome II
Code:
#include <bits/stdc++.h>
#include <iostream>
while(start<end)
{
if(s[start]==s[end])
{
start++;
end--;
}
else
return false;
}
return true;
}
bool validPalindrome(string s) {
int start=0;
int end=[Link]()-1;
shivani patel
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
num -= 40;
}
while(num >= 10){
ans += "X";
num -= 10;
}
if(num >= 9){
ans += "IX";
num -= 9;
}
while(num >= 5){
ans += "V";
num -= 5;
}
if(num >= 4){
ans += "IV";
num -= 4;
}
while(num >= 1){
ans += "I";
num -= 1;
}
return ans;
}
int main()
{
int num=3;
cout<<sTonum(num);
return 0;
}
2. Medium Level-Generate Parentheses
Code:
#include <bits/stdc++.h>
using namespace std;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
if(o<n){
generateParenthesis(n, o+1, c, s+"{", ans);
}
if(c<o){
generateParenthesis(n, o, c+1, s+"}", ans);
}
}
int main() {
int n = 3;
vector<string> ans;
generateParenthesis(n,0,0,"",ans);
for(auto s:ans){
cout<<s<<endl;
}
return 0;
}
3. Medium Level-Simplify Path
Code:
#include <bits/stdc++.h>
#include <iostream>
string res;
stack<string>s;
for(int i=0;i<[Link]();i++)
{
if(path[i]=='/')
continue;
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
string tmp;
while(i<[Link]() and path[i]!='/')
{
tmp+=path[i];
i++;
}
if(tmp==".")
continue;
else if(tmp=="..")
{
if(![Link]())
[Link]();
}
else
[Link](tmp);
}
while(![Link]())
{
res="/"+[Link]()+res;
[Link]();
}
if([Link]()==0)
return "/";
return res;
}
int main()
{
string path="/home/";
string p=simplifyPath(path);
cout<<p;
return 0;
}
4. Medium Level-Smallest window in a string containing all the
characters of another string
Code:
#include <bits/stdc++.h>
#include <iostream>
int i=0;
int j=0;
int count = 0;
int reqcount = 0;
string pans;
string ans;
unordered_map<char, int> mp1;
unordered_map<char, int> mp2;
for(int i=0 ; i<[Link]() ; i++){
mp1[p[i]]++;
}
reqcount = [Link]();
while(true){
bool loop1 = false;
bool loop2 = false;
while(i<[Link]() && count<reqcount){
mp2[s[i]]++;
if(mp2[s[i]] <= mp1[s[i]]){
count++;
}
loop1 == true;
i++;
}
while(j<i && count==reqcount){
pans = [Link](j, i-j);
if([Link]() == 0 || [Link]() < [Link]()){
ans = pans;
}
if(mp2[s[j]] == 1){
[Link](s[j]);
}
else{
mp2[s[j]]--;
}
if(mp2[s[j]] < mp1[s[j]]){
count--;
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
}
j++;
loop2 = true;
}
if(loop1 == false && loop2 == false){
break;
}
}
if([Link]() == 0){
return "-1";
}
return ans;
}
int main()
{
string s="timetopractice";
string p="toc";
string r=smallestWindow(s,p);
cout<<r;
return 0;
}
5. Medium Level-Reverse Words in a String
Code:
#include <bits/stdc++.h>
#include <iostream>
} reverse([Link](),[Link]());
ans+=temp; ans+=' ';
}
}
ans.pop_back();
return ans;
}
int main()
{
string s="the sky is blue";
string r=reverse(s);
cout<<r;
return 0;
}
6. Medium Level-Rabin-Karp Algorithm for Pattern Searching
Code:
#include <bits/stdc++.h>
#include <iostream>
#define d 256
using namespace std;
if ( p == t )
{
bool flag = true;
if (j == M)
cout<<"Pattern at index "<< i<<endl;
}
if ( i < N-M )
{
t = (d*(t - txt[i]*h) + txt[i+M])%q;
if (t < 0)
t = (t + q);
}
}
}
SHIVANI PATEL 8
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
int main()
{
char txt[] = "GEEKS FOR GEEKS";
char pat[] = "GEEK";
int q = 101;
vector<vector<string>>res;
unordered_map<string,vector<string>>m;
for(auto it:strs)
{
string curr=it;
sort([Link](),[Link]());
m[curr].push_back(it);
}
for(auto i:m)
res.push_back([Link]);
return res;
}
8. Medium Level-Word Wrap.
Code:
#include <bits/stdc++.h>
using namespace std;
return 0;
if(dp[ind] != -1)
return dp[ind];
int ans = INT_MAX;
int sum = 0 ;
for(int i = ind ; i < n ; i++){
sum += nums[i];
if(sum + (i - ind) <=k){
int cost = 0;
if(i != n - 1){
cost = pow(k - sum - i + ind , 2);
}
ans = min(ans ,cost + solve(i + 1, n , nums , k , dp));
}
}
return dp[ind] = ans;
}
int solveWordWrap(vector<int>nums, int k)
{
// Code here
int n = [Link]() ;
vector<int>dp(n , -1);
return solve(0 , n , nums , k , dp);
}
int main()
{
vector<int>nums={3, 2, 2, 5};
int k=6;
cout<<solveWordWrap(nums,k);
return 0;
}
9. Medium Level-Basic Calculator II
Code:
#include <bits/stdc++.h>
using namespace std;
SHIVANI PATEL 10
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
int calculate(string s) {
s += '+';
stack<int>x;
char sign='+';
int curr=0;
int ans=0;
for(int i=0;i<[Link]();i++)
{
if(isdigit(s[i]))
{
curr=10*curr+(s[i]-'0');
}
else if(s[i]=='+' || s[i]=='-' || s[i]=='*' ||s[i]=='/' )
{
if(sign=='+')
{
[Link](curr);
else if(sign=='-')
{
[Link](-curr);
else if(sign=='*')
{
int a=[Link]();
[Link]();
int b=curr*a;
[Link](b);
else if(sign=='/')
{
int a=[Link]();
[Link]();
SHIVANI PATEL 11
DSA Sheet By Arsh
Solution Of String Medium Problem
shivani patel
int b=a/curr;
[Link](b);
}
curr=0;
sign=s[i];
}
while(![Link]())
{
ans=ans+[Link]();
[Link]();
}
return ans;
}
int main()
{
string s="3+2*2";
cout<<calculate(s);
return 0;
}
SHIVANI PATEL 12
DSA Sheet By Arsh
Solution of Easy Level of Mathematical Problem
Shivani Patel
string res;
int carry=0;
while(n1>=0 || n2>=0)
{
int sum=carry;
if(n1>=0)
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution of Easy Level of Mathematical Problem
Shivani Patel
sum+=a[n1--]-'0';
if(n2>=0)
sum+=b[n2--]-'0';
carry=sum>1?1:0;
res+=to_string(sum%2);
}
if(carry)
res+=to_string(carry);
reverse([Link](),[Link]());
return res;
}
int main()
{
string a="11";
string b="1";
int n1=[Link]()-1;
int n2=[Link]()-1;
cout<<addBinary(a,b,n1,n2);
return 0;
}
3. Easy Level:Maximum Product of Three Numbers.
Code:
#include <bits/stdc++.h>
#include <iostream>
vector<int>nums={1,2,3};
int n=[Link]();
cout<<maxProduct(nums,n);
return 0;
}
#include <bits/stdc++.h>
#include <iostream>
#include <bits/stdc++.h>
#include <iostream>
n=n*n;
}
while(n>9)
{
long long sum=0;
while(n)
{
sum=sum+pow(n%10,2);
n=n/10;
}
n=sum;
}
if(n==1 || n==7)
{
return true;
}
else {
return false;
}
}
int main()
{
int n=19;
if(isHappy(n))
cout<<"Yes";
else
cout<<"No";
return 0;
}
6. Easy Level: Palindrome Number.
Code:
#include <bits/stdc++.h>
#include <iostream>
bool palindrome(int x)
{
int rem,a;
long long int sum=0;
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution of Easy Level of Mathematical Problem
Shivani Patel
a=x;
while(x!=0)
{
rem=x%10;
sum=sum*10+rem;
x=x/10;
}
if(a>=0 and sum==a)
{
return true;
}
return false;
}
int main()
{
int x=121;
if(palindrome(x))
cout<<"True";
else
cout<<"False";
return 0;
}
7. Easy Level : Missing Number.
Code:
#include <bits/stdc++.h>
#include <iostream>
int main()
{
int a[]={3,0,1};
int n=sizeof(a)/sizeof(a[0]);
cout<<missing(a,n);
return 0;
}
8. Easy Level : Reverse Integer.
Code:
#include <bits/stdc++.h>
#include <iostream>
int reverse(int x) {
int rev=0;
while(x!=0)
{
int p=x%10;
x/=10;
if(rev>INT_MAX/10||(rev==INT_MAX/10&&p>7))
return 0;
if(rev<INT_MIN/10||(rev==INT_MIN/10&&p<-8))
return 0;
rev=rev*10+p;
}
return rev;
int main()
{
int x=123;
cout<<reverse(x);
return 0;
}
9. Easy Level : Power of Two
Code:
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution of Easy Level of Mathematical Problem
Shivani Patel
#include <bits/stdc++.h>
#include <iostream>
bool isPowerOfTwo(int n) {
if(n==0)
{
return false;
}
while(n!=0)
{
if(n==1)
return true;
if(n%2!=0)
return false;
else
n=n/2;
}
return true;
}
int main()
{
int n=1;
if(isPowerOfTwo(n))
cout<<"YES";
else
cout<<"NO";
return 0;
}
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
for(int j=i+1;j<m;j++)
if(a[i]+b[j]>=k)
return true;
else
return false;
}
int main()
{
int a[]={2, 1, 3};
int n=sizeof(a)/sizeof(a[0]);
int b[]={7, 8, 9};
int m=sizeof(b)/sizeof(b[0]);
int k=10;
if(permute(a,n,b,m,k))
cout<<"YES";
else
cout<<"NO";
return 0;
}
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
#include <bits/stdc++.h>
#include <iostream>
int i;
return low;
if(a[i] == x)
return i;
return i+1;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
return -1;
int main()
int n=sizeof(a)/sizeof(a[0]);
int x=3;
int p=findceil(a,0,n-1,x);
if(p==-1)
cout<<x;
else
return 0;
#include <bits/stdc++.h>
#include <iostream>
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
int i=0;
int j=1;
cout<<a[i]<<" "<<a[j];
return true;
else if(abs(a[i]-a[j])<diff)
j++;
else
i++;
return false;
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
int main()
int n=sizeof(a)/sizeof(a[0]);
int diff=60;
findpair(a,n,diff);
return 0;
#include<bits/stdc++.h>
using namespace std;
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
int i;
for (i=1; i < n && a[i-1] < a[i]; i++);
if (i == n)
return true;
int j = i;
while (j < n && a[j] < a[j-1])
{
if (i > 1 && a[j] < a[i-2])
return false;
j++;
}
if (j == n)
return true;
int k = j;
int main()
{
int a[] = {1, 3, 4, 10, 9, 8};
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
int n = sizeof(a)/sizeof(a[0]);
checkReverse(a, n)? cout << "Yes" : cout << "No";
return 0;
}
3. Medium Level : Product of Array except itself
Code:
#include <bits/stdc++.h>
using namespace std;
if (n == 1) {
cout << 0;
return;
}
int i, temp = 1;
memset(prod, 1, n);
temp = 1;
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
return;
}
int main()
{
int arr[] = { 10, 3, 5, 6, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
productArray(arr, n);
}
else
o=(a[n/2]+a[(n-2)/2])/2;
int sum=0;
for(int i=0;i<n;i++)
sum+=abs(a[i]-o);
return sum;
}
int main()
SHIVANI PATEL 8
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
{
int a[] = { 1, 100, 101 };
int n = sizeof(a) / sizeof(a[0]);
int left=0,right=[Link]()-1;
while(left<right)
{
int mid=(left+right)/2;
if(nums[mid]>nums[mid+1])
right=mid;
else
left=mid+1;
}
return left;
}
int main()
{
vector<int>nums={1,2,3,1};
int n=[Link]();
cout<<findPeakElement(nums);
return 0;
}
SHIVANI PATEL 9
DSA Sheet By Arsh
Solution Of Easy And Medium Level Problem of Sorting And Searching
shivani patel
SHIVANI PATEL 10
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
Output: [3,4,5]
Output: true
ListNode*slow=head;
ListNode*fast=head;
while(fast!=NULL && fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
if(fast==slow){
return true;
}
}
return false;
}
3. Easy Level : Convert Binary Number in a Linked List to
Integer.
Code:
Output: 5
int num=head->val;
while(head->next!=NULL)
{
num=num*2+head->next->val;
head=head->next;
}
return num;
}
4. Easy Level : Remove Duplicates from Sorted List.
Code:
Input: head = [1,1,2]
Output: [1,2]
if(head==NULL)
return head;
ListNode* tmp=head;
while(tmp->next!=NULL)
{
if(tmp->next->val==tmp->next->val)
tmp->next=tmp->next->next;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
else
tmp=tmp->next;
}
return head;
}
5. Easy Level : Sort a linked list of 0s, 1s and 2s.
Code:
Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL
Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL
Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL
Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL
Output: [1,2,3,4,5]
if(head==NULL)
return NULL;
head->next=removeElements(head->next,val);
if(head->val==val)
return head->next;
return head;
}
7. Easy Level : Merge Two Sorted Lists.
Code:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
ListNode *ans=NULL;
if(!l1)
return l2;
else if(!l2)
return l1;
if(l1->val <= l2->val)
{
ans=l1;
ans->next=mergeTwoLists(l1->next,l2);
}
else
{
ans=l2;
ans->next=mergeTwoLists(l1,l2->next);
}
return ans;
}
8. Easy Level : Multiply two numbers represented by Linked Lists.
Code:
Input : 9->4->6
8->4
Output : 79464
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
Input : 3->2->1
1->2
Output : 3852
if(l1){
num1 = ((num1)*10)%N + l1->data;
l1 = l1->next;
}
if(l2)
{
num2 = ((num2)*10)%N + l2->data;
l2 = l2->next;
}
}
return ((num1%N)*(num2%N))%N;
}
9. Easy Level : Intersection of Two Linked Lists.
Code:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5],
skipA = 2, skipB = 3
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
ListNode* a=headA;
ListNode* b=headB;
while(a!=b)
{
a = a == NULL? headB : a->next;
b = b == NULL ? headA : b->next;
}
return a;
}
10. Easy Level : Given only a pointer/reference to a node to be
deleted in a singly linked list, how do you delete it?
Code:
void deleteNode(Node* node)
{
Node* prev;
if(prev==NULL)
return;
else
{
while(node->next!=NULL)
{
node->data=node->next->data;
prev=node;
node=node->next;
}
prev->next=NULL;
}
}
11. Easy Level : Palindrome Linked List.
Code:
Input: head = [1,2,2,1]
Output: true
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution Of Linked List Easy Level Problem
shivani patel
}
if(fast!=NULL)
slow=slow->next;
while(![Link]() and slow)
{
if([Link]()!=slow->val)
return false;
[Link]();
slow=slow->next;
}
return true;
}
12. Easy Level : Reverse Linked List.
Code:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
Output: [7,0,8]
SHIVANI PATEL 1
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
class Solution {
public:
Node* copyRandomList(Node* head) {
Node *curr=head,*front=head;
while(curr!=NULL)
{
front=curr->next;
Node *copy=new Node(curr->val);
curr->next=copy;
copy->next=front;
curr=front;
}
curr=head;
while(curr!=NULL)
{
if(curr->random!=NULL)
{
curr->next->random=curr->random->next;
}
curr=curr->next->next;
}
curr=head;
Node *dummy=new Node(0);
Node *copy=dummy;
while(curr!=NULL)
{
front=curr->next->next;
copy->next=curr->next;
curr->next=front;
copy=copy->next;
curr=curr->next;
}
return dummy->next;
SHIVANI PATEL 2
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
}
};
3. Medium Level : Add Two Numbers II.
Code:
Output: [7,8,0,7]
if(![Link]())
{
sum+=[Link]();
[Link]();
}
SHIVANI PATEL 3
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
ans->val=sum%10;
sum/=10;
ListNode* head=new ListNode(sum);
head->next=ans;
ans=head;
}
return ans->val==0?ans->next:ans;
}
4. Medium Level : Reverse Linked List II.
Code:
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
return prev;
}
SHIVANI PATEL 4
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
prev = prev->next;
}
tail = prev->next;
for(int i=0; i< right - left;i++){
temp = prev->next;
prev->next = tail->next;
tail->next = tail->next->next;
prev->next->next = temp;
}
return [Link];
}
5. Medium Level : Reorder List.
Code:
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
{
next=curr->next;
curr->next=[Link]();
[Link]();
curr=curr->next;
curr->next=next;
curr=curr->next;
SHIVANI PATEL 5
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
}
curr->next=NULL;
}
6. Medium Level : Remove Nth Node From End of List.
Code :
Output: [1,2,3,5]
SHIVANI PATEL 6
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
Output: [1,3,2]
}
if(![Link]() && head->next == nullptr){
head->next = [Link]();
head->next->prev = head;
[Link]();
}
head = head->next;
}
return final;
}
8. Medium Level : Partition List.
Code:
SHIVANI PATEL 7
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
Output: [1,2,2,4,3,5]
while(head!=NULL)
{
if(head->val<x)
{
small->next=head;
small=small->next;
}
else
{
high->next=head;
high=high->next;
}
head=head->next;
}
high->next=NULL;
small->next=high_head->next;
return small_head->next;
}
9. Medium Level : Remove Duplicates from Sorted List II.
Code:
Output: [1,2,5]
return NULL;
unordered_map<int,int>m;
ListNode* tmp=head;
while(tmp)
{
m[tmp->val]++;
tmp=tmp->next;
}
ListNode* ans=new ListNode(-1);
ListNode* tmp2=ans;
for(auto i:m)
{
if([Link]==1)
temp2->next = new ListNode([Link]);
temp2 = temp2->next;
}
return ans->next;
}
10. Medium Level: Rearrange a Linked List in Zig-Zag fashion
Code:
Input: 1->2->3->4
Output: 1->3->2->4
Explanation : 1 and 3 should come first before 2 and 4 in
zig-zag fashion, So resultant linked-list will be 1->3->2-
>4.
Input: 11->15->20->5->10
Output: 11->20->5->15->10
SHIVANI PATEL 9
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
{
swap(head->data,head->next->data);
return zigzag(head->next,!flag);
}
}
else {
if (head->data < head->next->data)
swap(head->data, head->next->data);
return zigzag(head->next, !flag);
}
}
11. Medium Level: Sort List.
Code:
Output: [1,2,3,4]
SHIVANI PATEL 10
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
Code:
Input: 17->15->8->12->10->5->4->1->7->6->NULL
Output: 8->12->10->4->6->17->15->5->1->7->NULL
Input: 8->12->10->5->4->1->6->NULL
Output: 8->12->10->4->6->5->1->NULL
SHIVANI PATEL 11
DSA Sheet By Arsh
Solution of Linked List Medium Level Problem
shivani patel
{
if (head == NULL)
return;
while (curr) {
prev = curr->next;
if (!curr->next)
break;
curr = curr->next->next;
}
}
SHIVANI PATEL 12