-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Palindrome_array.dart
79 lines (65 loc) · 1.33 KB
/
Palindrome_array.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Check wether the array is Palindrome by using DART.
Given an array of integers, check wether the array is Palindrome or not.
*/
import 'dart:io';
int palindrome_array(var array, int len) {
int flag = 0;
for (int i = 0; i <= len ~/ 2; i++) {
if (array[i] != array[len - i - 1]) {
flag = 1;
break;
}
}
return flag;
}
void main() {
var data = [];
// taking input from user and storing it in data array
var n, ele;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
for (int i = 1; i <= n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
data.add(ele);
}
print("\nInputted array is $data");
int ans = palindrome_array(data, n);
if (ans == 1)
print("Array is not a Palindrome");
else
print("Array is a Palindrome");
}
/*
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
Example 1:
Enter the number of Elements:
3
Enter Element 1:
1
Enter Element 2:
2
Enter Element 3:
3
Inputted array is [1, 2, 3]
Array is not a Palindrome
Example 2:
Enter the number of Elements:
5
Enter Element 1:
1
Enter Element 2:
2
Enter Element 3:
3
Enter Element 4:
2
Enter Element 5:
1
Inputted array is [1, 2, 3, 2, 1]
Array is a Palindrome
*/