-
Notifications
You must be signed in to change notification settings - Fork 47
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 #36 from Acejoy/main
Added the Solution To Check whether a String is Palindrome or not, Find the triplet that sum to a given value, Find median in a row wise sorted matrix
- Loading branch information
Showing
3 changed files
with
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Problem link : https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1 | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution{ | ||
public: | ||
// function to find the triplet which sum to x | ||
// arr[] : The input Array | ||
// N : Size of the Array | ||
// X : Sum which you need to search for | ||
|
||
bool find3Numbers(int arr[], int N, int X) | ||
{ | ||
//Your Code Here | ||
|
||
int Sum = 0; | ||
sort(arr, arr+N); | ||
//printArray(arr); | ||
|
||
for(int i=0; i<N-2; i++){ | ||
|
||
int leftPtr = i+1; | ||
int rightPtr = N-1; | ||
|
||
while(leftPtr<rightPtr) { | ||
|
||
Sum = arr[rightPtr] + arr[leftPtr] +arr[i]; | ||
//printf("leftptr:%d ,rightptr:%d, partial sum:%d\n",leftPtr, rightPtr, Sum); | ||
if(Sum == X) { | ||
return true; | ||
} else if( Sum > X) { | ||
rightPtr--; | ||
} else { | ||
leftPtr++; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
}; | ||
|
||
// n = 5, X = 10 | ||
// arr[] = [1 2 4 3 6] | ||
// Output: | ||
// 1 | ||
// Explanation: | ||
// The triplet {1, 3, 6} in | ||
// the array sums up to 10. | ||
|
||
int main() | ||
{ | ||
int N=5,sum=10; | ||
int i,A[N] = {1,2,4,3,6}; | ||
|
||
Solution ob; | ||
cout << ob.find3Numbers(A, N, sum) << endl; | ||
|
||
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,64 @@ | ||
// { Driver Code Starts | ||
//Initial template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
class Solution{ | ||
public: | ||
int median(vector<vector<int>> &matrix, int r, int c){ | ||
// code here | ||
int minElement = INT_MAX; | ||
int maxELement = INT_MIN; | ||
|
||
for(int i=0; i<r; i++) { | ||
minElement = min(minElement, matrix[i][0]); | ||
maxELement = max(maxELement, matrix[i][c-1]); | ||
} | ||
|
||
int desiredElements = (r*c+1)/2; | ||
|
||
while(minElement < maxELement) { | ||
|
||
int currentElements = 0; | ||
int mid = minElement + (maxELement- minElement)/2; | ||
|
||
for(int i=0; i<r; i++) { | ||
currentElements += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin(); | ||
} | ||
|
||
if(currentElements< desiredElements) { | ||
minElement = mid+1; | ||
} else { | ||
maxELement = mid; | ||
} | ||
} | ||
|
||
return minElement; | ||
} | ||
}; | ||
|
||
// Input: | ||
// R = 3, C = 3 | ||
// M = [[1, 3, 5], | ||
// [2, 6, 9], | ||
// [3, 6, 9]] | ||
|
||
// Output: 5 | ||
|
||
// Explanation: | ||
// Sorting matrix elements gives us | ||
// {1,2,3,3,5,6,6,9,9}. Hence, 5 is median. | ||
|
||
int main() | ||
{ | ||
int r=3, c=3; | ||
vector<vector<int>> matrix(r, vector<int>(c)); | ||
matrix[0] = {1,3,5}; | ||
matrix[1] = {2,6,9}; | ||
matrix[2] = {3,6,9}; | ||
Solution obj; | ||
cout<<obj.median(matrix, r, c)<<endl; | ||
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,46 @@ | ||
// Problem link : https://practice.geeksforgeeks.org/problems/palindrome-string0817/1 | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution{ | ||
public: | ||
|
||
|
||
int isPalindrome(string S) | ||
{ | ||
// Your code goes here | ||
int isPalin=1; | ||
|
||
int firstIndex = 0; | ||
int lastIndex = S.length()-1; | ||
|
||
while(firstIndex<lastIndex){ | ||
if(S[firstIndex]!=S[lastIndex]){ | ||
isPalin=0; | ||
break; | ||
} else { | ||
firstIndex++; | ||
lastIndex--; | ||
} | ||
} | ||
|
||
return isPalin; | ||
} | ||
|
||
}; | ||
|
||
int main(){ | ||
|
||
Solution s; | ||
|
||
string demoString = "abba"; | ||
int res = s.isPalindrome(demoString); | ||
if(res==1){ | ||
cout<<"String is Palindrome\n"; | ||
} else { | ||
cout<<"String is not Palindrome\n"; | ||
} | ||
|
||
return 0; | ||
} |