update and print array element?

difference array range update
c program to update an element in an array
replace every array element by multiplication of previous and next
how to update an array in python
update value of list
multiply each element of an array by a number in c
c program to replace an element in an array

You are given an array of N positive integers, A1, A2 ,…, AN.

Also, given a Q updates of form:- i j: Update Ai = j. 1 ≤ i ≤ N.

Perform all updates and after each such update report mode of the array. Therefore, return an array of Q elements, where ith element is mode of the array after ith update has been executed.

Notes

• Mode is the most frequently occuring element on the array.
• If multiple modes are possible, return the smallest one.
• Update array input is via a Q*2 array, where each row represents a update.

For example,

```A=[2, 2, 2, 3, 3]

[ [5, 4] ]
[ [2, 4] ]

A = [3, 2, 2, 3, 3] after 1st update.
3 is mode.

A = [3, 2, 2, 3, 4] after 2nd update.
2 and 3 both are mode. Return smaller i.e. 2.

A = [3, 4, 2, 3, 4] after 3rd update.
3 and 4 both are mode. Return smaller i.e. 3.

Return array [3, 2, 3].
Constraints
1 ≤ N, Q ≤ 105
1 ≤ j, Ai ≤ 109
```

What's wrong with my implementation using simple array?

```  /**
* @input A : Integer array
* @input n1 : Integer array's ( A ) length
* @input B : 2D integer array
* @input n21 : Integer array's ( B ) rows
* @input n22 : Integer array's ( B ) columns
*
* @Output Integer array. You need to malloc memory, and fill the length in len1
*/
int* getMode(int* A, int n1, int** B, int n21, int n22, int *len1) {
int *ans = (int *) malloc (sizeof(int)); // dynamic allocation of array
int x = 0,i,j;
for(i = 0; i < n1 ; i ++ )
for(j = 0; j < n21; j++)
A[B[j][0]] = B[j][1];
// frequency calculate

int c[n1] ;

for(i = 0; i < n1; i++)
c[i] = 0;

for(i = 0; i < n1; i++)
c[A[i]]++;

int mx = INT_MAX;
int idx = -1, idx1 = -1;

for(i = 0; i < n1; i++){
if (mx  < A[i]){
idx1 = idx;
mx = A[i];
idx = i;
}

int p;
if (A[idx]> A[idx1])
p = A[idx];
else
p = A[idx1];

ans[x++] = p; }

return ans; }
```

I am using TreeMap within TreeMap, one TreeMap is for storing modes in sorted order and the other TreeMap is for their frequencies in sorted order.

```public class Solution {
HashMap<Integer, Integer> map;
TreeMap<Integer, TreeMap<Integer, Integer>> map2;
public ArrayList<Integer> getMode(ArrayList<Integer> A, ArrayList<ArrayList<Integer>> B) {
map = new HashMap<Integer, Integer>();
map2 = new TreeMap<>();
ArrayList<Integer> result = new ArrayList<Integer>();

for(int i : A) {
if(map.containsKey(i))
map.put(i, map.get(i) +1);
else
map.put(i, 1);
}
for (Map.Entry m : map.entrySet()) {
int freq = (int)m.getValue();
TreeMap<Integer, Integer> x = new TreeMap<>();
if (map2.containsKey(freq) == true) {
x = map2.get(freq);
}
x.put((int)m.getKey(),1);
map2.put(freq, x);
}
for(ArrayList<Integer> update : B){
int index = update.get(0);
int num = update.get(1);

int toUpdate = A.get(index - 1);

if(map.get(toUpdate) != null) {
int freq = map.get(toUpdate);
TreeMap<Integer, Integer> temp = map2.get(freq);
if (temp.size() == 1) {
map2.remove(freq);
}
else {
temp.remove(toUpdate);
map2.put(freq, temp);
}
if (freq == 1) {
map.remove(toUpdate);
}
else {
map.put(toUpdate, freq - 1);
int z = freq-1;
TreeMap<Integer, Integer> tt = new TreeMap<>();
if (map2.containsKey(z)) {
tt = map2.get(z);
}
tt.put(toUpdate, 1);
map2.put(z, tt);
}
}
A.set(index - 1, num);
if(map.containsKey(num)) {
int tt = map.get(num);
TreeMap<Integer, Integer> w = map2.get(tt);
if (w.size() == 1) {
map2.remove(tt);
}
else {
w.remove(num);
map2.put(tt, w);
}
map.put(num, tt+1);
TreeMap<Integer, Integer> q = new TreeMap<>();
if (map2.containsKey(tt+1)) {
q = map2.get(tt+1);
}
q.put(num,1);
map2.put(tt+1, q);
}
else {
map.put(num, 1);
TreeMap<Integer, Integer> qq = new TreeMap<>();
if (map2.containsKey(1)) {
qq = map2.get(1);
}
qq.put(num, 1);
map2.put(1, qq);
}
int rr = (int)map2.lastKey();
TreeMap<Integer, Integer> xyz = map2.get(rr);
}
return result;
}
}
```

C exercises: Update every array element with multiplication of , Write a program in C to update every array element with multiplication of previous and next numbers in array. Sample Sample Output: Update − Updates an element at the given index. Array is created in Python by importing array module to the python program. Then the array is declared as shown eblow. from array import * arrayName = array(typecode, [Initializers])

```class compare
{
public:
bool operator()(pair<int,int> p1,pair<int,int> p2)
{
if(p1.first==p2.first)
return p1.second>p2.second;
return p1.first<p2.first;
}
};
vector<int> Solution::getMode(vector<int> &a, vector<vector<int> > &b) {
map<int,int> m;// map store the exact count of elements
int n=a.size();
for(int i=0;i<n;i++)
{
m[a[i]]++;
}
vector<int> ans;

// take a max heap which store element count and its value
priority_queue<pair<int,int> ,vector<pair<int,int> >,compare >pq;
for(int i=0;i<n;i++)
{
pq.push(make_pair(m[a[i]],a[i]));
}
int i=0,q=b.size();
while(i<q)
{
int index=b[i][0]-1;
int val=b[i][1];
int val1=a[index];
a[index]=val;
if(val!=val1)
{
m[val1]--;
m[val]++;
pq.push(make_pair(m[val1],val1));
pq.push(make_pair(m[val],val));
}
while(true)
{
pair<int,int> t=pq.top();
int cnt=t.first;int vall=t.second;
if(m[vall]==cnt)
{

// when top of heap has cnt same as in map then this the answer for the ith query
ans.push_back(vall);
break;
}
else
pq.pop();
}
i++;
}
return ans;
}
```

Difference Array, Consider an array A[] of integers and following two types of queries. update(l, r, x) : Adds x to all values from A[l] to A[r] (both inclusive). printArray() : Prints the  In the above program, since each element in array contains another array, just using Arrays.toString() prints the address of the elements (nested array). To get the numbers from the inner array, we just another function Arrays.deepToString(). This gets us the numbers 1, 2 and so on, we are looking for.

Java Solution:Updating the element and using a hashMap to obtain mode after every updation. (Not optimal though)

```public int[] getMode(int[] a, int[][] b) {
int res[]=new int[b.length];
int j=0;
for(int i=0;i<b.length;i++)
{
int index=b[i][0];
int element=b[i][1];
a[index-1]=element;
int m=mode(a);
res[j]=m;
j++;
}
return res;
}
public int mode(int[] a)
{
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
int max=0,m=0;
for(int i=0;i<a.length;i++)
{
if(hm.containsKey(a[i]))
{
hm.put(a[i],hm.get(a[i])+1);
}
else
{
hm.put(a[i],1);
}
if(hm.get(a[i])>max)
{
max=hm.get(a[i]);
m=a[i];
}
else if(hm.get(a[i])==max)
{
m=(m<a[i])?m:a[i];
}
}
return m;
}
```

Replace every array element by multiplication of previous and next , Given an array of integers, update every element with multiplication of previous Input: arr[] = {2, 3, 4, 5, 6} Output: arr[] = {6, 8, 15, 24, 30} // We get the above  Write a C program to read an array of length 6, change the first element by the last, the second element by the fifth and the third element by the fourth. Print the elements of the modified array.

```// this is classic implementation of LRU cache , I have used hash map and priority queue (heap to sort by frequency)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

class Node implements Comparable<Node> {
Integer key;
Integer count;

public Node(Integer key) {
this.key = key;
this.count = 1;

}

@Override
public int compareTo(Node a) {
return count != a.count ? a.count - count : key - a.key;
}

}

public class Ninja1 {
public static void main(String[] args) {
Ninja1 n = new Ninja1();
ArrayList<ArrayList<Integer>> b = new ArrayList<ArrayList<Integer>>();
System.out.println(n.getMode(new ArrayList<Integer>(Arrays.asList(2, 2, 2, 3, 3)), b));

}

public ArrayList<Integer> getMode(ArrayList<Integer> a, ArrayList<ArrayList<Integer>> b) {
Queue<Node> q = new PriorityQueue<Node>();
Map<Integer, Node> map = new HashMap<Integer, Node>();
ArrayList<Integer> sol = new ArrayList<Integer>();

Node temp = null;
for (Integer i : a) {
temp = map.get(i);
if (temp == null) {
Node n = new Node(i);
map.put(i, n);
} else {
q.remove(temp);
temp.count = temp.count + 1;
}
}

for (List<Integer> i : b) {
if (a.get(i.get(0) - 1) != i.get(1)) {
temp = map.get(a.get(i.get(0) - 1));
q.remove(temp);

if (temp.count != 1) {
temp.count = temp.count - 1;
}

temp = map.get(i.get(1));
if (temp == null) {
Node n = new Node(i.get(1));
map.put(i.get(1), n);
} else {
q.remove(temp);
temp.count = temp.count + 1;
}

}
}
return sol;
}
}
```

Python Arrays, This tutorials explain how to create an array, add/update, index, remove, and slice. Print array values using indices print("1st array element is {} at index 0. At some point I need to add some new elements to the array, but first I need to check if element with the same name already exists. If yes, the value must be updated. If no, the new element must be created and added. If the value in new element equals zero and the element exists, it must be removed from the array.

```My answer is in C++
int getModeHash(unordered_map<int, int> &map)
{
int maxCount = 0;
int maxCountNum = INT_MIN;
for ( auto it = map.begin(); it != map.end(); ++it)
{
if(maxCount <= it->second)
{
if(maxCount == it->second)
{
maxCountNum = min(maxCountNum, it->first);
}
else
{
maxCountNum = it->first;
maxCount = it->second;
}
}
}
return maxCountNum;
}
vector<int> Solution::getMode(vector<int> &A, vector<vector<int> > &B) {
unordered_map<int, int> map;
vector<int> result;

//Store A array elements count in hash i.e.. map
for(int i=0; i< A.size(); i++)
{
if(map.find(A[i]) == map.end())
{
map[A[i]] = 0;
}
map[A[i]]++;
}
// now update hash map for each element in B,
//and get large count element
for(int i=0; i < B.size(); i++)
{
map[A[B[i][0]-1]]--;
if(map[A[B[i][0]-1]] == 0)
{
map.erase(A[B[i][0]-1]);
}
A[B[i][0]-1] = B[i][1];
if(map.find(A[B[i][0]-1]) == map.end())
{
map[A[B[i][0]-1]] = 0;
}
map[A[B[i][0]-1]]++;

result.push_back(getModeHash(map));
}
return result;
}
```

Storing Data in Arrays, name and the index inside parentheses, then use the assignment operator (=) followed by a value. You can update the in exact element array in MongoDB with the help of below statement. The syntax is as follows: {"yourArrayDocumentName.\$.yourNestedArrayDocument.yourPosition":"yourValue"}}); To understand the above syntax, let us create a collection with some documents. The query to create a collection with document is as follows:

Program to print array in C, How do you replace an element in an array in Java? C Array: Exercise-1 with Solution. Write a program in C to store elements in an array and print it. Pictorial Presentation: Sample Solution: C Code:

How to replace an element of ArrayList in Java?, We need to declare & define one array and then loop upto the length of array. At each iteration we shall print one index value of array. We can take this index value  In this post, we will see how to print contents of an array in C++. 1. Simple solution would be to iterate over the elements of an array and print each element. 2. std::copy 3. Range based for loop 4. Iterators for printing arrays. 5. std::for_each..

C Program to update an array element at specified position, C Array - Part 1 | C Language Tutorial | Mr. Srinivas - Duration: 17:33. Naresh i Technologies Duration: 6:55 Posted: Dec 5, 2018 Given an array of integers, update every element with multiplication of previous and next elements with following exceptions. a) First element is replaced by multiplication of first and second. b) Last element is replaced by multiplication of last and second last. Example: