Skip to content

Instantly share code, notes, and snippets.

@asutosh97
Last active August 13, 2018 12:36
Show Gist options
  • Save asutosh97/1e93975afc0474b4cb908983de848a06 to your computer and use it in GitHub Desktop.
Save asutosh97/1e93975afc0474b4cb908983de848a06 to your computer and use it in GitHub Desktop.
Questions asked in HSBC placement drive coding round

1. Given an array and its length, return 1 if its possible if its possible to split the array at any index such that all the elements in the left half are strictly decreasing and all elements in right half are strictly increasing

#include <iostream>
using namespace std;

int isSplitPossible(int arr[], int len) {
	bool increased = false;
	prev = arr[0];

	for(int i = 0; i < len; i++) {
		if (arr[i] == prev)
			return 0;
		else if (!increased && arr[i] > prev) {
			increased = true;
		} 
		else if (arr[i] < prev) {
			return 0;
		}
		prev = arr[i];
	}
	return 1;
}

int main() {
	int n;
	cin >> n;
	int arr[n];
	for (int i = 0; i < n; i++)
		cin >> arr[i];
	cout << isSplitPossible(arr, n) << endl;
	return 0;
}

2. Remove Consecutive Duplicate elements recursively

#include <iostream>
#include <deque>
#define MAX 100
using namespace std;


char* removeDuplicates(char *str) {
	deque<char> s;
	
	while(*str != '\0') {
		if (!s.empty() && s.back() == *str) {
			char ch = *str;
			s.pop_back();
			while(*str == ch)
				str++;
		}
		else {
			s.push_back(*str);
			str++;
		}
	}

	char *ans = (char *) malloc(sizeof(char) * MAX);
	int ptr = 0;
	while (!s.empty()) {
		ans[ptr++]= s.front();
		s.pop_front();
	}
	
	ans[ptr] = '\0';
	return ans;
}

int main() {
	char str[MAX];
	cin >> str;
	cout << removeDuplicates(str) << endl;
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment