Question

In: Computer Science

Using Java please You are given an array of integers arr. Your task is to count...

Using Java please

You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.

Solutions

Expert Solution

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

import java.util.Scanner;
import java.util.HashMap;
public class Hello
{
   // function to count the number of contiguous subarrays, such that each element of the subarray appears at least twice
public static int duplicatesOnSegment(int[] arr)
{
// get the length of array
int n = arr.length;
// initialize ans = 0
int ans = 0;
// iterate over the array
for(int i=0;i<n;i++)
{
// declare a hashmap
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
// initialize a counter count_unique to count unique numbers
int count_unique=0;
// iterate from i to 0
for(int j=i;j>=0;j--)
{
int x = arr[j];
// if the element is present in hashmap then increment its frequency
if (hmap.containsKey(x))
{
hmap.put(x, hmap.get(x) + 1);
}
// otherwise initialize its frequency to 1
else
{
hmap.put(x, 1);
}
// now if the element is present in the map then get its value
if(hmap.get(x)!=-1)
{
   int val=hmap.get(x);
   // if value is 1 then increment count_unique
   if(val==1)
count_unique++;
   // if the value is 2 then decrement count_unique
   else if(val==2)
count_unique--;
}
// if the count_unique counter becomes 0 then increment answer
if(count_unique==0)
{
ans++;
}
}
}
return ans;
}
// main function
public static void main(String []args)
{
// make an object of Scanner class
Scanner sc = new Scanner(System.in);
// input the size of array
int n = sc.nextInt();
// declare an array of size n
int[] arr = new int[n];
// input array elements
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
// call the function
int res = duplicatesOnSegment(arr);
// print the result
System.out.println(res);
}
}


Related Solutions

In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
You are given an array of arrays a. Your task is to group the arrays a[i]...
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups. Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Fix the C++ code to count the number of inversions using the given array. The answer...
Fix the C++ code to count the number of inversions using the given array. The answer is 8 inversions, but I am only getting 6 inversions. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int mergeInversion(int arr[], int l, int m, int r) {     // size of the two arrays     int n1 = m - l + 1;     int n2 = r - m;     // create temporary arrays     int L[n1];     int R[n2];     // Copy the...
Given a square matrix of integers m, your task is to rearrange its numbers in the...
Given a square matrix of integers m, your task is to rearrange its numbers in the following way: First, sort its values in ascending order of how frequently the number occurs in m. In the case of a tie, sort the equally frequent numbers by their values, in ascending order. Second, place the sorted numbers diagonally, starting from the bottom right corner, like this: Example For m = [[ 1, 4, -2], [-2, 3, 4], [ 3, 1, 3]] the...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Given an array ? of ? integers. Divide ? into three subsets such that the total...
Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT