Skip to content

Commit 7f6f344

Browse files
committed
solved consecutiveCheckArray
1 parent 7c1aa6b commit 7f6f344

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/************************************************
14+
/* Check if numbers in array are consecutive
15+
/************************************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
bool consecutiveCheckArray(int* arr,int len);
23+
void someSortingAlgorithm(int* arr,int length);
24+
25+
int main(){
26+
// Lines to handle user input go here, be descriptive...
27+
// use cin, cout
28+
int arr[] = {5, 2, 3, 1, 4};
29+
int arr2[] = {34, 23, 52, 12, 3};
30+
31+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
32+
int array2Length = sizeof(arr2)/sizeof(arr2[0]);
33+
34+
35+
// Call your algorithms here
36+
cout<<consecutiveCheckArray(arr, arrayLength)<<endl;
37+
cout<<consecutiveCheckArray(arr2, array2Length)<<endl;
38+
39+
40+
return 0;
41+
}
42+
43+
/**********************************************************
44+
/* <consecutiveCheckArray>
45+
/*
46+
/* @description : description here
47+
/* @param : Integer<variable> (Datatype<variable_name>)
48+
/* @return : datatype (int,float,double etc)
49+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
50+
/* @explanation : Detailed description about what the algorithm does
51+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
52+
/**********************************************************/
53+
bool consecutiveCheckArray(int* arr,int len){
54+
// Algorithm content goes here.
55+
someSortingAlgorithm(arr,len);
56+
57+
int minElement =arr[0];
58+
59+
for(int i=0;i<len;i++){
60+
if(arr[i]==minElement){
61+
minElement++;
62+
}
63+
else{
64+
return false;
65+
}
66+
}
67+
return true;
68+
}
69+
70+
void someSortingAlgorithm(int* arr,int length){
71+
72+
for(int i=0;i<length;i++){
73+
for(int j=i+1;j<length;j++){
74+
if(arr[i]>arr[j]){
75+
int temp = arr[i];
76+
arr[i] = arr[j];
77+
arr[j] = temp;
78+
}
79+
}
80+
}
81+
}

Arrays and Strings/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@
8686
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
8787

8888

89-
#### 13. [Check if array elements are consecutive (consecutiveCheckArray))](C++/consecutiveCheckArray.cpp)
89+
#### 14. [Check if array elements are consecutive (consecutiveCheckArray))](C++/consecutiveCheckArray.cpp)
9090

91-
Given an unsorted array of numbers, write a function that returns true if array consists of consecutive numbers
92-
a) If array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
93-
b) If array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive numbers from 78 to 83.
94-
c) If the array is {34, 23, 52, 12, 3 }, then the function should return false because the elements are not consecutive.
91+
Given an unsorted array, write an algorithm to check if the given array has consecutive elements or not, must return true if it does, false if it doesn't.
92+
93+
A ={5, 2, 3, 1, 4}, must return true.
94+
A ={83, 78, 80, 81, 79, 82}, must return true
95+
A ={34, 23, 52, 12, 3 } must return false;
9596

9697

9798
TODO :

0 commit comments

Comments
 (0)