Skip to content

ARRAYS #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Arrays/Break Words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
You are given a sentence contained in given string S. Write a function which will replace all the words within the sentence whose length is even and greater than equal to 4, with a space between the two equal halves of the word.
Space complexity should be O(1).
Input Format :

String S

Output Format :

Updated string

Constraints :
1 <= Length of S <= 10000
Sample Input :

Helloo world good morniing

Sample Output :

Hel loo world go od morn iing


*/

#include <bits/stdc++.h>
using namespace std;

void splitWord(string &word){
int mid=word.size()/2;
word.insert(mid," ");
}

void breakWords(char* S)
{
/*
* Don't write main.
* Don't return or print anything.
* Changes should be done in the given string.
*/
string ans="";
string word="";
for(int i=0;S[i]!='\0';i++){

if(S[i]!=' ' && S[i]!='\0')
word+=S[i];
else{
if (word.size()>=4 && word.size()%2==0){
splitWord(word);
}
ans+=word+" ";
word="";
}
}
strcpy(S,ans.c_str());

}


#include<iostream>
#include<string.h>
using namespace std;
#include"solution.h"
int main()
{
char str[100000];
cin.getline(str,100000);
breakWords(str);
cout<<str;
}
64 changes: 64 additions & 0 deletions Arrays/Check_Permuations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Given two strings, check if they are permutations of each other. Return true or false.
Permutation means - length of both the strings should same and should contain same set of characters. Order of characters doesn't matter.
Note : Input strings contain only lowercase english alphabets.
Input format :

Line 1 : String 1
Line 2 : String 2

Sample Input 1 :

abcde
baedc

Sample Output 1 :

true

Sample Input 2 :

abc
cbd

Sample Output 2 :

false


*/

// input1 - first input string
// input2 - second input string
#include <bits/stdc++.h>
using namespace std;

bool isPermutation(char input1[], char input2[]) {
// Write your code here
int size1=strlen(input1),size2=strlen(input2);
sort(input1,input1+size1);
sort(input2,input2+size2);

if(!strcmp(input1,input2))
return true;
else
return false;

}

#include <iostream>
using namespace std;
#include "solution.h"

int main() {
char input1[1000], input2[1000];
cin.getline(input1, 1000);
cin.getline(input2, 1000);
if(isPermutation(input1, input2) == 1) {
cout << "true";
}
else {
cout << "false";
}
}

64 changes: 64 additions & 0 deletions Arrays/Compress_the_String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.
For e.g. if a String has 'x' repeated 5 times, replace this "xxxxx" with "x5".
Note : Consecutive count of every character in input string is less than equal to 9.
Input Format :

Input string S

Output Format :

Compressed string

Sample Input:

aaabbccdsa

Sample Output:

a3b2c2dsa


*/

// input - given string
// You need to update in the given string itself i.e. in input. No need to return or print.
#include <bits/stdc++.h>
using namespace std;

void stringCompression(char input[]) {
// Write your code here
string ans="";

for(int i=0;input[i]!=0;){
int j=i+1;
while(input[i]==input[j]){
j++;
}
int c=j-i;
//cout<<input[i]<<endl;
//cout<<char(48+c)<<endl;
//cout<<ans<<endl;
char cd='0'+c;
if (c!=0 && c!=1){
ans+=input[i];
ans+=(char)(cd);
}
else
ans+=input[i];
i=j;
}
strcpy(input,ans.c_str());

}

#include <iostream>
#include "solution.h"
using namespace std;

int main() {
char input[1000];
cin.getline(input, 1000);
stringCompression(input);
cout << input << endl;
}
86 changes: 86 additions & 0 deletions Arrays/Count Platforms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Given two arrays (both of size n), one containing arrival time of trains and other containing the corresponding trains departure time (in the form of an integer) respectively. Return the minimum number of platform required, such that no train waits.
Arrival and departure time of a train can't be same.
Input Format :

Line 1: Integer n, number of elements in arrival and departure array
Line 2: Elements of Arrival Array (separated by spaces).
Line 3: Elements of Departure Array (separated by spaces).

Output Format:

Minimum Number of Platform Needed

Constraints :
1 <= n <= 100
Sample Input 1 :

6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000

Sample Output 1 :

3

Sample Input 2 :

4
1100 1101 1103 1105
1110 1102 1104 1106

Sample Output 2 :

2

*/

#include <bits/stdc++.h>
using namespace std;
int platformsNeeded(int arrival[], int departure[], int n) {
/*
* Don't write main().
* Don't read input, it is passed as function argument.
* Don't print anything just return integer value.
*/
int maxc=1;
int c=1;
int i=0,j=0;

sort(arrival,arrival+n);
sort(departure,departure+n);

while(i<n && j<n){
if(arrival[i]<departure[j]){
i++;
c++;
}
else{
j++;
c--;
maxc=max(maxc,c);
}
}
return maxc;
}

#include<iostream>
using namespace std;
#include"solution.h"

int main()
{
int n;
cin>>n;
int* arr=new int[n];
int* dep=new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
cin>>dep[i];
}
cout<< platformsNeeded(arr, dep, n);
}
40 changes: 40 additions & 0 deletions Arrays/Find_Duplicate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;
#include "solution.h"


// arr - input array
// size - size of array

int MissingNumber(int arr[], int size){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int sum=(size*(size-1))/2;

int calsum=0;
for(int i=0;i<size;i++)
calsum+=arr[i];
int k=sum-calsum;
return size-1-k;


}


int main() {
int size;
cin >> size;
int *input = new int[1 + size];

for(int i = 0; i < size; i++)
cin >> input[i];

cout << MissingNumber(input, size);

delete [] input;

return 0;
}
56 changes: 56 additions & 0 deletions Arrays/Find_an_element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
using namespace std;


void findElement(int input[][100], int m, int n, int x) {
int i = 0, j = n - 1;
while(i < m && j >= 0) {
if(input[i][j] == x) {
cout << i << " " << j << endl;
return;
}
else if(x > input[i][j]) {
i++;
}
else {
j--;
}
}
cout << "-1" << endl;
}

void printArray(int input[][100], int m, int n) {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cout << input[i][j] << " ";
}
cout << endl;
}

}

int main() {
int a[100][100];
int m, n;

// Rows - m
// Cols - n
cin >> m >> n;


// Taking input
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cin >> a[i][j];
}
}

int x;
cin >> x;

findElement(a, m, n, x);

//printArray(a, m, n);

}

Loading