chore(C): add maximum difference in an Array (#603)

pull/599/head^2
tanishk-agarwal 2021-10-21 18:28:30 +05:30 committed by GitHub
parent cb60762c39
commit c8863e33f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 1 deletions

View File

@ -1,19 +1,24 @@
# C # C
## Arrays ## Arrays
- [Even and Odd](arrays/even-and-odd.c) - [Even and Odd](arrays/even-and-odd.c)
- [Unique Elements in an array](arrays/unique-elements-in-an-array.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 ## Bit Manipulation
- [Add and Subtract](bit-manipulation/add-and-sub-bitwise.c) - [Add and Subtract](bit-manipulation/add-and-sub-bitwise.c)
- [Multiply](bit-manipulation/multiply-bitwise.c) - [Multiply](bit-manipulation/multiply-bitwise.c)
- [Divide bitwise](bit-manipulation/divide-bitwise.c) - [Divide bitwise](bit-manipulation/divide-bitwise.c)
## Graphs ## Graphs
- [Prim's Algorithm](graphs/Prim's-algorithm.c) - [Prim's Algorithm](graphs/Prim's-algorithm.c)
## Linked Lists ## Linked Lists
- [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c) - [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c)
- [Josephus Problem](linked-lists/josephus-problem.c) - [Josephus Problem](linked-lists/josephus-problem.c)
- [Circular Linked List](linked-lists/Insert-and-del-beginning-circular-ll.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) - [Glued-Linked-List](linked-lists/gl-threads.c)
## Maths ## Maths
- [Palindrome Number](maths/palindrome.c) - [Palindrome Number](maths/palindrome.c)
## Queues ## Queues
- [Double Ended Queue using array](queues/double-ended-queue-using-array.c) - [Double Ended Queue using array](queues/double-ended-queue-using-array.c)
## Sorting ## Sorting
- [Bubble Sort](sorting/bubble-sort.c) - [Bubble Sort](sorting/bubble-sort.c)
- [Merge Sort](sorting/merge-sort.c) - [Merge Sort](sorting/merge-sort.c)
- [Insertion Sort](sorting/insertion-sort.c) - [Insertion Sort](sorting/insertion-sort.c)
@ -36,12 +44,14 @@
- [Selection Sort](sorting/selection-sort.c) - [Selection Sort](sorting/selection-sort.c)
## Strings ## Strings
- [Count Words](strings/count-words.c) - [Count Words](strings/count-words.c)
- [Palindrome](strings/palindrome.c) - [Palindrome](strings/palindrome.c)
- [Permutation of String](strings/Permutation-of-String.c) - [Permutation of String](strings/Permutation-of-String.c)
- [Longest Common Subsequence](strings/longest-common-subsequence.c) - [Longest Common Subsequence](strings/longest-common-subsequence.c)
## Tree ## Tree
- [Height Of Tree](tree/height-of-a-tree.c) - [Height Of Tree](tree/height-of-a-tree.c)
- [Max and Min Element Of Tree](tree/min-and-max-of-tree.c) - [Max and Min Element Of Tree](tree/min-and-max-of-tree.c)
- [Binary Search Tree](tree/binary-search-tree.c) - [Binary Search Tree](tree/binary-search-tree.c)
@ -50,6 +60,7 @@
- [Max Heap](tree/max-heap.c) - [Max Heap](tree/max-heap.c)
## Searching ## Searching
- [Binary Search](searching/Binary-search.c) - [Binary Search](searching/Binary-search.c)
- [Jump Search](searching/Jump-search.c) - [Jump Search](searching/Jump-search.c)
- [Ternary Search](searching/Ternary-search.c) - [Ternary Search](searching/Ternary-search.c)

View File

@ -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 <stdio.h>
/* 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
*/

View File

@ -0,0 +1,59 @@
/*PROBLEM:Given an array, of size n, reverse it. */
#include <stdio.h>
/* 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
*/