Finding largest farthest increasing pairs in array?

Is there a simple linear algorithm for finding in an array A two such elements that $ A[i] > A[j]$ and $ i – j$ is as large as possible? If one wants to actually return the indices, then linear memory usage seems impossible to avoid. If the only result needed is the value $ i – j$ , i.e. the distance, can we get away with less than linear memory?

Obviously, one can use constant memory if one is willing to loop over all pairs (i, j).

Postgres- update a value in a array on particular index

I have a column named ids that is of type bigint[]. ids cane be null or of any length

It has values based on certain hierarchy.

My use case is I want to update value that is at a particular index. For example ids[2] = 50.

How can I achieve this?

I looked at this but couldn’t find anything that could help.

Thanks in advance.

wordpress output numbers from an array of checkboxes

I’m not a php expert at all, but I’ve just started to learn it. I’m using a function on wordpress which creates a xml file after pressing send button on a contact form (with contact form 7). That’s my code for the moment:

add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );  function CF7_pre_send($  cf7) {     $  output = "";     $  output .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";     $  output .= "<cf>" . $  _POST['codicefiscale'] . "</cf>";     $  output .= "<cognome>" . $  _POST['cognome'] . "</cognome>";      .......      $  output .= "<aree_tematiche>";     $  output .= "<area><macroarea>1</macroarea><subarea>" . implode(', ', $  _POST['area1']) . </subarea></area>";       $  output .= "</aree_tematiche>";     .......     file_put_contents("wp-content/uploads/cf7outputtest.xml", $  output); } 

Which is a really simple code. I’d like that if the user selects on the frontend a checkbox, his selection is displayed as a number and not with the entire string (as I’m doing now with the implode function). Furthermore if the user selects 2 checkboxes, I’d like that in the xml file the text is displayed as a loop like that, assuming that he has selected for instance checkbox 3 and 5:

<aree_tematiche>             begin of the loop  <area>                       first element of the loop, which shows checkbox n. 3 <macroarea></macroarea> <subarea>3</subarea>          </area>  <area>                       first element of the loop, which shows checkbox n. 5 <macroarea></macroarea> <subarea>5</subarea>          </area>  </aree_tematiche>            end of the loop 

So basically I want that based on my selection is displayed the selection integer number and not the string! Thanks, Luca

Algorithm for splitting an array into k sub-arrays

We want implement a data structure that have the following methods: Init(A,k)- Gets an array A with n different values and initialize our data structure that so it will divide A into k equally sized sub-arrays (+-1), that each value in the i’th sub-array will be larger than any value in the (i-1)’th sub-array, and smaller than any value in the (i+1)’th sub-array. This method need to be applied in complexity of O(n log k).

Insert(x)- Gets a value x which isn’t in our data structure, and adds it. This method need to be applied in complexity of O(k logn).

I did the init method using Medians- ofMedians QuickSelect, by dividing the array into k’ sub arrays when k’ equals to the closest power of 2 for k, and then I adjusted my pointers to the dividers by using Select on the smaller arrays which added me only O(n).

With the Insert part I’m having some trouble and would appreciate any help, Thanks:)

Linear Partition problem (Dynamic Programming) in a circular array

I am practicing algorithms for the last couple of days and I came across this question, the gist of which is:

Given apples and oranges arranged in a circle indexed form 1 to n , where n is an odd number(we know which ones apple and which ones orange), divide these into k contiguous groups each havind odd number of fruits such that most of the groups have more apples than oranges. Also, the arrangement can be such that a group can contain fruits of indices (n-3, n-2, n-1, n, 1 , 2, 3).

This appears like the linear partition problem to me, but the circular arrangement confuses me. Also, I was thinking of masking Apples as 1 and Oranges as -1 so that it’s easy to calculate the which one is higher in the group( if sum is +ve, then apples are higher else oranges). Also, I observed that k must be an odd number as n is an odd and each of the k groups have odd fruits, so sum of odd number of odds is an odd number.

We have to maximize the sum of each of the groups in this case right?

It would be great if someone can help out.

Thanks a lot!!!

How to wiggle sort an array in linear time complexity?

The wiggle sort is nums[0]nums[2]nums[4]…

For an input: nums = [1, 5, 1, 1, 6, 4], the expected output is [1, 4, 1, 5, 1, 6] and there can be many other possible outputs satisfying the aforementioned criteria.

I realised that the problem has a pattern: nums[1] will be greatest among nums[0:3], nums[3] will be greatest among nums[3:6],…

So, I targetted at getting the next greatest element. This made me implement the heap:

import heapq   class Solution:     def wiggleSort(self, nums: List[int]) -> None:         """         Do not return anything, modify nums in-place instead.         """         nums_heap = []         for num in nums:             heapq.heappush(nums_heap, -1*num)         i = 1         while i < len(nums):             nums[i] = -1 * heapq.heappop(nums_heap)             i += 2         i = 0         while i<len(nums):             nums[i] = -1 * heapq.heappop(nums_heap)             i += 2  

However, the time complexity of my solution is O(n) along with O(n) space complexity. I want to solve this in O(1) space complexity and that would require me to not use heap(extra space).

How to do that?

The complete question is also posted here.

Minimum steps to sort array

Consider you have a permutation of $ 1$ to $ n$ in an array $ array$ . Now select three distinct indices $ i$ ,$ j$ ,$ k$ , there is no need to be sorted. Let $ array_i$ , $ array_j$ and $ array_k$ be the values at those indices and now you make a right shift to it, that is $ new$ $ array_i$ = $ old$ $ array_j$ and $ new$ $ array_j$ = $ old$ $ array_k$ and $ new$ $ array_k$ =$ old$ $ array_i$ . Find the minimum number of operations required to sort the array or if is impossible how to determine it.

Example : Consider $ array= [3,1,2]$ ; consider indices $ (1,3,2)$ in the given order after applying one operation it is $ s =[1,3,2]$ .

Can anybody share your approach.

Find combination of vectors from array that sum up to s

I have an array of $ n$ $ m$ -dimensional vectors (in my case, they’re 27 dimensional). I also have an $ m$ -dimensional vector $ s$ . I want to find all combinations of $ k$ vectors from my array whose vector sum is equal to $ s$ . How to do this efficiently?

The best I could do is just a brute force which is $ O(n^k)$ and impossibly slow.

Any help is appreciated.