Skip to content

Commit 7e64917

Browse files
Initial Commit
1 parent 9c468a6 commit 7e64917

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

easyq/src/easyq/rotationsum.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package easyq;
2+
/* BY-M.NAVEEN
3+
* engineerscodes
4+
* code -Program for array rotation
5+
* sr.code-Ar6
6+
* Q- Find maximum value of Sum( i*arr[i]) with only rotations on given
7+
* */
8+
import java.util.*;
9+
10+
public class rotationsum // class name
11+
{
12+
static Scanner nav=new Scanner(System.in); // calling Scanner and creating object nav
13+
static int array[],size,sum,highest=0,count=0,pos; // class instances variables
14+
public rotationsum(int c) // constructor
15+
{
16+
size=c;
17+
array=new int[size];//intialization of variables
18+
}
19+
20+
public static void rotation() //method to do right_rotation
21+
{
22+
int temp=array[size-1];
23+
for(int i=size-2;i>=0;i--)
24+
{
25+
array[i+1]=array[i];
26+
}
27+
array[0]=temp;
28+
System.out.println("after rotation element in array are::"+Arrays.toString(array));
29+
sum();
30+
}
31+
public static void sum() //Find maximum value of Sum( i*arr[i]) with only rotations on given
32+
{ count++;
33+
sum=0; //sum must be initizated to zero
34+
for(int i=0;i<size;i++)
35+
sum=sum+(i*array[i]);
36+
if(highest<=sum)
37+
{highest=sum;
38+
pos=count;
39+
}
40+
if(count==size)
41+
{
42+
return;
43+
}
44+
else
45+
rotation(); //recursive call
46+
}
47+
48+
public static void input() //array input method
49+
{
50+
for(int i=0;i<array.length;i++)
51+
array[i]=nav.nextInt();
52+
System.out.println("element in array are::"+Arrays.toString(array));
53+
rotation();
54+
}
55+
public static void main(String arg[]) //main method
56+
{
57+
System.out.println("enter the size of the array");
58+
rotationsum n=new rotationsum (nav.nextInt());
59+
System.out.println("Enter the element of array");
60+
input();
61+
count=count%size;
62+
System.out.println("the highest sum of i*a[t] is "+highest+" when rotated by "+count);
63+
}
64+
}

0 commit comments

Comments
 (0)