# Help Me In Increasing Array CSES

**URL:** <https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248>\
**Category:** help\
**Created:** [May 26, 2021, 7:00pm UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248 "2021-05-26T19:00:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 26, 2021, 7:00pm UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/1 "2021-05-26T19:00:41Z")

</div>

Please Help Me In `Increasing Array` CSES I Am Not Able To Understand The Question  
Question Link: [CSES - Increasing Array](https://cses.fi/problemset/task/1094)  
Thanks In Advance  
Krishna Wadhwani

---

<div class="post-metadata">

**Author:** ![cubefreak777](https://discuss.codechef.com/user_avatar/discuss.codechef.com/cubefreak777/32/215911_2.png) [@cubefreak777](https://discuss.codechef.com/u/cubefreak777)\
**Post date:** [May 26, 2021, 7:49pm UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/2 "2021-05-26T19:49:34Z")

</div>

You have to make the array increasing i.e. modify the array such that A\_i \le A\_{i+1} \ \forall \ 1 \le i \<N using a minimum number of operations and an operation means adding 1 to any element of the array, for example, consider the sample given in the problem.  
Initial array:  
A = 3 2 5 1 7  
Array obtained after modifications  
A= 3 3 5 5 7  
Here we performed 1 operation on 2nd element, 4 operations on 4th element and 0 operations on all other elements so total number of operations = 1+4=5 and it can be proven that this is the minimum number of operations that you need to perform.

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 3:36am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/3 "2021-05-27T03:36:52Z")

</div>

Thanks @cubefreak777

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 4:10am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/4 "2021-05-27T04:10:17Z")

</div>

[https://cses.fi/paste/bd6c241d1bb5736121e3b2/](https://cses.fi/paste/bd6c241d1bb5736121e3b2/)  
Its Showing Wrong Answer On Some Test Cases

 ![Screenshot from 2021-05-27 09-44-33](https://s3.amazonaws.com/discourseproduction/original/3X/8/d/8d093c5619270009fe38b25c25feefc62bc1733e.png)

---

<div class="post-metadata">

**Author:** ![sudo\_s](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sudo_s/32/24810_2.png) [@sudo\_s](https://discuss.codechef.com/u/sudo_s)\
**Post date:** [May 27, 2021, 4:33am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/5 "2021-05-27T04:33:42Z")

</div>

checkout this test case-

```auto
5
5 1 2 1 3
your output = 5
required output = 13

```

Hint:- use the fact that max(v[0],v[i])\<=v[i] 🙂

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 27, 2021, 5:07am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/6 "2021-05-27T05:07:48Z")

</div>

What went wrong?

```auto
if(a[i - 1] >= a[i]) {
    count += a[i - 1] - a[i];
    a[i] = a[i - 1]; // Additional statement
}

```

What could have gone wrong?

```auto
int count = 0;
long count = 0; // Correction

```

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 5:21am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/7 "2021-05-27T05:21:21Z")

</div>

### Test details

#### Test 1

Verdict: ACCEPTED

input  
10  
1 1 1 1 1 1 1 1 1 1

[view](https://cses.fi/file/b3d27753d290a4ce2e844ac6e8c1a172df6c1426314089a1db2afb7ad2ceb68c/1/1/) [save](https://cses.fi/file/b3d27753d290a4ce2e844ac6e8c1a172df6c1426314089a1db2afb7ad2ceb68c/1/2/)

correct output  
0

[view](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/1/) [save](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/2/)

user output  
0

[view](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/3/1/) [save](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/3/2/)

#### Test 2

Verdict: WRONG ANSWER

input  
10  
1000000000 1 1 1 1 1 1 1 1 1

[view](https://cses.fi/file/ea19372f2dd9bba0816ff0822ef8fa7cb73fba0db53228b2d65b14edfddbd1f2/1/1/) [save](https://cses.fi/file/ea19372f2dd9bba0816ff0822ef8fa7cb73fba0db53228b2d65b14edfddbd1f2/1/2/)

correct output  
8999999991

[view](https://cses.fi/file/2e3cee4f478d2a820e83cd5f744034d7805b662bc4a47ef0b5690ff68eb140eb/2/1/) [save](https://cses.fi/file/2e3cee4f478d2a820e83cd5f744034d7805b662bc4a47ef0b5690ff68eb140eb/2/2/)

user output  
999999999

[view](https://cses.fi/file/7fb189f5b2832d64c6668c6569b1860cd7d30734b63f50bbd064be5d4e817ae2/3/1/) [save](https://cses.fi/file/7fb189f5b2832d64c6668c6569b1860cd7d30734b63f50bbd064be5d4e817ae2/3/2/)

#### Test 3

Verdict: WRONG ANSWER

input  
10  
6 10 4 10 2 8 9 2 7 7

[view](https://cses.fi/file/dd7d45caea8ec8b5c1b53df2f6e8b52932a67e913568caec14f7697821b5da81/1/1/) [save](https://cses.fi/file/dd7d45caea8ec8b5c1b53df2f6e8b52932a67e913568caec14f7697821b5da81/1/2/)

correct output  
31

[view](https://cses.fi/file/32c2643e0dc65524c9f1f6f9f00937322fd68d59986bc381d9ff2285d23e353d/2/1/) [save](https://cses.fi/file/32c2643e0dc65524c9f1f6f9f00937322fd68d59986bc381d9ff2285d23e353d/2/2/)

user output  
21

[view](https://cses.fi/file/6e2ae11dad0616f66bbb2b6e6556f580bb987fd911d7132aa6bee2bfc7cc7b52/3/1/) [save](https://cses.fi/file/6e2ae11dad0616f66bbb2b6e6556f580bb987fd911d7132aa6bee2bfc7cc7b52/3/2/)

#### Test 4

Verdict: ACCEPTED

input  
200000  
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 …

[view](https://cses.fi/file/19d069a1577bf0ca04ba13c31dabeb8de05f9e8d9fdc6548ed48df252403f3ca/1/1/) [save](https://cses.fi/file/19d069a1577bf0ca04ba13c31dabeb8de05f9e8d9fdc6548ed48df252403f3ca/1/2/)

correct output  
0

[view](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/1/) [save](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/2/)

user output  
0

[view](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/3/1/) [save](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/3/2/)

#### Test 5

Verdict: WRONG ANSWER

input  
200000  
1000000000 1 1 1 1 1 1 1 1 1 1…

[view](https://cses.fi/file/ba92435d1dd4f068936f0631993c17fd29d159fbcf78cc130e490cd4bc088315/1/1/) [save](https://cses.fi/file/ba92435d1dd4f068936f0631993c17fd29d159fbcf78cc130e490cd4bc088315/1/2/)

correct output  
199998999800001

[view](https://cses.fi/file/9d6b94767fc3a8fe244b7a1b2f024c014dc9725942800ed22676ea1b9818d605/2/1/) [save](https://cses.fi/file/9d6b94767fc3a8fe244b7a1b2f024c014dc9725942800ed22676ea1b9818d605/2/2/)

user output  
999999999

[view](https://cses.fi/file/7fb189f5b2832d64c6668c6569b1860cd7d30734b63f50bbd064be5d4e817ae2/3/1/) [save](https://cses.fi/file/7fb189f5b2832d64c6668c6569b1860cd7d30734b63f50bbd064be5d4e817ae2/3/2/)

#### Test 6

Verdict: WRONG ANSWER

input  
200000  
763977854 530688450 486447012 …

[view](https://cses.fi/file/a6ae5bb865adda449c3f22d3a5529d791dd7ea922515cf92f764f3d0a7f3c7e1/1/1/) [save](https://cses.fi/file/a6ae5bb865adda449c3f22d3a5529d791dd7ea922515cf92f764f3d0a7f3c7e1/1/2/)

correct output  
100126194120455

[view](https://cses.fi/file/b1a0faf822d72fc95d271b7c2886c859270560e0b77e78298ace03535f087b96/2/1/) [save](https://cses.fi/file/b1a0faf822d72fc95d271b7c2886c859270560e0b77e78298ace03535f087b96/2/2/)

user output  
165085756

[view](https://cses.fi/file/039936485403d925d11ddc048d880d9141b71d203797d091cbf811433a541e03/3/1/) [save](https://cses.fi/file/039936485403d925d11ddc048d880d9141b71d203797d091cbf811433a541e03/3/2/)

#### Test 7

Verdict: ACCEPTED

input  
1  
329873232

[view](https://cses.fi/file/145db7dcfbd1478bc0725d339e047f0c8dced9ae49db8dd543e98e004331575e/1/1/) [save](https://cses.fi/file/145db7dcfbd1478bc0725d339e047f0c8dced9ae49db8dd543e98e004331575e/1/2/)

correct output  
0

[view](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/1/) [save](https://cses.fi/file/9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa/2/2/)

user output  
0

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 5:22am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/8 "2021-05-27T05:22:33Z")

</div>

I have pasted testcases please check

---

<div class="post-metadata">

**Author:** ![bharat81091](https://discuss.codechef.com/user_avatar/discuss.codechef.com/bharat81091/32/24644_2.png) [@bharat81091](https://discuss.codechef.com/u/bharat81091)\
**Post date:** [May 27, 2021, 5:35am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/9 "2021-05-27T05:35:15Z")

</div>

brother if you put this logic then you got the worng ans… bcz if you test for suppose 42531

then 4-2=2  
then 2\<5 condition false  
then 5-3=2  
then here is logic goes wrong if we see there is condition true  
3\>1 then 3-1=2  
then the totle of cases is 6  
but if see go according to que… it must be 8 bcz after increment array become 44555  
so we have to substract it by the incremented value …

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 27, 2021, 5:42am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/10 "2021-05-27T05:42:26Z")

</div>

You might want to paste your code as well.

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 5:51am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/11 "2021-05-27T05:51:28Z")

</div>

Link To Code: [https://cses.fi/paste/bd6c241d1bb5736121e3b2/](https://discuss.codechef.com/external-redirect/?url=https://cses.fi/paste/bd6c241d1bb5736121e3b2/)

```auto
import java.util.Scanner;
 
public class Test {
    public static void main(String[] args){
        Scanner scin = new Scanner(System.in);
        int t = scin.nextInt();
        int[] a = new int[t];
        int count = 0;
        for (int i = 0 ; i < t ; i++){
            int k = scin.nextInt();
            a[i]=k;
        }
        for (int i = 1; i < t; i++){
            if (a[i-1]>=a[i]){
                count+=a[i-1]-a[i];
            }
        }
        System.out.println(count);
    }
}

```

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 5:55am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/12 "2021-05-27T05:55:14Z")

</div>

@bharat81091 Welcome! To Codechef Discuss, Could You Please Share The Code Cause We Have To Tell The Incremented Value I Did That But Its Wrong

---

<div class="post-metadata">

**Author:** ![sudo\_s](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sudo_s/32/24810_2.png) [@sudo\_s](https://discuss.codechef.com/u/sudo_s)\
**Post date:** [May 27, 2021, 6:43am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/13 "2021-05-27T06:43:22Z")

</div>

Sample code

```auto
ll ans = 0,maxi = a[0];
for(ll i=1;i<n;i++){
    maxi = max(maxi,a[i]);
    ans+=maxi-a[i];
}
cout<<ans;

```

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 27, 2021, 6:48am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/14 "2021-05-27T06:48:11Z")

</div>

Dude, I already mentioned the changes.

> [@suman\_18733097](#):
>
> What went wrong?
> 
> ```auto
> if(a[i - 1] >= a[i]) {
> count += a[i - 1] - a[i];
> a[i] = a[i - 1]; // Additional statement Needed
> }
> 
> ```
> 
> What could have gone wrong?
> 
> ```auto
> int count = 0;
> long count = 0; // Correction (use long instead of int)
> 
> ```

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 7:06am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/15 "2021-05-27T07:06:54Z")

</div>

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 7:10am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/16 "2021-05-27T07:10:34Z")

</div>

![Screenshot from 2021-05-27 12-38-44](https://s3.amazonaws.com/discourseproduction/original/3X/5/c/5cc5835fba988e8b9a31902571e9680dc27a3c61.png)

Thanks @suman_18733097 @cubefreak777 @bharat81091 @sudo_s For The Help

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 7:12am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/17 "2021-05-27T07:12:59Z")

</div>

> [@suman\_18733097](#):
>
> `a[i] = a[i - 1]; // Additional statement Needed`

@suman_18733097 Please Explain Why You Added This

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 27, 2021, 7:15am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/18 "2021-05-27T07:15:32Z")

</div>

Find it yourself.  
😉

---

<div class="post-metadata">

**Author:** ![anon25350622](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@anon25350622](https://discuss.codechef.com/u/anon25350622)\
**Post date:** [May 27, 2021, 7:15am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/19 "2021-05-27T07:15:53Z")

</div>

Ok 😉

---

<div class="post-metadata">

**Author:** ![bharat81091](https://discuss.codechef.com/user_avatar/discuss.codechef.com/bharat81091/32/24644_2.png) [@bharat81091](https://discuss.codechef.com/u/bharat81091)\
**Post date:** [May 27, 2021, 7:35am UTC](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248/20 "2021-05-27T07:35:52Z")

</div>

import java.util.Scanner;

class Codechef  
{  
public static void main(String []arg)  
{  
Scanner sc=new Scanner(System.in);  
System.out.println(“entee the sixe of array”);  
int n=sc.nextInt();  
int a[]=new int[n];  
int count=0;  
for(int i=0;i\<n;i++)  
{  
a[i]=sc.nextInt();  
}  
for(int b:a)  
{  
System.out.print(b);

```
	}
	
	for(int i=1;i<n;i++)
	{
		if(a[i-1]>a[i])
		{
			while(a[i]!=a[i-1])
			{
				a[i]=a[i]+1;
				count++;
			}
		}
	}
	
	
	System.out.println(count);
	
}

```

}

[Next page](https://discuss.codechef.com/t/help-me-in-increasing-array-cses/90248.md?page=2)
