Skip to content

Commit

Permalink
Merge pull request #36 from Acejoy/main
Browse files Browse the repository at this point in the history
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
parteek10 authored Oct 2, 2021
2 parents f4c31b0 + 53df8bc commit 459f364
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Array/27.cpp
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;
}
64 changes: 64 additions & 0 deletions Matrix/2.cpp
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;
}
46 changes: 46 additions & 0 deletions String/1.cpp
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;
}

0 comments on commit 459f364

Please sign in to comment.