Find a sorted subsequence of size 3 three in linear time
来源:互联网 发布:java 发送邮件带图片 编辑:程序博客网 时间:2024/05/29 16:37
Given an array of n integers, find the 3 elements such that a[i] < a[j] < a[k] and i < j < k in 0(n) time. If there are multiple such triplets, then print any one of them.
Examples:Input: arr[] = {12, 11, 10, 5, 6, 2, 30}Output: 5, 6, 30Input: arr[] = {1, 2, 3, 4}Output: 1, 2, 3 OR 1, 2, 4 OR 2, 3, 4Input: arr[] = {4, 3, 2, 1}Output: No such triplet
Solution:
1) Create an auxiliary array smaller[0..n-1]. smaller[i] should store the index of a number which is smaller than arr[i] and is on left side of arr[i]. smaller[i] should contain -1 if there is no such element.
2) Create another auxiliary array greater[0..n-1]. greater[i] should store the index of a number which is greater than arr[i] and is on right side of arr[i]. greater[i] should contain -1 if there is no such element.
3) Finally traverse both smaller[] and greater[] and find the index i for which both smaller[i] and greater[i] are not -1.
// A function to fund a sorted subsequence of size 3void find3Numbers(int arr[], int n){ int max = n-1; //Index of maximum element from right side int min = 0; //Index of minimum element from left side int i; // Create an array that will store index of a smaller // element on left side. If there is no smaller element // on left side, then smaller[i] will be -1. int *smaller = new int[n]; smaller[0] = -1; // first entry will always be -1 for (i = 1; i < n; i++) { if (arr[i] <= arr[min]) { min = i; smaller[i] = -1; } else smaller[i] = min; } // Create another array that will store index of a // greater element on right side. If there is no greater // element on right side, then greater[i] will be -1. int *greater = new int[n]; greater[n-1] = -1; // last entry will always be -1 for (i = n-2; i >= 0; i--) { if (arr[i] >= arr[max]) { max = i; greater[i] = -1; } else greater[i] = max; } // Now find a number which has both a greater number on // right side and smaller number on left side for (i = 0; i < n; i++) { if (smaller[i] != -1 && greater[i] != -1) { printf("%d %d %d", arr[smaller[i]], arr[i], arr[greater[i]]); return; } } // If we reach number, then there are no such 3 numbers printf("No such triplet found"); // Free the dynamically alloced memory to avoid memory leak delete [] smaller; delete [] greater; return;}
- Find a sorted subsequence of size 3 three in linear time
- Find common elements in three sorted arrays
- ...find the size of a file?
- The Program of Algorithms ------- Sorting in Linear Time---- Counting Sort
- The Program of Algorithms ------- Sorting in Linear Time---- Bucket Sort
- Identifying Hierarchical Structure in Sequences: A linear-time algorithm 1997
- Find missing number in a sorted array
- Selection in expected linear time
- In Three month's time
- Introduction to Algorithm - Summary of Chapter 8 - Sorting in Linear Time
- Find the Minimum Element in A sorted and Rotated Array
- PIQ12: Find pivot in a sorted rotated array
- Selection in expected linear time O(n)
- Fastest Gaussian Blur (in linear time)
- Fastest Gaussian Blur (in linear time)
- How to find size of array in C/C++ without using sizeof ?
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- Find common characters in a set of strings
- 文件流
- 《C++程序设计案例教程》笔记之多态
- VMware Workstation 11序列号
- Normal Distribute
- 目前制作游戏太忙,没时间做spritekit教程,暂时搁浅
- Find a sorted subsequence of size 3 three in linear time
- java中date,string,timestamp,时间戳的相互转换大全
- Android 使用createBitmap时出现异常 width and height must be > 0
- ios 团队开发-UITabbarController整合多个storyboard的controllers
- 开源、高效、跨平台:深剖Google FlatBuffers工作原理
- Java之JDBC
- [LeetCode-206] Reverse Linked List(链表反转)
- 生产者和消费者信号灯实现 N-1
- 欢迎使用CSDN-markdown编辑器