From c8863e33f26d4a349f90917008ddf7d6287b843a Mon Sep 17 00:00:00 2001 From: tanishk-agarwal <88491381+tanishk-agarwal@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:28:30 +0530 Subject: [PATCH] chore(C): add maximum difference in an Array (#603) --- algorithms/C/README.md | 13 +++++- algorithms/C/arrays/maximum-difference.c | 56 ++++++++++++++++++++++ algorithms/C/arrays/reverse-array.c | 59 ++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 algorithms/C/arrays/maximum-difference.c create mode 100644 algorithms/C/arrays/reverse-array.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 140bdd49..8d19b2b5 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -1,19 +1,24 @@ # C ## Arrays + - [Even and Odd](arrays/even-and-odd.c) - [Unique Elements in an array](arrays/unique-elements-in-an-array.c) +- [Reverse an array](arrays/reverse-array.c) +- [Maximum difference](arrays/maximum-difference.c) ## Bit Manipulation + - [Add and Subtract](bit-manipulation/add-and-sub-bitwise.c) - [Multiply](bit-manipulation/multiply-bitwise.c) - [Divide bitwise](bit-manipulation/divide-bitwise.c) - ## Graphs + - [Prim's Algorithm](graphs/Prim's-algorithm.c) ## Linked Lists + - [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c) - [Josephus Problem](linked-lists/josephus-problem.c) - [Circular Linked List](linked-lists/Insert-and-del-beginning-circular-ll.c) @@ -23,12 +28,15 @@ - [Glued-Linked-List](linked-lists/gl-threads.c) ## Maths + - [Palindrome Number](maths/palindrome.c) ## Queues + - [Double Ended Queue using array](queues/double-ended-queue-using-array.c) ## Sorting + - [Bubble Sort](sorting/bubble-sort.c) - [Merge Sort](sorting/merge-sort.c) - [Insertion Sort](sorting/insertion-sort.c) @@ -36,12 +44,14 @@ - [Selection Sort](sorting/selection-sort.c) ## Strings + - [Count Words](strings/count-words.c) - [Palindrome](strings/palindrome.c) - [Permutation of String](strings/Permutation-of-String.c) - [Longest Common Subsequence](strings/longest-common-subsequence.c) ## Tree + - [Height Of Tree](tree/height-of-a-tree.c) - [Max and Min Element Of Tree](tree/min-and-max-of-tree.c) - [Binary Search Tree](tree/binary-search-tree.c) @@ -50,6 +60,7 @@ - [Max Heap](tree/max-heap.c) ## Searching + - [Binary Search](searching/Binary-search.c) - [Jump Search](searching/Jump-search.c) - [Ternary Search](searching/Ternary-search.c) diff --git a/algorithms/C/arrays/maximum-difference.c b/algorithms/C/arrays/maximum-difference.c new file mode 100644 index 00000000..efd8b908 --- /dev/null +++ b/algorithms/C/arrays/maximum-difference.c @@ -0,0 +1,56 @@ +/* PROBLEM: Given an array arr[] of integers, find out the maximum difference between any two elements such that larger element appears after the smaller number. */ + +#include + +/* The function assumes that there are at least two +elements in array. +The function returns a negative value if the array is +sorted in decreasing order. +Returns 0 if elements are equal */ +int maxDiff(int arr[], int arr_size) +{ + int max_diff = arr[1] - arr[0]; + int min_element = arr[0]; + int i; + for (i = 1; i < arr_size; i++) + { + if (arr[i] - min_element > max_diff) + max_diff = arr[i] - min_element; + if (arr[i] < min_element) + min_element = arr[i]; + } + return max_diff; +} + +int main() +{ + int n; + printf("Enter n : "); // n is number of elements in the array + scanf("%d", &n); + int arr[n]; + if(n<2){ + printf("Invalid Input!!!"); + return 0; + } + for (int i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + printf("Maximum difference is %d\n", maxDiff(arr, n)); + getchar(); + return 0; +} + +/* +Case 1: +Input: Enter size: 5 + 1 2 3 4 5 +Output: Maximum difference is 4 + +Case 2: +Input: Enter size: 6 + 7 9 5 6 3 2 +Output: Maximum difference is 5 + +Time complexity: O(N) where N is the size of array +*/ diff --git a/algorithms/C/arrays/reverse-array.c b/algorithms/C/arrays/reverse-array.c new file mode 100644 index 00000000..438afc56 --- /dev/null +++ b/algorithms/C/arrays/reverse-array.c @@ -0,0 +1,59 @@ +/*PROBLEM:Given an array, of size n, reverse it. */ + +#include + +/* Function to reverse arr[] from start to end*/ +void rvereseArray(int arr[], int start, int end) +{ + int temp; + while (start < end) + { + temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* Function to print out an array on a line */ +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + printf("%d ", arr[i]); + + printf("\n"); +} + +int main() +{ + int n; + printf("Enter n : "); //n is number of elements in the array + scanf("%d", &n); + int arr[n]; + for (int i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + rvereseArray(arr, 0, n - 1); + printf("Reversed array is \n"); + printArray(arr, n); + return 0; +} + +/* +Case 1: +Input: Enter size: 5 + 1 2 3 4 5 +Output: Reversed array is + 5 4 3 2 1 + +Case 2: +Input: Enter size: 7 + 1 13 15 20 12 13 2 +Output: Reversed array is + 2 13 12 20 15 13 1 + +Time complexity: O(N) where N is the size of array +*/