0% found this document useful (0 votes)
7 views6 pages

Import Java

Uploaded by

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

Import Java

Uploaded by

99210041211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//1st code solution

import [Link];

import [Link];

public class LongestConsecutiveSequence {

public int longestConsecutive(int[] nums) {

if ([Link] == 0) return 0;

HashSet<Integer> set = new HashSet<>();

for (int num : nums) {

[Link](num);

int longest = 0;

for (int num : set) {

if (![Link](num - 1)) {

int currentNum = num;

int currentStreak = 1;

while ([Link](currentNum + 1)) {

currentNum += 1;

currentStreak += 1;

longestStreak = [Link](longestStreak, currentStreak);

return longestStreak;

}
public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of elements in the array:");

int n = [Link]();

int[] nums = new int[n];

[Link]("Enter the elements of the array:");

for (int i = 0; i < n; i++) {

nums[i] = [Link]();

LongestConsecutiveSequence sequenceFinder = new LongestConsecutiveSequence();

int result = [Link](nums);

[Link]("The length of the longest consecutive sequence is: " + result);

}
//second code solution

import [Link].*;

public class KingdomConnection {

static class Edge {

int jump;

int cost;

Edge(int jump, int cost) {

[Link] = jump;

[Link] = cost;

static class Node implements Comparable<Node> {

int city;

long cost;

Node(int city, long cost) {


[Link] = city;

[Link] = cost;

public int compareTo(Node other) {

return [Link]([Link], [Link]);

public static long findMinimumCost(int N, int M, List<Edge> edges) {

long[] minCost = new long[N];

[Link](minCost, Long.MAX_VALUE);

minCost[0] = 0;

PriorityQueue<Node> pq = new PriorityQueue<>();

[Link](new Node(0, 0));

while (![Link]()) {

Node current = [Link]();

int city = [Link];

long cost = [Link];

if (cost > minCost[city]) continue;

for (Edge edge : edges) {

int nextCity = (city + [Link]) % N;

long newCost = cost + [Link];


if (newCost < minCost[nextCity]) {

minCost[nextCity] = newCost;

[Link](new Node(nextCity, newCost));

long totalCost = 0;

for (long c : minCost) {

if (c == Long.MAX_VALUE) {

return -1;

totalCost += c;

return totalCost;

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int N = [Link]();

int M = [Link]();

List<Edge> edges = new ArrayList<>();

for (int i = 0; i < M; i++) {

int A = [Link]();

int C = [Link]();

[Link](new Edge(A, C));

long result = findMinimumCost(N, M, edges);


[Link](result);

You might also like