Adding Ones | Practice | GeeksforGeeks

You start with an array A of size N. Also, A[i] = 0 for i = 1 to N. You will be given K positive integers. Let j be one of these integers, you have to add 1 to all A[i], for i ≥ j. Your task is to print array A after all these K updates are done.

Example 1: 
Input:
3 4
1 1 2 3
Output:
2 3 4
Explanation:
Initially the array is {0, 0, 0}. After the
first 1, it becomes {1, 1, 1}. After the
second 1 it becomes {2, 2, 2}. After 2, it 
becomes {2, 3, 3}. After 3 it becomes, {2, 3, 4}. 
 
Example 2: 
Input:
2 3
1 1 1
Output:
3 3 
Explanation:
Initially the array is {0, 0}. After the
first 1, it becomes {1, 1}. After the
second 1 it becomes {2, 2}. After the
third 1, it becomes {3, 3}.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function update() which takes the array A[] and its size N as inputs and make the updates and fill the array A[].
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N, K ≤ 105
0
19003001002372 days ago
class Solution{   public:   void update(int a[], int n, int updates[], int k)   {       // Your code goes here       for(int i=0;i<k;i++)       {           a[updates[i] - 1]++;       }       for(int i=1;i<n;i++)       {           a[i] = a[i] + a[i-1];       }   }}; 
0
arvind16yadav2 weeks ago
/*JAVA CODE (TIME : 1.1 / 2.7)*/
public static void update(int a[], int n, int updates[], int k)   {       // Your code goes here       for(int x : updates){           ++a[x-1];        }              int sum = a[0];       for(int i=1; i<n; i++){           a[i] += sum;           sum = a[i];       }   }
0
anirbanhaldar6003 weeks ago
Very simple TWO lines solution C++
 
void update(int a[], int n, int u[], int k){
    for(int i = 0; i < k; i++) a[u[i]-1]++;
    for(int i = 1; i < n; i++) a[i] += a[i-1];
}
0
dangrio2 months ago
//Simple two liner solution
for(int j=0;j<k;j++) a[updates[j]-1]++;for(int i=1;i<n;i++) a[i]+=a[i-1];
0
kumardiv20012 months ago
// can anyone explain me why is this solution showing me run time //error(segmentaiton fault)
class Solution{   public:   void update(int a[], int n, int updates[], int k)   {       // Your code goes here   while(updates[k] != '/0'){   for(int i = 0; i < n; i++){       a[i] += updates[k];   }   updates++;   }   }};
0
mohituniyal462 months ago
here is a simple implementation
 
class Solution{
    public:
    void update(int a[], int n, int updates[], int k)
    {
        // Your code goes here
        for(int i=0;i<k;i++){
            a[updates[i]-1]++;
        }
        int prevSum=0;
        for(int i=0;i<n;i++){
            a[i]+=prevSum;
            prevSum=a[i];
        }
    }
};
+1
9986610789csr4 months ago
for(int j=0;j<k;j++)           for(int i=updates[j]-1;i<n;i++)a[i]+=1;
 
what can i modifie to optimize my code
0
c1phani1simha6 months ago
class Solution{   public:   void update(int a[], int n, int updates[], int k)   {for(int i=0;i<k;i++)     {         int num=updates[i];         for(;num-1<n;(num)++)         {             a[num-1]=a[num-1]+1;         }     }   }};
+1
vivekpatel87700328877 months ago
//easy problem using hashing
void update(int a[],int n,int updates[],int k)
{
   
    for(int i=0;i<k;i++)
        a[updates[i]-1]++;
   for(int i=1;i<n;i++)
       a[i]=a[i]+a[i-1];
     
}
0
aniketsingh27220017 months ago
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.