Found 2616 Articles for Java

Find array using different XORs of elements in groups of size 4 in Java

Sunidhi Bansal
Updated on 05-Nov-2021 06:03:24

123 Views

We are given with an integer array of size N(size of multiple 4) and we have to perform Xclusive OR operation on the array such that input[1- 4] resembles utility_arr[1- 4] and the conditions of computing is If arr[1 – 4] = {a1, a2, a3, a4} then q[1 – 4] = {a1 ⊕ a2 ⊕ a3, a1 ⊕ a2 ⊕ a4, a1 ⊕ a3 ⊕ a4, a2 ⊕ a3 ⊕ a4}Let us see various input output scenarios for this -In − int[] input = { 5, 2, 3, 4 };Out − Result after XOR operation 4 3 2 5Explanation −An Exclusive-OR gate's output ... Read More

Maximum subarray sum after dividing array into subarrays based on the given queries in Java

Sunidhi Bansal
Updated on 05-Nov-2021 06:04:04

212 Views

We are given two integer arrays, one having the elements which are computed and other having split points which are required to split the array for making subset and we have to calculate the sum of each subset in every split and return the maximum subset sum.Let us understand with example:-Input − int arr[] = int arr[] = { 9, 4, 5, 6, 7 } int splitPoints[] = { 0, 2, 3, 1 };Output − Maximum subarray sum after each split [22, 13, 9, 9]Explanation − Here we are breaking the array according to their split points and obtaining the maximum subset sum ... Read More

Meet in the middle in Java

Sunidhi Bansal
Updated on 05-Nov-2021 05:34:40

267 Views

We are provided with an array and a sum value; the problem statement is to calculate the maximum subset sum which does not exceed the given sum value. We cannot apply the brute force approach here because the structure of the given array is not the same as the divide and conquer approach.Let us see various input output scenarios for this -Let us understand with exampleInput − long arr[] = { 21, 1, 2, 45, 9, 8 } long given_Sum = 12Output −The maximum sum subset having sum less than or equal to the given sum-->12Explanation −The array is split into a set ... Read More

Maximize the total profit of all the persons X in Java

Sunidhi Bansal
Updated on 05-Nov-2021 05:27:21

267 Views

We are given 5 Integer variables Num, P1, P2, profit_P1, profit_P2 and the task is to maximize the profit and from all the natural numbers in the range of [1, Num]. The approach here is that if a positive number is divisible by P1 the profit increases by profit_P1 and similarly if if the number in the range is divisible by P2 the profit margin of profit_P2 increases. Also, the profit from a positive integer can be added at most once.Let us understand with example:-Input − int num = 4, P1 = 6, P2 = 2, profit_P1 = 8, profit_P2 = ... Read More

Merge K sorted linked lists in Java

Sunidhi Bansal
Updated on 05-Nov-2021 05:20:42

623 Views

We are given a K number of linked lists of variable sizes which are sorted in their sequence and we have to merge the list into a resultant list in such a way that the resultant array is sorted in order and the resultant array is printed as the output to the user.Let us understand with example:-Input −int k = 3;list[0] = new Node(11);list[0].next = new Node(15);list[0].next.next = new Node(17);list[1] = new Node(2);list[1].next = new Node(3);list[1].next.next = new Node(26);list[1].next.next.next = new Node(39);list[2] = new Node(4);list[2].next = new Node(8);list[2].next.next = new Node(10);Output −The merged list is-->2>> 3>> 4>> 8>> 10>> 11>> 15>> 17>> ... Read More

Minimum number of bombs in Java

Sunidhi Bansal
Updated on 05-Nov-2021 05:12:36

169 Views

The problem statement here is to kill the goons in the rooms of a building with minimum number of bombings. The rooms are labelled as 1 to n. The goons are injured by the first bombing attack and die in the second. When the rooms are bombed the goons rush to the nearest room in the building specially the neighboring room. We must calculate the number of bombing it is required to bomb the rooms in order to kill all the goons in the building.Let us understand with exampleInput − int number of rooms = 3Output −Total Bombings required42 1 3 2Explanation − ... Read More

Merge k sorted arrays in Java

Sunidhi Bansal
Updated on 05-Nov-2021 05:07:13

574 Views

We are given an ‘n’ number of arrays, let's say we take three arrays i.e. arr1[], arr2[] and arr3[] of integer type. The task is to merge all the given integer arrays in such a manner that the resultant array is sorted in the runtime only.Let us understand with exampleInput −Inta[]={21, 22, 23, 24};int b[ ] ={28, 31, 35}Output −int resultant[ ]={21, 22, 23, 24, 28, 31, 35}.Explanation − The array elements are compared before they are added and added according to their suitable position in the resultant array.Input −inta[]={1, 3, 5, 7, 9, 11, 13};int b[ ] ={14, 16, 18}int c[ ] ={19, ... Read More

Reverse actual bits of the given number in Java

Sunidhi Bansal
Updated on 03-Nov-2021 07:58:16

929 Views

Given an integer n that is not negative. The goal is to reverse the bits of n and report the number that results from doing so. While reversing the bits, the actual binary form of the integer is used; no leading 0s are taken into account.Let us see various input output scenarios for thisInput − 13Output − Reverse actual bits of the given number 11(13)10 = (1101)2. After reversing the bits, we get: (1011)2 = (11)10.Explanation − The binary bits are obtained from the input number which is then reversed and finally converted to decimal format which is returned as output.Input − 18Output − ... Read More

Reverse and Add Function in Java

Sunidhi Bansal
Updated on 03-Nov-2021 08:02:33

523 Views

We are given with an integer and the agenda here is to reverse the digits of the number and add the reversed number to the original number and check if the resultant number is a palindrome or not and the process is repeated until it does. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).For ExamplesInput − 1678Output − Palindrome of the given input 1678 293392Explanation − The input number is first reversed and then added to the original number, it is then checked for palindrome if it is not a palindrome ... Read More

Merge a linked list into another linked list at alternate positions in Java

Sunidhi Bansal
Updated on 22-Oct-2021 10:00:39

489 Views

We are given two data structures as linked list Let’s say, List_1 and List_2. The task is to merge the elements of linked list ‘List_2’ into the linked list ‘List_1’ at an alternate position and if we are left with the elements which can't be merged into ‘List_1’ then it will be printed as the ‘List_2’ remaining elements.For Example-:In −List_1 =List_2 =Out − Merged List is-:Explanation − we are given with two lists i.e. List_1 and List_2. We had merged the possible elements of list_2 into List_1 at alternate positions. So, elements after merging in List_1 are 10- >3->2->1->1->4->2->5->5 and in List_2 are ... Read More

Advertisements