anushkanocoding's blog

By anushkanocoding, history, 6 weeks ago, In English

Click Here for the question

this solution is wrong for testcase 2 can someone please tell me why

import java.util.*;
public class B_Collecting_Game{
    public static void main(String[]args){
        java.util.Scanner sc=new java.util.Scanner(System.in);
        int t=sc.nextInt();
    
        for(int i=0;i<t;i++){
            int n=sc.nextInt();
            
            long [][]arr=new long[n][2];
            
            long []prefix=new long[n];
            for(int j=0;j<n;j++){
                arr[j][0]=sc.nextLong();
                arr[j][1]=j;
            }
            Arrays.sort(arr, (a, b) -> Long.compare(a[0], b[0]));
            prefix[0]=arr[0][0];
            for(int j=1;j<n;j++){
                prefix[j]=arr[j][0]+prefix[j-1];
            }
            long []answer=new long[n];

            for(int index=0;index<arr.length-1;index++){
                if(prefix[index]<arr[index+1][0]){
                    answer[(int)arr[index][1]]=index;
                }
                else{
                    int left=index+1;
                    int right=arr.length-1;
                    int couldBe=index;
                    while(left<=right){
                        int mid = left + (right - left) / 2;


                        if(arr[mid][0]<=prefix[mid-1]){
                            couldBe=mid;
                            left=mid+1;
                        }
                        else{
                            right=mid-1;
                        }
                    }
                    answer[(int)arr[index][1]]=couldBe;
                }
            }
            answer[(int)arr[arr.length-1][1]]=arr.length-1;
            for(int j=0;j<answer.length;j++){
                System.out.print(answer[j] + " ");
            }
            System.out.println();
        }
        sc.close();
    }
}

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By anushkanocoding, history, 2 months ago, In English

heres the link for the question "Divan and a new Project" [](https://codeforces.me/contest/1614/problem/B)

Why wont this solution work? It prints the correct minimum time taken and also prints the coordinates properly. But when i submit it shows error on the first testcase and says — "wrong answer answer is wrong: pans = 14 is not equal real_pans = 18 (test case 1)" i dont get this. the answer to the first example is 14 why is it saying its 18?

import java.util.*;
public class divan {
    public static void main(String[]args){
        java.util.Scanner sc=new java.util.Scanner(System.in);
        int t=sc.nextInt();
        
        for(int i=0;i<t;i++){
            int n=sc.nextInt();
            int []arr=new int[n];

            for(int j=0;j<n;j++)arr[j]=sc.nextInt();

            Arrays.sort(arr);

            
            boolean negative=true;
            int neg=-1;
            int pos=1;
            boolean positive=false;
            int time=0;
            int []ans=new int[n+1];
            ans[0]=0;
            int idx=1;
            for(int j=n-1;j>=0;j--){
                if(negative){
                    int now=2*Math.abs(0-neg)*arr[j];
                    ans[idx]=neg;
                    idx++;
                    time+=now;
                    positive=true;
                    negative=false;
                }
                else{
                    int now=2*Math.abs(0-pos)*arr[j];
                    ans[idx]=pos;
                    idx++;
                    time+=now;
                    positive=false;
                    negative=true;
                    neg--;
                    pos++;
                }
            }
            System.out.println(time);
            for(int j=0;j<ans.length;j++)System.out.print(ans[j] + " ");
            System.out.println();
        }
        sc.close();
    }
}

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By anushkanocoding, history, 3 months ago, In English

https://leetcode.com/problems/predict-the-winner/ Please check this question out on leetcode and can anyone tell me why my code wont work?

class Solution {
    public boolean stoneGame(int[] piles) {
        int total=0;
        for(int pile:piles)total+=pile;
        int [][]dp=new int[piles.length][piles.length];
        for(int []arr:dp)Arrays.fill(arr,-1);
        int alice=recursion(0,piles.length-1,piles,true,dp);
        int bob=total-alice;
        return alice>bob;
    }
    public int recursion(int start,int end,int []piles,boolean alice,int [][]dp){
        if(start>end){
            return 0;
        }
        if(dp[start][end]!=-1)return dp[start][end];
        if(alice){
            int stTake=piles[start]+recursion(start+1,end,piles,false,dp);
            int endTake=piles[end]+recursion(start,end-1,piles,false,dp);
            return dp[start][end]=Math.max(stTake,endTake);
        }
        else{
            int stTake=piles[start]+recursion(start+1,end,piles,true,dp);
            int endTake=piles[end]+recursion(start,end-1,piles,true,dp);
            return dp[start][end]=Math.min(stTake,endTake);
        }
    }
}

Full text and comments »

  • Vote: I like it
  • -21
  • Vote: I do not like it

By anushkanocoding, history, 3 months ago, In English

import java.util.*; public class balanced { public static void main(String[]args){ java.util.Scanner sc=new java.util.Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int k=sc.nextInt(); int []arr=new int[n]; for(int j=0;j<arr.length;j++){ arr[j]=sc.nextInt(); }

int ops=0;
        Arrays.sort(arr);
        int a=0;
        int b=1;
        while(b<arr.length){ 
            if(arr[b]-arr[b-1]>k){
                int leftNums=b-a;
                int rightNums=arr.length-b;

                if(leftNums<rightNums){
                    a=b;
                    ops+=leftNums;
                }else if(leftNums>=rightNums){
                    ops+=rightNums;
                    break;
                }
            }
            b++;
        }
        System.out.println(ops);
    }
}

} For Question 1850D-BALANCED ROUND Can anyone please tell me what the problem is with the code? It Fails on TestCase 409 i cant see the particular testcase (also can we actually see particular testcases? if not, how is one supposed to know which testcase is wrong?

Full text and comments »

  • Vote: I like it
  • -24
  • Vote: I do not like it