Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Cognizant Coding Question- 3

One comment on “Cognizant Coding Question- 3”


  • Aqib

    Solution:-

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int p=sc.nextInt();
    int v=sc.nextInt();
    int miss=0,f=0,r=0;
    int arr[]=new int[v];
    int que[]=new int[v];
    for(int i=0;i<v;i++)
    arr[i]=sc.nextInt();
    for(int i=0;i<v;i++)
    {
    int flag=0,pos=-1;
    if(r<p)
    {
    que[i]=arr[i];
    r++;
    miss++;
    }
    else
    {
    for(int j=f;j<r;j++)
    {
    if(arr[i]==que[j])
    {
    flag=1;
    pos=j;
    break;
    }
    }
    if(flag==1)
    {
    for(int j=pos;j<r-1;j++)
    {
    que[j]=que[j+1];
    }
    que[r-1]=arr[i];
    }
    else
    {
    f++;
    que[r++]=arr[i];
    miss++;
    }
    }
    }
    System.out.println("Miss="+miss);
    }
    }