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:
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...
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].
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...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
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
Write a statement to call prepare Arr to set values for the array (Using C++) #include...
Write a statement to call prepare Arr to set values for the array (Using C++) #include using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i cout << arr[i] <<" "; cout < return 0; } void prepareArr(int a[]) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT