No. 22 - Turning Number in an Array

No. 22 - Turning Number in an Array

 

Problem: Turning number is the maximum number in an array whichincreases and then decreases. This kind of array is also named unimodal array.Please write a function which gets the index of the turning number in such anarray.

 

For example, theturning number in array {1, 2, 3, 4, 5, 10, 9, 8, 7, 6} is 10, so its index 5is the expected output.

 

Analysis: As we know, the binary search algorithmis suitable to search a number in a sorted array. Since the input array forthis problem is partially sorted, we may also have a try with binary search.

 

Let us try toget the middle number in an array. The middle number of array {1, 2, 3, 4, 5,10, 9, 8, 7, 6} is 5 (the fourth number). It is greater than its previousnumber 4, and less than its next number 10, so it is in the increasingsub-array. Therefore, numbers before 5 can be discarded in the next round ofsearch.

 

The remainingnumbers for the next round of search are {5, 10, 9, 8, 7, 6}, and the number 9is in the middle of them. Since 9 is less than is previous number 10 andgreater than its next number 8, it is in the decreasing sub-array. Therefore,numbers after 9 can be discarded in the next round of search.

 

The remainingnumbers for the next round of search are {5, 10, 9}, and the number 10 is inthe middle. It is noticeable that number 10 is greater than its previous number5 and greater than its next number 9, so it is the maximum number. That is tosay, the number 10 is the turning number in the input array.

 

We can see theprocess above is actually a classic binary search. Therefore, we can implementthe required function based on binary search algorithm, as listed below:

 

int TurningNumberIndex(int* numbers, int length)

{

    if(numbers == NULL || length <= 2)

        return -1;

 

    int left = 0;

    int right = length - 1;

    while(right > left + 1)

   {

        int middle = (left + right) / 2;

        if(middle == 0 || middle == length - 1)

            return -1;

 

        if(numbers[middle] > numbers[middle - 1] &&

           numbers[middle] > numbers[middle + 1])

            return middle;

        else if(numbers[middle] > numbers[middle - 1] &&

           numbers[middle] < numbers[middle + 1])

           left = middle;

        else

           right = middle;

   }

 

    return -1;

}

 

The author Harry Heowns all the rights of this post. If you are going to use part of or the wholeof this ariticle in your blog or webpages,  please add a reference to   http://codercareer.blogspot.com/ . If you are going to use it in your books,please contact me (zhedahht@gmail.com) . Thanks. 

2023-06-08T02:25:37.583259Z 1 [Note] WSREP: GCache history reset: 00000000-0000-0000-0000-000000000000:0 -> c443b2d8-05a0-11ee-86b8-2e0fddf21737:0 2023-06-08T02:25:39.261528Z WSREP_SST: [INFO] Streaming with xbstream 2023-06-08T02:25:39.273174Z WSREP_SST: [INFO] WARNING: Stale temporary SST directory: /data/mysql//.sst from previous state transfer. Removing 2023-06-08T02:25:39.279749Z WSREP_SST: [INFO] Proceeding with SST......... 2023-06-08T02:25:39.519583Z 0 [Note] WSREP: (c05c7a4e, 'tcp://0.0.0.0:4567') turning message relay requesting off 2023-06-08T02:25:39.553817Z WSREP_SST: [INFO] ............Waiting for SST streaming to complete! 2023-06-08T02:25:49.257301Z WSREP_SST: [ERROR] ******************* FATAL ERROR ********************** 2023-06-08T02:25:49.260159Z WSREP_SST: [ERROR] xtrabackup_checkpoints missing. xtrabackup/SST failed on DONOR. Check DONOR log 2023-06-08T02:25:49.262811Z WSREP_SST: [ERROR] ****************************************************** 2023-06-08T02:25:49.266472Z WSREP_SST: [ERROR] Cleanup after exit with status:2 2023-06-08T02:25:49.289335Z 0 [Warning] WSREP: 1.0 (host78): State transfer to 0.0 (host79) failed: -22 (Invalid argument) 2023-06-08T02:25:49.289400Z 0 [ERROR] WSREP: gcs/src/gcs_group.cpp:gcs_group_handle_join_msg():811: Will never receive state. Need to abort. 2023-06-08T02:25:49.289465Z 0 [Note] WSREP: gcomm: terminating thread 2023-06-08T02:25:49.289494Z 0 [Note] WSREP: gcomm: joining thread 2023-06-08T02:25:49.289662Z 0 [Note] WSREP: gcomm: closing backend 2023-06-08T02:25:49.593055Z 0 [ERROR] WSREP: Process completed with error: wsrep_sst_xtrabackup-v2 --role 'joiner' --address '10.106.113.79' --datadir '/data/mysql/' --defaults-file '/etc/my.cnf' --defaults-group-suffix '' --parent '9996' --mysqld-version '5.7.41-44-57' '' : 2 (No such file or directory) 2023-06-08T02:25:49.593124Z 0 [ERROR] WSREP: Failed to read uuid:seqno from joiner script. 2023-06-08T02:25:49.593137Z 0 [ERROR] WSREP: SST script aborted with error 2 (No such file or directory) 2023-06-08T02:25:49.593186Z 0 [ERROR] WSREP: SST failed: 2 (No such file or directory) 2023-06-08T02:25:49.593234Z 0 [ERROR] Aborting 2023-06-08T02:25:49.593269Z 0 [Note] WSREP: Signalling cancellation of the SST request. 2023-06-08T02:25:49.593306Z 0 [Note] WSREP: SST request was cancelled 2023-06-08T02:25:49.593337Z 0 [Note] Giving 2 client threads a chance to die gracefully 2023-06-08T02:25:49.593357Z 1 [Note] WSREP: Closing send monitor... 2023-06-08T02:25:49.593370Z 1 [Note] WSREP: Closed send monitor. 2023-06-08T02:25:50.292465Z 0 [Note] WSREP: Current view of cluster as seen by this node
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值