I wrote a stack implementation yes, but it still comes down to an algorithm.
My stack main alg. has too much conditional logic. Instead I have crafted this simple loop.
It is close I think. It fails on the last test. It is counting 9-7-2 twice for some reason.
I will continue the stack solution after if this current try is not possible.
Code:
#include <iostream>
using namespace std;
int main(){
int start = 0;
int array_size = 9;
int end = array_size - 1;
//int array[array_size] = {7,7,2}; //pass 1
//int array[array_size] = {2,7,7}; //pass 1
//int array[array_size] = {2}; //pass 1
//int array[array_size] = {2,7}; //pass 2
//int array[array_size] = {2,7,4}; //pass 3
//int array[array_size] = {2,1,2,1}; //pass 4
//int array[array_size] = {2,1,2,1,2}; //pass 5
//int array[array_size] = {2,1,2,1,2,1}; //pass 6
//int array[array_size] = {2,2,2}; //pass 0
//int array[array_size] = {5,1,5}; //pass 3
int array[array_size] = {2,7,5,5,7,7,7,9,2}; //fail 5
int singleton_count = 0;
for (int i=0; i<=end; i++){
for (int j=i; j>=start && array[j-1] !=array[j] && array[j+1] !=array[j]; j--){
cout << array[j] << " " << array[j-1] << " " << array[j+1] << endl;
singleton_count += 1;
start += 1;
}
}
cout << "there are "<< singleton_count << " singletons" << endl;
return 0;
}