0% found this document useful (0 votes)
75 views4 pages

Thought Works Imp

The document contains code to find the maximum value of H for all pairs of elements in a 2D array. It calculates H as the sum of two elements minus the absolute difference of their indices. It then contains additional code to find the maximum number of friends for all permutations of an input array by tracking running sums.

Uploaded by

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

Thought Works Imp

The document contains code to find the maximum value of H for all pairs of elements in a 2D array. It calculates H as the sum of two elements minus the absolute difference of their indices. It then contains additional code to find the maximum number of friends for all permutations of an input array by tracking running sums.

Uploaded by

sriuthra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.io.

*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Frequency {
public static void main (String[] args) {
/*int [][]arr = { { 9, 9, 6, 10, 4 },
{ 5, 5, 10, 2, 2 },
{ 5, 5, 8, 7, 3 },
{ 2, 6, 6, 1, 6 },
{1, 7, 3, 8, 9}};*/
int [][]arr = {{1,9,1,1},
{1,1,4,1},
{3,4,1,2},
{1,2,7,7}
};

int k=arr.length-1 ;
int l=arr[k].length-1;

List<Integer> list = new ArrayList<Integer>();


for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {

list.add(arr[i][j]);
}
}

int[] vector = new int[list.size()];


for (int i = 0; i < vector.length; i++) {
vector[i] = list.get(i);
}

//System.out.println(Arrays.toString(vector));

int count=0;
int b=0;int H=0;
int j;
int a=-1,d,c=0,p=0,q=0;
int H_max=0;
int counter=1;
while(count<vector.length-1)
{

b=p%(l+1);

if(b==0)
{
a++;
}

//System.out.println("("+a+","+b+")");
//System.out.println(" ");
counter=a+1;
for(int i=count+1;i<vector.length;i++)
{

d=i%(l+1);

if(i%(l+1)==0)
{
counter++;

}
c=counter-1;
//System.out.println("("+c+","+d+")");
//System.out.println(" ");
H=vector[count]+vector[i]-Math.abs(a-c)-Math.abs(b-d);

if(H_max<H)
H_max=H;
}

System.out.println(H_max);

count++;
c=0;
d=0;
p++;

}
}

import java.util.*;

public class Mirror_program {


public static void main(String[] args) throws Exception {

int[] A=new int[] {6,2,3,1};

int [][]arr = {{1,3},{2,4}};

int sum=0;
int friend=0;
int friend_max=0;

List<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < arr.length; i++) {


for (int j = 0; j < arr[i].length; j++) {

list.add(arr[i][j]);
}
}

int[] vector = new int[list.size()];


for (int i = 0; i < vector.length; i++) {
vector[i] = list.get(i);
}
System.out.println(Arrays.toString(vector));
int count=0;

List<Integer> temp = new ArrayList<Integer>();

while(count<vector.length)
{
if(!(temp.contains(vector[count])))
{
temp.add(0);
temp.add(vector[count]);
temp.add(vector[count+1]);

for( int i=count+1;i<vector.length;i++)


{

if(vector[count]==vector[i])
{
temp.add(vector[i+1]);
}

}
}
count=count+2;
}

temp.add(0);
System.out.println(temp);

int[] nums1 = new int[A.length];


int inc=0;
for(int i=1;i<=A.length;i++)
{
nums1[inc++]=i;
}

System.out.println("\nOriginal array: "+Arrays.toString(nums1));


List<List<Integer>> result1 = new Mirror_program().permute(nums1);

System.out.println(result1);
for(int i=0;i<result1.size();i++)
{
System.out.println(result1.get(i));
result1.toArray();
friend=0;

for(int j=1;j<temp.size();j++)
{
if(temp.get(j)!=0 )
{
sum=sum+A[result1.get(i).indexOf(temp.get(j))];
}
else if(temp.get(j)==0 )
{
friend=friend+((sum*(sum-1))/2);

if(friend_max<friend)
{
friend_max=friend;
}
sum=0;

}
}
}
System.out.println(friend_max );
}

public List<List<Integer>> permute(int[] nums) {


List<List<Integer>> result = new ArrayList<>();
Permutation(0, nums, result);
return result;
}

private void Permutation(int i, int[] nums, List<List<Integer>>


result) {
if (i == nums.length - 1) {
List<Integer> list = new ArrayList<>();
for (int n : nums) list.add(n);
result.add(list);
} else {
for (int j = i, l = nums.length; j < l; j++) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
Permutation(i + 1, nums, result);
temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy