Capgemini ADAPT Sprint 1 Case Study Solution Java | BSAByCustomerId Problem Solution

Capgemini ADAPT Sprint 1 Case Study Solution Java BSAByCustomerId Problem Solution
Capgemini ADAPT Sprint 1 Case Study Solution Java BSAByCustomerId Problem Solution

In this post, you will get the solution to the first problem of Capgemini ADAPT Sprint 1 – JBR-CS5A-L2-2-BSAByCustomerId.

Note: The solutions are provided for learning purposes only. 

Table of Contents

Sprint 1 – Problem 5: JBR-CS5A-L2-3-BSAByCustomerId:


Capgemini ADAPT Problem Description:


Write a program to search the customer based on the customer’s Id using Binary Search Algorithm.

This program should print “No Record Found” if the Id is not available in the given array.

Note: Use binary search algorithm

Sample Input:


1005

Sample Output:


1005

Ganesh

Chennai

Sample Input:


2345

Sample Output:


No Record Found

Execution Time Limit:


10 seconds

The solution to JBR-CS5A-L2-3-BSAByCustomerId:


 

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

public class Source{
static class Customer{
int id;
String name;
String city;

public Customer(int id, String name, String city){
this.id = id;
this.name = name;
this.city = city;
}
}
public static void main(String[] args){
Customer[] customers = new Customer[5];

customers[0] = new Customer(1001, "Raj", "Chennai");
customers[1] = new Customer(1002, "Simrath", "Amristar");
customers[2] = new Customer(1005, "Ganesh", "Chennai");
customers[3] = new Customer(1008, "Akshay", "Pune");
customers[4] = new Customer(1204, "Gaurav", "Delhi");

Scanner sc = new Scanner(System.in);

int key = sc.nextInt();
sc.close();

int first = 0;
int last = customers.length-1;
int mid = (first+last)/2;
while(first<=last){
if(customers[mid].id<key){
fid = mid + 1;
}else if(customers[mid].id == key){
Customer customer = customers[mid];
System.out.println(customer.id);
System.out.println(customer.name);
System.out.println(customer.city);
break;
} else {
last = mid - 1;
}
mid = (first+last)/2;
}
if(first>last){
System.out.println("No Record Found");
}
}
}

 

 

Previous articleCapgemini ADAPT Sprint 1 Solution Java | LSAByCustomerName Problem Solution
Next articleSalesforce Off Campus Drive 2022 Freshers Hiring as Associate Member of Technical Staff
Job Seeker 24 provides Off-Campus Drive Updates, Engineering Job Updates, IT Job Updates, Jobs for Freshers, Private Sector Jobs, Public Sector Jobs, various Educational Opportunities, etc.