0% found this document useful (0 votes)
3K views2 pages

3 - Find Docs With Odd Pages

The document defines a Solution class that takes in an array of Document objects, filters them to only those with odd page counts, and sorts them by ID. It contains a main method that reads in Document data, calls the docsWithOddPages method, and prints the results. It also defines a Document class to represent documents with ID, title, folder name, and page count properties.

Uploaded by

Shaquib Nesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views2 pages

3 - Find Docs With Odd Pages

The document defines a Solution class that takes in an array of Document objects, filters them to only those with odd page counts, and sorts them by ID. It contains a main method that reads in Document data, calls the docsWithOddPages method, and prints the results. It also defines a Document class to represent documents with ID, title, folder name, and page count properties.

Uploaded by

Shaquib Nesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


Scanner sc = new Scanner(System.in);
Document[] docs = new Document[4];

for(int i = 0;i<docs.length;i++)
{
int id = sc.nextInt();sc.nextLine();
String title = sc.nextLine();
String folderName = sc.nextLine();
int pages = sc.nextInt();

docs[i] = new Document(id,title,folderName,pages);

Document[] newDocs = docsWithOddPages(docs);

for(Document doc:newDocs)
{
System.out.println(doc.getId() + " " + doc.getTitle() + " " +
doc.getFolderName() + " " + doc.getPages());
}
}

public static Document[] docsWithOddPages(Document[] docs)


{
Document[] newDocs = null;

int n = 0;

for(Document doc:docs)
{
if(doc.getPages()%2 == 1) n++;
}

newDocs = new Document[n];

n = 0;

for(Document doc:docs)
{
if(doc.getPages()%2 == 1) newDocs[n++] = doc;
}

for(int i = 0;i<newDocs.length;i++)
{
for(int j = 0;j<i;j++)
{
if(newDocs[i].getId() < newDocs[j].getId())
{
Document tmp = newDocs[i];
newDocs[i] = newDocs[j];
newDocs[j] = tmp;
}
}
}

return newDocs;
}
}

class Document
{
private int id;
private String title;
private String folderName;
private int pages;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getFolderName() {


return folderName;
}

public void setFolderName(String folderName) {


this.folderName = folderName;
}

public int getPages() {


return pages;
}

public void setPages(int pages) {


this.pages = pages;
}

public Document(int id, String title, String folderName, int pages)


{
this.id = id;
this.title = title;
this.folderName = folderName;
this.pages = pages;
}
}

You might also like