Please Help Me In Increasing Array
CSES I Am Not Able To Understand The Question
Question Link: CSES - Increasing Array
Thanks In Advance
Krishna Wadhwani
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.
checkout this test case-
5
5 1 2 1 3
your output = 5
required output = 13
Hint:- use the fact that max(v[0],v[i])<=v[i]
What went wrong?
if(a[i - 1] >= a[i]) {
count += a[i - 1] - a[i];
a[i] = a[i - 1]; // Additional statement
}
What could have gone wrong?
int count = 0;
long count = 0; // Correction
Test details
Test 1
Verdict: ACCEPTED
input
10
1 1 1 1 1 1 1 1 1 1
correct output
0
user output
0
Test 2
Verdict: WRONG ANSWER
input
10
1000000000 1 1 1 1 1 1 1 1 1
correct output
8999999991
user output
999999999
Test 3
Verdict: WRONG ANSWER
input
10
6 10 4 10 2 8 9 2 7 7
correct output
31
user output
21
Test 4
Verdict: ACCEPTED
input
200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 …
correct output
0
user output
0
Test 5
Verdict: WRONG ANSWER
input
200000
1000000000 1 1 1 1 1 1 1 1 1 1…
correct output
199998999800001
user output
999999999
Test 6
Verdict: WRONG ANSWER
input
200000
763977854 530688450 486447012 …
correct output
100126194120455
user output
165085756
Test 7
Verdict: ACCEPTED
input
1
329873232
correct output
0
user output
0
I have pasted testcases please check
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 …
You might want to paste your code as well.
Link To Code: https://cses.fi/paste/bd6c241d1bb5736121e3b2/
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);
}
}
@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
Sample code
ll ans = 0,maxi = a[0];
for(ll i=1;i<n;i++){
maxi = max(maxi,a[i]);
ans+=maxi-a[i];
}
cout<<ans;
Dude, I already mentioned the changes.
(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)
Find it yourself.
Ok
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);
}
}