Algorithms Analysis and Design Concepts Hands-Ons | Accenture TFA Training Pre-Learning Modules
Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
All Question of the Hands-On Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
1. Bank Ticketing System
Path: Algorithms Analysis and Design Concepts/Bank Ticketing System/Ticket.java
import java.util.*;
public class Ticket
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of customer takes the tickets:");
int n=sc.nextInt();
if(n>0)
{
int[] tickets=new int[n];
System.out.println("The tickets in the system are:");
for(int i=0;i<n;i++)
{
tickets[i]=i+1;
System.out.print(tickets[i]+" ");
}
System.out.println();
System.out.println("Enter the number of tickets served:");
int served=sc.nextInt();
if(served>0 && served<=n)
{
System.out.println("The served tickets are:");
for(int i=0;i<served;i++)
{
System.out.print(tickets[i]+" ");
}
System.out.println();
if(served!=n)
{
System.out.println("The unserved tickets are:");
for(int i=served;i<n;i++)
{
System.out.print(tickets[i]+" ");
}
}
else
{
System.out.println("No more tickets to be served");
}
}
}
else
{
System.out.println("Invalid Number");
}
}
}
2. Chocolate Boxes
Path: Algorithms Analysis and Design Concepts/Chocolate Boxes/Chocolate.java
import java.util.*;
public class Chocolate
{
public static void main (String[] args) {
int flag=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the no. of boxes: ");
int n=sc.nextInt();
System.out.println();
if(n>=1 && n<=10)
{int start=0;
int[] choc=new int[n];
int temp=n;
for(int i=0;i<temp;i++)
{
System.out.print("Enter the no. of chocolates in box "+(i+1)+": ");
choc[i]=sc.nextInt();
System.out.println();
if(i==0)
{
if(choc[i]%2!=0)
{
System.out.println("Sorry!!! First box always should contain positive even no. of chocolates");
flag++;
break;
}
}
else
{
if(choc[i]%2!=0)
{
start++;
}
}
}
if(flag!=1)
{
System.out.print("No. of chocolates in each box: ");
for(int i=start;i<n;i++)
{
System.out.print(choc[i]+" ");
}
}
}
else
{
System.out.println("Invalid input");
}
}
}
3. Credit Points - STACK
Path: Algorithms Analysis and Design Concepts/Credit Points - STACK/ShopMain.java
import java.util.*;
public class ShopMain
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the no. of users:");
int n=sc.nextInt();
System.out.println();
if(n>=1 && n<=1000)
{
int[] a=new int[n];
int temp=0;
while(temp<n)
{
System.out.print("Enter the credit points for user "+(temp+1)+":");
a[temp]=sc.nextInt();
if(a[temp]>=1 && a[temp]<=100)
{
temp++;
System.out.println();
}
else
{
break;
}
}
System.out.print("Enter the no. of users to serve: ");
int k=sc.nextInt();
if(k>=1 && k<n)
{
System.out.print("The unserved user's credit points are: ");
for(int i=(k);i<n;i++)
{
System.out.print(a[i]+" ");
}
}
else if(k==n)
{
System.out.println("0 users to serve");
}
else
{
System.out.println("Invalid no. of users");
}
}
else{
System.out.println("Invalid no. of users");
}
}
}
4. Linked List - Recursion
Path: Algorithms Analysis and Design Concepts/Linked List - Recursion/ListDriver.java
import java.util.Scanner;
class ListDriver
{ Node head;
class Node{
int data;
Node next;
Node(int d){
data=d;
next=null;
}
}
public void push(int new_data){
Node new_node=new Node(new_data);
new_node.next=head;
head=new_node;
}
int count(int search_for){
Node current=head;
int count=0;
while(current!=null){
if(current.data==search_for)
count++;
current=current.next;
}
return count;
}
public static void main(String args[]){
ListDriver llist= new ListDriver();
Scanner l= new Scanner(System.in);
System.out.println("Enter the size of the list:");
int n= l.nextInt();
int a[]= new int[100];
if(n==0||n<0){
System.out.println("Invalid Input");}
else{
for(int i=0;i<n;i++)
{ a[i]=l.nextInt();
llist.push(a[i]);
}
System.out.println("Printing the list");
for(int i=0;i<n;i++)
{ System.out.print(a[i]+" ");}
System.out.println();
System.out.println("Enter the key to find it's occurrence:");
int x=l.nextInt();
System.out.println(x+" occurs for "+llist.count(x)+" times");
}
}
}
5. Merge Sort - Arrays
Path: Algorithms Analysis and Design Concepts/Merge Sort - Arrays/MergeSortDriver.java
import java.util.*;
class MergeSortDriver
{
public static void merge(int arr[],int l,int m,int r)
{
int n1=m-l+1;
int n2=r-m;
int L[]=new int[n1];
int R[]=new int[n2];
for (int i=0;i<n1 ;++i )
L[i]=arr[l+i];
for (int j=0;j<n2 ;++j )
R[j]=arr[m+1+j];
int i=0,j=0;
int k=l;
while(i<n1 && j<n2)
{
if (L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
public static void MergeSort(int arr[],int l,int r)
{
if (l<r)
{
int m=(l+r)/2;
MergeSort(arr,l,m);
MergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
}
public static void printArray(int arr[])
{
int n=arr.length;
for (int i=0;i<n ;i++ )
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main (String[] args) {
/* code */
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of an array:");
int n=sc.nextInt();
if(n>0)
{
int arr[]=new int[n];
System.out.print("Enter the elements:");
for (int i=0;i<n ;i++ )
arr[i]=sc.nextInt();
System.out.println("Given array is");
printArray(arr);
MergeSort(arr,0,arr.length-1);
System.out.println("\nSorted array is");
printArray(arr);
}
else
{
System.out.println("Invalid array size");
}
}
}
6. Occurrences
Path: Algorithms Analysis and Design Concepts/Occurrences/DeleteMain.java
import java.util.*;
class Node
{
int data;
Node next;
public Node(int data)
{
this.data=data;
this.next=null;
}
}
public class DeleteMain
{Node head;
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
DeleteMain obj = new DeleteMain();
int n = sc.nextInt();
if(n>0)
{
sc.nextLine();
String[] in = (sc.nextLine().split(" "));
for(int i=0; i<n; i++)
{
obj.add(Integer.parseInt(in[i]));
}
System.out.println("Enter the element to be deleted: ");
int key=sc.nextInt();
obj.delete(key);
System.out.print("The list after deletion: ");
obj.display();
}
}
public void add(int data)
{
Node newNode = new Node(data);
Node current = head;
if(head == null)
{
head = newNode;
return;
}
else
{
while(current.next != null)
{
current = current.next;
}
current.next = newNode;
return;
}
}
public void delete(int key)
{
Node temp = head, prev = null;
while(temp != null && temp.data == key)
{
head = temp.next;
temp = head;
}
while(temp != null)
{
while(temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
if(temp == null)
{
return;
}
prev.next = temp.next;
temp = prev.next;
}
}
public void display()
{
Node temp=head;
while(temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
}
}
7. Quick Sort Algorithm
Path: Algorithms Analysis and Design Concepts/Quick Sort Algorithm/QuickSort.java
import java.util.Scanner;
public class QuickSort
{ public static void main(String[] args)
{ int i,n;
Scanner q= new Scanner(System.in);
int []arr= new int[100];
System.out.print("How many elements?");
n=q.nextInt();
if(n==0||n<0)
{ System.out.println("Invalid Input");}
else
{System.out.print("Enter array elements:");
for(i=1;i<=n;i++)
{ arr[i]=q.nextInt();
}
quickSort(arr,1,n);
System.out.print("Array after sorting:");
for(i=1;i<=n;i++)
System.out.print(arr[i]+" ");
}}
public static int partition(int a[],int beg,int end)
{ int left, right,temp,loc,flag;
loc=left=beg;
right=end;
flag=0;
while(flag!=1)
{ while((a[loc]<=a[right])&&(loc!=right))
right--;
if(loc==right)
flag=1;
else if(a[loc]>a[right])
{ temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
if(flag!=1)
{ while((a[loc]>=a[left])&&(loc!=left))
left++;
if(loc==left)
flag=1;
else if(a[loc]<a[left])
{ temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
}
}
}
return loc;
}
static void quickSort(int a[], int beg, int end)
{ int loc;
if(beg<end)
{ loc=partition(a,beg,end);
quickSort(a,beg,loc-1);
quickSort(a,loc+1,end);
}
}
}
8. Replace Character
Path: Algorithms Analysis and Design Concepts/Replace Character/ReplaceDriver.java
import java.util.Scanner;
public class ReplaceDriver{
public static String replace(String str, char c1, char c2){
if(str.length()<1){
return str;
}
else{
char first=c1==str.charAt(0)? c2 :str.charAt(0);
return first+ replace(str.substring(1),c1,c2);
}
}
public static void main(String[] args)
{ Scanner r= new Scanner(System.in);
String str= new String();
System.out.println("Enter the string");
str=r.nextLine();
System.out.println("Enter the character to be replaced");
char c1= r.next().charAt(0);
System.out.println("Enter the character to be replaced with");
char c2=r.next().charAt(0);
System.out.print("The modified string is "+ replace(str,c1,c2));
}
}
9. Reversing a List
Path: Algorithms Analysis and Design Concepts/Reversing a List/List.java
import java.util.*;
class Node{
int data;
Node next;
Node(int data)
{
this.data=data;
this.next=null;
}
}
public class List
{static Node head;
public static void main (String[] args) {
Scanner sc=new Scanner (System.in);
List obj = new List();
do{
System.out.println("Enter the value:");
int val=sc.nextInt();
sc.nextLine();
obj.append(val);
System.out.println("Do you want to add another node? Type Yes/No");
}while(sc.nextLine().equalsIgnoreCase("yes"));
System.out.print("The elements in the linked list are: ");
obj.display();
obj.Reverse();
System.out.println();
System.out.print("The elements in the reversed linked list are : ");
obj.display();
}
public void append(int data)
{
Node newNode= new Node(data);
Node current = head;
if(head == null)
{
head = newNode;
return;
}
else
{
while(current.next!=null)
{
current = current.next;
}
current.next=newNode;
return;
}
}
public void display()
{
Node temp=head;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp=temp.next;
}
}
public Node Reverse()
{
Node prev=null;
Node current=head;
Node next=null;
while(current!=null)
{
next=current.next;
current.next=prev;
prev=current;
current=next;
}
head=prev;
return head;
}
}
10. Scorecard - Queue
Path: Algorithms Analysis and Design Concepts/Scorecard - Queue/ScoreCardDriver.java
import java.util.*;
class ScoreCardDriver
{
final static int MaxQ=100;
int front = 0, rear = 0;
int[] QA = new int[MaxQ];
public boolean empty()
{
return front == rear;
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
ScoreCardDriver obj = new ScoreCardDriver();
System.out.print("Enter the size of the score card:");
int n=sc.nextInt();
if(n>0 && n<=100)
{
int[] in = new int[n];
for(int i=0; i<n; i++)
{
System.out.print("Enter the hurdle race score "+(i+1)+":");
obj.enQueue(sc.nextInt());
System.out.println();
}
System.out.print("Latest hurdle race scores are: ");
obj.display();
}
else
{
System.out.println("Invalid score card size");
}
}
public void enQueue(int n)
{
/*if(rear != front)
{
return;
}*/
if((rear-front) == 5)
{
deQueue();
}
QA[rear] = n;
rear++;
return;
}
public void deQueue()
{
if(this.empty())
{
return;
}
front++;
}
public void display()
{
for(int i=front; i<rear ;i++)
{
System.out.print(QA[i]+" ");
}
}
}
11. Singly Linked List
Path: Algorithms Analysis and Design Concepts/Singly Linked List/ListDriver.java
import java.util.Scanner;
public class ListDriver
{ class Node { int data;
Node next;
public Node(int data)
{ this.data=data;
this.next=null;
}
}
public int size;
public Node head= null;
public Node tail=null;
public void addNode(int data)
{ Node newNode=new Node(data);
if(head==null)
{ head = newNode;
tail=newNode;
}
else{tail.next=newNode;
tail =newNode;
}
size++;
}
public Node reverseList(Node temp)
{ Node current=temp;
Node prevNode=null, nextNode=null;
while(current!=null)
{nextNode=current.next;
current.next=prevNode;
prevNode=current;
current=nextNode;
}
return prevNode;
}
public void isPalindrome()
{ Node current= head;
boolean flag=true;
int mid=(size%2==0)?(size/2):((size+1)/2);
for(int i=1;i<mid;i++)
{current=current.next;
}
Node revHead= reverseList(current.next);
while (head!= null&& revHead!= null)
{ if(head.data!=revHead.data)
{ flag=false;
break;
}
head=head.next;
revHead=revHead.next;
}
if(flag)
System.out.println("1");
else
System.out.println("0");
}
public void display()
{ Node current=head;
if(head==null)
{ return;
}
while(current!=null)
{
current=current.next;
}
System.out.println();
}
public static void main(String[] args)
{ ListDriver sList=new ListDriver();
Scanner s= new Scanner(System.in);
int n;
n=s.nextInt();
do{sList.addNode(s.nextInt());
n--;
} while(n>=1);
sList.display();
sList.isPalindrome();
}
}
12. Stack Operation
Path: Algorithms Analysis and Design Concepts/Stack Operation/StackDriver.java
import java.util.*;
class StackDriver
{ int top=-1;
int[] stk=new int[100];
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
StackDriver obj=new StackDriver();
System.out.println("Enter the number of elements to be pushed in the stack:");
int n=sc.nextInt();
if(n>0)
{
for(int i=0;i<n;i++)
{
System.out.println("Enter the element "+(i+1)+":");
obj.push(sc.nextInt());
}
System.out.println("The middle element is: "+obj.findMiddle());
System.out.println("The popped element is: "+obj.pop());
}
else
{
System.out.println("Invalid Input");
}
}
public void push(int data)
{
stk[++top]=data;
}
public int pop()
{
return stk[top--];
}
public int findMiddle()
{
if((top+1)%2==0)
{
return stk[(int)top/2];
}
else
{
return stk[(int)(top+1)/2];
}
}
}
13. Stack Reverse - Recursion
Path: Algorithms Analysis and Design Concepts/Stack Reverse - Recursion/StackDriver.java
import java.util.Stack;
import java.util.Scanner;
class StackDriver
{ static Stack<Integer>st=new Stack<>();
static void insert_at_bottom(int x)
{ if(st.isEmpty())
st.push(x);
else
{ int a= st.peek();
st.pop();
insert_at_bottom(x);
st.push(a);
}
}
static void reverse()
{ if(st.size()>0)
{ int x= st.peek();
st.pop();
reverse();
insert_at_bottom(x);
}
}
public static void main(String[] args)
{ Scanner s=new Scanner(System.in);
System.out.println("Enter length of List:");
int n=s.nextInt();
if(n==0||n<0)
{ System.out.println("Invalid Length");}
else
{for(int i=1;i<=n;i++)
{ st.push(i);}
System.out.println("Elements in Stack ");
System.out.println(st);
reverse();
System.out.println("Elements in the stack after reversal");
System.out.println(st);}
}
}
14. String Reverse - Recursion
Path: Algorithms Analysis and Design Concepts/String Reverse - Recursion/ReverseDriver.java
import java.util.Scanner;
public class ReverseDriver{
public static void main(String[] args)
{ String str;
System.out.print("Enter the string: ");
Scanner scanner=new Scanner(System.in);
str=scanner.nextLine();
scanner.close();
String reversed =reverseString(str);
System.out.println("Reversed string is: "+ reversed);
}
public static String reverseString(String str)
{ if(str.isEmpty())
return str;
return reverseString(str.substring(1))+str.charAt(0);
}
}
15. String Sorting
Path: Algorithms Analysis and Design Concepts/String Sorting/SortDriver.java
import java.util.Scanner;
import java.lang.*;
class SortDriver{
public final static int MAX =100;
public static String alternateSort(String s1)
{
int n= s1.length();
char [] s = s1.toCharArray();
int[] lCount = new int [MAX];
int[] uCount = new int [MAX];
for (int i=0;i<n;i++)
{
if(Character.isUpperCase(s[i]))
uCount[s[i] - 'A']++;
else
lCount[s[i] - 'a']++;
}
int i=0,j=0,k=0;
while(k<n)
{
while(i<MAX && uCount[i] ==0)
i++;
if(i<MAX)
{
s[k++]= (char)('A'+i);
uCount[i]--;
}
while(j<MAX && lCount[j]==0)
j++;
if(j<MAX)
{
s[k++]= (char)('a'+j);
lCount[j]--;
}
}
return(new String(s));
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the String:");
String str;
str =input.nextLine();
System.out.println("the Sorted String is:");
System.out.println(alternateSort(str));
}
}
16. Tower-of-Hanoi
Path: Algorithms Analysis and Design Concepts/Tower-of-Hanoi/TowerTest.java
import java.util.*;
public class TowerTest
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of disks :");
int n=sc.nextInt();
if(n>0 && n<=6)
{ System.out.println("The sequence of moves involved in the Tower of Hanoi are:");
towerOfHanoi(n, 'A', 'C', 'B');
}
else
{
System.out.println("Invalid input");
}
}
static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if(n==1)
{
System.out.println("Move disk 1 from peg "+ from_rod + " to peg " + to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
System.out.println("Move disk "+n+" from peg "+ from_rod + " to peg "+to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
}
17. World Cup -Binary Search
Path: Algorithms Analysis and Design Concepts/World Cup -Binary Search/BinarySearch.java
import java.util.Scanner;
class BinarySearch
{ public static int binarySearch(int arr[],int l,int r ,int x)
{ if(r>=l)
{ int mid=l+(r-1)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return binarySearch(arr,l,mid-1,x);
return binarySearch(arr,mid+1,r,x);
}
return -1;
}
public static void main(String args[])
{ BinarySearch ob=new BinarySearch();
Scanner b =new Scanner(System.in);
System.out.println("Enter the number of Teams:");
int n =b.nextInt();
if(n==0||n<0)
{ System.out.println("Invalid Input");}
else
{int a[]=new int[100];
System.out.println("Enter the score:");
for(int i=0;i<n;i++)
{ a[i]=b.nextInt();}
System.out.println("Enter the score to be searched:");
int x=b.nextInt();
if(x==0||x<0)
{ System.out.println("Invalid Input");}
else
{int result=ob.binarySearch(a,0,n-1,x);
int r= result+1;
if(result==-1)
System.out.println("Score Not Found");
else
System.out.println(x+" is the score of Team "+r);}}
}
}
If you have any queries, please feel free to ask on the comment section.If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.Please share and support our page!
Post a Comment